@@ -17,15 +17,14 @@ use CSS::Module::CSS3;
1717
1818use LibXML::Document;
1919use LibXML::Element;
20- use LibXML::_ParentNode; # document, element or document fragment
2120use LibXML::XPath::Context;
2221
2322use URI;
24-
25- has LibXML::_ParentNode: D $ . doc is required ;
23+ has LibXML::Document: D $ . doc is required ;
2624has CSS::Stylesheet $ ! stylesheet ;
2725method stylesheet handles <Str gist ast page page-properties font-face font-sources font-family base-url > { $ ! stylesheet }
2826has Array [CSS::Ruleset] % . rulesets ; # rulesets to node-path mapping
27+ has Array [CSS::Properties] % . propsets ; # rulesets to node-path mapping
2928has CSS::Module: D $ . module = CSS::Module::CSS3. module;
3029has CSS::Properties % . style ; # per node-path styling, including tags
3130has CSS::TagSet $ . tag-set ;
@@ -43,8 +42,8 @@ method !build(
4342 if $ ! doc . isa (LibXML::Document);
4443
4544 $ ! tag-set // = $ ! doc ~~ LibXML::Document::HTML
46- ?? CSS::TagSet::XHTML. new : : $ ! module
47- !! CSS::TagSet. new ;
45+ ?? CSS::TagSet::XHTML. new : : $ ! module
46+ !! CSS::TagSet. new ;
4847
4948 $ ! stylesheet // = $ ! tag-set . stylesheet($ ! doc , : $ media , : $ base-url , : $ imports , : $ links , : $ font-family );
5049 my LibXML::XPath::Context $ xpath-context .= new : : $ ! doc ;
@@ -56,20 +55,19 @@ method !build(
5655 % ! rulesets {. nodePath}. push : $ rule ;
5756 }
5857 }
58+
59+ for % ! rulesets {
60+ my CSS::Properties @ propsets = . value . sort (*. specificity)». properties;
61+ % ! propsets {. key } = @ propsets ;
62+ }
5963}
6064
61- multi submethod TWEAK (Str : D : stylesheet($ string )! , | c) {
62- $ ! stylesheet .= parse ($ string );
63- self ! build(| c);
64- }
65-
66- multi submethod TWEAK (CSS::Stylesheet : $ ! stylesheet , | c) {
65+ submethod TWEAK (CSS::Stylesheet() : $ ! stylesheet , | c) {
6766 self ! build(| c);
6867}
6968
70- multi method COERCE (Str : D $ _ ) { self . new : : stylesheet($ _ ); }
71- multi method COERCE (CSS::Stylesheet: D $ stylesheet ) { self . new : : $ stylesheet ; }
7269multi method COERCE (LibXML::_ParentNode: D $ doc ) { self . new : : $ doc }
70+ multi method COERCE (CSS::Stylesheet: D () $ stylesheet ) { self . new : : $ stylesheet ; }
7371
7472# compute the style of an individual element
7573method ! base-style (Str : D $ tag , Str : style-attr($ style ), Str : $ path ! ) {
@@ -80,8 +78,7 @@ method !base-style(Str:D $tag, Str :style-attr($style), Str :$path!) {
8078 } // CSS::Properties. new : : $ ! module ;
8179
8280 # Apply CSS Selector styles. Lower precedence than inline rules
83- my CSS::Properties: D @ prop-sets = . sort (*. specificity)». properties
84- with % ! rulesets {$ path };
81+ my CSS::Properties: D @ prop-sets = . List with % ! propsets {$ path };
8582
8683 CSS::Stylesheet::merge-properties(@ prop-sets , $ props );
8784
@@ -106,7 +103,7 @@ method !add-tag-styling(Str:D $tag, CSS::Properties $style, :%attrs) {
106103
107104# styling, including any tag-specific styling
108105multi method style (LibXML::Element: D $ elem ) {
109- my $ path = $ elem . nodePath;
106+ my Str : D $ path = $ elem . nodePath;
110107
111108 % ! style {$ path } // = do {
112109 fail " document does not contain this element"
@@ -129,7 +126,12 @@ multi method style(LibXML::Element:D $elem) {
129126}
130127
131128multi method style (Str : D $ xpath ) {
132- self . style: $ ! doc . first ($ xpath );
129+ do with $ ! doc . first ($ xpath ) {
130+ self . style: $ _ ;
131+ }
132+ else {
133+ CSS::Properties;
134+ }
133135}
134136
135137method prune ($ node = $ ! doc . root) {
0 commit comments