1
//! Representation of CSS types, and the CSS parsing and matching engine.
2
//!
3
//! # Terminology
4
//!
5
//! Consider a CSS **stylesheet** like this:
6
//!
7
//! ```css
8
//! @import url("another.css");
9
//!
10
//! foo, .bar {
11
//!         fill: red;
12
//!         stroke: green;
13
//! }
14
//!
15
//! #baz { stroke-width: 42; }
16
//! ```
17
//! The example contains three **rules**, the first one is an **at-rule*,
18
//! the other two are **qualified rules**.
19
//!
20
//! Each rule is made of two parts, a **prelude** and an optional **block**
21
//! The prelude is the part until the first `{` or until `;`, depending on
22
//! whether a block is present.  The block is the part between curly braces.
23
//!
24
//! Let's look at each rule:
25
//!
26
//! `@import` is an **at-rule**.  This rule has a prelude, but no block.
27
//! There are other at-rules like `@media` and some of them may have a block,
28
//! but librsvg doesn't support those yet.
29
//!
30
//! The prelude of the following rule is `foo, .bar`.
31
//! It is a **selector list** with two **selectors**, one for
32
//! `foo` elements and one for elements that have the `bar` class.
33
//!
34
//! The content of the block between `{}` for a qualified rule is a
35
//! **declaration list**.  The block of the first qualified rule contains two
36
//! **declarations**, one for the `fill` **property** and one for the
37
//! `stroke` property.
38
//!
39
//! After the first qualified rule, we have a second qualified rule with
40
//! a single selector for the `#baz` id, with a single declaration for the
41
//! `stroke-width` property.
42
//!
43
//! # Helper crates we use
44
//!
45
//! * `cssparser` crate as a CSS tokenizer, and some utilities to
46
//!   parse CSS rules and declarations.
47
//!
48
//! * `selectors` crate for the representation of selectors and
49
//!   selector lists, and for the matching engine.
50
//!
51
//! Both crates provide very generic implementations of their concepts,
52
//! and expect the caller to provide implementations of various traits,
53
//! and to provide types that represent certain things.
54
//!
55
//! For example, `cssparser` expects one to provide representations of
56
//! the following types:
57
//!
58
//! * A parsed CSS rule.  For `fill: blue;` we have
59
//!   `ParsedProperty::Fill(...)`.
60
//!
61
//! * A parsed selector list; we use `SelectorList` from the
62
//!   `selectors` crate.
63
//!
64
//! In turn, the `selectors` crate needs a way to navigate and examine
65
//! one's implementation of an element tree.  We provide `impl
66
//! selectors::Element for RsvgElement` for this.  This implementation
67
//! has methods like "does this element have the id `#foo`", or "give
68
//! me the next sibling element".
69
//!
70
//! Finally, the matching engine ties all of this together with
71
//! `matches_selector()`.  This takes an opaque representation of an
72
//! element, plus a selector, and returns a bool.  We iterate through
73
//! the rules in the stylesheets and gather the matches; then sort the
74
//! matches by specificity and apply the result to each element.
75

            
76
use cssparser::{
77
    self, match_ignore_ascii_case, parse_important, AtRuleParser, BasicParseErrorKind, CowRcStr,
78
    DeclarationParser, Parser, ParserInput, ParserState, QualifiedRuleParser, RuleBodyItemParser,
79
    RuleBodyParser, SourceLocation, StyleSheetParser, ToCss,
80
};
81
use data_url::mime::Mime;
82
use language_tags::LanguageTag;
83
use markup5ever::{self, namespace_url, ns, Namespace, QualName};
84
use selectors::attr::{AttrSelectorOperation, CaseSensitivity, NamespaceConstraint};
85
use selectors::matching::{
86
    ElementSelectorFlags, IgnoreNthChildForInvalidation, MatchingContext, MatchingMode,
87
    NeedsSelectorFlags, QuirksMode,
88
};
89
use selectors::parser::ParseRelative;
90
use selectors::{NthIndexCache, OpaqueElement, SelectorImpl, SelectorList};
91
use std::cmp::Ordering;
92
use std::fmt;
93
use std::str;
94
use std::str::FromStr;
95

            
96
use crate::element::Element;
97
use crate::error::*;
98
use crate::io::{self, BinaryData};
99
use crate::node::{Node, NodeBorrow, NodeCascade};
100
use crate::properties::{parse_value, ComputedValues, ParseAs, ParsedProperty};
101
use crate::rsvg_log;
102
use crate::session::Session;
103
use crate::url_resolver::{AllowedUrl, UrlResolver};
104

            
105
/// A parsed CSS declaration
106
///
107
/// For example, in the declaration `fill: green !important`, the
108
/// `prop_name` would be `fill`, the `property` would be
109
/// `ParsedProperty::Fill(...)` with the green value, and `important`
110
/// would be `true`.
111
pub struct Declaration {
112
    pub prop_name: QualName,
113
    pub property: ParsedProperty,
114
    pub important: bool,
115
}
116

            
117
/// This enum represents the fact that a rule body can be either a
118
/// declaration or a nested rule.
119
pub enum RuleBodyItem {
120
    Decl(Declaration),
121
    #[allow(dead_code)] // We don't support nested rules yet
122
    Rule(Rule),
123
}
124

            
125
/// Dummy struct required to use `cssparser::DeclarationListParser`
126
///
127
/// It implements `cssparser::DeclarationParser`, which knows how to parse
128
/// the property/value pairs from a CSS declaration.
129
pub struct DeclParser;
130

            
131
impl<'i> DeclarationParser<'i> for DeclParser {
132
    type Declaration = RuleBodyItem;
133
    type Error = ValueErrorKind;
134

            
135
    /// Parses a CSS declaration like `name: input_value [!important]`
136
39733
    fn parse_value<'t>(
137
        &mut self,
138
        name: CowRcStr<'i>,
139
        input: &mut Parser<'i, 't>,
140
    ) -> Result<RuleBodyItem, cssparser::ParseError<'i, Self::Error>> {
