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',
     61             'CSSParser', 'CSSSerializer']
     62  __docformat__ = 'restructuredtext'
     63  __author__ = '$LastChangedBy: cthedot $'
     64  __date__ = '$LastChangedDate: 2008-01-13 18:10:54 +0100 (So, 13 Jan 2008) $'
     65  __version__ = '0.9.5a1 $LastChangedRevision: 846 $'
     66
     67  import codec
     68
     69  # order of imports is important (maybe as it is partly circular)
     70  import xml.dom
     71
     72  import errorhandler
     73  log = errorhandler.ErrorHandler()
     74
     75  import util
     76  import css
     77  import stylesheets
     78  from parse import CSSParser
     79
     80  from serialize import CSSSerializer
     81  ser = CSSSerializer()
     82
     83 -class DOMImplementationCSS(object):
     84      """
     85      This interface allows the DOM user to create a CSSStyleSheet
     86      outside the context of a document. There is no way to associate
     87      the new CSSStyleSheet with a document in DOM Level 2.
     88
     89      This class is its *own factory*, as it is given to
     90      xml.dom.registerDOMImplementation which simply calls it and receives
     91      an instance of this class then.
     92      """
     93      _features = [
     94          ('css', '1.0'),
     95          ('css', '2.0'),
     96          ('stylesheets', '1.0'),
     97          ('stylesheets', '2.0')
     98      ]
     99
    100 -    def createCSSStyleSheet(self, title, media):
    101          """
    102          Creates a new CSSStyleSheet.
    103
    104          title of type DOMString
    105              The advisory title. See also the Style Sheet Interfaces
    106              section.
    107          media of type DOMString
    108              The comma-separated list of media associated with the new style
    109              sheet. See also the Style Sheet Interfaces section.
    110
    111          returns
    112              CSSStyleSheet: A new CSS style sheet.
    113
    114          TODO: DOMException
    115              SYNTAX_ERR: Raised if the specified media string value has a
    116              syntax error and is unparsable.
    117          """
    118          return css.CSSStyleSheet(title=title, media=media)
    119
    120 -    def createDocument(self, *args):
    121          # not needed to HTML, also not for CSS?
    122          raise NotImplementedError
    123
    124 -    def createDocumentType(self, *args):
    125          # not needed to HTML, also not for CSS?
    126          raise NotImplementedError
    127
    128 -    def hasFeature(self, feature, version):
    129          return (feature.lower(), unicode(version)) in self._features
    130
    131  xml.dom.registerDOMImplementation('cssutils', DOMImplementationCSS)
    132
    133
    134  # parser helper functions
    135 -def parse(filename, encoding=None, href=None, media=None):
    136      """
    137      reads file with given filename in given encoding (if given)
    138      and returns CSSStyleSheet object
    139      """
    140      return CSSParser().parse(filename, encoding)
    141
    142 -def parseString(cssText, href=None, media=None):
    143      """
    144      parses given string and returns CSSStyleSheet object
    145      """
    146      return CSSParser().parseString(cssText, href=href, media=media)
    147
    148  # set "ser", default serializer
    149 -def setSerializer(serializer):
    150      """
    151      sets the global serializer used by all class in cssutils
    152      """
    153      global ser
    154      ser = serializer
    155
    156
    157  if __name__ == '__main__':
    158      print __doc__
    159