[Linux-disciples] CSS parser

Stephen R Laniel steve at laniels.org
Thu Jun 23 14:51:34 EDT 2005


Does anyone know if there's a good CSS parser available for
Perl? I have a client that's using a tremendously wasteful
and labor-intensive set of Cascading Style Sheets: they use
one sheet for every kind of browser they might serve, which
is fine, but every sheet duplicates the functions in the
rest. So if I want to set the <em> tag to be rendered bold
rather than italic, I have to type

em {
	font-weight: bold;
}

into 9 separate stylesheets. If I then want to edit the
attributes for a given element, I normally have to go
through and manually edit 9 stylesheets.

There's a reason why the word 'cascading' is in the phrase
'Cascading Style Sheets': you can pull out all the common
styles, put them all in, say, global.css, then do an

@import url('global.css');

at the top of your browser-specific stylesheet. Then include
all the browser-specific styles in the cascaded stylesheets.

So what I need is a library that will take a given
stylesheet, parse it, and optionally (though quite usefully)
tell me that the stylesheet directives

	em {
		font-weight: bold;
	}
	em {
		font-weight: normal;
	}

are equivalent to

em {
	font-weight: normal;
}

given the precedence rules of the cascade.

It would be able to compare N stylesheets, render them all
into some canonical form, then tell me those places where
the stylesheets differ.

For now I think I'll bang out a quick, primitive parser that
calls anything between '{' and '}' a 'block', then sorts the
directives inside each block, then sorts the blocks by the
elements they correspond to. This will be a cheap parser,
but I'm wondering if there's a better one out there.

-- 
Stephen R. Laniel
steve at laniels.org
+(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.bostoncoop.net/pipermail/linux-disciples/attachments/20050623/1d821ec9/attachment.pgp


More information about the Linux-disciples mailing list