yocto_css.py revision ce73efc40e53170d5ebd6de7102c6d718083e456
# -*- coding: utf-8 -*-
# yocto-css, an extremely bare minimum CSS parser
#
# Copyright 2009 Jeff Schiller
#
# This file is part of Scour, http://www.codedread.com/scour/
#
# it either under the terms of the Apache License, Version 2.0, or, at
# your option, under the terms and conditions of the GNU General
# Public License, Version 2 or newer as published by the Free Software
# Foundation. You may obtain a copy of these Licenses at:
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# In order to resolve Bug 368716 (https://bugs.launchpad.net/scour/+bug/368716)
# scour needed a bare-minimum CSS parser in order to determine if some elements
# were still referenced by CSS properties.
# I looked at css-py (a CSS parser built in Python), but that library
# is about 35k of Python and requires ply to be installed. I just need
# something very basic to suit scour's needs.
# yocto-css takes a string of CSS and tries to spit out a list of rules
# A rule is an associative array (dictionary) with the following keys:
# - selector: contains the string of the selector (see CSS grammar)
# - properties: contains an associative array of CSS properties for this rule
# TODO: need to build up some unit tests for yocto_css
# stylesheet : [ CDO | CDC | S | statement ]*;
# statement : ruleset | at-rule;
# at-rule : ATKEYWORD S* any* [ block | ';' S* ];
# block : '{' S* [ any | block | ATKEYWORD S* | ';' S* ]* '}' S*;
# ruleset : selector? '{' S* declaration? [ ';' S* declaration? ]* '}' S*;
# selector : any+;
# declaration : property S* ':' S* value;
# property : IDENT;
# value : [ any | block | ATKEYWORD S* ]+;
# any : [ IDENT | NUMBER | PERCENTAGE | DIMENSION | STRING
# | DELIM | URI | HASH | UNICODE-RANGE | INCLUDES
# | DASHMATCH | FUNCTION S* any* ')'
# | '(' S* any* ')' | '[' S* any* ']' ] S*;
def parseCssString(str):
rules = []
# first, split on } to get the rule chunks
# second, split on { to get the selector and the list of properties
rule = {}
# third, split on ; to get the property declarations
props = {}
# fourth, split on : to get the property name and value
return rules