VERSION

Version 0.14

This is an alpha version. The \s-1API\s0 is still subject to change. Many features have not been implemented yet (but patches would be welcome :-).

The interface for feeding \s-1CSS\s0 code to \s-1CSS::DOM\s0 changed incompatibly in version 0.03.

SYNOPSIS

  use CSS::DOM;

  my $sheet = CSS::DOM::parse( $css_source );

  use CSS::DOM::Style;
  my $style = CSS::DOM::Style::parse(
      'background: red; font-size: large'
  );

  my $other_sheet = new CSS::DOM; # empty
  $other_sheet->insertRule(
     'a{ text-decoration: none }',
      $other_sheet->cssRules->length,
  );
  # etc.

  # access DOM properties
  $other_sheet->cssRules->[0]->selectorText('p'); # change it
  $style->fontSize;          # returns 'large'
  $style->fontSize('small'); # change it

DESCRIPTION

This set of modules provides the CSS-specific interfaces described in the W3C \s-1DOM\s0 recommendation.

The \s-1CSS::DOM\s0 class itself implements the StyleSheet and CSSStyleSheet \s-1DOM\s0 interfaces.

This set of modules has two modes:

1.

It can validate property values, ignoring those that are invalid (just like a real web browser), and support shorthand properties. This means you can set font to '13px/15px My Font' and have the font-size, line-height, and font-family properties (among others) set automatically. Also, \*(C`color: green; color: kakariki\*(C' will assign 'green' to the color property, 'kakariki' not being a recognised color value.

2.

It can blithely accept all property assignments as being valid. In the case of \*(C`color: green; color kakariki\*(C', 'kakariki' will be assigned, since it overrides the previous assignment.

These two modes are controlled by the \*(C`property_parser\*(C' option to the constructors.

CONSTRUCTORS

This method parses the $string and returns a style sheet object. If you just have a \s-1CSS\s0 style declaration, e.g., from an \s-1HTML\s0 \*(C`style\*(C' attribute, see \*(L"parse\*(R" in CSS::DOM::Style.

new \s-1CSS::DOM\s0

Creates a new, empty style sheet object. Use this only if you plan to build the style sheet piece by piece, instead of parsing a block of \s-1CSS\s0 code.

You can pass named arguments to both of those. \*(C`parse\*(C' accepts all of them; \*(C`new\*(C' understands only the first two, \*(C`property_parser\*(C' and \*(C`url_fetcher\*(C'.

property_parser

Set this to a PropertyParser object to specify which properties are supported and how they are parsed. If this option is not specified or is set to \*(C`undef\*(C', all property values are treated as valid. See CSS::DOM::PropertyParser for more details.

url_fetcher

This has to be a code ref that returns the contents of the style sheet at the \s-1URL\s0 passed as the sole argument. E.g., # Disclaimer: This does not work with relative URLs. use LWP::Simple; use CSS::DOM; $css = '@import "file.css"; /* other stuff ... '; $ss = CSS::DOM::parse $css, url_fetcher => sub { get shift }; $ss->cssRules->[0]->styleSheet; # returns a style sheet object # corresponding to file.css The subroutine can choose to return \*(C`undef\*(C' or an empty list, in which case the @import rule's \*(C`styleSheet\*(C' method will return null (empty list or \*(C`undef\*(C'), as it would if no \*(C`url_fetcher\*(C' were specified. It can also return named items after the \s-1CSS\s0 code, like this: return $css_code, decode => 1, encoding_hint => 'iso-8859-1'; These correspond to the next two items:

decode

If this is specified and set to a true value, then \s-1CSS::DOM\s0 will treat the \s-1CSS\s0 code as a string of bytes, and try to decode it based on @charset rules and byte order marks. By default it assumes that it is already in Unicode (i.e., decoded).

encoding_hint

Use this to provide a hint as to what the encoding might be. If this is specified, and \*(C`decode\*(C' is not, then \*(C`decode => 1\*(C' is assumed.

STYLE SHEET ENCODING

See the options above. This section explains how and when you should use those options.

According to the \s-1CSS\s0 spec, any encoding specified in the 'charset' field on an \s-1HTTP\s0 Content-Type header, or the equivalent in other protocols, takes precedence. In such a case, since \s-1CSS::DOM\s0 doesn't deal with \s-1HTTP\s0, you have to decode it yourself.

Otherwise, you should use \*(C`decode => 1\*(C' to instruct \s-1CSS::DOM\s0 to use byte order marks or @charset rules.

If neither of those is present, then encoding data in the referencing document (e.g., <link charset=\*(L"...\*(R"> or an \s-1HTML\s0 document's own encoding), if available/applicable, should be used. In this case, you should use the \*(C`encoding_hint\*(C' option, so that \s-1CSS::DOM\s0 has something to fall back to.

If you use \*(C`decode => 1\*(C' with no encoding hint, and no \s-1BOM\s0 or @charset is to be found, \s-1UTF-8\s0 is assumed.

SYNTAX ERRORS

The two constructors above, and also \*(C`CSS::DOM::Style::parse\*(C', set $@ to the empty string upon success. If they encounter a syntax error, they set $@ to the error and return an object that represents whatever was parsed up to that point.

Other methods that parse \s-1CSS\s0 code might die on encountering syntax errors, and should usually be wrapped in an \*(C`eval\*(C'.

The parser follows the 'future-compatible' syntax described in the \s-1CSS\s0 2.1 specification, and also the spec's rules for handling parsing errors. Anything not handled by those two is a syntax error.

In other words, a syntax error is one of the following:

  • An unexpected closing bracket, as in these examples a { text-decoration: none ) *[name=~'foo'} {} #thing { clip: rect( ]

  • An \s-1HTML\s0 comment delimiter within a rule; e.g., a { text-decoration : none <!-- /* Oops! */ } <!-- /*ok*/ @media --> /* bad! */ print { }

  • An extra \*(C`@\*(C' keyword or semicolon where it doesn't belong; e.g., @media @print { .... } @import "file.css" @print; td, @page { ... } #tabbar td; #tab1 { }

OBJECT METHODS

Attributes

type

Returns the string 'text/css'.

disabled

Allows one to specify whether the style sheet is used. (This attribute is not actually used yet by \s-1CSS::DOM\s0.) You can set it by passing an argument.

ownerNode

Returns the node that 'owns' this style sheet.

parentStyleSheet

If the style sheet belongs to an '@import' rule, this returns the style sheet containing that rule. Otherwise it returns an empty list.

href

Returns the style sheet's \s-1URI\s0, if applicable.

title

Returns the value of the owner node's title attribute.

media

Returns the MediaList associated with the style sheet (or a plain list in list context). This defaults to an empty list. You can pass a comma-delimited string to the MediaList's \*(C`mediaText\*(C' method to initialise it. (The medium information is not actually used [yet] by \s-1CSS::DOM\s0, but you can put it there.)

ownerRule

If this style sheet was created by an @import rule, this returns the rule; otherwise it returns an empty list (or undef in scalar context).

cssRules

In scalar context, this returns a CSS::DOM::RuleList object (simply a blessed array reference) of CSS::DOM::Rule objects. In list context it returns a list.

Methods

Parses the rule contained in the $css_code, inserting it in the style sheet's list of rules at the given $index. Deletes the rule at the given $index. You can call this either as an object or class method. This is actually supposed to be a method of the 'DOMImplementation' object. (See, for instance, HTML::DOM::Interface's method of the same name, which delegates to this one.) This returns a boolean indicating whether a particular \s-1DOM\s0 module is implemented. Right now it returns true only for the '\s-1CSS2\s0' and 'StyleSheets' features (version '2.0').

Non-DOM Methods

set_ownerNode

This allows you to set the value of \*(C`ownerNode\*(C'. Passing an argument to \*(C`ownerNode\*(C' does nothing, because it is supposed to be read-only. But you have to be able to set it somehow, so that's why this method is here. The style sheet will hold a weak reference to the object passed to this method.

set_href

Like \*(C`set_ownerNode\*(C', but for \*(C`href\*(C'.

property_parser
url_fetcher

These two both return what was passed to the constructor. The second one, \*(C`url_fetcher\*(C' also allows an assignment, but this is not propagated to sub-rules and is intended mainly for internal use.

FUNCTIONS

CSS::DOM::parse

See \*(L"\s-1CONSTRUCTORS\s0\*(R", above. Warning: This is still highly experimental and crawling with bugs. This computes the style for a given \s-1HTML\s0 element. It does not yet calculate actual measurements (e.g., converting percentages to pixels), but simply applies the cascading rules and selectors. Pseudo-classes are not yet supported (but pseudo-elements are). The precedence rules for normal vs important declarations in the \s-1CSS\s0 2 specification are used. (\s-1CSS\s0 2.1 is unclear.) The precedence is as follows, from lowest to highest: user agent normal declarations user normal declarations author normal " user agent !important declarations author !important " user " " The %options are as follows. They are all optional except for \*(C`element\*(C'.

ua_sheet

The user agent style sheet

user_sheet

The user style sheet

author_sheets

Array ref of style sheets that the \s-1HTML\s0 document defines or links to.

element

The element, as an HTML::DOM::Element object.

pseudo

The pseudo-element (e.g., 'first-line'). This can be specified with no colons (the way Opera requires it) or with one or two colons (the way Firefox requires it).

medium
height
width
ppi

(To be implemented)

The

CLASSES AND DOM INTERFACES

Here are the inheritance hierarchy of \s-1CSS::DOM\s0's various classes and the \s-1DOM\s0 interfaces those classes implement. For brevity's sake, a simple '::' at the beginning of a class name in the left column is used for '\s-1CSS::DOM::\s0'. Items in brackets do not exist yet. (See also CSS::DOM::Interface for a machine-readable list of standard methods.)

Class Inheritance Hierarchy Interfaces --------------------------- ----------

CSS::DOM StyleSheet, CSSStyleSheet ::Array ::MediaList MediaList ::StyleSheetList StyleSheetList ::RuleList CSSRuleList ::Rule CSSRule, CSSUnknownRule ::Rule::Style CSSStyleRule ::Rule::Media CSSMediaRule ::Rule::FontFace CSSFontFaceRule ::Rule::Page CSSPageRule ::Rule::Import CSSImportRule ::Rule::Charset CSSCharsetRule ::Style CSSStyleDeclaration, CSS2Properties ::Value CSSValue ::Value::Primitive CSSPrimitiveValue, RGBColor, Rect ::Value::List CSSValueList [::Counter Counter]

\s-1CSS::DOM\s0 does not implement the following interfaces (see \s-1HTML::DOM\s0 for these):

LinkStyle DocumentStyle ViewCSS DocumentCSS DOMImplementationCSS ElementCSSInlineStyle

IMPLEMENTATION NOTES

  • Attributes of objects are accessed via methods of the same name. When the method is invoked, the current value is returned. If an argument is supplied, the attribute is set (unless it is read-only) and its old value returned.

  • Where the \s-1DOM\s0 spec. says to use null, undef or an empty list is used.

  • Instead of \s-1UTF-16\s0 strings, \s-1CSS::DOM\s0 uses Perl's Unicode strings.

  • Each method that the specification says returns an array-like object (e.g., a RuleList) will return such an object in scalar context, or a simple list in list context. You can use the object as an array ref in addition to calling its \*(C`item\*(C' and \*(C`length\*(C' methods.

PREREQUISITES

perl 5.8.2 or higher

Exporter 5.57 or later

Encode 2.10 or higher

Clone 0.09 or higher

BUGS

The parser has not been updated to conform to the April 2009 revision of the \s-1CSS\s0 2.1 candidate recommendation. Specifically, unexpected closing brackets are not ignored, but cause syntax errors; and @media rules containing unrecognised statements are themselves currently treated as unrecognised (the unrecognised inner statements should be ignored, rendering the outer @media rule itself valid).

If you create a custom property parser that defines 'list-style-type' to include multiple tokens, then counters will become \*(C`CSS_CUSTOM\*(C' CSSValue objects instead of \*(C`CSS_COUNTER\*(C' CSSPrimitiveValue objects.

If you change a property parser's property definitions such that a primitive value becomes a list, or vice versa, and then try to modify the \*(C`cssText\*(C' property of an existing value object belonging to that property, things will go awry.

Whitespace and comments are sometimes preserved in serialised \s-1CSS\s0 and sometimes not. Expect inconsistency.

To report bugs, please e-mail the author.

ACKNOWLEDGEMENTS

Thanks to Ville Skytta\*: and Nicholas Bamber for their contributions.

AUTHOR & COPYRIGHT

Copyright (C) 2007-12 Father Chrysostomos <sprout [at] cpan [dot] org>

This program is free software; you may redistribute it and/or modify it under the same terms as perl. The full text of the license can be found in the \s-1LICENSE\s0 file included with this module.

RELATED TO CSS::DOM…

All the classes listed above under \*(L"\s-1CLASSES\s0 \s-1AND\s0 \s-1DOM\s0 \s-1INTERFACES\s0\*(R".

\s-1CSS::SAC\s0, \s-1CSS\s0.pm and \s-1HTML::DOM\s0

The \s-1DOM\s0 Level 2 Style specification at http://www.w3.org/TR/DOM-Level-2-Style <http://www.w3.org/TR/DOM-Level-2-Style>

The \s-1CSS\s0 2.1 specification at <http://www.w3.org/TR/CSS21/>