Package cssutils |
[hide private]
[frames] | no frames]

# Source Code for Package cssutils

      1  #!/usr/bin/env python
      2  """cssutils - CSS Cascading Style Sheets library for Python
      3
      4      Copyright (C) 2004-2008 Christof Hoeke
      5
      6      This library is free software; you can redistribute it and/or
      7      modify it under the terms of the GNU Lesser General Public
      8      License as published by the Free Software Foundation; either
      9      version 2.1 of the License, or (at your option) any later version.
     10
     11      This library is distributed in the hope that it will be useful,
     12      but WITHOUT ANY WARRANTY; without even the implied warranty of
     13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14      Lesser General Public License for more details.
     15
     16      You should have received a copy of the GNU Lesser General Public
     17      License along with this library; if not, write to the Free Software
     18      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
     19
     20  A Python package to parse and build CSS Cascading Style Sheets.
     21
     22  Based upon and partly implements the following specifications (DOM only, not any rendering facilities):
     23
     24  `DOM Level 2 Style CSS <http://www.w3.org/TR/DOM-Level-2-Style/css.html>`__
     25      DOM for package css
     26  `DOM Level 2 Style Stylesheets <http://www.w3.org/TR/DOM-Level-2-Style/stylesheets.html>`__
     27      DOM for package stylesheets
     28  `CSSOM <http://dev.w3.org/csswg/cssom/>`__
     29      A few details (mainly the NamespaceRule DOM) is taken from here. Plan is to move implementation to the stuff defined here which is newer but still no REC so might change in the future
     30
     31  `CSS 2.1 <http://www.w3.org/TR/CSS21/>`__
     32      Rules and properties are defined here
     33  `CSS 2.1 Errata  <http://www.w3.org/Style/css2-updates/CR-CSS21-20070719-errata.html>`__
     34      A few erratas, mainly the definition of CHARSET_SYM tokens
     35  `MediaQueries <http://www.w3.org/TR/css3-mediaqueries/>`__
     36      MediaQueries are part of ``stylesheets.MediaList`` since v0.9.4, used in @import and @media rules.
     37  `Namespaces <http://www.w3.org/TR/css3-namespace/>`__
     38      Added in v0.9.1 and updated to definition in CSSOM in v0.9.4
     39  `Selectors <http://www.w3.org/TR/css3-selectors/>`__
     40      The selector syntax defined here (and not in CSS 2.1) should be parsable with cssutils (*should* mind though ;) )
     41
     42
     43  Please visit http://cthedot.de/cssutils/ for full details and updates.
     44
     45  Tested with Python 2.5 on Windows XP.
     46
     47
     48  This library is optimized for usage of ``from cssutils import *`` which
     49  import subpackages ``css`` and ``stylesheets``, CSSParser and
     50  CSSSerializer classes only.
     51
     52  Usage may be::
     53
     54      >>> from cssutils import *
     55      >>> parser = CSSParser()
     56      >>> sheet = parser.parseString(u'a { color: red}')
     57      >>> print sheet.cssText
     58
     59  """
     60  __all__ = ['css', 'stylesheets', 'CSSParser', 'CSSSerializer']
     61  __docformat__ = 'restructuredtext'
     62  __author__ = '$LastChangedBy: cthedot $'
     63  __date__ = '$LastChangedDate: 2008-02-22 19:20:37 +0100 (Fr, 22 Feb 2008) $'
     64  __version__ = '0.9.5a4 $LastChangedRevision: 1073 $'
     65
     66  import codec
     67
     68  # order of imports is important (maybe as it is partly circular)
     69  import xml.dom
     70
     71  import errorhandler
     72  log = errorhandler.ErrorHandler()
     73
     74  import util
     75  import css
     76  import stylesheets
     77  from parse import CSSParser
     78
     79  from serialize import CSSSerializer
     80  ser = CSSSerializer()
     81
     82  # used by Selector defining namespace prefix '*'
     83  _ANYNS = -1
     84
     85 -class DOMImplementationCSS(object):
     86      """
     87      This interface allows the DOM user to create a CSSStyleSheet
     88      outside the context of a document. There is no way to associate
     89      the new CSSStyleSheet with a document in DOM Level 2.
     90
     91      This class is its *own factory*, as it is given to
     92      xml.dom.registerDOMImplementation which simply calls it and receives
     93      an instance of this class then.
     94      """
     95      _features = [
     96          ('css', '1.0'),
     97          ('css', '2.0'),
     98          ('stylesheets', '1.0'),
     99          ('stylesheets', '2.0')
    100      ]
    101
    102 -    def createCSSStyleSheet(self, title, media):
    103          """
    104          Creates a new CSSStyleSheet.
    105
    106          title of type DOMString
    107              The advisory title. See also the Style Sheet Interfaces
    108              section.
    109          media of type DOMString
    110              The comma-separated list of media associated with the new style
    111              sheet. See also the Style Sheet Interfaces section.
    112
    113          returns
    114              CSSStyleSheet: A new CSS style sheet.
    115
    116          TODO: DOMException
    117              SYNTAX_ERR: Raised if the specified media string value has a
    118              syntax error and is unparsable.
    119          """
    120          return css.CSSStyleSheet(title=title, media=media)
    121
    122 -    def createDocument(self, *args):
    123          # not needed to HTML, also not for CSS?
    124          raise NotImplementedError
    125
    126 -    def createDocumentType(self, *args):
    127          # not needed to HTML, also not for CSS?
    128          raise NotImplementedError
    129
    130 -    def hasFeature(self, feature, version):
    131          return (feature.lower(), unicode(version)) in self._features
    132
    133  xml.dom.registerDOMImplementation('cssutils', DOMImplementationCSS)
    134
    135
    136  # parser helper functions
    137 -def parse(filename, encoding=None, href=None, media=None):
    138      """
    139      reads file with given filename in given encoding (if given)
    140      and returns CSSStyleSheet object
    141      """
    142      return CSSParser().parse(filename, encoding)
    143
    144 -def parseString(cssText, href=None, media=None):
    145      """
    146      parses given string and returns CSSStyleSheet object
    147      """
    148      return CSSParser().parseString(cssText, href=href, media=media)
    149
    150  # set "ser", default serializer
    151 -def setSerializer(serializer):
    152      """
    153      sets the global serializer used by all class in cssutils
    154      """
    155      global ser
    156      ser = serializer
    157
    158
    159  if __name__ == '__main__':
    160      print __doc__
    161