141
39733
        let prop_name = QualName::new(None, ns!(), markup5ever::LocalName::from(name.as_ref()));
142
39733
        let property = parse_value(&prop_name, input, ParseAs::Property)?;
143

            
144
34459
        let important = input.try_parse(parse_important).is_ok();
145

            
146
34339
        Ok(RuleBodyItem::Decl(Declaration {
147
34339
            prop_name,
148
34339
            property,
149
            important,
150
        }))
151
39595
    }
152
}
153

            
154
// cssparser's DeclarationListParser requires this; we just use the dummy
155
// implementations from cssparser itself.  We may want to provide a real
156
// implementation in the future, although this may require keeping track of the
157
// CSS parsing state like Servo does.
158
impl<'i> AtRuleParser<'i> for DeclParser {
159
    type Prelude = ();
160
    type AtRule = RuleBodyItem;
161
    type Error = ValueErrorKind;
162
}
163

            
164
/// We need this dummy implementation as well.
165
impl<'i> QualifiedRuleParser<'i> for DeclParser {
166
    type Prelude = ();
167
    type QualifiedRule = RuleBodyItem;
168
    type Error = ValueErrorKind;
169
}
170

            
171
impl<'i> RuleBodyItemParser<'i, RuleBodyItem, ValueErrorKind> for DeclParser {
172
    /// We want to parse declarations.
173
39593
    fn parse_declarations(&self) -> bool {
174
        true
175
39593
    }
176

            
177
    /// We don't wanto parse qualified rules though.
178
5252
    fn parse_qualified(&self) -> bool {
179
        false
180
5252
    }
181
}
182

            
183
/// Struct to implement cssparser::QualifiedRuleParser and cssparser::AtRuleParser
184
pub struct RuleParser {
185
    session: Session,
186
}
187

            
188
/// Errors from the CSS parsing process
189
#[allow(dead_code)] // looks like we are not actually using this yet?
190
#[derive(Debug)]
191
pub enum ParseErrorKind<'i> {
192
    Selector(selectors::parser::SelectorParseErrorKind<'i>),
193
}
194

            
195
impl<'i> From<selectors::parser::SelectorParseErrorKind<'i>> for ParseErrorKind<'i> {
196
    fn from(e: selectors::parser::SelectorParseErrorKind<'_>) -> ParseErrorKind<'_> {
197
        ParseErrorKind::Selector(e)
198
    }
199
}
200

            
201
/// A CSS qualified rule (or ruleset)
202
pub struct QualifiedRule {
203
    selectors: SelectorList<Selector>,
204
    declarations: Vec<Declaration>,
205
}
206

            
207
/// Prelude of at-rule used in the AtRuleParser.
208
pub enum AtRulePrelude {
209
    Import(String),
210
}
211

            
212
/// A CSS at-rule (or ruleset)
213
pub enum AtRule {
214
    Import(String),
215
}
216

            
217
/// A CSS rule (or ruleset)
218
pub enum Rule {
219
    AtRule(AtRule),
220
    QualifiedRule(QualifiedRule),
221
}
222

            
223
// Required to implement the `Prelude` associated type in `cssparser::QualifiedRuleParser`
224
impl<'i> selectors::Parser<'i> for RuleParser {
225
    type Impl = Selector;
226
    type Error = ParseErrorKind<'i>;
227

            
228
2693
    fn default_namespace(&self) -> Option<<Self::Impl as SelectorImpl>::NamespaceUrl> {
229
2693
        Some(ns!(svg))
230
2693
    }
231

            
232
    fn namespace_for_prefix(
233
        &self,
234
        _prefix: &<Self::Impl as SelectorImpl>::NamespacePrefix,
235
    ) -> Option<<Self::Impl as SelectorImpl>::NamespaceUrl> {
236
        // FIXME: Do we need to keep a lookup table extracted from libxml2's
237
        // XML namespaces?
238
        //
239
        // Or are CSS namespaces completely different, declared elsewhere?
240
        None
241
    }
242
2
    fn parse_non_ts_pseudo_class(
243
        &self,
244
        location: SourceLocation,
245
        name: CowRcStr<'i>,
246
    ) -> Result<NonTSPseudoClass, cssparser::ParseError<'i, Self::Error>> {
247
2
        match &*name {
248
2
            "link" => Ok(NonTSPseudoClass::Link),
249
            "visited" => Ok(NonTSPseudoClass::Visited),
250
            _ => Err(location.new_custom_error(
251
                selectors::parser::SelectorParseErrorKind::UnsupportedPseudoClassOrElement(name),
252
            )),
253
        }
254
2
    }
255
6
    fn parse_non_ts_functional_pseudo_class(
256
        &self,
257
        name: CowRcStr<'i>,
258
        arguments: &mut Parser<'i, '_>,
259
    ) -> Result<NonTSPseudoClass, cssparser::ParseError<'i, Self::Error>> {
