| 1 | // Copyright 2019 the SimpleCSS Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | |
| 4 | //! Select |
| 5 | |
| 6 | struct XmlNoderust-analyzer cargo simplecss 0.2.2 XmlNode#<'a, 'input: 'a>(roxmltreerust-analyzer cargo roxmltree 0.20.0 crate/::Noderust-analyzer cargo roxmltree 0.20.0 Node#<'a, 'input>); |
| 7 | |
| 8 | impl<'a, 'input: 'a> XmlNoderust-analyzer cargo simplecss 0.2.2 XmlNode#<'a, 'input> { |
| 9 | fn selectrust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'a, 'input>`]select().(&self, text: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<roxmltreerust-analyzer cargo roxmltree 0.20.0 crate/::Noderust-analyzer cargo roxmltree 0.20.0 Node#<'a, 'input>> { |
| 10 | let selectors = simplecssrust-analyzer cargo simplecss 0.2.2 crate/::Selectorrust-analyzer cargo simplecss 0.2.2 selector/Selector#::parserust-analyzer cargo simplecss 0.2.2 selector/impl#[`Selector<'a>`]parse().(text)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 11 | self.0rust-analyzer cargo simplecss 0.2.2 XmlNode#0. |
| 12 | .descendantsrust-analyzer cargo roxmltree 0.20.0 impl#[`Node<'a, 'input>`]descendants().() |
| 13 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|n| n.is_elementrust-analyzer cargo roxmltree 0.20.0 impl#[`Node<'a, 'input>`]is_element().()) |
| 14 | .findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#find().(|&node| selectors.matchesrust-analyzer cargo simplecss 0.2.2 selector/impl#[`Selector<'a>`]matches().(&XmlNoderust-analyzer cargo simplecss 0.2.2 XmlNode#(node))) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | impl simplecssrust-analyzer cargo simplecss 0.2.2 crate/::Elementrust-analyzer cargo simplecss 0.2.2 selector/Element# for XmlNoderust-analyzer cargo simplecss 0.2.2 XmlNode#<'_, '_> { |
| 19 | fn parent_elementrust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'_, '_>`][Element]parent_element().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'_, '_>`][Element]> { |
| 20 | self.0rust-analyzer cargo simplecss 0.2.2 XmlNode#0..parent_elementrust-analyzer cargo roxmltree 0.20.0 impl#[`Node<'a, 'input>`]parent_element().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(XmlNoderust-analyzer cargo simplecss 0.2.2 XmlNode#) |
| 21 | } |
| 22 | |
| 23 | fn prev_sibling_elementrust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'_, '_>`][Element]prev_sibling_element().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'_, '_>`][Element]> { |
| 24 | self.0rust-analyzer cargo simplecss 0.2.2 XmlNode#0. |
| 25 | .prev_siblingsrust-analyzer cargo roxmltree 0.20.0 impl#[`Node<'a, 'input>`]prev_siblings().() |
| 26 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|n| n.is_elementrust-analyzer cargo roxmltree 0.20.0 impl#[`Node<'a, 'input>`]is_element().()) |
| 27 | .nthrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#nth().(0) |
| 28 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(XmlNoderust-analyzer cargo simplecss 0.2.2 XmlNode#) |
| 29 | } |
| 30 | |
| 31 | fn has_local_namerust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'_, '_>`][Element]has_local_name().(&self, local_name: &str) -> bool { |
| 32 | self.0rust-analyzer cargo simplecss 0.2.2 XmlNode#0..tag_namerust-analyzer cargo roxmltree 0.20.0 impl#[`Node<'a, 'input>`]tag_name().().namerust-analyzer cargo roxmltree 0.20.0 impl#[`ExpandedName<'a, 'b>`]name().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().local_name |
| 33 | } |
| 34 | |
| 35 | fn attribute_matchesrust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'_, '_>`][Element]attribute_matches().( |
| 36 | &self, |
| 37 | local_name: &str, |
| 38 | operator: simplecssrust-analyzer cargo simplecss 0.2.2 crate/::AttributeOperatorrust-analyzer cargo simplecss 0.2.2 selector/AttributeOperator#<'_>, |
| 39 | ) -> bool { |
| 40 | match self.0rust-analyzer cargo simplecss 0.2.2 XmlNode#0..attributerust-analyzer cargo roxmltree 0.20.0 impl#[`Node<'a, 'input>`]attribute().(local_name) { |
| 41 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(value) => operator.matchesrust-analyzer cargo simplecss 0.2.2 selector/impl#[`AttributeOperator<'_>`]matches().(value), |
| 42 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => false, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn pseudo_class_matchesrust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'_, '_>`][Element]pseudo_class_matches().(&self, class: simplecssrust-analyzer cargo simplecss 0.2.2 crate/::PseudoClassrust-analyzer cargo simplecss 0.2.2 selector/PseudoClass#<'_>) -> bool { |
| 47 | match class { |
| 48 | simplecssrust-analyzer cargo simplecss 0.2.2 crate/::PseudoClassrust-analyzer cargo simplecss 0.2.2 selector/PseudoClass#::FirstChildrust-analyzer cargo simplecss 0.2.2 selector/PseudoClass#FirstChild# => self.prev_sibling_elementrust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'_, '_>`][Element]prev_sibling_element().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().(), |
| 49 | _ => false, // Since we are querying a static XML we can ignore other pseudo-classes. |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn mainrust-analyzer cargo simplecss 0.2.2 main().() { |
| 55 | let doc = roxmltreerust-analyzer cargo roxmltree 0.20.0 crate/::Documentrust-analyzer cargo roxmltree 0.20.0 Document#::parserust-analyzer cargo roxmltree 0.20.0 parse/impl#[`Document<'input>`]parse().( |
| 56 | "<svg> |
| 57 | <g> |
| 58 | <rect id='rect1' class='round blue'/> |
| 59 | <rect id='rect2' color='red'/> |
| 60 | </g> |
| 61 | </svg>", |
| 62 | ) |
| 63 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 64 | let root = XmlNoderust-analyzer cargo simplecss 0.2.2 XmlNode#(doc.root_elementrust-analyzer cargo roxmltree 0.20.0 impl#[`Document<'input>`]root_element().()); |
| 65 | |
| 66 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 67 | root.selectrust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'a, 'input>`]select().("rect:first-child") |
| 68 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() |
| 69 | .attributerust-analyzer cargo roxmltree 0.20.0 impl#[`Node<'a, 'input>`]attribute().("id") |
| 70 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), |
| 71 | "rect1", |
| 72 | "selected wrong element" |
| 73 | ); |
| 74 | |
| 75 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 76 | root.selectrust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'a, 'input>`]select().("[color=red]").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().attributerust-analyzer cargo roxmltree 0.20.0 impl#[`Node<'a, 'input>`]attribute().("id").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), |
| 77 | "rect2", |
| 78 | "selected wrong element" |
| 79 | ); |
| 80 | |
| 81 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 82 | root.selectrust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'a, 'input>`]select().("svg rect").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().attributerust-analyzer cargo roxmltree 0.20.0 impl#[`Node<'a, 'input>`]attribute().("id").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), |
| 83 | "rect1", |
| 84 | "selected wrong element" |
| 85 | ); |
| 86 | |
| 87 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 88 | root.selectrust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'a, 'input>`]select().("svg > g > rect") |
| 89 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() |
| 90 | .attributerust-analyzer cargo roxmltree 0.20.0 impl#[`Node<'a, 'input>`]attribute().("id") |
| 91 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), |
| 92 | "rect1", |
| 93 | "selected wrong element" |
| 94 | ); |
| 95 | |
| 96 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 97 | root.selectrust-analyzer cargo simplecss 0.2.2 impl#[`XmlNode<'a, 'input>`]select().(".blue").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().attributerust-analyzer cargo roxmltree 0.20.0 impl#[`Node<'a, 'input>`]attribute().("id").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), |
| 98 | "rect1", |
| 99 | "selected wrong element" |
| 100 | ); |
| 101 | } |