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