260
6
        match &*name {
261
6
            "lang" => {
262
                // Comma-separated lists of languages are a Selectors 4 feature,
263
                // but a pretty stable one that hasn't changed in a long time.
264
13
                let tags = arguments.parse_comma_separated(|arg| {
265
7
                    let language_tag = arg.expect_ident_or_string()?.clone();
266
7
                    LanguageTag::from_str(&language_tag).map_err(|_| {
267
                        arg.new_custom_error(selectors::parser::SelectorParseErrorKind::UnsupportedPseudoClassOrElement(language_tag))
268
                    })
269
7
                })?;
270
12
                arguments.expect_exhausted()?;
271
6
                Ok(NonTSPseudoClass::Lang(tags))
272
6
            }
273
            _ => Err(arguments.new_custom_error(
274
                selectors::parser::SelectorParseErrorKind::UnsupportedPseudoClassOrElement(name),
275
            )),
276
        }
277
6
    }
278
}
279

            
280
// `cssparser::StyleSheetParser` is a struct which requires that we provide a type that
281
// implements `cssparser::QualifiedRuleParser` and `cssparser::AtRuleParser`.
282
//
283
// In turn, `cssparser::QualifiedRuleParser` requires that we
284
// implement a way to parse the `Prelude` of a ruleset or rule.  For
285
// example, in this ruleset:
286
//
287
// ```css
288
// foo, .bar { fill: red; stroke: green; }
289
// ```
290
//
291
// The prelude is the selector list with the `foo` and `.bar` selectors.
292
//
293
// The `parse_prelude` method just uses `selectors::SelectorList`.  This
294
// is what requires the `impl selectors::Parser for RuleParser`.
295
//
296
// Next, the `parse_block` method takes an already-parsed prelude (a selector list),
297
// and tries to parse the block between braces.  It creates a `Rule` out of
298
// the selector list and the declaration list.
299
impl<'i> QualifiedRuleParser<'i> for RuleParser {
300
    type Prelude = SelectorList<Selector>;
301
    type QualifiedRule = Rule;
302
    type Error = ValueErrorKind;
303

            
304
494
    fn parse_prelude<'t>(
305
        &mut self,
306
        input: &mut Parser<'i, 't>,
307
    ) -> Result<Self::Prelude, cssparser::ParseError<'i, Self::Error>> {
308
494
        SelectorList::parse(self, input, ParseRelative::No).map_err(|e| ParseError {
309
            kind: cssparser::ParseErrorKind::Custom(ValueErrorKind::parse_error(
310
                "Could not parse selector",
311
            )),
312
            location: e.location,
313
        })
314
494
    }
315

            
316
494
    fn parse_block<'t>(
317
        &mut self,
318
        prelude: Self::Prelude,
319
        _start: &ParserState,
320
        input: &mut Parser<'i, 't>,
321
    ) -> Result<Self::QualifiedRule, cssparser::ParseError<'i, Self::Error>> {
322
494
        let declarations = RuleBodyParser::<_, _, Self::Error>::new(input, &mut DeclParser)
323
546
            .filter_map(|r| match r {
324
545
                Ok(RuleBodyItem::Decl(decl)) => Some(decl),
325
                Ok(RuleBodyItem::Rule(_)) => None,
326
1
                Err(e) => {
327
1
                    rsvg_log!(self.session, "Invalid declaration; ignoring: {:?}", e);
328
1
                    None
329
1
                }
330
546
            })
331
            .collect();
332

            
333
494
        Ok(Rule::QualifiedRule(QualifiedRule {
334
494
            selectors: prelude,
335
            declarations,
336
        }))
337
494
    }
338
}
339

            
340
// Required by `cssparser::StyleSheetParser`.
341
//
342
// This only handles the `@import` at-rule.
343
impl<'i> AtRuleParser<'i> for RuleParser {
344
    type Prelude = AtRulePrelude;
345
    type AtRule = Rule;
346
    type Error = ValueErrorKind;
347

            
348
    #[allow(clippy::type_complexity)]
349
4
    fn parse_prelude<'t>(
350
        &mut self,
351
        name: CowRcStr<'i>,
352
        input: &mut Parser<'i, 't>,
353
    ) -> Result<Self::Prelude, cssparser::ParseError<'i, Self::Error>> {
354
4
        match_ignore_ascii_case! {
355
            &name,
356

            
357
            // FIXME: at the moment we ignore media queries
358

            
359
            "import" => {
360
                let url = input.expect_url_or_string()?.as_ref().to_owned();
361
2
                Ok(AtRulePrelude::Import(url))
362
            },
363

            
364
            _ => Err(input.new_error(BasicParseErrorKind::AtRuleInvalid(name))),
365
        }
366
4
    }
367

            
368
2
    fn rule_without_block(
369
        &mut self,
370
        prelude: Self::Prelude,
371
        _start: &ParserState,
372
    ) -> Result<Self::AtRule, ()> {
373
2
        let AtRulePrelude::Import(url) = prelude;
374
2
        Ok(Rule::AtRule(AtRule::Import(url)))
375
2
    }
376

            
377
    // When we implement at-rules with blocks, implement the trait's parse_block() method here.
