@@ -17,15 +17,14 @@ use CSS::Module::CSS3;
17
17
18
18
use LibXML::Document;
19
19
use LibXML::Element;
20
- use LibXML::_ParentNode; # document, element or document fragment
21
20
use LibXML::XPath::Context;
22
21
23
22
use URI;
24
-
25
- has LibXML::_ParentNode: D $ . doc is required ;
23
+ has LibXML::Document: D $ . doc is required ;
26
24
has CSS::Stylesheet $ ! stylesheet ;
27
25
method stylesheet handles <Str gist ast page page-properties font-face font-sources font-family base-url > { $ ! stylesheet }
28
26
has Array [CSS::Ruleset] % . rulesets ; # rulesets to node-path mapping
27
+ has Array [CSS::Properties] % . propsets ; # rulesets to node-path mapping
29
28
has CSS::Module: D $ . module = CSS::Module::CSS3. module;
30
29
has CSS::Properties % . style ; # per node-path styling, including tags
31
30
has CSS::TagSet $ . tag-set ;
@@ -43,8 +42,8 @@ method !build(
43
42
if $ ! doc . isa (LibXML::Document);
44
43
45
44
$ ! 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 ;
48
47
49
48
$ ! stylesheet // = $ ! tag-set . stylesheet($ ! doc , : $ media , : $ base-url , : $ imports , : $ links , : $ font-family );
50
49
my LibXML::XPath::Context $ xpath-context .= new : : $ ! doc ;
@@ -56,20 +55,19 @@ method !build(
56
55
% ! rulesets {. nodePath}. push : $ rule ;
57
56
}
58
57
}
58
+
59
+ for % ! rulesets {
60
+ my CSS::Properties @ propsets = . value . sort (*. specificity)». properties;
61
+ % ! propsets {. key } = @ propsets ;
62
+ }
59
63
}
60
64
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) {
67
66
self ! build(| c);
68
67
}
69
68
70
- multi method COERCE (Str : D $ _ ) { self . new : : stylesheet($ _ ); }
71
- multi method COERCE (CSS::Stylesheet: D $ stylesheet ) { self . new : : $ stylesheet ; }
72
69
multi method COERCE (LibXML::_ParentNode: D $ doc ) { self . new : : $ doc }
70
+ multi method COERCE (CSS::Stylesheet: D () $ stylesheet ) { self . new : : $ stylesheet ; }
73
71
74
72
# compute the style of an individual element
75
73
method ! 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!) {
80
78
} // CSS::Properties. new : : $ ! module ;
81
79
82
80
# 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 };
85
82
86
83
CSS::Stylesheet::merge-properties(@ prop-sets , $ props );
87
84
@@ -106,7 +103,7 @@ method !add-tag-styling(Str:D $tag, CSS::Properties $style, :%attrs) {
106
103
107
104
# styling, including any tag-specific styling
108
105
multi method style (LibXML::Element: D $ elem ) {
109
- my $ path = $ elem . nodePath;
106
+ my Str : D $ path = $ elem . nodePath;
110
107
111
108
% ! style {$ path } // = do {
112
109
fail " document does not contain this element"
@@ -129,7 +126,12 @@ multi method style(LibXML::Element:D $elem) {
129
126
}
130
127
131
128
multi 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
+ }
133
135
}
134
136
135
137
method prune ($ node = $ ! doc . root) {
0 commit comments