Skip to content

Latest commit

 

History

History
104 lines (77 loc) · 4.14 KB

README.md

File metadata and controls

104 lines (77 loc) · 4.14 KB

CSS::Language

CSS::Language is under construction as a Perl 6 validating parser for CSS Levels 1 and 2.1 and stable/complete Level 3 modules.

CSS::Language extends CSS::Grammar. It performs property-specific parsing and validation of declarations.

This module implements the following grammars and actions:

  • CSS::Language::CSS1 + CSS::Language::CSS1::Actions
  • CSS::Language::CSS21 + CSS::Language::CSS21::Actions
  • CSS::Language::CSS3 + CSS::Language::CSS3::Actions

Parser Actions

CSS::Language::CSS1::Actions, CSS::Language::CSS21::Actions or CSS::Language::CSS3::Actions perform validation and abstract syntax tree (AST) construction. Warnings are produced for any unexpected input.

use v6;
use CSS::Language::CSS21;
use CSS::Language::CSS21::Actions;

my $css = 'H1 { color: blue; foo: bar; background-color: zzz }';

my $actions =  CSS::Language::CSS21::Actions.new;
my $p = CSS::Language::CSS21.parse($css, :actions($actions));
note $_ for $actions.warnings;
say "declaration: " ~ $p.ast[0]<ruleset><declarations>.perl;
# output:
# unknown property: foo - declaration dropped
# usage background-color: <color> | transparent | inherit
# declaration: {"color" => {"expr" => ["color" => {"r" => 0, "g" => 0, "b" => 255}]}}

CSS3 Extension Modules

CSS Level 3 inherits all CSS2.1 properties and definitions. These are then extended via CSS3 Extension Modules that are at various levels of maturity. Furthermore, modules may be domain or media specific - see http://www.css3.info/modules/

CSS::Langauge::CSS3 mirrors, this structure, inheriting from CSS2.1, then extending the language through a number of loosely coupled extension modules.

  • CSS::Langauge::CSS3::CSS21_Imported - the full set of CSS21 properties
  • CSS::Language::CSS3::Colors - CSS 3.0 Colors (@color-profile)
  • CSS::Language::CSS3::Fonts - CSS 3.0 Fonts (@font-face)
  • CSS::Language::CSS3::Selectors - CSS 3.0 Selectors
  • CSS::Language::CSS3::Namespaces - CSS 3.0 Namespace (@namespace)
  • CSS::Language::CSS3::Media - CSS 3.0 Media (@media)
  • CSS::Language::CSS3::PagedMedia - CSS 3.0 Paged Media (@page)

Specification Grammar

CSS::Language::Specification is also included in this distribution.

It implements the synopsis grammar used throughout the W3C documents to specify properties and functions. For example, the specification for border-color is:

'border-color' [ <color> | transparent ]{1,4} | inherit

It was used to generate the initial grammars and actions in this module. Example usage:

% etc/gen-properties.pl gen grammar etc/css21-properties.txt

Installation

This module works with Rakudo Star 2013.02 [download from http://rakudo.org/downloads/star/ - don't forget the final make install]:

Ensure that perl6 and panda are available on your path, e.g. :

% export PATH=~/src/rakudo-star-2013.02/install/bin:$PATH

You can then use panda to test and install CSS::Language:

% panda install CSS::Language

To try parsing some content:

% perl6 -MCSS::Language::CSS21 -e"say CSS::Language::CSS21.parse('h1 {margin:2pt; color: blue}')"

See Also

References