378
}
379

            
380
/// Dummy type required by the SelectorImpl trait.
381
#[allow(clippy::upper_case_acronyms)]
382
#[derive(Clone, Debug, Eq, PartialEq)]
383
pub enum NonTSPseudoClass {
384
    Link,
385
    Visited,
386
    Lang(Vec<LanguageTag>),
387
}
388

            
389
impl ToCss for NonTSPseudoClass {
390
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
391
    where
392
        W: fmt::Write,
393
    {
394
        match self {
395
            NonTSPseudoClass::Link => write!(dest, "link"),
396
            NonTSPseudoClass::Visited => write!(dest, "visited"),
397
            NonTSPseudoClass::Lang(lang) => write!(
398
                dest,
399
                "lang(\"{}\")",
400
                lang.iter()
401
                    .map(ToString::to_string)
402
                    .collect::<Vec<_>>()
403
                    .join("\",\"")
404
            ),
405
        }
406
    }
407
}
408

            
409
impl selectors::parser::NonTSPseudoClass for NonTSPseudoClass {
410
    type Impl = Selector;
411

            
412
    fn is_active_or_hover(&self) -> bool {
413
        false
414
    }
415

            
416
    fn is_user_action_state(&self) -> bool {
417
        false
418
    }
419
}
420

            
421
/// Dummy type required by the SelectorImpl trait
422
#[derive(Clone, Debug, Eq, PartialEq)]
423
pub struct PseudoElement;
424

            
425
impl ToCss for PseudoElement {
426
    fn to_css<W>(&self, _dest: &mut W) -> fmt::Result
427
    where
428
        W: fmt::Write,
429
    {
430
        Ok(())
431
    }
432
}
433

            
434
impl selectors::parser::PseudoElement for PseudoElement {
435
    type Impl = Selector;
436
}
437

            
438
/// Holds all the types for the SelectorImpl trait
439
#[derive(Debug, Clone)]
440
pub struct Selector;
441

            
442
/// Wrapper for attribute values.
443
///
444
/// We use a newtype because the associated type Selector::AttrValue
445
/// must implement `From<&str>` and `ToCss`, which are foreign traits.
446
///
447
/// The `derive` requirements come from the `selectors` crate.
448
#[derive(Clone, PartialEq, Eq)]
449
pub struct AttributeValue(String);
450

            
451
impl From<&str> for AttributeValue {
452
13
    fn from(s: &str) -> AttributeValue {
453
13
        AttributeValue(s.to_owned())
454
13
    }
455
}
456

            
457
impl ToCss for AttributeValue {
458
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
459
    where
460
        W: fmt::Write,
461
    {
462
        use std::fmt::Write;
463

            
464
        write!(cssparser::CssStringWriter::new(dest), "{}", &self.0)
465
    }
466
}
467

            
468
impl AsRef<str> for AttributeValue {
469
59
    fn as_ref(&self) -> &str {
470
59
        self.0.as_ref()
471
59
    }
472
}
473

            
474
/// Wrapper for identifier values.
475
///
476
/// Used to implement `ToCss` on the `LocalName` foreign type.
477
#[derive(Clone, PartialEq, Eq)]
478
pub struct Identifier(markup5ever::LocalName);
479

            
480
impl From<&str> for Identifier {
481
110
    fn from(s: &str) -> Identifier {
482
110
        Identifier(markup5ever::LocalName::from(s))
483
110
    }
484
}
485

            
486
impl ToCss for Identifier {
487
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
488
    where
489
        W: fmt::Write,
490
    {
491
        cssparser::serialize_identifier(&self.0, dest)
492
    }
493
}
494

            
495
/// Wrapper for local names.
496
///
497
/// Used to implement `ToCss` on the `LocalName` foreign type.
498
722136
#[derive(Clone, PartialEq, Eq)]
499
361068
pub struct LocalName(markup5ever::LocalName);
500

            
501
impl From<&str> for LocalName {
502
4741
    fn from(s: &str) -> LocalName {
503
4741
        LocalName(markup5ever::LocalName::from(s))
504
4741
    }
505
}
506

            
507
impl ToCss for LocalName {
508
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
509
    where
510
        W: fmt::Write,
511
    {
512
        cssparser::serialize_identifier(&self.0, dest)
513
    }
514
}
515

            
516
/// Wrapper for namespace prefixes.
517
///
518
/// Used to implement `ToCss` on the `markup5ever::Prefix` foreign type.
519
#[derive(Clone, Default, PartialEq, Eq)]
520
pub struct NamespacePrefix(markup5ever::Prefix);
521

            
522
impl From<&str> for NamespacePrefix {
523
    fn from(s: &str) -> NamespacePrefix {
524
        NamespacePrefix(markup5ever::Prefix::from(s))
525
    }
526
}
527

            
528
impl ToCss for NamespacePrefix {
529
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
530
    where
531
        W: fmt::Write,
532
    {
533
        cssparser::serialize_identifier(&self.0, dest)
534
    }
535
}
536

            
537
impl SelectorImpl for Selector {
538
    type ExtraMatchingData<'a> = ();
539
    type AttrValue = AttributeValue;
540
    type Identifier = Identifier;
541
    type LocalName = LocalName;
542
    type NamespaceUrl = Namespace;
543
    type NamespacePrefix = NamespacePrefix;
544
    type BorrowedNamespaceUrl = Namespace;
545
    type BorrowedLocalName = LocalName;
546
    type NonTSPseudoClass = NonTSPseudoClass;
547
    type PseudoElement = PseudoElement;
548
}
549

            
550
/// Newtype wrapper around `Node` so we can implement [`selectors::Element`] for it.
551
///
552
/// `Node` is an alias for [`rctree::Node`], so we can't implement
553
/// `selectors::Element` directly on it.  We implement it on the
554
/// `RsvgElement` wrapper instead.
555
1072
#[derive(Clone, PartialEq)]
556
536
pub struct RsvgElement(Node);
557

            
558
impl From<Node> for RsvgElement {
559
786
    fn from(n: Node) -> RsvgElement {
560
        RsvgElement(n)
561
786
    }
562
}
563

            
564
impl fmt::Debug for RsvgElement {
565
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
566
        write!(f, "{}", self.0.borrow())
567
    }
