The manakai project

Web::CSS::Parser

A CSS parser

DESCRIPTION

The Web::CSS::Parser module provides a CSS parser.

This module is not intended for direct use by general applications. It should be used through standard DOM/CSSOM APIs implemented by, e.g., Web::DOM::StyleSheet and related modules.

METHODS

The following method can be used to construct the parser:

$p = Web::CSS::Parser->new

Return a new instance of CSS parser.

Note that the Web::CSS::Selectors::Parser and Web::CSS::MediaQueries::Parser modules also have the new method, which can be used to create a Selectors or Media Queries parser, respectively, which provide subsets of methods of this module.

Following methods can be used to set parameters of the parser:

$context = $parser->context
$parser->context ($context)

Return or specify the Web::CSS::Context object for parsing.

$mr = $parser->media_resolver
$parser->media_resolver ($mr)

Return or specify the Web::CSS::MediaResolver object for parsing.

$code = $parser->onerror
$parser->onerror ($code)

Return or specify the code reference to which any errors and warnings during the parsing is reported.

XXX

Following methods can be used to parse fragments of CSS style sheets:

$p->init_parser

Initialize the parser internal states. If you invoke the parsing method only once, you don't have to invoke this method. If you are intended to invoke parsing methods multiple times, this method should be invoked before any subsequent parser invocations to clear internal states.

$ss = $p->parse_byte_string_as_rule ($string, OPTIONS)

Parse the input string as a rule. The result is returned as the parsed style sheet struct, where at most one rule is contained directly by the style sheet. If there is not exactly one rule, or the only rule is invalid, an empty style sheet struct is returned.

The input string is interpreted as a byte sequence. As described in the CSS specification [CSSSYNTAX], the charset parameter and any @charset rule is taken into account for determining the character encoding. Following name/value pairs can be specified as OPTIONS arguments:

transport_encoding_name => $name

The encoding name or alias, as specified in the charset parameter in the Content-Type: HTTP header field (or equivalent). If no charset metadata is available, this option has to be left unspecified (or undef).

parent_encoding_name => $encoding_label

An encoding label, provided by the context in which the reference to the style sheet appears. If the style sheet is referenced by a link element or xml-stylseheet processing instruction, its charset attribute value should be specified here. If no such contextual character encoding metadata is available, this option has to be left undefined (or undef).

embed_encoding_key => $encoding_key

The encoding key (see Web::Encoding), determined by the context in which the reference to the style sheet appears. If the style sheet is referenced by an HTML link element, it is the document's encoding. Otherwise, if the style sheet is referenced by a CSS @import rule, it is the style sheet's encoding. Otherwise, this option has to be left undefined (or undef).

The encoding key acutally used to parse the style sheet is available as $ss->{input_encoding}. (If the style sheet is created from a character string by one of following methods, this field is left unspecified.)

$mq = $p->parse_char_string_as_mq ($string)

Parse the input character string as a media query and return the parsed structure. See also Web::CSS::MediaQueries::Parser.

$mq_list = $p->parse_char_string_as_mq_list ($string)

Parse the input character string as a list of media queries and return the parsed structure. See also Web::CSS::MediaQueries::Parser.

$props = $p->parse_char_string_as_prop_decls ($string)

Parse the input character string as a list of declarations, where each declaration is a property specification, and return the parsed structure.

$props = $p->parse_char_string_as_prop_value ($prop_name, $string)

Parse the input character string as a property value of the specified property and return the parsed structure. If the specified property is not supported, it returns undef. If the property value is not valid, an empty property list is returned.

$ss = $p->parse_char_string_as_rule ($string)

Parse the input character string as a rule. The result is returned as the parsed style sheet struct, where at most one rule is contained directly by the style sheet. If there is not exactly one rule, or the only rule is invalid, an empty style sheet struct is returned.

$selectors = $p->parse_char_string_as_selectors ($string)

Parse the input character string as a list of selectors and return the parsed structure. See also Web::CSS::Selectors::Parser.

$ss = $p->parse_char_string_as_ss ($string)

Parse the input character string as a style sheet and return the parsed structure.

Following method is intended to be used with appropriate Web::DOM::Node, Web::DOM::StyleSheet, and/or Web::DOM::CSSRule objects:

$p->get_parser_of_document ($obj)

Return the CSS parser associated with the document. If the argument is the Web::DOM::Document object, its associated CSS parser is returned. If the argument is other Web::DOM::Node object, its owner document's associated CSS parser is returned. If the argument is Web::DOM::CSSStyleSheet or Web::CSS::CSSRule object, its associated CSS parser is returned.

$p->parse_style_element ($el)

XXX at risk

Parse the content of the HTML style element and set the sheet IDL attribute. The argument MUST be a Web::DOM::Element object representing the style element.

DEPENDENCY

See "DEPENDENCY" in Web::CSS.

SPECIFICATIONS

CSSSYNTAX

CSS Syntax Module <https://drafts.csswg.org/css-syntax/>.

An invalid declaration MUST NOT be included in the style rule.

CSSCONDITIONAL

CSS Conditional Rules <http://dev.w3.org/csswg/css-conditional/>.

CSSCASCADE

CSS Cascading and Inheritance <http://dev.w3.org/csswg/css-cascade/>.

When a property value is '-moz-initial', the parser MUST replace it by 'initial'.

CSSNAMESPACES

CSS Namespaces <http://dev.w3.org/csswg/css-namespaces/>.

CSSOM

CSSOM <http://dev.w3.org/csswg/cssom/>.

HTML

HTML Living Standard <http://www.whatwg.org/specs/web-apps/current-work/#styling>.

SEE ALSO

Web::CSS::Props contains the list of supported CSS properties.

Web::CSS::Selectors::Parser, Web::CSS::MediaQueries::Parser.

Web::CSS::Serializer.

Web::DOM::CSSStyleSheet, Web::DOM::CSSRule, Web::DOM::CSSStyleDeclaration.

AUTHOR

Wakaba <wakaba@suikawiki.org>.

LICENSE

Copyright 2007-2017 Wakaba <wakaba@suikawiki.org>.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.