1package CSS::Parse::Lite; 2 3$VERSION = 1.02; 4 5use CSS::Parse; 6@ISA = qw(CSS::Parse); 7 8use strict; 9use warnings; 10 11use Carp; 12use CSS::Style; 13use CSS::Selector; 14use CSS::Property; 15 16# The main parser 17sub parse_string { 18 my $self = shift; 19 my $string = shift; 20 21 $string =~ s/\r\n|\r|\n/ /g; 22 23 # Split into styles 24 foreach ( grep { /\S/ } split /(?<=\})/, $string ) { 25 unless ( /^\s*([^{]+?)\s*\{(.*)\}\s*$/ ) { 26 croak( "Invalid or unexpected style data '$_'" ); 27 } 28 $self->add_style($1, $2); 29 } 30} 31 32# add a style to the style list 33sub add_style { 34 my $self = shift; 35 my $style = shift; 36 my $contents = shift; 37 38 my $style_obj = new CSS::Style({ 39 'adaptor' => $self->{parent}->{adaptor} 40 }); 41 42 # parse the selectors 43 for(split(/\s*,\s*/, $style)){ 44 my $selector_obj = new CSS::Selector({'name' => $_}); 45 $style_obj->add_selector($selector_obj); 46 } 47 48 # parse the properties 49 foreach ( grep { /\S/ } split /\;/, $contents ) { 50 unless ( /^\s*([\w._-]+)\s*:\s*(.*?)\s*$/ ) { 51 croak( "Invalid or unexpected property '$_' in style '$style'" ); 52 } 53 my $property_obj = new CSS::Property({ 54 'property' => $1, 55 'value' => $2, 56 'adaptor' => $style_obj->{adaptor}, 57 }); 58 $style_obj->add_property($property_obj); 59 } 60 61 push @{$self->{parent}->{styles}}, $style_obj; 62} 63 641; 65 66__END__ 67 68=head1 NAME 69 70CSS::Parse::Lite - A CSS::Parse module using regular expressions 71 72=head1 SYNOPSIS 73 74 use CSS; 75 76 # Create a css stylesheet 77 my $CSS = CSS->new({'parser' => 'CSS::Parse::Lite'}); 78 79=head1 DESCRIPTION 80 81This module is a parser for CSS.pm. Read the CSS.pm pod for more details 82 83=head1 AUTHOR 84 85Copyright (C) 2003-2004, Cal Henderson <cal@iamcal.com> 86 87=head1 SEE ALSO 88 89L<CSS>, http://www.w3.org/TR/REC-CSS1 90 91=cut 92