568
}
569

            
570
// The selectors crate uses this to examine our tree of elements.
571
impl selectors::Element for RsvgElement {
572
    type Impl = Selector;
573

            
574
    /// Converts self into an opaque representation.
575
19
    fn opaque(&self) -> OpaqueElement {
576
        // The `selectors` crate uses this value just for pointer comparisons, to answer
577
        // the question, "is this element the same as that one?".  So, we'll give it a
578
        // reference to our actual node's data, i.e. skip over whatever wrappers there
579
        // are in rctree.
580
        //
581
        // We use an explicit type here to make it clear what the type is; otherwise you
582
        // may be fooled by the fact that borrow_element() returns a Ref<Element>, not a
583
        // plain reference: &Ref<T> is transient and would get dropped at the end of this
584
        // function, but we want something long-lived.
585
19
        let element: &Element = &self.0.borrow_element();
586
19
        OpaqueElement::new::<Element>(element)
587
19
    }
588

            
589
789
    fn parent_element(&self) -> Option<Self> {
590
1457
        self.0.parent().map(|n| n.into())
591
789
    }
592

            
593
    /// Whether the parent node of this element is a shadow root.
594
120
    fn parent_node_is_shadow_root(&self) -> bool {
595
        // unsupported
596
        false
597
120
    }
598

            
599
    /// The host of the containing shadow root, if any.
600
    fn containing_shadow_host(&self) -> Option<Self> {
601
        // unsupported
602
        None
603
    }
604

            
605
    /// Whether we're matching on a pseudo-element.
606
    fn is_pseudo_element(&self) -> bool {
607
        // unsupported
608
        false
609
    }
610

            
611
    /// Skips non-element nodes
612
179
    fn prev_sibling_element(&self) -> Option<Self> {
613
179
        let mut sibling = self.0.previous_sibling();
614

            
615
357
        while let Some(ref sib) = sibling {
616
282
            if sib.is_element() {
617
208
                return sibling.map(|n| n.into());
618
            }
619

            
620
178
            sibling = sib.previous_sibling();
621
        }
622

            
623
75
        None
624
179
    }
625

            
626
    /// Skips non-element nodes
627
23
    fn next_sibling_element(&self) -> Option<Self> {
628
23
        let mut sibling = self.0.next_sibling();
629

            
630
46
        while let Some(ref sib) = sibling {
631
37
            if sib.is_element() {
632
28
                return sibling.map(|n| n.into());
633
            }
634

            
635
23
            sibling = sib.next_sibling();
636
        }
637

            
638
9
        None
639
23
    }
640

            
641
58378
    fn is_html_element_in_html_document(&self) -> bool {
642
        false
643
58378
    }
644

            
645
359135
    fn has_local_name(&self, local_name: &LocalName) -> bool {
646
359135
        self.0.borrow_element().element_name().local == local_name.0
647
358367
    }
648

            
649
    /// Empty string for no namespace
650
456863
    fn has_namespace(&self, ns: &Namespace) -> bool {
651
456863
        self.0.borrow_element().element_name().ns == *ns
652
451703
    }
653

            
654
    /// Whether this element and the `other` element have the same local name and namespace.
655
20
    fn is_same_type(&self, other: &Self) -> bool {
656
20
        self.0.borrow_element().element_name() == other.0.borrow_element().element_name()
657
20
    }
658

            
659
456
    fn attr_matches(
660
        &self,
661
        ns: &NamespaceConstraint<&Namespace>,
662
        local_name: &LocalName,
663
        operation: &AttrSelectorOperation<&AttributeValue>,
664
    ) -> bool {
665
456
        self.0
666
            .borrow_element()
667
            .get_attributes()
668
            .iter()
669
1403
            .find(|(attr, _)| {
670
                // do we have an attribute that matches the namespace and local_name?
671
1403
                match *ns {
672
                    NamespaceConstraint::Any => local_name.0 == attr.local,
673
1403
                    NamespaceConstraint::Specific(ns) => {
674
1403
                        QualName::new(None, ns.clone(), local_name.0.clone()) == *attr
675
1403
                    }
676
                }
677
1403
            })
678
69
            .map(|(_, value)| {
679
                // we have one; does the attribute's value match the expected operation?
680
69
                operation.eval_str(value)
681
69
            })
682
            .unwrap_or(false)
683
456
    }
684

            
685
54
    fn match_non_ts_pseudo_class(
686
        &self,
687
        pc: &<Self::Impl as SelectorImpl>::NonTSPseudoClass,
688
        _context: &mut MatchingContext<'_, Self::Impl>,
689
    ) -> bool
690
where {
691
54
        match pc {
692
3
            NonTSPseudoClass::Link => self.is_link(),
693
            NonTSPseudoClass::Visited => false,
694
51
            NonTSPseudoClass::Lang(css_lang) => self
695
                .0
696
                .borrow_element()
697
                .get_computed_values()
698
                .xml_lang()
699
                .0
700
                .as_ref()
701
35
                .map_or(false, |e_lang| {
702
70
                    css_lang
703
                        .iter()
704
74
                        .any(|l| l.is_language_range() && l.matches(e_lang))
705
86
                }),
706
        }
707
54
    }
708

            
709
    fn match_pseudo_element(
710
        &self,
711
        _pe: &<Self::Impl as SelectorImpl>::PseudoElement,
712
        _context: &mut MatchingContext<'_, Self::Impl>,
713
    ) -> bool {
714
        // unsupported
715
        false
716
    }
717

            
718
    /// Whether this element is a `link`.
719
876
    fn is_link(&self) -> bool {
720
        // Style as link only if href is specified at all.
721
        //
722
        // The SVG and CSS specifications do not seem to clearly
723
        // say what happens when you have an `<svg:a>` tag with no
724
        // `(xlink:|svg:)href` attribute. However, both Firefox and Chromium
725
        // consider a bare `<svg:a>` element with no href to be NOT
726
        // a link, so to avoid nasty surprises, we do the same.
727
        // Empty href's, however, ARE considered links.
728
876
        self.0.is_element()
729
876
            && match *self.0.borrow_element_data() {
730
3
                crate::element::ElementData::Link(ref link) => link.link.is_some(),
731
873
                _ => false,
732
876
            }
733
876
    }
734

            
735
    /// Returns whether the element is an HTML `<slot>` element.
736
    fn is_html_slot_element(&self) -> bool {
737
        false
738
    }
739

            
740
718
    fn has_id(&self, id: &Identifier, case_sensitivity: CaseSensitivity) -> bool {
741
718
        self.0
742
            .borrow_element()
743
            .get_id()
744
412
            .map(|self_id| case_sensitivity.eq(self_id.as_bytes(), id.0.as_bytes()))
745
            .unwrap_or(false)
746
718
    }
747

            
748
925
    fn has_class(&self, name: &Identifier, case_sensitivity: CaseSensitivity) -> bool {
749
925
        self.0
750
            .borrow_element()
751
            .get_class()
752
331
            .map(|classes| {
753
662
                classes
754
                    .split_whitespace()
755
766
                    .any(|class| case_sensitivity.eq(class.as_bytes(), name.0.as_bytes()))
756
331
            })
757
            .unwrap_or(false)
758
925
    }
759

            
760
    fn imported_part(&self, _name: &Identifier) -> Option<Identifier> {
761
        // unsupported
762
        None
763
    }
764

            
765
    fn is_part(&self, _name: &Identifier) -> bool {
766
        // unsupported
767
        false
768
    }
769

            
770
    /// Returns whether this element matches `:empty`.
771
    ///
772
    /// That is, whether it does not contain any child element or any non-zero-length text node.
773
    /// See <http://dev.w3.org/csswg/selectors-3/#empty-pseudo>.
774
4
    fn is_empty(&self) -> bool {
775
        // .all() returns true for the empty iterator
776
4
        self.0
777
            .children()
778
1
            .all(|child| child.is_chars() && child.borrow_chars().is_empty())
779
4
    }
780

            
781
    /// Returns whether this element matches `:root`,
782
    /// i.e. whether it is the root element of a document.
783
    ///
784
    /// Note: this can be false even if `.parent_element()` is `None`
785
    /// if the parent node is a `DocumentFragment`.
786
1301
    fn is_root(&self) -> bool {
787
1301
        self.0.parent().is_none()
788
1301
    }
789

            
790
    /// Returns the first child element of this element.
791
    fn first_element_child(&self) -> Option<Self> {
792
        self.0
793
            .children()
794
            .find(|child| child.is_element())
795
            .map(|n| n.into())
796
    }
797

            
798
    /// Applies the given selector flags to this element.
799
    fn apply_selector_flags(&self, _: ElementSelectorFlags) {
800
        todo!()
801
    }
802
}
803

            
804
/// Origin for a stylesheet, per CSS 2.2.
805
///
806
/// This is used when sorting selector matches according to their origin and specificity.
807
///
808
/// CSS2.2: <https://www.w3.org/TR/CSS22/cascade.html#cascading-order>
809
37916
#[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd)]
810
pub enum Origin {
811
    UserAgent,
812
    User,
813
    Author,
814
}
815

            
816
/// A parsed CSS stylesheet.
817
pub struct Stylesheet {
818
    origin: Origin,
819
    qualified_rules: Vec<QualifiedRule>,
820
}
821

            
822
/// A match during the selector matching process
823
///
824
/// This struct comes from [`Stylesheet::get_matches`], and represents
825
/// that a certain node matched a CSS rule which has a selector with a
826
/// certain `specificity`.  The stylesheet's `origin` is also given here.
827
///
828
/// This type implements [`Ord`] so a list of `Match` can be sorted.
829
/// That implementation does ordering based on origin and specificity
830
/// as per <https://www.w3.org/TR/CSS22/cascade.html#cascading-order>.
831
struct Match<'a> {
832
    specificity: u32,
