NAME
CSS::Parser - parser for CSS-style syntax
SYNOPSIS
use CSS::Parser;
my $css = CSS::Parser->new(
handlers => {
css_comment => \&css_com,
selector_string => \&sel,
block_start => \&blk_s,
property => \&prop,
value => \&val,
block_end => \&blk_e,
at_rule => \&atr,
at_symbol => \&ats,
error => \&error
}
);
$css->parse(\$some_css_text);
sub css_com {
my $self = shift;
my $comment = shift;
print "css comment:\n\t$comment\n";
}
...
DESCRIPTION
The CSS::Parser
deals simply with fairly loose CSS syntax. It doesn't know anything about what it is parsing, for that see other classes. This allows it to be the base for CSS::CSSn
, CSS::STTSn
and any other parser for a css-compatible syntax.
Chances are, you will want to use one of the existing subclasses or to subclass it yourself.
The interface to CSS::Parser
is:
- my $css = CSS::Parser->new([style => $parse_style],[handlers => $hashref_of_handlers]);
-
The constructor takes a variety of options provided in a hash.
style defines the style of parsing that you want from the parser. As of now the only style is 'callback' (default), however this may evolve in the future.
handlers specifies a hash of callbacks to be called when a given token has been met. Callbacks are CODEREFS. The callback sub will receive the parser object as it's first argument, and optionally the token when it makes sense (eg: selector_string will be passed the selector, but block_start won't receive the '{').
One may use 'default' to activate them all. Note that you can set a callback to the empty string (not undef) if you wish to set a default for all but to deactivate that specific callback.
html_open_comment
Called when a <!-- comment is seens, no second argument.
html_close_comment
Called when a --> comment is seens, no second argument.
css_comment
Called when a css comment of the form /* text */ is seen. Receives the comment with leading /* and trailing */.
comment
Called when any of the above types of comment is seen. Receives the comment as is.
at_symbol
Called when an at-symbol (@ident) is seen and is followed by a block. The block is parsed subsequently. Receives the at-symbol.
at_rule
Called when a block-less at-rule (@import url('foo') print;) is seen, receives the entire rule (it isn't further tokenized, that is the job of the subclass because it requires it to know what is expected after the specific at-keyword).
selector_string
Called whenever a selector string is seen and receives the entire string as tokenising it further would require knowledge of the specific variant of CSS.
block_start
Called when the beginning of a block is seen ({), receives nothing.
property
Called when a property is seen, receives the property.
value
Called when a value is seen, receives the value which is not parsed to know it's individual components, that belongs to the subclass.
declaration
Called with property : $value ;
block_end
Same as block_start, for }.
ruleset
Called with selector_string '{' [declaration;]* '}'
error
Called whenever the parser notices an error, receives what is left of the CSS text when the error occurred plus an informative message.
default
The use for default is to substitute a standard callback for all callbacks that are not defined.
- $css->parse( $string_ref );
-
Tells the parser to go ahead and parse the provided reference to a string. Note that the string will be empty after the parser has finished, unless the stylesheet contains an error. It returns 1 upon success and undef upon failure.
SEE ALSO
XML::Parser, HTML::Parser, HTML::TreeBuilder
COPYRIGHT
Copyright 1998-1999 Robin Berjon (robin@knowscape.com). All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.