833
    origin: Origin,
834
    declaration: &'a Declaration,
835
}
836

            
837
impl<'a> Ord for Match<'a> {
838
479
    fn cmp(&self, other: &Self) -> Ordering {
839
479
        match self.origin.cmp(&other.origin) {
840
471
            Ordering::Equal => self.specificity.cmp(&other.specificity),
841
8
            o => o,
842
        }
843
479
    }
844
}
845

            
846
impl<'a> PartialOrd for Match<'a> {
847
479
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
848
479
        Some(self.cmp(other))
849
479
    }
850
}
851

            
852
impl<'a> PartialEq for Match<'a> {
853
    fn eq(&self, other: &Self) -> bool {
854
        self.origin == other.origin && self.specificity == other.specificity
855
    }
856
}
857

            
858
impl<'a> Eq for Match<'a> {}
859

            
860
impl Stylesheet {
861
159
    fn empty(origin: Origin) -> Stylesheet {
862
159
        Stylesheet {
863
            origin,
864
159
            qualified_rules: Vec::new(),
865
        }
866
159
    }
867

            
868
    /// Parses a new stylesheet from CSS data in a string.
869
    ///
870
    /// The `url_resolver_url` is required for `@import` rules, so that librsvg can determine if
871
    /// the requested path is allowed.
872
158
    pub fn from_data(
873
        buf: &str,
874
        url_resolver: &UrlResolver,
875
        origin: Origin,
876
        session: Session,
877
    ) -> Result<Self, LoadingError> {
878
158
        let mut stylesheet = Stylesheet::empty(origin);
879
158
        stylesheet.add_rules_from_string(buf, url_resolver, session)?;
880
158
        Ok(stylesheet)
881
158
    }
882

            
883
    /// Parses a new stylesheet by loading CSS data from a URL.
884
1
    pub fn from_href(
885
        aurl: &AllowedUrl,
886
        origin: Origin,
887
        session: Session,
888
    ) -> Result<Self, LoadingError> {
889
1
        let mut stylesheet = Stylesheet::empty(origin);
890
1
        stylesheet.load(aurl, session)?;
891
1
        Ok(stylesheet)
892
1
    }
893

            
894
    /// Parses the CSS rules in `buf` and appends them to the stylesheet.
895
    ///
896
    /// The `url_resolver_url` is required for `@import` rules, so that librsvg can determine if
897
    /// the requested path is allowed.
898
    ///
899
    /// If there is an `@import` rule, its rules will be recursively added into the
900
    /// stylesheet, in the order in which they appear.
901
161
    fn add_rules_from_string(
902
        &mut self,
903
        buf: &str,
904
        url_resolver: &UrlResolver,
905
        session: Session,
906
    ) -> Result<(), LoadingError> {
907
161
        let mut input = ParserInput::new(buf);
908
161
        let mut parser = Parser::new(&mut input);
909
161
        let mut rule_parser = RuleParser {
910
161
            session: session.clone(),
911
        };
912

            
913
161
        StyleSheetParser::new(&mut parser, &mut rule_parser)
914
498
            .filter_map(|r| match r {
915
496
                Ok(rule) => Some(rule),
916
2
                Err(e) => {
917
2
                    rsvg_log!(session, "Invalid rule; ignoring: {:?}", e);
918
2
                    None
919
2
                }
920
498
            })
921
657
            .for_each(|rule| match rule {
922
2
                Rule::AtRule(AtRule::Import(url)) => match url_resolver.resolve_href(&url) {
923
2
                    Ok(aurl) => {
924
                        // ignore invalid imports
925
2
                        let _ = self.load(&aurl, session.clone());
926
2
                    }
927

            
928
                    Err(e) => {
929
                        rsvg_log!(session, "Not loading stylesheet from \"{}\": {}", url, e);
930
                    }
931
2
                },
932

            
933
494
                Rule::QualifiedRule(qr) => self.qualified_rules.push(qr),
934
496
            });
935

            
936
161
        Ok(())
937
161
    }
938

            
939
    /// Parses a stylesheet referenced by an URL
940
3
    fn load(&mut self, aurl: &AllowedUrl, session: Session) -> Result<(), LoadingError> {
941
3
        io::acquire_data(aurl, None)
942
            .map_err(LoadingError::from)
943
3
            .and_then(|data| {
944
                let BinaryData {
945
3
                    data: bytes,
946
3
                    mime_type,
947
3
                } = data;
948

            
949
3
                if is_text_css(&mime_type) {
950
3
                    Ok(bytes)
951
                } else {
952
                    rsvg_log!(session, "\"{}\" is not of type text/css; ignoring", aurl);
953
                    Err(LoadingError::BadCss)
954
                }
955
3
            })
956
3
            .and_then(|bytes| {
957
3
                String::from_utf8(bytes).map_err(|_| {
958
                    rsvg_log!(
959
                        session,
960
                        "\"{}\" does not contain valid UTF-8 CSS data; ignoring",
961
                        aurl
962
                    );
963
                    LoadingError::BadCss
964
                })
965
3
            })
966
6
            .and_then(|utf8| {
967
3
                let url = (**aurl).clone();
968
3
                self.add_rules_from_string(&utf8, &UrlResolver::new(Some(url)), session)
969
3
            })
970
3
    }
971

            
972
    /// Appends the style declarations that match a specified node to a given vector
973
25459
    fn get_matches<'a>(
974
        &'a self,
975
        node: &Node,
976
        match_ctx: &mut MatchingContext<'_, Selector>,
977
        acc: &mut Vec<Match<'a>>,
978
    ) {
979
103219
        for rule in &self.qualified_rules {
980
529542
            for selector in &rule.selectors.0 {
981
                // This magic call is stolen from selectors::matching::matches_selector_list()
982
451782
                let matches = selectors::matching::matches_selector(
983
                    selector,
984
                    0,
985
451782
                    None,
986
451782
                    &RsvgElement(node.clone()),
987
                    match_ctx,
988
451782
                );
989

            
990
451782
                if matches {
991
7035
                    for decl in rule.declarations.iter() {
992
3621
                        acc.push(Match {
993
                            declaration: decl,
994
3621
                            specificity: selector.specificity(),
995
3621
                            origin: self.origin,
996
                        });
997
                    }
998
                }
999
            }
        }
25459
    }
}
3
fn is_text_css(mime_type: &Mime) -> bool {
3
    mime_type.type_ == "text" && mime_type.subtype == "css"
3
}
/// Runs the CSS cascade on the specified tree from all the stylesheets
50752
pub fn cascade(
    root: &mut Node,
    ua_stylesheets: &[Stylesheet],
    author_stylesheets: &[Stylesheet],
    user_stylesheets: &[Stylesheet],
    session: &Session,
) {
109414
    for mut node in root.descendants().filter(|n| n.is_element()) {
24983
        let mut matches = Vec::new();
        // xml:lang needs to be inherited before selector matching, so it
        // can't be done in the usual SpecifiedValues::to_computed_values,
        // which is called by cascade() and runs after matching.
24983
        let parent = node.parent().clone();
24663
        node.borrow_element_mut().inherit_xml_lang(parent);
25148
        let mut cache = NthIndexCache::default();
24877
        let mut match_ctx = MatchingContext::new(
24877
            MatchingMode::Normal,
            // FIXME: how the fuck does one set up a bloom filter here?
24877
            None,
            &mut cache,
24877
            QuirksMode::NoQuirks,
24877
            NeedsSelectorFlags::No,
24877
            IgnoreNthChildForInvalidation::No,
        );
50084
        for s in ua_stylesheets
            .iter()
            .chain(author_stylesheets)
            .chain(user_stylesheets)
        {
25458
            s.get_matches(&node, &mut match_ctx, &mut matches);
        }
24672
        matches.as_mut_slice().sort();
24665
        let mut element = node.borrow_element_mut();
28329
        for m in matches {
3621
            element.apply_style_declaration(m.declaration, m.origin);
24685
        }
24670
        element.set_style_attribute(session);
25777
    }
1114
    let values = ComputedValues::default();
1114
    root.cascade(&values);
1114
}
#[cfg(test)]
mod tests {
    use super::*;
    use selectors::Element;
    use crate::document::Document;
    use crate::is_element_of_type;
    #[test]
2
    fn xml_lang() {
1
        let document = Document::load_from_bytes(
            br#"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xml:lang="zh">
  <text id="a" x="10" y="10" width="30" height="30"></text>
  <text id="b" x="10" y="20" width="30" height="30" xml:lang="en"></text>
</svg>
"#,
        );
1
        let a = document.lookup_internal_node("a").unwrap();
2
        assert_eq!(
1
            a.borrow_element()
                .get_computed_values()
                .xml_lang()
                .0
                .unwrap()
                .as_str(),
            "zh"
        );
1
        let b = document.lookup_internal_node("b").unwrap();
2
        assert_eq!(
1
            b.borrow_element()
                .get_computed_values()
                .xml_lang()
                .0
                .unwrap()
                .as_str(),
            "en"
        );
2
    }
    #[test]
2
    fn impl_element() {
1
        let document = Document::load_from_bytes(
            br#"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" id="a">
  <rect id="b" x="10" y="10" width="30" height="30"/>
  <circle id="c" cx="10" cy="10" r="10"/>
  <rect id="d" class="foo bar"/>
</svg>
"#,
        );
1
        let a = document.lookup_internal_node("a").unwrap();
1
        let b = document.lookup_internal_node("b").unwrap();
1
        let c = document.lookup_internal_node("c").unwrap();
1
        let d = document.lookup_internal_node("d").unwrap();
        // Node types
1
        assert!(is_element_of_type!(a, Svg));
1
        assert!(is_element_of_type!(b, Rect));
1
        assert!(is_element_of_type!(c, Circle));
1
        assert!(is_element_of_type!(d, Rect));
1
        let a = RsvgElement(a);
1
        let b = RsvgElement(b);
1
        let c = RsvgElement(c);
1
        let d = RsvgElement(d);
        // Tree navigation
2
        assert_eq!(a.parent_element(), None);
2
        assert_eq!(b.parent_element(), Some(a.clone()));
2
        assert_eq!(c.parent_element(), Some(a.clone()));
2
        assert_eq!(d.parent_element(), Some(a.clone()));
2
        assert_eq!(b.next_sibling_element(), Some(c.clone()));
2
        assert_eq!(c.next_sibling_element(), Some(d.clone()));
2
        assert_eq!(d.next_sibling_element(), None);
2
        assert_eq!(b.prev_sibling_element(), None);
2
        assert_eq!(c.prev_sibling_element(), Some(b.clone()));
2
        assert_eq!(d.prev_sibling_element(), Some(c.clone()));
        // Other operations
1
        assert!(a.has_local_name(&LocalName::from("svg")));
1
        assert!(a.has_namespace(&ns!(svg)));
1
        assert!(!a.is_same_type(&b));
1
        assert!(b.is_same_type(&d));
1
        assert!(a.has_id(
1
            &Identifier::from("a"),
1
            CaseSensitivity::AsciiCaseInsensitive
        ));
1
        assert!(!b.has_id(
1
            &Identifier::from("foo"),
1
            CaseSensitivity::AsciiCaseInsensitive
        ));
1
        assert!(d.has_class(
1
            &Identifier::from("foo"),
1
            CaseSensitivity::AsciiCaseInsensitive
        ));
1
        assert!(d.has_class(
1
            &Identifier::from("bar"),
1
            CaseSensitivity::AsciiCaseInsensitive
        ));
1
        assert!(!a.has_class(
1
            &Identifier::from("foo"),
1
            CaseSensitivity::AsciiCaseInsensitive
        ));
1
        assert!(d.is_empty());
1
        assert!(!a.is_empty());
2
    }
}