├── .gitignore ├── AUTHORS ├── .taplo.toml ├── .clippy.toml ├── .typos.toml ├── .github ├── copyright.sh └── workflows │ └── ci.yml ├── examples ├── parse.rs └── select.rs ├── LICENSE-MIT ├── tests ├── warnings.rs ├── specificity.rs ├── declaration_tokenizer.rs ├── stylesheet.rs ├── select.rs └── selector_tokenizer.rs ├── Cargo.lock ├── CHANGELOG.md ├── Cargo.toml ├── README.md ├── src ├── stream.rs ├── lib.rs └── selector.rs └── LICENSE-APACHE /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | # This is the list of SimpleCSS's significant contributors. 2 | # 3 | # This does not necessarily list everyone who has contributed code, 4 | # especially since many employees of one corporation may be contributing. 5 | # To see the full list of contributors, see the revision history in 6 | # source control. 7 | Yevhenii Reizner 8 | -------------------------------------------------------------------------------- /.taplo.toml: -------------------------------------------------------------------------------- 1 | # See https://taplo.tamasfe.dev/configuration/file.html 2 | # and https://taplo.tamasfe.dev/configuration/formatter-options.html 3 | 4 | [formatting] 5 | # Aligning comments with the largest line creates 6 | # diff noise when neighboring lines are changed. 7 | align_comments = false 8 | 9 | # Matches how rustfmt formats Rust code 10 | column_width = 100 11 | indent_string = " " 12 | -------------------------------------------------------------------------------- /.clippy.toml: -------------------------------------------------------------------------------- 1 | # LINEBENDER LINT SET - .clippy.toml - v1 2 | # See https://linebender.org/wiki/canonical-lints/ 3 | 4 | # The default Clippy value is capped at 8 bytes, which was chosen to improve performance on 32-bit. 5 | # Given that we are building for the future and even low-end mobile phones have 64-bit CPUs, 6 | # it makes sense to optimize for 64-bit and accept the performance hits on 32-bit. 7 | # 16 bytes is the number of bytes that fits into two 64-bit CPU registers. 8 | trivial-copy-size-limit = 16 9 | 10 | # END LINEBENDER LINT SET 11 | -------------------------------------------------------------------------------- /.typos.toml: -------------------------------------------------------------------------------- 1 | # See the configuration reference at 2 | # https://github.com/crate-ci/typos/blob/master/docs/reference.md 3 | 4 | # Corrections take the form of a key/value pair. The key is the incorrect word 5 | # and the value is the correct word. If the key and value are the same, the 6 | # word is treated as always correct. If the value is an empty string, the word 7 | # is treated as always incorrect. 8 | 9 | # Match Identifier - Case Sensitive 10 | [default.extend-identifiers] 11 | 12 | # Match Inside a Word - Case Insensitive 13 | [default.extend-words] 14 | 15 | [files] 16 | # Include .github, .cargo, etc. 17 | ignore-hidden = false 18 | extend-exclude = [ 19 | # /.git isn't in .gitignore, because git never tracks it. 20 | # Typos doesn't know that, though. 21 | "/.git", 22 | ] 23 | -------------------------------------------------------------------------------- /.github/copyright.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # If there are new files with headers that can't match the conditions here, 4 | # then the files can be ignored by an additional glob argument via the -g flag. 5 | # For example: 6 | # -g "!src/special_file.rs" 7 | # -g "!src/special_directory" 8 | 9 | # Check all the standard Rust source files 10 | output=$(rg "^// Copyright (19|20)[\d]{2} (.+ and )?the SimpleCSS Authors( and .+)?$\n^// SPDX-License-Identifier: Apache-2\.0 OR MIT$\n\n" --files-without-match --multiline -g "*.rs" -g "!vello_shaders/{shader,src/cpu}" .) 11 | 12 | if [ -n "$output" ]; then 13 | echo -e "The following files lack the correct copyright header:\n" 14 | echo $output 15 | echo -e "\n\nPlease add the following header:\n" 16 | echo "// Copyright $(date +%Y) the SimpleCSS Authors" 17 | echo "// SPDX-License-Identifier: Apache-2.0 OR MIT" 18 | echo -e "\n... rest of the file ...\n" 19 | exit 1 20 | fi 21 | 22 | echo "All files have correct copyright headers." 23 | exit 0 24 | 25 | -------------------------------------------------------------------------------- /examples/parse.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2019 the SimpleCSS Authors 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT 3 | 4 | //! Parse 5 | 6 | use std::io::{Read, Write}; 7 | 8 | fn main() { 9 | let args: Vec<_> = std::env::args().collect(); 10 | if args.len() != 2 { 11 | println!("Usage:\n\tparse style.css\n\tparse - 'p {{ color:red }}'"); 12 | std::process::exit(1); 13 | } 14 | 15 | std::env::set_var("RUST_LOG", "simplecss=warn"); 16 | env_logger::builder() 17 | .format(|buf, record| writeln!(buf, "{}: {}", record.level(), record.args())) 18 | .init(); 19 | 20 | let text = if args[1] == "-" { 21 | let mut buffer = String::new(); 22 | let stdin = std::io::stdin(); 23 | let mut handle = stdin.lock(); 24 | handle.read_to_string(&mut buffer).unwrap(); 25 | buffer 26 | } else { 27 | std::fs::read_to_string(&args[1]).unwrap() 28 | }; 29 | 30 | let style = simplecss::StyleSheet::parse(&text); 31 | println!("{style:#?}"); 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Reizner Evgeniy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /tests/warnings.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2019 the SimpleCSS Authors 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT 3 | 4 | //! Warnings 5 | 6 | fn run_process(input: &str) -> String { 7 | use std::io::Write; 8 | use std::process::Stdio; 9 | 10 | let mut child = std::process::Command::new("target/debug/examples/parse") 11 | .arg("-") 12 | .stdin(Stdio::piped()) 13 | .stdout(Stdio::null()) 14 | .stderr(Stdio::piped()) 15 | .spawn() 16 | .unwrap(); 17 | 18 | child 19 | .stdin 20 | .as_mut() 21 | .unwrap() 22 | .write_all(input.as_bytes()) 23 | .unwrap(); 24 | 25 | let output = child.wait_with_output().expect("Failed to read stdout"); 26 | String::from_utf8(output.stderr).unwrap() 27 | } 28 | 29 | #[test] 30 | fn style_01() { 31 | assert_eq!( 32 | run_process("> {}"), 33 | "WARN: Selector parsing failed cause unexpected combinator.\n" 34 | ); 35 | } 36 | 37 | #[test] 38 | fn style_02() { 39 | assert_eq!( 40 | run_process("@import 'subs.css';"), 41 | "WARN: The @import rule is not supported. Skipped.\n" 42 | ); 43 | } 44 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "env_filter" 7 | version = "0.1.3" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0" 10 | dependencies = [ 11 | "log", 12 | ] 13 | 14 | [[package]] 15 | name = "env_logger" 16 | version = "0.11.6" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "dcaee3d8e3cfc3fd92428d477bc97fc29ec8716d180c0d74c643bb26166660e0" 19 | dependencies = [ 20 | "env_filter", 21 | "log", 22 | ] 23 | 24 | [[package]] 25 | name = "log" 26 | version = "0.4.22" 27 | source = "registry+https://github.com/rust-lang/crates.io-index" 28 | checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" 29 | 30 | [[package]] 31 | name = "roxmltree" 32 | version = "0.20.0" 33 | source = "registry+https://github.com/rust-lang/crates.io-index" 34 | checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97" 35 | 36 | [[package]] 37 | name = "simplecss" 38 | version = "0.2.2" 39 | dependencies = [ 40 | "env_logger", 41 | "log", 42 | "roxmltree", 43 | ] 44 | -------------------------------------------------------------------------------- /tests/specificity.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2019 the SimpleCSS Authors 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT 3 | 4 | //! Specificity 5 | 6 | use simplecss::*; 7 | 8 | #[test] 9 | fn spec_01() { 10 | let selectors = Selector::parse("*").unwrap(); 11 | assert_eq!(selectors.specificity(), [0, 0, 0]); 12 | } 13 | 14 | #[test] 15 | fn spec_02() { 16 | let selectors = Selector::parse("li").unwrap(); 17 | assert_eq!(selectors.specificity(), [0, 0, 1]); 18 | } 19 | 20 | #[test] 21 | fn spec_03() { 22 | let selectors = Selector::parse("ul li").unwrap(); 23 | assert_eq!(selectors.specificity(), [0, 0, 2]); 24 | } 25 | 26 | #[test] 27 | fn spec_04() { 28 | let selectors = Selector::parse("ul ol + li").unwrap(); 29 | assert_eq!(selectors.specificity(), [0, 0, 3]); 30 | } 31 | 32 | #[test] 33 | fn spec_05() { 34 | let selectors = Selector::parse("h1 + *[rel=up]").unwrap(); 35 | assert_eq!(selectors.specificity(), [0, 1, 1]); 36 | } 37 | 38 | #[test] 39 | fn spec_06() { 40 | let selectors = Selector::parse("ul ol li.red").unwrap(); 41 | assert_eq!(selectors.specificity(), [0, 1, 3]); 42 | } 43 | 44 | #[test] 45 | fn spec_07() { 46 | let selectors = Selector::parse("li.red.level").unwrap(); 47 | assert_eq!(selectors.specificity(), [0, 2, 1]); 48 | } 49 | 50 | #[test] 51 | fn spec_08() { 52 | let selectors = Selector::parse("#x34y").unwrap(); 53 | assert_eq!(selectors.specificity(), [1, 0, 0]); 54 | } 55 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | 10 | 11 | The latest published SimpleCSS release is [0.2.2](#022-2025-01-06) which was released on 2022-01-06. 12 | You can find its changes [documented below](#022-2025-01-06). 13 | 14 | ## [Unreleased] 15 | 16 | This release has an [MSRV][] of 1.65. 17 | 18 | ## [0.2.2][] (2025-01-06) 19 | 20 | This release has an [MSRV][] of 1.65. 21 | 22 | This is the first release under the stewardship of [Linebender][], who is now responsible for maintenance of this crate. 23 | Many thanks to Yevhenii Reizner for the years of hard work that he has poured into this and other crates. 24 | 25 | ### Added 26 | 27 | - Support for `no_std`. ([#17][] by [@waywardmonkeys][]) 28 | 29 | ## [0.2.1][] (2021-07-20) 30 | 31 | - Add rules sorting by specificity. ([#7][] by [@baskerville][]) 32 | 33 | ## [0.2.0][] (2019-08-17) 34 | 35 | - A complete rewrite. 36 | 37 | ## 0.1.0 (2017-01-14) 38 | 39 | - Initial release. 40 | 41 | [MSRV]: README.md#minimum-supported-rust-version-msrv 42 | [Linebender]: https://github.com/linebender 43 | 44 | [#7]: https://github.com/linebender/simplecss/pull/7 45 | [#17]: https://github.com/linebender/simplecss/pull/17 46 | 47 | [@baskerville]: https://github.com/baskerville 48 | [@waywardmonkeys]: https://github.com/waywardmonkeys 49 | 50 | [Unreleased]: https://github.com/RazrFalcon/simplecss/compare/v0.2.2...HEAD 51 | [0.2.2]: https://github.com/RazrFalcon/simplecss/compare/v0.2.1...v0.2.2 52 | [0.2.1]: https://github.com/RazrFalcon/simplecss/compare/v0.2.0...v0.2.1 53 | [0.2.0]: https://github.com/RazrFalcon/simplecss/compare/v0.1.0...v0.2.0 54 | -------------------------------------------------------------------------------- /examples/select.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2019 the SimpleCSS Authors 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT 3 | 4 | //! Select 5 | 6 | struct XmlNode<'a, 'input: 'a>(roxmltree::Node<'a, 'input>); 7 | 8 | impl<'a, 'input: 'a> XmlNode<'a, 'input> { 9 | fn select(&self, text: &str) -> Option> { 10 | let selectors = simplecss::Selector::parse(text)?; 11 | self.0 12 | .descendants() 13 | .filter(|n| n.is_element()) 14 | .find(|&node| selectors.matches(&XmlNode(node))) 15 | } 16 | } 17 | 18 | impl simplecss::Element for XmlNode<'_, '_> { 19 | fn parent_element(&self) -> Option { 20 | self.0.parent_element().map(XmlNode) 21 | } 22 | 23 | fn prev_sibling_element(&self) -> Option { 24 | self.0 25 | .prev_siblings() 26 | .filter(|n| n.is_element()) 27 | .nth(0) 28 | .map(XmlNode) 29 | } 30 | 31 | fn has_local_name(&self, local_name: &str) -> bool { 32 | self.0.tag_name().name() == local_name 33 | } 34 | 35 | fn attribute_matches( 36 | &self, 37 | local_name: &str, 38 | operator: simplecss::AttributeOperator<'_>, 39 | ) -> bool { 40 | match self.0.attribute(local_name) { 41 | Some(value) => operator.matches(value), 42 | None => false, 43 | } 44 | } 45 | 46 | fn pseudo_class_matches(&self, class: simplecss::PseudoClass<'_>) -> bool { 47 | match class { 48 | simplecss::PseudoClass::FirstChild => self.prev_sibling_element().is_none(), 49 | _ => false, // Since we are querying a static XML we can ignore other pseudo-classes. 50 | } 51 | } 52 | } 53 | 54 | fn main() { 55 | let doc = roxmltree::Document::parse( 56 | " 57 | 58 | 59 | 60 | 61 | ", 62 | ) 63 | .unwrap(); 64 | let root = XmlNode(doc.root_element()); 65 | 66 | assert_eq!( 67 | root.select("rect:first-child") 68 | .unwrap() 69 | .attribute("id") 70 | .unwrap(), 71 | "rect1", 72 | "selected wrong element" 73 | ); 74 | 75 | assert_eq!( 76 | root.select("[color=red]").unwrap().attribute("id").unwrap(), 77 | "rect2", 78 | "selected wrong element" 79 | ); 80 | 81 | assert_eq!( 82 | root.select("svg rect").unwrap().attribute("id").unwrap(), 83 | "rect1", 84 | "selected wrong element" 85 | ); 86 | 87 | assert_eq!( 88 | root.select("svg > g > rect") 89 | .unwrap() 90 | .attribute("id") 91 | .unwrap(), 92 | "rect1", 93 | "selected wrong element" 94 | ); 95 | 96 | assert_eq!( 97 | root.select(".blue").unwrap().attribute("id").unwrap(), 98 | "rect1", 99 | "selected wrong element" 100 | ); 101 | } 102 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "simplecss" 3 | version = "0.2.2" 4 | license = "Apache-2.0 OR MIT" 5 | edition = "2021" 6 | description = "A simple CSS 2 parser and selector." 7 | repository = "https://github.com/linebender/simplecss" 8 | keywords = ["css", "parser", "selector"] 9 | categories = ["parser-implementations"] 10 | readme = "README.md" 11 | # Keep in sync with RUST_MIN_VER in .github/workflows/ci.yml and with the relevant README.md files. 12 | # and with the MSRV in the `Unreleased` section of CHANGELOG.md. 13 | rust-version = "1.65" 14 | exclude = [".github", ".clippy.toml", ".gitignore", ".typos.toml"] 15 | 16 | [lints] 17 | rust.unsafe_code = "forbid" 18 | 19 | # LINEBENDER LINT SET - Cargo.toml - v3 20 | # See https://linebender.org/wiki/canonical-lints/ 21 | rust.keyword_idents_2024 = "forbid" 22 | rust.non_ascii_idents = "forbid" 23 | rust.non_local_definitions = "forbid" 24 | rust.unsafe_op_in_unsafe_fn = "forbid" 25 | 26 | rust.elided_lifetimes_in_paths = "warn" 27 | rust.let_underscore_drop = "warn" 28 | rust.missing_debug_implementations = "warn" 29 | rust.missing_docs = "warn" 30 | rust.single_use_lifetimes = "warn" 31 | rust.trivial_numeric_casts = "warn" 32 | rust.unexpected_cfgs = "warn" 33 | rust.unit_bindings = "warn" 34 | rust.unnameable_types = "warn" 35 | rust.unreachable_pub = "warn" 36 | rust.unused_import_braces = "warn" 37 | rust.unused_lifetimes = "warn" 38 | rust.unused_macro_rules = "warn" 39 | rust.unused_qualifications = "warn" 40 | rust.variant_size_differences = "warn" 41 | 42 | clippy.too_many_arguments = "allow" 43 | 44 | clippy.allow_attributes = "warn" 45 | clippy.allow_attributes_without_reason = "warn" 46 | clippy.cast_possible_truncation = "warn" 47 | clippy.collection_is_never_read = "warn" 48 | clippy.dbg_macro = "warn" 49 | clippy.debug_assert_with_mut_call = "warn" 50 | clippy.doc_markdown = "warn" 51 | clippy.exhaustive_enums = "warn" 52 | clippy.fn_to_numeric_cast_any = "warn" 53 | clippy.infinite_loop = "warn" 54 | clippy.large_include_file = "warn" 55 | clippy.large_stack_arrays = "warn" 56 | clippy.match_same_arms = "warn" 57 | clippy.mismatching_type_param_order = "warn" 58 | clippy.missing_assert_message = "warn" 59 | clippy.missing_errors_doc = "warn" 60 | clippy.missing_fields_in_debug = "warn" 61 | clippy.missing_panics_doc = "warn" 62 | clippy.partial_pub_fields = "warn" 63 | clippy.return_self_not_must_use = "warn" 64 | clippy.same_functions_in_if_condition = "warn" 65 | clippy.semicolon_if_nothing_returned = "warn" 66 | clippy.shadow_unrelated = "warn" 67 | clippy.should_panic_without_expect = "warn" 68 | clippy.todo = "warn" 69 | clippy.unseparated_literal_suffix = "warn" 70 | clippy.use_self = "warn" 71 | clippy.wildcard_imports = "warn" 72 | 73 | clippy.cargo_common_metadata = "warn" 74 | clippy.negative_feature_names = "warn" 75 | clippy.redundant_feature_names = "warn" 76 | clippy.wildcard_dependencies = "warn" 77 | # END LINEBENDER LINT SET 78 | 79 | [features] 80 | default = ["std"] 81 | std = ["log/std"] 82 | 83 | [dependencies] 84 | log = { version = "0.4.22", default-features = false } 85 | 86 | [dev-dependencies] 87 | env_logger = { version = "0.11.6", default-features = false } 88 | roxmltree = "0.20.0" 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | # SimpleCSS 4 | 5 | **A simple [CSS 2.1](https://www.w3.org/TR/CSS21/) parser and selector.** 6 | 7 | [![Linebender Zulip, #resvg channel](https://img.shields.io/badge/Linebender-%23resvg-blue?logo=Zulip)](https://xi.zulipchat.com/#narrow/channel/465085-resvg) 8 | [![dependency status](https://deps.rs/repo/github/linebender/simplecss/status.svg)](https://deps.rs/repo/github/linebender/simplecss) 9 | [![Apache 2.0 or MIT license.](https://img.shields.io/badge/license-Apache--2.0_OR_MIT-blue.svg)](#license) 10 | [![Build status](https://github.com/linebender/simplecss/workflows/CI/badge.svg)](https://github.com/linebender/simplecss/actions) 11 | [![Crates.io](https://img.shields.io/crates/v/simplecss.svg)](https://crates.io/crates/simplecss) 12 | [![Docs](https://docs.rs/simplecss/badge.svg)](https://docs.rs/simplecss) 13 | ![](https://img.shields.io/badge/unsafe-forbidden-brightgreen.svg) 14 | 15 |
16 | 17 | This is not a browser-grade CSS parser. 18 | If you need one, use [cssparser](https://crates.io/crates/cssparser) + [selectors](https://crates.io/crates/selectors). 19 | 20 | Since it's very simple we will start with limitations: 21 | 22 | ## Limitations 23 | 24 | - [Most at-rules](https://www.w3.org/TR/CSS21/syndata.html#at-rules) are not supported. 25 | They will be skipped during parsing. The only supported at-rule is `@font-face`. 26 | - Property values are not parsed. 27 | In CSS like `* { width: 5px }` you will get a `width` property with a `5px` value as a string. 28 | - CDO/CDC comments are not supported. 29 | - Parser is case sensitive. 30 | All keywords must be lowercase. 31 | - Unicode escape, like `\26`, is not supported. 32 | 33 | ## Features 34 | 35 | - Selector matching support. 36 | - The rules are sorted by specificity. 37 | - `@font-face` parsing support. 38 | - `!important` parsing support. 39 | - Has a high-level parsers and low-level, zero-allocation tokenizers. 40 | - No unsafe. 41 | 42 | ## Minimum supported Rust Version (MSRV) 43 | 44 | This version of SimpleCSS has been verified to compile with **Rust 1.65** and later. 45 | 46 | Future versions of SimpleCSS might increase the Rust version requirement. 47 | It will not be treated as a breaking change and as such can even happen with small patch releases. 48 | 49 |
50 | Click here if compiling fails. 51 | 52 | As time has passed, some of SimpleCSS's dependencies could have released versions with a higher Rust requirement. 53 | If you encounter a compilation issue due to a dependency and don't want to upgrade your Rust toolchain, then you could downgrade the dependency. 54 | 55 | ```sh 56 | # Use the problematic dependency's name and version 57 | cargo update -p package_name --precise 0.1.1 58 | ``` 59 |
60 | 61 | ## Community 62 | 63 | [![Linebender Zulip, #resvg channel](https://img.shields.io/badge/Linebender-%23resvg-blue?logo=Zulip)](https://xi.zulipchat.com/#narrow/channel/465085-resvg) 64 | 65 | Discussion of SimpleCSS development happens in the Linebender Zulip at , specifically the [#resvg channel](https://xi.zulipchat.com/#narrow/channel/465085-resvg). 66 | All public content can be read without logging in. 67 | 68 | ## License 69 | 70 | Licensed under either of 71 | 72 | - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or ) 73 | - MIT license ([LICENSE-MIT](LICENSE-MIT) or ) 74 | 75 | at your option. 76 | 77 | ## Contribution 78 | 79 | Contributions are welcome by pull request. The [Rust code of conduct] applies. 80 | Please feel free to add your name to the [AUTHORS] file in any substantive pull request. 81 | 82 | Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions. 83 | 84 | [Rust Code of Conduct]: https://www.rust-lang.org/policies/code-of-conduct 85 | [AUTHORS]: ./AUTHORS 86 | -------------------------------------------------------------------------------- /tests/declaration_tokenizer.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2019 the SimpleCSS Authors 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT 3 | 4 | //! Declaration Tokenizer 5 | 6 | use simplecss::*; 7 | 8 | macro_rules! tokenize { 9 | ($name:ident, $text:expr, $( $token:expr ),*) => ( 10 | #[test] 11 | fn $name() { 12 | let mut t = DeclarationTokenizer::from($text); 13 | $( 14 | assert_eq!(t.next().unwrap(), $token); 15 | )* 16 | 17 | assert!(t.next().is_none()); 18 | } 19 | ) 20 | } 21 | 22 | fn declare<'a>(name: &'a str, value: &'a str) -> Declaration<'a> { 23 | Declaration { 24 | name, 25 | value, 26 | important: false, 27 | } 28 | } 29 | 30 | fn declare_important<'a>(name: &'a str, value: &'a str) -> Declaration<'a> { 31 | Declaration { 32 | name, 33 | value, 34 | important: true, 35 | } 36 | } 37 | 38 | tokenize!(tokenize_01, "",); 39 | 40 | tokenize!(tokenize_02, " ",); 41 | 42 | tokenize!(tokenize_03, "/**/",); 43 | 44 | tokenize!(tokenize_04, "color:red", declare("color", "red")); 45 | 46 | tokenize!(tokenize_05, "color:red;", declare("color", "red")); 47 | 48 | tokenize!(tokenize_06, "color:red ", declare("color", "red")); 49 | 50 | tokenize!(tokenize_07, " color: red; ", declare("color", "red")); 51 | 52 | tokenize!(tokenize_08, " color : red ; ", declare("color", "red")); 53 | 54 | tokenize!( 55 | tokenize_09, 56 | " color:red;;;;color:red; ", 57 | declare("color", "red"), 58 | declare("color", "red") 59 | ); 60 | 61 | tokenize!( 62 | tokenize_10, 63 | "background: url(\"img.png\");", 64 | declare("background", "url(\"img.png\")") 65 | ); 66 | 67 | tokenize!( 68 | tokenize_11, 69 | "background: url(\"{}\");", 70 | declare("background", "url(\"{}\")") 71 | ); 72 | 73 | tokenize!( 74 | tokenize_12, 75 | "color: red ! important", 76 | declare_important("color", "red") 77 | ); 78 | 79 | tokenize!( 80 | tokenize_13, 81 | "color: red !important", 82 | declare_important("color", "red") 83 | ); 84 | 85 | tokenize!( 86 | tokenize_14, 87 | "color: red!important", 88 | declare_important("color", "red") 89 | ); 90 | 91 | tokenize!( 92 | tokenize_15, 93 | "color: red !/**/important", 94 | declare_important("color", "red") 95 | ); 96 | 97 | tokenize!( 98 | tokenize_16, 99 | "border: 1em solid blue", 100 | declare("border", "1em solid blue") 101 | ); 102 | 103 | tokenize!( 104 | tokenize_17, 105 | "background: navy url(support/diamond.png) -2em -2em no-repeat", 106 | declare( 107 | "background", 108 | "navy url(support/diamond.png) -2em -2em no-repeat" 109 | ) 110 | ); 111 | 112 | tokenize!(tokenize_18, "/**/color:red", declare("color", "red")); 113 | 114 | tokenize!(tokenize_19, "/* *\\/*/color: red;", declare("color", "red")); 115 | 116 | tokenize!( 117 | tokenize_20, 118 | "/**/color/**/:/**/red/**/;/**/", 119 | declare("color", "red") 120 | ); 121 | 122 | tokenize!(tokenize_21, "\ncolor\n:\nred\n;\n", declare("color", "red")); 123 | 124 | tokenize!(tokenize_22, "{color:red}",); 125 | 126 | tokenize!(tokenize_23, "(color:red)",); 127 | 128 | tokenize!(tokenize_24, "[color:red]",); 129 | 130 | tokenize!(tokenize_25, "color:",); 131 | 132 | tokenize!(tokenize_26, "value:\"text\"", declare("value", "\"text\"")); 133 | 134 | tokenize!(tokenize_27, "value:'text'", declare("value", "'text'")); 135 | 136 | tokenize!(tokenize_28, "color:#fff", declare("color", "#fff")); 137 | tokenize!(tokenize_29, "color:0.5", declare("color", "0.5")); 138 | 139 | tokenize!(tokenize_30, "color:.5", declare("color", ".5")); 140 | 141 | tokenize!(tokenize_31, "color:#FFF", declare("color", "#FFF")); 142 | 143 | tokenize!( 144 | tokenize_32, 145 | "content: counter(chapno, upper-roman) \". \"", 146 | declare("content", "counter(chapno, upper-roman) \". \"") 147 | ); 148 | 149 | tokenize!( 150 | tokenize_33, 151 | "font-family:'Noto Serif','DejaVu Serif',serif", 152 | declare("font-family", "'Noto Serif','DejaVu Serif',serif") 153 | ); 154 | 155 | tokenize!(tokenize_34, "*zoom:1;", declare("zoom", "1")); 156 | 157 | //tokenize!(tokenize_, "@unsupported { splines: reticulating } color: green", 158 | // declare("color", "green") 159 | //); 160 | 161 | //tokenize!(tokenize_, "/*\\*/*/color: red;", declare("color", "red")); 162 | 163 | //tokenize!(tokenize_, "\"this is a string]}\"\"[{\\\"'\"; /*should be parsed as a string but be ignored*/ 164 | // {{}}[]''; /*should be parsed as nested blocks and a string but be ignored*/ 165 | // color: red;", declare("color", "red")); 166 | -------------------------------------------------------------------------------- /tests/stylesheet.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2019 the SimpleCSS Authors 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT 3 | 4 | //! Stylesheet 5 | 6 | use simplecss::*; 7 | 8 | #[test] 9 | fn style_01() { 10 | let style = StyleSheet::parse(""); 11 | assert_eq!(style.to_string(), ""); 12 | } 13 | 14 | #[test] 15 | fn style_02() { 16 | let style = StyleSheet::parse("a {}"); 17 | assert_eq!(style.to_string(), ""); 18 | } 19 | 20 | #[test] 21 | fn style_03() { 22 | let style = StyleSheet::parse("a { color:red }"); 23 | assert_eq!(style.to_string(), "a { color:red; }"); 24 | } 25 | 26 | #[test] 27 | fn style_04() { 28 | let style = StyleSheet::parse("/**/"); 29 | assert_eq!(style.to_string(), ""); 30 | } 31 | 32 | #[test] 33 | fn style_05() { 34 | let style = StyleSheet::parse("a { color:red } /**/"); 35 | assert_eq!(style.to_string(), "a { color:red; }"); 36 | } 37 | 38 | #[test] 39 | fn style_06() { 40 | let style = StyleSheet::parse("a, b { color:red }"); 41 | assert_eq!(style.to_string(), "a { color:red; }\nb { color:red; }"); 42 | } 43 | 44 | #[test] 45 | fn style_07() { 46 | let style = StyleSheet::parse("a, { color:red }"); 47 | assert_eq!(style.to_string(), "a { color:red; }"); 48 | } 49 | 50 | #[test] 51 | fn style_08() { 52 | let style = StyleSheet::parse("a,, { color:red }"); 53 | assert_eq!(style.to_string(), "a { color:red; }"); 54 | } 55 | 56 | #[test] 57 | fn style_09() { 58 | let style = StyleSheet::parse("a,,b { color:red }"); 59 | assert_eq!(style.to_string(), "a { color:red; }\nb { color:red; }"); 60 | } 61 | 62 | #[test] 63 | fn style_10() { 64 | let style = StyleSheet::parse(",a { color:red }"); 65 | assert_eq!(style.to_string(), "a { color:red; }"); 66 | } 67 | 68 | #[test] 69 | fn style_11() { 70 | let style = StyleSheet::parse("@import \"subs.css\";\na { color:red }"); 71 | assert_eq!(style.to_string(), "a { color:red; }"); 72 | } 73 | 74 | #[test] 75 | fn style_12() { 76 | let style = StyleSheet::parse( 77 | "\ 78 | @media screen { 79 | p:before { content: 'Hello'; } 80 | } 81 | a { color:red }", 82 | ); 83 | assert_eq!(style.to_string(), "a { color:red; }"); 84 | } 85 | 86 | #[test] 87 | fn style_13() { 88 | let style = StyleSheet::parse("a > { color:red }"); 89 | assert_eq!(style.to_string(), ""); 90 | } 91 | 92 | #[test] 93 | fn style_14() { 94 | let style = StyleSheet::parse("p { color:green; color }"); 95 | assert_eq!(style.to_string(), "p { color:green; }"); 96 | } 97 | 98 | #[test] 99 | fn style_15() { 100 | let style = StyleSheet::parse("p { color; color:green }"); 101 | assert_eq!(style.to_string(), ""); // TODO: should be 'p { color:green; }' 102 | } 103 | 104 | #[test] 105 | fn style_16() { 106 | let style = StyleSheet::parse("p { color:green; color: }"); 107 | assert_eq!(style.to_string(), "p { color:green; }"); 108 | } 109 | 110 | #[test] 111 | fn style_17() { 112 | let style = StyleSheet::parse("p { color:green; color:; color:red; }"); 113 | assert_eq!(style.to_string(), "p { color:green; }"); 114 | } 115 | 116 | #[test] 117 | fn style_18() { 118 | let style = StyleSheet::parse("p { color:green; color{;color:maroon} }"); 119 | assert_eq!(style.to_string(), "p { color:green; }"); 120 | } 121 | 122 | #[test] 123 | fn style_19() { 124 | let style = StyleSheet::parse("p { color{;color:maroon} color:green; }"); 125 | assert_eq!(style.to_string(), ""); // TODO: should be 'p { color:green; }' 126 | } 127 | 128 | #[test] 129 | fn style_20() { 130 | let style = StyleSheet::parse( 131 | "\ 132 | h1 { color: green } 133 | h2 & h3 { color: red } 134 | h4 { color: black } 135 | ", 136 | ); 137 | assert_eq!( 138 | style.to_string(), 139 | "h1 { color:green; }\nh4 { color:black; }" 140 | ); 141 | } 142 | 143 | #[test] 144 | fn style_21() { 145 | let style = StyleSheet::parse(":le>*"); 146 | assert_eq!(style.to_string(), ""); 147 | } 148 | 149 | #[test] 150 | fn font_face_01() { 151 | let style = StyleSheet::parse( 152 | "@font-face { font-family: 'Noto Serif'; src: url(NotoSerif.woff2) format('woff2'); }", 153 | ); 154 | 155 | assert_eq!(style.rules.len(), 0); 156 | assert_eq!(style.font_faces.len(), 1); 157 | 158 | let ff = &style.font_faces[0]; 159 | assert_eq!( 160 | ff.declarations, 161 | vec![ 162 | Declaration { 163 | name: "font-family", 164 | value: "'Noto Serif'", 165 | important: false, 166 | }, 167 | Declaration { 168 | name: "src", 169 | value: "url(NotoSerif.woff2) format('woff2')", 170 | important: false, 171 | }, 172 | ] 173 | ); 174 | } 175 | 176 | #[test] 177 | fn font_face_02_mixed_with_rules() { 178 | let style = StyleSheet::parse( 179 | "@font-face { font-family: 'MyFont'; src: url(https://foo.com/my.woff2); font-weight: normal; } div { color: red; }", 180 | ); 181 | 182 | assert_eq!(style.rules.len(), 1); 183 | assert_eq!(style.font_faces.len(), 1); 184 | 185 | assert_eq!(style.rules[0].selector.to_string(), "div"); 186 | assert_eq!(style.rules[0].declarations.len(), 1); 187 | assert_eq!(style.rules[0].declarations[0].name, "color"); 188 | assert_eq!(style.rules[0].declarations[0].value, "red"); 189 | 190 | assert_eq!(style.font_faces[0].declarations[0].name, "font-family"); 191 | assert_eq!(style.font_faces[0].declarations[0].value, "'MyFont'"); 192 | assert_eq!(style.font_faces[0].declarations[1].name, "src"); 193 | assert_eq!( 194 | style.font_faces[0].declarations[1].value, 195 | "url(https://foo.com/my.woff2)" 196 | ); 197 | assert_eq!(style.font_faces[0].declarations[2].name, "font-weight"); 198 | assert_eq!(style.font_faces[0].declarations[2].value, "normal"); 199 | } 200 | 201 | #[test] 202 | fn font_face_03_mixed_with_rules() { 203 | let style = StyleSheet::parse( 204 | "@font-palette-values --identifier { font-family: Bixa; override-colors: 0 green, 1 #999; } div { color: red; } @font-face { font-family: 'MyFont'; src: local('Airal'); font-weight: 200 800; }", 205 | ); 206 | 207 | assert_eq!(style.rules.len(), 1); 208 | assert_eq!(style.font_faces.len(), 1); 209 | 210 | assert_eq!(style.rules[0].selector.to_string(), "div"); 211 | assert_eq!(style.rules[0].declarations.len(), 1); 212 | assert_eq!(style.rules[0].declarations[0].name, "color"); 213 | assert_eq!(style.rules[0].declarations[0].value, "red"); 214 | 215 | assert_eq!(style.font_faces[0].declarations[0].name, "font-family"); 216 | assert_eq!(style.font_faces[0].declarations[0].value, "'MyFont'"); 217 | assert_eq!(style.font_faces[0].declarations[1].name, "src"); 218 | assert_eq!(style.font_faces[0].declarations[1].value, "local('Airal')"); 219 | assert_eq!(style.font_faces[0].declarations[2].name, "font-weight"); 220 | assert_eq!(style.font_faces[0].declarations[2].value, "200 800"); 221 | } 222 | -------------------------------------------------------------------------------- /src/stream.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2016 the SimpleCSS Authors 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT 3 | 4 | use core::str; 5 | 6 | use crate::{Error, TextPos}; 7 | 8 | trait CssCharExt { 9 | fn is_name_start(&self) -> bool; 10 | fn is_name_char(&self) -> bool; 11 | fn is_non_ascii(&self) -> bool; 12 | fn is_escape(&self) -> bool; 13 | } 14 | 15 | impl CssCharExt for char { 16 | #[inline] 17 | fn is_name_start(&self) -> bool { 18 | match *self { 19 | '_' | 'a'..='z' | 'A'..='Z' => true, 20 | _ => self.is_non_ascii() || self.is_escape(), 21 | } 22 | } 23 | 24 | #[inline] 25 | fn is_name_char(&self) -> bool { 26 | match *self { 27 | '_' | 'a'..='z' | 'A'..='Z' | '0'..='9' | '-' => true, 28 | _ => self.is_non_ascii() || self.is_escape(), 29 | } 30 | } 31 | 32 | #[inline] 33 | fn is_non_ascii(&self) -> bool { 34 | *self as u32 > 237 35 | } 36 | 37 | #[inline] 38 | fn is_escape(&self) -> bool { 39 | // TODO: this 40 | false 41 | } 42 | } 43 | 44 | #[derive(Clone, Copy, PartialEq, Debug)] 45 | pub(crate) struct Stream<'a> { 46 | text: &'a str, 47 | pos: usize, 48 | end: usize, 49 | } 50 | 51 | impl<'a> From<&'a str> for Stream<'a> { 52 | fn from(text: &'a str) -> Self { 53 | Stream::new(text) 54 | } 55 | } 56 | 57 | impl<'a> Stream<'a> { 58 | pub fn new(text: &'a str) -> Self { 59 | Stream { 60 | text, 61 | pos: 0, 62 | end: text.len(), 63 | } 64 | } 65 | 66 | #[inline] 67 | pub fn pos(&self) -> usize { 68 | self.pos 69 | } 70 | 71 | #[inline] 72 | pub fn jump_to_end(&mut self) { 73 | self.pos = self.end; 74 | } 75 | 76 | #[inline] 77 | pub fn at_end(&self) -> bool { 78 | self.pos >= self.end 79 | } 80 | 81 | #[inline] 82 | pub fn curr_byte(&self) -> Result { 83 | if self.at_end() { 84 | return Err(Error::UnexpectedEndOfStream); 85 | } 86 | 87 | Ok(self.curr_byte_unchecked()) 88 | } 89 | 90 | #[inline] 91 | pub fn curr_byte_unchecked(&self) -> u8 { 92 | self.text.as_bytes()[self.pos] 93 | } 94 | 95 | #[inline] 96 | pub fn next_byte(&self) -> Result { 97 | if self.pos + 1 >= self.end { 98 | return Err(Error::UnexpectedEndOfStream); 99 | } 100 | 101 | Ok(self.text.as_bytes()[self.pos + 1]) 102 | } 103 | 104 | #[inline] 105 | pub fn advance(&mut self, n: usize) { 106 | debug_assert!(self.pos + n <= self.end); 107 | self.pos += n; 108 | } 109 | 110 | pub fn consume_byte(&mut self, c: u8) -> Result<(), Error> { 111 | if self.curr_byte()? != c { 112 | return Err(Error::InvalidByte { 113 | expected: c, 114 | actual: self.curr_byte()?, 115 | pos: self.gen_text_pos(), 116 | }); 117 | } 118 | 119 | self.advance(1); 120 | Ok(()) 121 | } 122 | 123 | pub fn try_consume_byte(&mut self, c: u8) { 124 | if self.curr_byte() == Ok(c) { 125 | self.advance(1); 126 | } 127 | } 128 | 129 | pub fn consume_bytes(&mut self, f: F) -> &'a str 130 | where 131 | F: Fn(u8) -> bool, 132 | { 133 | let start = self.pos; 134 | self.skip_bytes(f); 135 | self.slice_back(start) 136 | } 137 | 138 | pub fn skip_bytes(&mut self, f: F) 139 | where 140 | F: Fn(u8) -> bool, 141 | { 142 | while !self.at_end() && f(self.curr_byte_unchecked()) { 143 | self.advance(1); 144 | } 145 | } 146 | 147 | #[inline] 148 | fn chars(&self) -> str::Chars<'a> { 149 | self.text[self.pos..self.end].chars() 150 | } 151 | 152 | #[inline] 153 | pub fn slice_range(&self, start: usize, end: usize) -> &'a str { 154 | &self.text[start..end] 155 | } 156 | 157 | #[inline] 158 | pub fn slice_back(&self, pos: usize) -> &'a str { 159 | &self.text[pos..self.pos] 160 | } 161 | 162 | #[inline] 163 | pub fn slice_tail(&self) -> &'a str { 164 | &self.text[self.pos..] 165 | } 166 | 167 | #[inline] 168 | pub fn skip_spaces(&mut self) { 169 | while !self.at_end() { 170 | match self.curr_byte_unchecked() { 171 | b' ' | b'\t' | b'\n' | b'\r' | b'\x0C' => self.advance(1), 172 | _ => break, 173 | } 174 | } 175 | } 176 | 177 | #[inline] 178 | pub fn skip_spaces_and_comments(&mut self) -> Result<(), Error> { 179 | self.skip_spaces(); 180 | while self.curr_byte() == Ok(b'/') && self.next_byte() == Ok(b'*') { 181 | self.skip_comment()?; 182 | self.skip_spaces(); 183 | } 184 | 185 | Ok(()) 186 | } 187 | 188 | pub fn consume_ident(&mut self) -> Result<&'a str, Error> { 189 | let start = self.pos(); 190 | 191 | if self.curr_byte() == Ok(b'-') { 192 | self.advance(1); 193 | } 194 | 195 | let mut iter = self.chars(); 196 | if let Some(c) = iter.next() { 197 | if c.is_name_start() { 198 | self.advance(c.len_utf8()); 199 | } else { 200 | return Err(Error::InvalidIdent(self.gen_text_pos_from(start))); 201 | } 202 | } 203 | 204 | for c in iter { 205 | if c.is_name_char() { 206 | self.advance(c.len_utf8()); 207 | } else { 208 | break; 209 | } 210 | } 211 | 212 | if start == self.pos() { 213 | return Err(Error::InvalidIdent(self.gen_text_pos_from(start))); 214 | } 215 | 216 | let name = self.slice_back(start); 217 | Ok(name) 218 | } 219 | 220 | pub fn consume_string(&mut self) -> Result<&'a str, Error> { 221 | // Check for opening quote. 222 | let quote = self.curr_byte()?; 223 | if quote == b'\'' || quote == b'"' { 224 | let mut prev = quote; 225 | self.advance(1); 226 | 227 | let start = self.pos(); 228 | 229 | while !self.at_end() { 230 | let curr = self.curr_byte_unchecked(); 231 | 232 | // Advance until the closing quote. 233 | if curr == quote { 234 | // Check for escaped quote. 235 | if prev != b'\\' { 236 | break; 237 | } 238 | } 239 | 240 | prev = curr; 241 | self.advance(1); 242 | } 243 | 244 | let value = self.slice_back(start); 245 | 246 | // Check for closing quote. 247 | self.consume_byte(quote)?; 248 | 249 | Ok(value) 250 | } else { 251 | self.consume_ident() 252 | } 253 | } 254 | 255 | pub fn skip_comment(&mut self) -> Result<(), Error> { 256 | let start = self.pos(); 257 | self.skip_comment_impl() 258 | .map_err(|_| Error::InvalidComment(self.gen_text_pos_from(start)))?; 259 | Ok(()) 260 | } 261 | 262 | fn skip_comment_impl(&mut self) -> Result<(), Error> { 263 | self.consume_byte(b'/')?; 264 | self.consume_byte(b'*')?; 265 | 266 | while !self.at_end() { 267 | let curr = self.curr_byte_unchecked(); 268 | if curr == b'*' && self.next_byte() == Ok(b'/') { 269 | break; 270 | } 271 | 272 | self.advance(1); 273 | } 274 | 275 | self.consume_byte(b'*')?; 276 | self.consume_byte(b'/')?; 277 | Ok(()) 278 | } 279 | 280 | #[inline(never)] 281 | pub fn gen_text_pos(&self) -> TextPos { 282 | let row = Self::calc_curr_row(self.text, self.pos); 283 | let col = Self::calc_curr_col(self.text, self.pos); 284 | TextPos::new(row, col) 285 | } 286 | 287 | #[inline(never)] 288 | pub fn gen_text_pos_from(&self, pos: usize) -> TextPos { 289 | let mut s = *self; 290 | s.pos = core::cmp::min(pos, self.text.len()); 291 | s.gen_text_pos() 292 | } 293 | 294 | fn calc_curr_row(text: &str, end: usize) -> u32 { 295 | let mut row = 1; 296 | for c in &text.as_bytes()[..end] { 297 | if *c == b'\n' { 298 | row += 1; 299 | } 300 | } 301 | 302 | row 303 | } 304 | 305 | fn calc_curr_col(text: &str, end: usize) -> u32 { 306 | let mut col = 1; 307 | for c in text[..end].chars().rev() { 308 | if c == '\n' { 309 | break; 310 | } else { 311 | col += 1; 312 | } 313 | } 314 | 315 | col 316 | } 317 | } 318 | -------------------------------------------------------------------------------- /tests/select.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2019 the SimpleCSS Authors 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT 3 | 4 | //! Select 5 | 6 | use simplecss::*; 7 | 8 | struct XmlNode<'a, 'input: 'a>(roxmltree::Node<'a, 'input>); 9 | 10 | impl<'a, 'input: 'a> XmlNode<'a, 'input> { 11 | fn select(&self, text: &str) -> Vec> { 12 | let selectors = Selector::parse(text).unwrap(); 13 | let mut nodes = Vec::new(); 14 | for node in self.0.descendants().filter(|n| n.is_element()) { 15 | if selectors.matches(&XmlNode(node)) { 16 | nodes.push(node); 17 | } 18 | } 19 | 20 | nodes 21 | } 22 | } 23 | 24 | impl Element for XmlNode<'_, '_> { 25 | fn parent_element(&self) -> Option { 26 | self.0.parent_element().map(XmlNode) 27 | } 28 | 29 | fn prev_sibling_element(&self) -> Option { 30 | self.0.prev_sibling_element().map(XmlNode) 31 | } 32 | 33 | fn has_local_name(&self, local_name: &str) -> bool { 34 | self.0.tag_name().name() == local_name 35 | } 36 | 37 | fn attribute_matches(&self, local_name: &str, operator: AttributeOperator<'_>) -> bool { 38 | match self.0.attribute(local_name) { 39 | Some(value) => operator.matches(value), 40 | None => false, 41 | } 42 | } 43 | 44 | fn pseudo_class_matches(&self, class: PseudoClass<'_>) -> bool { 45 | match class { 46 | PseudoClass::FirstChild => self.prev_sibling_element().is_none(), 47 | _ => false, 48 | } 49 | } 50 | } 51 | 52 | macro_rules! match_single { 53 | ($doc:expr, $selector:expr) => {{ 54 | let nodes = XmlNode($doc.root_element()).select($selector); 55 | assert_eq!(nodes.len(), 1); 56 | nodes[0].attribute("id").unwrap() 57 | }}; 58 | } 59 | 60 | macro_rules! match_none { 61 | ($doc:expr, $selector:expr) => {{ 62 | assert_eq!(XmlNode($doc.root_element()).select($selector).len(), 0); 63 | }}; 64 | } 65 | 66 | #[test] 67 | fn select_01() { 68 | let doc = roxmltree::Document::parse("
").unwrap(); 69 | assert_eq!(match_single!(doc, "*"), "div1"); 70 | } 71 | 72 | #[test] 73 | fn select_02() { 74 | let doc = roxmltree::Document::parse("
").unwrap(); 75 | assert_eq!(match_single!(doc, "div"), "div1"); 76 | match_none!(doc, "p"); 77 | } 78 | 79 | #[test] 80 | fn select_03() { 81 | let doc = roxmltree::Document::parse("
").unwrap(); 82 | assert_eq!(match_single!(doc, "#div1"), "div1"); 83 | match_none!(doc, "#d1"); 84 | } 85 | 86 | #[test] 87 | fn select_04() { 88 | let doc = roxmltree::Document::parse("
").unwrap(); 89 | match_none!(doc, "p#div1"); 90 | } 91 | 92 | #[test] 93 | fn select_05() { 94 | let doc = roxmltree::Document::parse( 95 | "\ 96 |
97 |

98 |

99 | ", 100 | ) 101 | .unwrap(); 102 | 103 | assert_eq!(match_single!(doc, "div p"), "p1"); 104 | } 105 | 106 | #[test] 107 | fn select_06() { 108 | let doc = roxmltree::Document::parse( 109 | "\ 110 |
111 | 112 |

113 | 114 |

115 | ", 116 | ) 117 | .unwrap(); 118 | 119 | assert_eq!(match_single!(doc, "div p"), "p1"); 120 | } 121 | 122 | #[test] 123 | fn select_07() { 124 | let doc = roxmltree::Document::parse( 125 | "\ 126 |
127 |
128 | 129 |

130 | 131 |

132 |
133 | ", 134 | ) 135 | .unwrap(); 136 | 137 | assert_eq!(match_single!(doc, "div p"), "p1"); 138 | } 139 | 140 | #[test] 141 | fn select_08() { 142 | let doc = roxmltree::Document::parse( 143 | "\ 144 |
145 | 146 |

147 |

148 |

149 | 150 |
151 | ", 152 | ) 153 | .unwrap(); 154 | 155 | assert_eq!(match_single!(doc, "div p"), "p1"); 156 | } 157 | 158 | #[test] 159 | fn select_09() { 160 | let doc = roxmltree::Document::parse( 161 | "\ 162 |
163 | 164 |

165 | 166 |

167 | ", 168 | ) 169 | .unwrap(); 170 | 171 | assert_eq!(match_single!(doc, "div g p"), "p1"); 172 | } 173 | 174 | #[test] 175 | fn select_10() { 176 | let doc = roxmltree::Document::parse( 177 | "\ 178 |
179 | 180 |

181 | 182 |

183 | ", 184 | ) 185 | .unwrap(); 186 | 187 | match_none!(doc, "div g p"); 188 | } 189 | 190 | #[test] 191 | fn select_11() { 192 | let doc = roxmltree::Document::parse( 193 | "\ 194 |
195 | 196 |

197 | 198 |

199 | ", 200 | ) 201 | .unwrap(); 202 | 203 | assert_eq!(match_single!(doc, "div * p"), "p1"); 204 | } 205 | 206 | #[test] 207 | fn select_12() { 208 | let doc = roxmltree::Document::parse( 209 | "\ 210 |
211 |

212 | 213 | 214 |

215 |
216 | ", 217 | ) 218 | .unwrap(); 219 | 220 | assert_eq!(match_single!(doc, "div p *[color]"), "rect2"); 221 | assert_eq!(match_single!(doc, "div p [color]"), "rect2"); 222 | } 223 | 224 | #[test] 225 | fn select_13() { 226 | let doc = roxmltree::Document::parse( 227 | "\ 228 |
229 |

230 |

231 | ", 232 | ) 233 | .unwrap(); 234 | 235 | assert_eq!(match_single!(doc, "div > p"), "p1"); 236 | } 237 | 238 | #[test] 239 | fn select_14() { 240 | let doc = roxmltree::Document::parse( 241 | "\ 242 |

243 | ", 244 | ) 245 | .unwrap(); 246 | 247 | match_none!(doc, "div > p"); 248 | } 249 | 250 | #[test] 251 | fn select_15() { 252 | let doc = roxmltree::Document::parse( 253 | "\ 254 |

255 | 256 |

257 | 258 |

259 | ", 260 | ) 261 | .unwrap(); 262 | 263 | match_none!(doc, "div > p"); 264 | } 265 | 266 | #[test] 267 | fn select_16() { 268 | let doc = roxmltree::Document::parse( 269 | "\ 270 |
271 |

272 |

    273 |
  1. 274 | 275 |

    276 | 277 |

  2. 278 |
279 |

280 |
281 | ", 282 | ) 283 | .unwrap(); 284 | 285 | assert_eq!(match_single!(doc, "div ol>li p"), "p1"); 286 | } 287 | 288 | #[test] 289 | fn select_17() { 290 | let doc = roxmltree::Document::parse( 291 | "\ 292 |
293 |

294 |

    295 | 296 |
  1. 297 | 298 |

    299 | 300 |

  2. 301 |
    302 |
303 |

304 |
305 | ", 306 | ) 307 | .unwrap(); 308 | 309 | match_none!(doc, "div ol>li p"); 310 | } 311 | 312 | #[test] 313 | fn select_18() { 314 | let doc = roxmltree::Document::parse( 315 | "\ 316 |
317 | 318 |

319 |

320 | ", 321 | ) 322 | .unwrap(); 323 | 324 | assert_eq!(match_single!(doc, "g + p"), "p1"); 325 | } 326 | 327 | #[test] 328 | fn select_19() { 329 | let doc = roxmltree::Document::parse( 330 | "\ 331 |
332 | 333 | 334 |

335 |

336 | ", 337 | ) 338 | .unwrap(); 339 | 340 | assert_eq!(match_single!(doc, "g + p"), "p1"); 341 | } 342 | 343 | #[test] 344 | fn select_20() { 345 | let doc = roxmltree::Document::parse( 346 | "\ 347 |
348 |

349 | 350 |

351 | ", 352 | ) 353 | .unwrap(); 354 | 355 | match_none!(doc, "g + p"); 356 | } 357 | 358 | #[test] 359 | fn select_21() { 360 | let doc = roxmltree::Document::parse( 361 | "\ 362 |
363 |

364 |

365 | ", 366 | ) 367 | .unwrap(); 368 | 369 | match_none!(doc, "div + p"); 370 | } 371 | 372 | #[test] 373 | fn select_22() { 374 | let doc = roxmltree::Document::parse( 375 | "\ 376 |
377 |

378 |

379 | ", 380 | ) 381 | .unwrap(); 382 | 383 | assert_eq!(match_single!(doc, "[id=p1]"), "p1"); 384 | } 385 | 386 | #[test] 387 | fn select_23() { 388 | let doc = roxmltree::Document::parse( 389 | "\ 390 |
391 |

392 |

393 | ", 394 | ) 395 | .unwrap(); 396 | 397 | assert_eq!(match_single!(doc, "[class~=warn]"), "p1"); 398 | } 399 | 400 | #[test] 401 | fn select_24() { 402 | let doc = roxmltree::Document::parse( 403 | "\ 404 |
405 |

406 |

407 | ", 408 | ) 409 | .unwrap(); 410 | 411 | match_none!(doc, "[class~='test warn']"); 412 | } 413 | 414 | #[test] 415 | fn select_25() { 416 | let doc = roxmltree::Document::parse( 417 | "\ 418 |
419 |

420 |

421 | ", 422 | ) 423 | .unwrap(); 424 | 425 | assert_eq!(match_single!(doc, "[lang=en]"), "p1"); 426 | assert_eq!(match_single!(doc, "[lang|=en]"), "p1"); 427 | } 428 | 429 | #[test] 430 | fn select_26() { 431 | let doc = roxmltree::Document::parse( 432 | "\ 433 |
434 |

435 |

436 | ", 437 | ) 438 | .unwrap(); 439 | 440 | assert_eq!(match_single!(doc, "[lang='en-US']"), "p1"); 441 | assert_eq!(match_single!(doc, "[lang|=en]"), "p1"); 442 | } 443 | 444 | #[test] 445 | fn select_27() { 446 | let doc = roxmltree::Document::parse( 447 | "\ 448 |
449 |

450 |

451 | ", 452 | ) 453 | .unwrap(); 454 | 455 | assert_eq!(match_single!(doc, ".marine.pastoral"), "p1"); 456 | } 457 | 458 | #[test] 459 | fn select_28() { 460 | let doc = roxmltree::Document::parse( 461 | "\ 462 |
463 |

464 |

465 | ", 466 | ) 467 | .unwrap(); 468 | 469 | assert_eq!(match_single!(doc, "p:first-child"), "p1"); 470 | } 471 | 472 | #[test] 473 | fn select_29() { 474 | let doc = roxmltree::Document::parse( 475 | "\ 476 |
477 | 478 |

479 |

480 | ", 481 | ) 482 | .unwrap(); 483 | 484 | match_none!(doc, "p:first-child"); 485 | } 486 | 487 | #[test] 488 | fn select_30() { 489 | let doc = roxmltree::Document::parse( 490 | "\ 491 |
492 |

493 |

494 |

495 | ", 496 | ) 497 | .unwrap(); 498 | 499 | let nodes = XmlNode(doc.root_element()).select(":first-child"); 500 | assert_eq!(nodes.len(), 2); 501 | assert_eq!(nodes[0].attribute("id").unwrap(), "div1"); 502 | assert_eq!(nodes[1].attribute("id").unwrap(), "p1"); 503 | } 504 | 505 | #[test] 506 | fn to_string() { 507 | let selectors = Selector::parse("a > b").unwrap(); 508 | assert_eq!(selectors.to_string(), "a > b"); 509 | } 510 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /tests/selector_tokenizer.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2019 the SimpleCSS Authors 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT 3 | 4 | //! Selector Tokenizer 5 | 6 | use simplecss::*; 7 | 8 | macro_rules! tokenize { 9 | ($name:ident, $text:expr, $( $token:expr ),*) => ( 10 | #[test] 11 | fn $name() { 12 | let mut t = SelectorTokenizer::from($text); 13 | $( 14 | assert_eq!(t.next().unwrap().unwrap(), $token); 15 | )* 16 | 17 | assert!(t.next().is_none()); 18 | } 19 | ) 20 | } 21 | 22 | tokenize!(tokenize_01, "*", SelectorToken::UniversalSelector); 23 | 24 | tokenize!(tokenize_02, "div", SelectorToken::TypeSelector("div")); 25 | 26 | tokenize!(tokenize_03, "#div", SelectorToken::IdSelector("div")); 27 | 28 | tokenize!(tokenize_04, ".div", SelectorToken::ClassSelector("div")); 29 | 30 | tokenize!( 31 | tokenize_05, 32 | "[id]", 33 | SelectorToken::AttributeSelector("id", AttributeOperator::Exists) 34 | ); 35 | 36 | tokenize!( 37 | tokenize_06, 38 | "[id=test]", 39 | SelectorToken::AttributeSelector("id", AttributeOperator::Matches("test")) 40 | ); 41 | 42 | tokenize!( 43 | tokenize_07, 44 | "[id~=test]", 45 | SelectorToken::AttributeSelector("id", AttributeOperator::Contains("test")) 46 | ); 47 | 48 | tokenize!( 49 | tokenize_08, 50 | "[id|=test]", 51 | SelectorToken::AttributeSelector("id", AttributeOperator::StartsWith("test")) 52 | ); 53 | 54 | tokenize!( 55 | tokenize_09, 56 | "[id='test']", 57 | SelectorToken::AttributeSelector("id", AttributeOperator::Matches("test")) 58 | ); 59 | 60 | tokenize!( 61 | tokenize_10, 62 | "[id=\"test\"]", 63 | SelectorToken::AttributeSelector("id", AttributeOperator::Matches("test")) 64 | ); 65 | 66 | tokenize!( 67 | tokenize_11, 68 | "[id='te\\'st']", 69 | SelectorToken::AttributeSelector("id", AttributeOperator::Matches("te\\'st")) 70 | ); 71 | 72 | tokenize!( 73 | tokenize_12, 74 | "[id=\"te\\\"st\"]", 75 | SelectorToken::AttributeSelector("id", AttributeOperator::Matches("te\\\"st")) 76 | ); 77 | 78 | tokenize!( 79 | tokenize_13, 80 | "div:first-child", 81 | SelectorToken::TypeSelector("div"), 82 | SelectorToken::PseudoClass("first-child") 83 | ); 84 | 85 | tokenize!( 86 | tokenize_14, 87 | ":first-child", 88 | SelectorToken::PseudoClass("first-child") 89 | ); 90 | 91 | tokenize!( 92 | tokenize_15, 93 | "div p", 94 | SelectorToken::TypeSelector("div"), 95 | SelectorToken::DescendantCombinator, 96 | SelectorToken::TypeSelector("p") 97 | ); 98 | 99 | tokenize!( 100 | tokenize_16, 101 | "div p a", 102 | SelectorToken::TypeSelector("div"), 103 | SelectorToken::DescendantCombinator, 104 | SelectorToken::TypeSelector("p"), 105 | SelectorToken::DescendantCombinator, 106 | SelectorToken::TypeSelector("a") 107 | ); 108 | 109 | tokenize!( 110 | tokenize_17, 111 | "div>p", 112 | SelectorToken::TypeSelector("div"), 113 | SelectorToken::ChildCombinator, 114 | SelectorToken::TypeSelector("p") 115 | ); 116 | 117 | tokenize!( 118 | tokenize_18, 119 | "div >p", 120 | SelectorToken::TypeSelector("div"), 121 | SelectorToken::ChildCombinator, 122 | SelectorToken::TypeSelector("p") 123 | ); 124 | 125 | tokenize!( 126 | tokenize_19, 127 | "div> p", 128 | SelectorToken::TypeSelector("div"), 129 | SelectorToken::ChildCombinator, 130 | SelectorToken::TypeSelector("p") 131 | ); 132 | 133 | tokenize!( 134 | tokenize_20, 135 | "div > p", 136 | SelectorToken::TypeSelector("div"), 137 | SelectorToken::ChildCombinator, 138 | SelectorToken::TypeSelector("p") 139 | ); 140 | 141 | tokenize!( 142 | tokenize_21, 143 | "div .p", 144 | SelectorToken::TypeSelector("div"), 145 | SelectorToken::DescendantCombinator, 146 | SelectorToken::ClassSelector("p") 147 | ); 148 | 149 | tokenize!( 150 | tokenize_22, 151 | "div *", 152 | SelectorToken::TypeSelector("div"), 153 | SelectorToken::DescendantCombinator, 154 | SelectorToken::UniversalSelector 155 | ); 156 | 157 | tokenize!( 158 | tokenize_23, 159 | "div #p", 160 | SelectorToken::TypeSelector("div"), 161 | SelectorToken::DescendantCombinator, 162 | SelectorToken::IdSelector("p") 163 | ); 164 | 165 | tokenize!( 166 | tokenize_24, 167 | "div [id]", 168 | SelectorToken::TypeSelector("div"), 169 | SelectorToken::DescendantCombinator, 170 | SelectorToken::AttributeSelector("id", AttributeOperator::Exists) 171 | ); 172 | 173 | tokenize!( 174 | tokenize_25, 175 | "div :link", 176 | SelectorToken::TypeSelector("div"), 177 | SelectorToken::DescendantCombinator, 178 | SelectorToken::PseudoClass("link") 179 | ); 180 | 181 | tokenize!( 182 | tokenize_26, 183 | "div+p", 184 | SelectorToken::TypeSelector("div"), 185 | SelectorToken::AdjacentCombinator, 186 | SelectorToken::TypeSelector("p") 187 | ); 188 | 189 | tokenize!( 190 | tokenize_27, 191 | "div +p", 192 | SelectorToken::TypeSelector("div"), 193 | SelectorToken::AdjacentCombinator, 194 | SelectorToken::TypeSelector("p") 195 | ); 196 | 197 | tokenize!( 198 | tokenize_28, 199 | "div+ p", 200 | SelectorToken::TypeSelector("div"), 201 | SelectorToken::AdjacentCombinator, 202 | SelectorToken::TypeSelector("p") 203 | ); 204 | 205 | tokenize!( 206 | tokenize_29, 207 | "div + p", 208 | SelectorToken::TypeSelector("div"), 209 | SelectorToken::AdjacentCombinator, 210 | SelectorToken::TypeSelector("p") 211 | ); 212 | 213 | tokenize!(tokenize_30, "div {", SelectorToken::TypeSelector("div")); 214 | 215 | tokenize!(tokenize_31, "div,", SelectorToken::TypeSelector("div")); 216 | 217 | tokenize!(tokenize_32, "div{", SelectorToken::TypeSelector("div")); 218 | 219 | tokenize!(tokenize_33, "div ,", SelectorToken::TypeSelector("div")); 220 | 221 | tokenize!( 222 | tokenize_34, 223 | "div.test", 224 | SelectorToken::TypeSelector("div"), 225 | SelectorToken::ClassSelector("test") 226 | ); 227 | 228 | tokenize!( 229 | tokenize_35, 230 | "div.test.warn", 231 | SelectorToken::TypeSelector("div"), 232 | SelectorToken::ClassSelector("test"), 233 | SelectorToken::ClassSelector("warn") 234 | ); 235 | 236 | tokenize!( 237 | tokenize_36, 238 | "div#id", 239 | SelectorToken::TypeSelector("div"), 240 | SelectorToken::IdSelector("id") 241 | ); 242 | 243 | tokenize!( 244 | tokenize_37, 245 | "*[id]", 246 | SelectorToken::UniversalSelector, 247 | SelectorToken::AttributeSelector("id", AttributeOperator::Exists) 248 | ); 249 | 250 | tokenize!( 251 | tokenize_38, 252 | "*.test", 253 | SelectorToken::UniversalSelector, 254 | SelectorToken::ClassSelector("test") 255 | ); 256 | 257 | tokenize!( 258 | tokenize_39, 259 | "*#id", 260 | SelectorToken::UniversalSelector, 261 | SelectorToken::IdSelector("id") 262 | ); 263 | 264 | tokenize!( 265 | tokenize_40, 266 | "div * p", 267 | SelectorToken::TypeSelector("div"), 268 | SelectorToken::DescendantCombinator, 269 | SelectorToken::UniversalSelector, 270 | SelectorToken::DescendantCombinator, 271 | SelectorToken::TypeSelector("p") 272 | ); 273 | 274 | tokenize!( 275 | tokenize_41, 276 | "div[id=test][color=red]", 277 | SelectorToken::TypeSelector("div"), 278 | SelectorToken::AttributeSelector("id", AttributeOperator::Matches("test")), 279 | SelectorToken::AttributeSelector("color", AttributeOperator::Matches("red")) 280 | ); 281 | 282 | tokenize!( 283 | tokenize_42, 284 | "a.external:visited", 285 | SelectorToken::TypeSelector("a"), 286 | SelectorToken::ClassSelector("external"), 287 | SelectorToken::PseudoClass("visited") 288 | ); 289 | 290 | tokenize!( 291 | tokenize_43, 292 | ":lang(en)", 293 | SelectorToken::LangPseudoClass("en") 294 | ); 295 | 296 | tokenize!( 297 | tokenize_44, 298 | "a\nb", 299 | SelectorToken::TypeSelector("a"), 300 | SelectorToken::DescendantCombinator, 301 | SelectorToken::TypeSelector("b") 302 | ); 303 | 304 | tokenize!( 305 | tokenize_45, 306 | ".warn :first-child", 307 | SelectorToken::ClassSelector("warn"), 308 | SelectorToken::DescendantCombinator, 309 | SelectorToken::PseudoClass("first-child") 310 | ); 311 | 312 | macro_rules! malformed { 313 | ($name:ident, $text:expr, $err_str:expr) => { 314 | #[test] 315 | fn $name() { 316 | for token in SelectorTokenizer::from($text) { 317 | match token { 318 | Ok(_) => {} 319 | Err(e) => { 320 | assert_eq!(e.to_string(), $err_str); 321 | return; 322 | } 323 | } 324 | } 325 | 326 | unreachable!() 327 | } 328 | }; 329 | } 330 | 331 | malformed!(malformed_01, ">", "unexpected combinator"); 332 | 333 | malformed!(malformed_02, "+", "unexpected combinator"); 334 | 335 | malformed!(malformed_03, "> a", "unexpected combinator"); 336 | 337 | malformed!(malformed_04, "a >", "selector missing"); 338 | 339 | malformed!(malformed_05, "*a", "unexpected selector"); 340 | 341 | malformed!(malformed_06, "a*", "unexpected selector"); 342 | 343 | malformed!(malformed_07, "a > ,", "selector missing"); 344 | 345 | malformed!(malformed_08, "a > >", "unexpected combinator"); 346 | 347 | malformed!(malformed_09, "a > {", "selector missing"); 348 | 349 | malformed!(malformed_10, "a/**/b", "unexpected selector"); 350 | 351 | malformed!(malformed_11, "a < b", "invalid ident at 1:3"); 352 | 353 | malformed!(malformed_12, ":lang()", "invalid language pseudo-class"); 354 | 355 | malformed!(malformed_13, ":lang( )", "invalid language pseudo-class"); 356 | 357 | malformed!(malformed_14, "::first-child", "invalid ident at 1:2"); 358 | 359 | malformed!( 360 | malformed_15, 361 | "[olor:red", 362 | "invalid or unsupported attribute selector" 363 | ); 364 | 365 | malformed!(malformed_16, "", "selector missing"); 366 | 367 | malformed!(malformed_17, " ", "selector missing"); 368 | 369 | malformed!(malformed_18, "/**/", "selector missing"); 370 | 371 | tokenize!(comment_01, "/**/a", SelectorToken::TypeSelector("a")); 372 | 373 | tokenize!(comment_02, "/* */a", SelectorToken::TypeSelector("a")); 374 | 375 | tokenize!( 376 | comment_03, 377 | "/* comment */a", 378 | SelectorToken::TypeSelector("a") 379 | ); 380 | 381 | tokenize!(comment_04, "/**/ /**/a", SelectorToken::TypeSelector("a")); 382 | 383 | tokenize!(comment_05, "/**/ a /**/", SelectorToken::TypeSelector("a")); 384 | 385 | tokenize!( 386 | comment_06, 387 | "a /**/ b", 388 | SelectorToken::TypeSelector("a"), 389 | SelectorToken::DescendantCombinator, 390 | SelectorToken::TypeSelector("b") 391 | ); 392 | 393 | tokenize!( 394 | comment_08, 395 | "a /**/b", 396 | SelectorToken::TypeSelector("a"), 397 | SelectorToken::DescendantCombinator, 398 | SelectorToken::TypeSelector("b") 399 | ); 400 | 401 | tokenize!( 402 | comment_09, 403 | "a/**/ b", 404 | SelectorToken::TypeSelector("a"), 405 | SelectorToken::DescendantCombinator, 406 | SelectorToken::TypeSelector("b") 407 | ); 408 | 409 | tokenize!( 410 | comment_10, 411 | "a/**/ /**/b", 412 | SelectorToken::TypeSelector("a"), 413 | SelectorToken::DescendantCombinator, 414 | SelectorToken::TypeSelector("b") 415 | ); 416 | 417 | tokenize!( 418 | comment_11, 419 | "a /**/ /**/ b", 420 | SelectorToken::TypeSelector("a"), 421 | SelectorToken::DescendantCombinator, 422 | SelectorToken::TypeSelector("b") 423 | ); 424 | 425 | tokenize!( 426 | comment_12, 427 | "a /**//**/ b", 428 | SelectorToken::TypeSelector("a"), 429 | SelectorToken::DescendantCombinator, 430 | SelectorToken::TypeSelector("b") 431 | ); 432 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | env: 2 | # We aim to always test with the latest stable Rust toolchain, however we pin to a specific 3 | # version like 1.70. Note that we only specify MAJOR.MINOR and not PATCH so that bugfixes still 4 | # come automatically. If the version specified here is no longer the latest stable version, 5 | # then please feel free to submit a PR that adjusts it along with the potential clippy fixes. 6 | RUST_STABLE_VER: "1.88" # In quotes because otherwise (e.g.) 1.70 would be interpreted as 1.7 7 | # The purpose of checking with the minimum supported Rust toolchain is to detect its staleness. 8 | # If the compilation fails, then the version specified here needs to be bumped up to reality. 9 | # Be sure to also update the rust-version property in the workspace Cargo.toml file, 10 | # plus all the README.md files of the affected packages. 11 | RUST_MIN_VER: "1.65" 12 | # List of packages that will be checked with the minimum supported Rust version. 13 | # This should be limited to packages that are intended for publishing. 14 | RUST_MIN_VER_PKGS: "-p simplecss" 15 | # List of features that depend on the standard library and will be excluded from no_std checks. 16 | FEATURES_DEPENDING_ON_STD: "std,default" 17 | 18 | 19 | # Rationale 20 | # 21 | # We don't run clippy with --all-targets because then even --lib and --bins are compiled with 22 | # dev dependencies enabled, which does not match how they would be compiled by users. 23 | # A dev dependency might enable a feature that we need for a regular dependency, 24 | # and checking with --all-targets would not find our feature requirements lacking. 25 | # This problem still applies to cargo resolver version 2. 26 | # Thus we split all the targets into two steps, one with --lib --bins 27 | # and another with --tests --benches --examples. 28 | # Also, we can't give --lib --bins explicitly because then cargo will error on binary-only packages. 29 | # Luckily the default behavior of cargo with no explicit targets is the same but without the error. 30 | # 31 | # We use cargo-hack for a similar reason. Cargo's --workspace will do feature unification across 32 | # the whole workspace. While cargo-hack will instead check each workspace package separately. 33 | # 34 | # Using cargo-hack also allows us to more easily test the feature matrix of our packages. 35 | # We use --each-feature & --optional-deps which will run a separate check for every feature. 36 | # 37 | # We use cargo-nextest, which has a faster concurrency model for running tests. 38 | # However cargo-nextest does not support running doc tests, so we also have a cargo test --doc step. 39 | # For more information see https://github.com/nextest-rs/nextest/issues/16 40 | # 41 | # The MSRV jobs run only cargo check because different clippy versions can disagree on goals and 42 | # running tests introduces dev dependencies which may require a higher MSRV than the bare package. 43 | # 44 | # For no_std checks we target x86_64-unknown-none, because this target doesn't support std 45 | # and as such will error out if our dependency tree accidentally tries to use std. 46 | # https://doc.rust-lang.org/stable/rustc/platform-support/x86_64-unknown-none.html 47 | # 48 | # We don't save caches in the merge-group cases, because those caches will never be re-used (apart 49 | # from the very rare cases where there are multiple PRs in the merge queue). 50 | # This is because GitHub doesn't share caches between merge queues and the main branch. 51 | 52 | name: CI 53 | 54 | on: 55 | pull_request: 56 | merge_group: 57 | # We run on push, even though the commit is the same as when we ran in merge_group. 58 | # This allows the cache to be primed. 59 | # See https://github.com/orgs/community/discussions/66430 60 | push: 61 | branches: 62 | - main 63 | 64 | jobs: 65 | fmt: 66 | name: formatting 67 | runs-on: ubuntu-latest 68 | steps: 69 | - uses: actions/checkout@v4 70 | 71 | - name: install stable toolchain 72 | uses: dtolnay/rust-toolchain@master 73 | with: 74 | toolchain: ${{ env.RUST_STABLE_VER }} 75 | components: rustfmt 76 | 77 | - name: cargo fmt 78 | run: cargo fmt --all --check 79 | 80 | - name: Install Taplo 81 | uses: uncenter/setup-taplo@09968a8ae38d66ddd3d23802c44bf6122d7aa991 # v1 82 | with: 83 | version: "0.9.3" 84 | 85 | - name: Run taplo fmt 86 | run: taplo fmt --check --diff 87 | 88 | - name: install ripgrep 89 | run: | 90 | sudo apt update 91 | sudo apt install ripgrep 92 | 93 | - name: check copyright headers 94 | run: bash .github/copyright.sh 95 | 96 | clippy-stable: 97 | name: cargo clippy 98 | runs-on: ${{ matrix.os }} 99 | strategy: 100 | matrix: 101 | os: [windows-latest, macos-latest, ubuntu-latest] 102 | steps: 103 | - uses: actions/checkout@v4 104 | 105 | - name: install stable toolchain 106 | uses: dtolnay/rust-toolchain@master 107 | with: 108 | toolchain: ${{ env.RUST_STABLE_VER }} 109 | targets: x86_64-unknown-none 110 | components: clippy 111 | 112 | - name: install cargo-hack 113 | uses: taiki-e/install-action@v2 114 | with: 115 | tool: cargo-hack 116 | 117 | - name: restore cache 118 | uses: Swatinem/rust-cache@v2 119 | with: 120 | save-if: ${{ github.event_name != 'merge_group' }} 121 | 122 | - name: cargo clippy (no_std) 123 | run: cargo hack clippy --workspace --locked --optional-deps --each-feature --ignore-unknown-features --features libm --exclude-features ${{ env.FEATURES_DEPENDING_ON_STD }} --target x86_64-unknown-none -- -D warnings 124 | 125 | - name: cargo clippy 126 | run: cargo hack clippy --workspace --locked --optional-deps --each-feature --ignore-unknown-features --features std -- -D warnings 127 | 128 | - name: cargo clippy (auxiliary) 129 | run: cargo hack clippy --workspace --locked --optional-deps --each-feature --ignore-unknown-features --features std --tests --benches --examples -- -D warnings 130 | 131 | clippy-stable-wasm: 132 | name: cargo clippy (wasm32) 133 | runs-on: ubuntu-latest 134 | steps: 135 | - uses: actions/checkout@v4 136 | 137 | - name: install stable toolchain 138 | uses: dtolnay/rust-toolchain@master 139 | with: 140 | toolchain: ${{ env.RUST_STABLE_VER }} 141 | targets: wasm32-unknown-unknown 142 | components: clippy 143 | 144 | - name: install cargo-hack 145 | uses: taiki-e/install-action@v2 146 | with: 147 | tool: cargo-hack 148 | 149 | - name: restore cache 150 | uses: Swatinem/rust-cache@v2 151 | with: 152 | save-if: ${{ github.event_name != 'merge_group' }} 153 | 154 | - name: cargo clippy 155 | run: cargo hack clippy --workspace --locked --target wasm32-unknown-unknown --optional-deps --each-feature --ignore-unknown-features --features std -- -D warnings 156 | 157 | - name: cargo clippy (auxiliary) 158 | run: cargo hack clippy --workspace --locked --target wasm32-unknown-unknown --optional-deps --each-feature --ignore-unknown-features --features std --tests --benches --examples -- -D warnings 159 | 160 | test-stable: 161 | name: cargo test 162 | runs-on: ${{ matrix.os }} 163 | strategy: 164 | matrix: 165 | os: [windows-latest, macos-latest, ubuntu-latest] 166 | steps: 167 | - uses: actions/checkout@v4 168 | 169 | - name: install stable toolchain 170 | uses: dtolnay/rust-toolchain@master 171 | with: 172 | toolchain: ${{ env.RUST_STABLE_VER }} 173 | 174 | - name: install cargo-nextest 175 | uses: taiki-e/install-action@v2 176 | with: 177 | tool: cargo-nextest 178 | 179 | - name: restore cache 180 | uses: Swatinem/rust-cache@v2 181 | with: 182 | save-if: ${{ github.event_name != 'merge_group' }} 183 | 184 | - name: cargo nextest 185 | run: cargo nextest run --workspace --locked --all-features --no-fail-fast 186 | 187 | - name: cargo test --doc 188 | run: cargo test --doc --workspace --locked --all-features --no-fail-fast 189 | 190 | test-stable-wasm: 191 | name: cargo test (wasm32) 192 | runs-on: ubuntu-latest 193 | steps: 194 | - uses: actions/checkout@v4 195 | 196 | - name: install stable toolchain 197 | uses: dtolnay/rust-toolchain@master 198 | with: 199 | toolchain: ${{ env.RUST_STABLE_VER }} 200 | targets: wasm32-unknown-unknown 201 | 202 | - name: restore cache 203 | uses: Swatinem/rust-cache@v2 204 | with: 205 | save-if: ${{ github.event_name != 'merge_group' }} 206 | 207 | # TODO: Find a way to make tests work. Until then the tests are merely compiled. 208 | - name: cargo test compile 209 | run: cargo test --workspace --locked --target wasm32-unknown-unknown --all-features --no-run 210 | 211 | check-msrv: 212 | name: cargo check (msrv) 213 | runs-on: ${{ matrix.os }} 214 | strategy: 215 | matrix: 216 | os: [windows-latest, macos-latest, ubuntu-latest] 217 | steps: 218 | - uses: actions/checkout@v4 219 | 220 | - name: install msrv toolchain 221 | uses: dtolnay/rust-toolchain@master 222 | with: 223 | toolchain: ${{ env.RUST_MIN_VER }} 224 | targets: x86_64-unknown-none 225 | 226 | - name: install cargo-hack 227 | uses: taiki-e/install-action@v2 228 | with: 229 | tool: cargo-hack 230 | 231 | - name: restore cache 232 | uses: Swatinem/rust-cache@v2 233 | with: 234 | save-if: ${{ github.event_name != 'merge_group' }} 235 | 236 | - name: cargo check (no_std) 237 | run: cargo hack check ${{ env.RUST_MIN_VER_PKGS }} --locked --optional-deps --each-feature --ignore-unknown-features --features libm --exclude-features ${{ env.FEATURES_DEPENDING_ON_STD }} --target x86_64-unknown-none 238 | 239 | - name: cargo check 240 | run: cargo hack check ${{ env.RUST_MIN_VER_PKGS }} --locked --optional-deps --each-feature --ignore-unknown-features --features std 241 | 242 | check-msrv-wasm: 243 | name: cargo check (msrv) (wasm32) 244 | runs-on: ubuntu-latest 245 | steps: 246 | - uses: actions/checkout@v4 247 | 248 | - name: install msrv toolchain 249 | uses: dtolnay/rust-toolchain@master 250 | with: 251 | toolchain: ${{ env.RUST_MIN_VER }} 252 | targets: wasm32-unknown-unknown 253 | 254 | - name: install cargo-hack 255 | uses: taiki-e/install-action@v2 256 | with: 257 | tool: cargo-hack 258 | 259 | - name: restore cache 260 | uses: Swatinem/rust-cache@v2 261 | with: 262 | save-if: ${{ github.event_name != 'merge_group' }} 263 | 264 | - name: cargo check 265 | run: cargo hack check ${{ env.RUST_MIN_VER_PKGS }} --locked --target wasm32-unknown-unknown --optional-deps --each-feature --ignore-unknown-features --features std 266 | 267 | doc: 268 | name: cargo doc 269 | # NOTE: We don't have any platform specific docs in this workspace, so we only run on Ubuntu. 270 | # If we get per-platform docs (win/macos/linux/wasm32/..) then doc jobs should match that. 271 | runs-on: ubuntu-latest 272 | steps: 273 | - uses: actions/checkout@v4 274 | 275 | - name: install nightly toolchain 276 | uses: dtolnay/rust-toolchain@nightly 277 | 278 | - name: restore cache 279 | uses: Swatinem/rust-cache@v2 280 | with: 281 | save-if: ${{ github.event_name != 'merge_group' }} 282 | 283 | # We test documentation using nightly to match docs.rs. 284 | - name: cargo doc 285 | run: cargo doc --workspace --locked --all-features --no-deps --document-private-items 286 | env: 287 | RUSTDOCFLAGS: '--cfg docsrs -D warnings' 288 | 289 | # If this fails, consider changing your text or adding something to .typos.toml. 290 | typos: 291 | runs-on: ubuntu-latest 292 | steps: 293 | - uses: actions/checkout@v4 294 | 295 | - name: check typos 296 | uses: crate-ci/typos@v1.33.1 297 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2016 the SimpleCSS Authors 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT 3 | 4 | /*! 5 | A simple [CSS 2.1](https://www.w3.org/TR/CSS21/) parser and selector. 6 | 7 | This is not a browser-grade CSS parser. If you need one, 8 | use [cssparser](https://crates.io/crates/cssparser) + 9 | [selectors](https://crates.io/crates/selectors). 10 | 11 | Since it's very simple we will start with limitations: 12 | 13 | ## Limitations 14 | 15 | - [Most at-rules](https://www.w3.org/TR/CSS21/syndata.html#at-rules) are not supported. 16 | They will be skipped during parsing. The only supported at-rule is `@font-face`. 17 | - Property values are not parsed. 18 | In CSS like `* { width: 5px }` you will get a `width` property with a `5px` value as a string. 19 | - CDO/CDC comments are not supported. 20 | - Parser is case sensitive. All keywords must be lowercase. 21 | - Unicode escape, like `\26`, is not supported. 22 | 23 | ## Features 24 | 25 | - Selector matching support. 26 | - The rules are sorted by specificity. 27 | - `@font-face` parsing support. 28 | - `!important` parsing support. 29 | - Has a high-level parsers and low-level, zero-allocation tokenizers. 30 | - No unsafe. 31 | */ 32 | 33 | // LINEBENDER LINT SET - lib.rs - v2 34 | // See https://linebender.org/wiki/canonical-lints/ 35 | // These lints aren't included in Cargo.toml because they 36 | // shouldn't apply to examples and tests 37 | #![warn(unused_crate_dependencies)] 38 | #![warn(clippy::print_stdout, clippy::print_stderr)] 39 | // Targeting e.g. 32-bit means structs containing usize can give false positives for 64-bit. 40 | #![cfg_attr(target_pointer_width = "64", warn(clippy::trivially_copy_pass_by_ref))] 41 | // END LINEBENDER LINT SET 42 | #![cfg_attr(docsrs, feature(doc_cfg))] 43 | #![no_std] 44 | // The following lints are part of the Linebender standard set, 45 | // but resolving them has been deferred for now. 46 | // Feel free to send a PR that solves one or more of these. 47 | #![allow( 48 | missing_debug_implementations, 49 | unreachable_pub, 50 | clippy::use_self, 51 | clippy::missing_assert_message, 52 | clippy::missing_panics_doc, 53 | clippy::exhaustive_enums, 54 | clippy::unseparated_literal_suffix 55 | )] 56 | #![cfg_attr(test, allow(unused_crate_dependencies))] // Some dev dependencies are only used in tests 57 | 58 | extern crate alloc; 59 | #[cfg(feature = "std")] 60 | extern crate std; 61 | 62 | use alloc::vec::Vec; 63 | use core::fmt; 64 | 65 | use log::warn; 66 | 67 | mod selector; 68 | mod stream; 69 | 70 | pub use selector::*; 71 | use stream::Stream; 72 | 73 | /// A list of possible errors. 74 | #[derive(Clone, Copy, PartialEq, Debug)] 75 | pub enum Error { 76 | /// The steam ended earlier than we expected. 77 | /// 78 | /// Should only appear on invalid input data. 79 | UnexpectedEndOfStream, 80 | 81 | /// An invalid ident. 82 | InvalidIdent(TextPos), 83 | 84 | /// An unclosed comment. 85 | InvalidComment(TextPos), 86 | 87 | /// An invalid declaration value. 88 | InvalidValue(TextPos), 89 | 90 | /// An invalid byte. 91 | #[allow(missing_docs)] 92 | InvalidByte { 93 | expected: u8, 94 | actual: u8, 95 | pos: TextPos, 96 | }, 97 | 98 | /// A missing selector. 99 | SelectorMissing, 100 | 101 | /// An unexpected selector. 102 | UnexpectedSelector, 103 | 104 | /// An unexpected combinator. 105 | UnexpectedCombinator, 106 | 107 | /// An invalid or unsupported attribute selector. 108 | InvalidAttributeSelector, 109 | 110 | /// An invalid language pseudo-class. 111 | InvalidLanguagePseudoClass, 112 | } 113 | 114 | impl fmt::Display for Error { 115 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 116 | match *self { 117 | Error::UnexpectedEndOfStream => { 118 | write!(f, "unexpected end of stream") 119 | } 120 | Error::InvalidIdent(pos) => { 121 | write!(f, "invalid ident at {pos}") 122 | } 123 | Error::InvalidComment(pos) => { 124 | write!(f, "invalid comment at {pos}") 125 | } 126 | Error::InvalidValue(pos) => { 127 | write!(f, "invalid value at {pos}") 128 | } 129 | Error::InvalidByte { 130 | expected, 131 | actual, 132 | pos, 133 | } => { 134 | write!( 135 | f, 136 | "expected '{}' not '{}' at {}", 137 | expected as char, actual as char, pos 138 | ) 139 | } 140 | Error::SelectorMissing => { 141 | write!(f, "selector missing") 142 | } 143 | Error::UnexpectedSelector => { 144 | write!(f, "unexpected selector") 145 | } 146 | Error::UnexpectedCombinator => { 147 | write!(f, "unexpected combinator") 148 | } 149 | Error::InvalidAttributeSelector => { 150 | write!(f, "invalid or unsupported attribute selector") 151 | } 152 | Error::InvalidLanguagePseudoClass => { 153 | write!(f, "invalid language pseudo-class") 154 | } 155 | } 156 | } 157 | } 158 | 159 | #[cfg(feature = "std")] 160 | impl std::error::Error for Error {} 161 | 162 | /// A position in text. 163 | /// 164 | /// Position indicates a row/line and a column in the original text. Starting from 1:1. 165 | #[derive(Clone, Copy, PartialEq, Debug)] 166 | #[allow(missing_docs)] 167 | pub struct TextPos { 168 | pub row: u32, 169 | pub col: u32, 170 | } 171 | 172 | impl TextPos { 173 | /// Constructs a new `TextPos`. 174 | /// 175 | /// Should not be invoked manually, but rather via `Stream::gen_text_pos`. 176 | pub fn new(row: u32, col: u32) -> TextPos { 177 | TextPos { row, col } 178 | } 179 | } 180 | 181 | impl fmt::Display for TextPos { 182 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 183 | write!(f, "{}:{}", self.row, self.col) 184 | } 185 | } 186 | 187 | /// A declaration. 188 | #[derive(Clone, Copy, PartialEq, Debug)] 189 | #[allow(missing_docs)] 190 | pub struct Declaration<'a> { 191 | pub name: &'a str, 192 | pub value: &'a str, 193 | pub important: bool, 194 | } 195 | 196 | /// A `@font-face` rule. 197 | #[derive(Clone, Debug)] 198 | pub struct FontFaceRule<'a> { 199 | /// A list of declarations inside this `@font-face` rule. 200 | pub declarations: Vec>, 201 | } 202 | 203 | /// A rule. 204 | #[derive(Clone, Debug)] 205 | pub struct Rule<'a> { 206 | /// A rule selector. 207 | pub selector: Selector<'a>, 208 | /// A rule declarations. 209 | pub declarations: Vec>, 210 | } 211 | 212 | /// A style sheet. 213 | #[derive(Clone, Debug)] 214 | pub struct StyleSheet<'a> { 215 | /// A list of rules. 216 | pub rules: Vec>, 217 | /// A list of `@font-face` rules. 218 | pub font_faces: Vec>, 219 | } 220 | 221 | impl<'a> StyleSheet<'a> { 222 | /// Creates an empty style sheet. 223 | pub fn new() -> Self { 224 | StyleSheet { 225 | rules: Vec::new(), 226 | font_faces: Vec::new(), 227 | } 228 | } 229 | 230 | /// Parses a style sheet from text. 231 | /// 232 | /// Most at-rules are not supported and will be skipped, except `@font-face` 233 | /// rules which are parsed into [`FontFaceRule`]s. 234 | /// 235 | /// # Errors 236 | /// 237 | /// Doesn't produce any errors. In worst case scenario will return an empty stylesheet. 238 | /// 239 | /// All warnings will be logged. 240 | pub fn parse(text: &'a str) -> Self { 241 | let mut sheet = StyleSheet::new(); 242 | sheet.parse_more(text); 243 | sheet 244 | } 245 | 246 | /// Parses a style sheet from a text to the current style sheet. 247 | pub fn parse_more(&mut self, text: &'a str) { 248 | let mut s = Stream::from(text); 249 | 250 | if s.skip_spaces_and_comments().is_err() { 251 | return; 252 | } 253 | 254 | while !s.at_end() { 255 | if s.skip_spaces_and_comments().is_err() { 256 | break; 257 | } 258 | 259 | let _ = consume_statement(&mut s, &mut self.rules, &mut self.font_faces); 260 | } 261 | 262 | if !s.at_end() { 263 | warn!("{} bytes were left.", s.slice_tail().len()); 264 | } 265 | 266 | // Remove empty rules. 267 | self.rules.retain(|rule| !rule.declarations.is_empty()); 268 | 269 | // Sort the rules by specificity. 270 | self.rules 271 | .sort_by_cached_key(|rule| rule.selector.specificity()); 272 | } 273 | } 274 | 275 | impl fmt::Display for StyleSheet<'_> { 276 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 277 | for (i, rule) in self.rules.iter().enumerate() { 278 | write!(f, "{} {{ ", rule.selector)?; 279 | for dec in &rule.declarations { 280 | write!(f, "{}:{}", dec.name, dec.value)?; 281 | if dec.important { 282 | write!(f, " !important")?; 283 | } 284 | write!(f, ";")?; 285 | } 286 | write!(f, " }}")?; 287 | 288 | if i != self.rules.len() - 1 { 289 | writeln!(f)?; 290 | } 291 | } 292 | 293 | Ok(()) 294 | } 295 | } 296 | 297 | impl Default for StyleSheet<'_> { 298 | fn default() -> Self { 299 | Self::new() 300 | } 301 | } 302 | 303 | fn consume_statement<'a>( 304 | s: &mut Stream<'a>, 305 | rules: &mut Vec>, 306 | font_faces: &mut Vec>, 307 | ) -> Result<(), Error> { 308 | if s.curr_byte() == Ok(b'@') { 309 | s.advance(1); 310 | consume_at_rule(s, font_faces) 311 | } else { 312 | consume_rule_set(s, rules) 313 | } 314 | } 315 | 316 | fn consume_at_rule<'a>( 317 | s: &mut Stream<'a>, 318 | font_faces: &mut Vec>, 319 | ) -> Result<(), Error> { 320 | let ident = s.consume_ident()?; 321 | 322 | if ident == "font-face" { 323 | s.skip_spaces_and_comments()?; 324 | 325 | if s.curr_byte() == Ok(b'{') { 326 | s.advance(1); 327 | 328 | let declarations = consume_declarations(s)?; 329 | s.try_consume_byte(b'}'); 330 | 331 | if !declarations.is_empty() { 332 | font_faces.push(FontFaceRule { declarations }); 333 | } 334 | } else { 335 | // Malformed `@font-face`; fall back to skipping it as an unknown at-rule. 336 | skip_at_rule_body(s)?; 337 | } 338 | } else { 339 | warn!("The @{} rule is not supported. Skipped.", ident); 340 | skip_at_rule_body(s)?; 341 | } 342 | 343 | Ok(()) 344 | } 345 | 346 | fn skip_at_rule_body(s: &mut Stream<'_>) -> Result<(), Error> { 347 | s.skip_bytes(|c| c != b';' && c != b'{'); 348 | 349 | match s.curr_byte()? { 350 | b';' => s.advance(1), 351 | b'{' => consume_block(s), 352 | _ => {} 353 | } 354 | 355 | Ok(()) 356 | } 357 | 358 | fn consume_rule_set<'a>(s: &mut Stream<'a>, rules: &mut Vec>) -> Result<(), Error> { 359 | let start_rule_idx = rules.len(); 360 | 361 | while s.curr_byte()? == b',' || start_rule_idx == rules.len() { 362 | if s.curr_byte()? == b',' { 363 | s.advance(1); 364 | } 365 | 366 | let (selector, offset) = parse(s.slice_tail()); 367 | s.advance(offset); 368 | s.skip_spaces(); 369 | 370 | if let Some(selector) = selector { 371 | rules.push(Rule { 372 | selector, 373 | declarations: Vec::new(), 374 | }); 375 | } 376 | 377 | match s.curr_byte()? { 378 | b'{' => break, 379 | b',' => {} 380 | _ => { 381 | s.skip_bytes(|c| c != b'{'); 382 | break; 383 | } 384 | } 385 | } 386 | 387 | s.try_consume_byte(b'{'); 388 | 389 | let declarations = consume_declarations(s)?; 390 | for rule in rules.iter_mut().skip(start_rule_idx) { 391 | rule.declarations = declarations.clone(); 392 | } 393 | 394 | s.try_consume_byte(b'}'); 395 | 396 | Ok(()) 397 | } 398 | 399 | fn consume_block(s: &mut Stream<'_>) { 400 | s.try_consume_byte(b'{'); 401 | consume_until_block_end(s); 402 | } 403 | 404 | fn consume_until_block_end(s: &mut Stream<'_>) { 405 | // Block can have nested blocks, so we have to check for matching braces. 406 | // We simply counting the number of opening braces, which is incorrect, 407 | // since `{` can be inside a string, but it's fine for majority of the cases. 408 | 409 | let mut braces = 0; 410 | while !s.at_end() { 411 | match s.curr_byte_unchecked() { 412 | b'{' => { 413 | braces += 1; 414 | } 415 | b'}' => { 416 | if braces == 0 { 417 | break; 418 | } else { 419 | braces -= 1; 420 | } 421 | } 422 | _ => {} 423 | } 424 | 425 | s.advance(1); 426 | } 427 | 428 | s.try_consume_byte(b'}'); 429 | } 430 | 431 | fn consume_declarations<'a>(s: &mut Stream<'a>) -> Result>, Error> { 432 | let mut declarations = Vec::new(); 433 | 434 | while !s.at_end() && s.curr_byte() != Ok(b'}') { 435 | match consume_declaration(s) { 436 | Ok(declaration) => declarations.push(declaration), 437 | Err(_) => { 438 | consume_until_block_end(s); 439 | break; 440 | } 441 | } 442 | } 443 | 444 | Ok(declarations) 445 | } 446 | 447 | /// A declaration tokenizer. 448 | /// 449 | /// Tokenizer will stop at the first invalid token. 450 | /// 451 | /// # Example 452 | /// 453 | /// ``` 454 | /// use simplecss::{DeclarationTokenizer, Declaration}; 455 | /// 456 | /// let mut t = DeclarationTokenizer::from("background: url(\"img.png\"); color:red !important"); 457 | /// assert_eq!(t.next().unwrap(), Declaration { name: "background", value: "url(\"img.png\")", important: false }); 458 | /// assert_eq!(t.next().unwrap(), Declaration { name: "color", value: "red", important: true }); 459 | /// ``` 460 | pub struct DeclarationTokenizer<'a> { 461 | stream: Stream<'a>, 462 | } 463 | 464 | impl<'a> From<&'a str> for DeclarationTokenizer<'a> { 465 | fn from(text: &'a str) -> Self { 466 | DeclarationTokenizer { 467 | stream: Stream::from(text), 468 | } 469 | } 470 | } 471 | 472 | impl<'a> Iterator for DeclarationTokenizer<'a> { 473 | type Item = Declaration<'a>; 474 | 475 | fn next(&mut self) -> Option { 476 | let _ = self.stream.skip_spaces_and_comments(); 477 | 478 | if self.stream.at_end() { 479 | return None; 480 | } 481 | 482 | match consume_declaration(&mut self.stream) { 483 | Ok(v) => Some(v), 484 | Err(_) => { 485 | self.stream.jump_to_end(); 486 | None 487 | } 488 | } 489 | } 490 | } 491 | 492 | fn consume_declaration<'a>(s: &mut Stream<'a>) -> Result, Error> { 493 | s.skip_spaces_and_comments()?; 494 | 495 | // Parse name. 496 | 497 | // https://snook.ca/archives/html_and_css/targetting_ie7 498 | if s.curr_byte() == Ok(b'*') { 499 | s.advance(1); 500 | } 501 | 502 | let name = s.consume_ident()?; 503 | 504 | s.skip_spaces_and_comments()?; 505 | s.consume_byte(b':')?; 506 | s.skip_spaces_and_comments()?; 507 | 508 | // Parse value. 509 | let start = s.pos(); 510 | let mut end = s.pos(); 511 | while consume_term(s).is_ok() { 512 | end = s.pos(); 513 | s.skip_spaces_and_comments()?; 514 | } 515 | let value = s.slice_range(start, end).trim(); 516 | 517 | s.skip_spaces_and_comments()?; 518 | 519 | // Check for `important`. 520 | let mut important = false; 521 | if s.curr_byte() == Ok(b'!') { 522 | s.advance(1); 523 | s.skip_spaces_and_comments()?; 524 | if s.slice_tail().starts_with("important") { 525 | s.advance(9); 526 | important = true; 527 | } 528 | } 529 | 530 | s.skip_spaces_and_comments()?; 531 | 532 | while s.curr_byte() == Ok(b';') { 533 | s.advance(1); 534 | s.skip_spaces_and_comments()?; 535 | } 536 | 537 | s.skip_spaces_and_comments()?; 538 | 539 | if value.is_empty() { 540 | return Err(Error::InvalidValue(s.gen_text_pos_from(start))); 541 | } 542 | 543 | Ok(Declaration { 544 | name, 545 | value, 546 | important, 547 | }) 548 | } 549 | 550 | fn consume_term(s: &mut Stream<'_>) -> Result<(), Error> { 551 | fn consume_digits(s: &mut Stream<'_>) { 552 | while let Ok(b'0'..=b'9') = s.curr_byte() { 553 | s.advance(1); 554 | } 555 | } 556 | 557 | match s.curr_byte()? { 558 | b'#' => { 559 | s.advance(1); 560 | match s.consume_ident() { 561 | Ok(_) => {} 562 | Err(_) => { 563 | // Try consume as a hex color. 564 | while let Ok(c) = s.curr_byte() { 565 | match c { 566 | b'0'..=b'9' | b'a'..=b'f' | b'A'..=b'F' => s.advance(1), 567 | _ => break, 568 | } 569 | } 570 | } 571 | } 572 | } 573 | b'+' | b'-' | b'0'..=b'9' | b'.' => { 574 | // Consume number. 575 | 576 | s.advance(1); 577 | consume_digits(s); 578 | if s.curr_byte() == Ok(b'.') { 579 | s.advance(1); 580 | consume_digits(s); 581 | } 582 | 583 | if s.curr_byte() == Ok(b'%') { 584 | s.advance(1); 585 | } else { 586 | // Consume suffix if any. 587 | let _ = s.consume_ident(); 588 | } 589 | } 590 | b'\'' | b'"' => { 591 | s.consume_string()?; 592 | } 593 | b',' => { 594 | s.advance(1); 595 | } 596 | _ => { 597 | let _ = s.consume_ident()?; 598 | 599 | // Consume function. 600 | if s.curr_byte() == Ok(b'(') { 601 | s.skip_bytes(|c| c != b')'); 602 | s.consume_byte(b')')?; 603 | } 604 | } 605 | } 606 | 607 | Ok(()) 608 | } 609 | -------------------------------------------------------------------------------- /src/selector.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2019 the SimpleCSS Authors 2 | // SPDX-License-Identifier: Apache-2.0 OR MIT 3 | 4 | use alloc::{vec, vec::Vec}; 5 | use core::fmt; 6 | 7 | use log::warn; 8 | 9 | use crate::stream::Stream; 10 | use crate::Error; 11 | 12 | /// An attribute selector operator. 13 | #[derive(Clone, Copy, PartialEq, Debug)] 14 | pub enum AttributeOperator<'a> { 15 | /// `[attr]` 16 | Exists, 17 | /// `[attr=value]` 18 | Matches(&'a str), 19 | /// `[attr~=value]` 20 | Contains(&'a str), 21 | /// `[attr|=value]` 22 | StartsWith(&'a str), 23 | } 24 | 25 | impl AttributeOperator<'_> { 26 | /// Checks that value is matching the operator. 27 | pub fn matches(&self, value: &str) -> bool { 28 | match *self { 29 | AttributeOperator::Exists => true, 30 | AttributeOperator::Matches(v) => value == v, 31 | AttributeOperator::Contains(v) => value.split(' ').any(|s| s == v), 32 | AttributeOperator::StartsWith(v) => { 33 | // exactly `v` or beginning with `v` immediately followed by `-` 34 | if value == v { 35 | true 36 | } else if value.starts_with(v) { 37 | value.get(v.len()..v.len() + 1) == Some("-") 38 | } else { 39 | false 40 | } 41 | } 42 | } 43 | } 44 | } 45 | 46 | /// A pseudo-class. 47 | #[derive(Clone, Copy, PartialEq, Debug)] 48 | #[allow(missing_docs)] 49 | pub enum PseudoClass<'a> { 50 | FirstChild, 51 | Link, 52 | Visited, 53 | Hover, 54 | Active, 55 | Focus, 56 | Lang(&'a str), 57 | } 58 | 59 | impl fmt::Display for PseudoClass<'_> { 60 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 61 | match self { 62 | PseudoClass::FirstChild => write!(f, "first-child"), 63 | PseudoClass::Link => write!(f, "link"), 64 | PseudoClass::Visited => write!(f, "visited"), 65 | PseudoClass::Hover => write!(f, "hover"), 66 | PseudoClass::Active => write!(f, "active"), 67 | PseudoClass::Focus => write!(f, "focus"), 68 | PseudoClass::Lang(lang) => write!(f, "lang({lang})"), 69 | } 70 | } 71 | } 72 | 73 | /// A trait to query an element node metadata. 74 | pub trait Element: Sized { 75 | /// Returns a parent element. 76 | fn parent_element(&self) -> Option; 77 | 78 | /// Returns a previous sibling element. 79 | fn prev_sibling_element(&self) -> Option; 80 | 81 | /// Checks that the element has a specified local name. 82 | fn has_local_name(&self, name: &str) -> bool; 83 | 84 | /// Checks that the element has a specified attribute. 85 | fn attribute_matches(&self, local_name: &str, operator: AttributeOperator<'_>) -> bool; 86 | 87 | /// Checks that the element matches a specified pseudo-class. 88 | fn pseudo_class_matches(&self, class: PseudoClass<'_>) -> bool; 89 | } 90 | 91 | #[derive(Clone, Copy, PartialEq, Debug)] 92 | enum SimpleSelectorType<'a> { 93 | Type(&'a str), 94 | Universal, 95 | } 96 | 97 | #[derive(Clone, Copy, PartialEq, Debug)] 98 | enum SubSelector<'a> { 99 | Attribute(&'a str, AttributeOperator<'a>), 100 | PseudoClass(PseudoClass<'a>), 101 | } 102 | 103 | #[derive(Clone, Debug)] 104 | struct SimpleSelector<'a> { 105 | kind: SimpleSelectorType<'a>, 106 | subselectors: Vec>, 107 | } 108 | 109 | #[derive(Clone, Copy, PartialEq, Debug)] 110 | enum Combinator { 111 | None, 112 | Descendant, 113 | Child, 114 | AdjacentSibling, 115 | } 116 | 117 | #[derive(Clone, Debug)] 118 | struct Component<'a> { 119 | /// A combinator that precede the selector. 120 | combinator: Combinator, 121 | selector: SimpleSelector<'a>, 122 | } 123 | 124 | /// A selector. 125 | #[derive(Clone, Debug)] 126 | pub struct Selector<'a> { 127 | components: Vec>, 128 | } 129 | 130 | impl<'a> Selector<'a> { 131 | /// Parses a selector from a string. 132 | /// 133 | /// Will log any errors as a warnings. 134 | /// 135 | /// Parsing will be stopped at EOF, `,` or `{`. 136 | pub fn parse(text: &'a str) -> Option { 137 | parse(text).0 138 | } 139 | 140 | /// Compute the selector's specificity. 141 | /// 142 | /// Cf. . 143 | pub fn specificity(&self) -> [u8; 3] { 144 | let mut spec = [0u8; 3]; 145 | 146 | for selector in self.components.iter().map(|c| &c.selector) { 147 | if matches!(selector.kind, SimpleSelectorType::Type(_)) { 148 | spec[2] = spec[2].saturating_add(1); 149 | } 150 | 151 | for sub in &selector.subselectors { 152 | match sub { 153 | SubSelector::Attribute("id", _) => spec[0] = spec[0].saturating_add(1), 154 | _ => spec[1] = spec[1].saturating_add(1), 155 | } 156 | } 157 | } 158 | 159 | spec 160 | } 161 | 162 | /// Checks that the provided element matches the current selector. 163 | pub fn matches(&self, element: &E) -> bool { 164 | assert!(!self.components.is_empty(), "selector must not be empty"); 165 | assert_eq!( 166 | self.components[0].combinator, 167 | Combinator::None, 168 | "the first component must not have a combinator" 169 | ); 170 | 171 | self.matches_impl(self.components.len() - 1, element) 172 | } 173 | 174 | fn matches_impl(&self, idx: usize, element: &E) -> bool { 175 | let component = &self.components[idx]; 176 | 177 | if !match_selector(&component.selector, element) { 178 | return false; 179 | } 180 | 181 | match component.combinator { 182 | Combinator::Descendant => { 183 | let mut parent = element.parent_element(); 184 | while let Some(e) = parent { 185 | if self.matches_impl(idx - 1, &e) { 186 | return true; 187 | } 188 | 189 | parent = e.parent_element(); 190 | } 191 | 192 | false 193 | } 194 | Combinator::Child => { 195 | if let Some(parent) = element.parent_element() { 196 | if self.matches_impl(idx - 1, &parent) { 197 | return true; 198 | } 199 | } 200 | 201 | false 202 | } 203 | Combinator::AdjacentSibling => { 204 | if let Some(prev) = element.prev_sibling_element() { 205 | if self.matches_impl(idx - 1, &prev) { 206 | return true; 207 | } 208 | } 209 | 210 | false 211 | } 212 | Combinator::None => true, 213 | } 214 | } 215 | } 216 | 217 | fn match_selector(selector: &SimpleSelector<'_>, element: &E) -> bool { 218 | if let SimpleSelectorType::Type(ident) = selector.kind { 219 | if !element.has_local_name(ident) { 220 | return false; 221 | } 222 | } 223 | 224 | for sub in &selector.subselectors { 225 | match sub { 226 | SubSelector::Attribute(name, operator) => { 227 | if !element.attribute_matches(name, *operator) { 228 | return false; 229 | } 230 | } 231 | SubSelector::PseudoClass(class) => { 232 | if !element.pseudo_class_matches(*class) { 233 | return false; 234 | } 235 | } 236 | } 237 | } 238 | 239 | true 240 | } 241 | 242 | pub(crate) fn parse(text: &str) -> (Option>, usize) { 243 | let mut components: Vec> = Vec::new(); 244 | let mut combinator = Combinator::None; 245 | 246 | let mut tokenizer = SelectorTokenizer::from(text); 247 | for token in &mut tokenizer { 248 | let mut add_sub = |sub| { 249 | if combinator == Combinator::None && !components.is_empty() { 250 | if let Some(ref mut component) = components.last_mut() { 251 | component.selector.subselectors.push(sub); 252 | } 253 | } else { 254 | components.push(Component { 255 | selector: SimpleSelector { 256 | kind: SimpleSelectorType::Universal, 257 | subselectors: vec![sub], 258 | }, 259 | combinator, 260 | }); 261 | 262 | combinator = Combinator::None; 263 | } 264 | }; 265 | 266 | let token = match token { 267 | Ok(t) => t, 268 | Err(e) => { 269 | warn!("Selector parsing failed cause {}.", e); 270 | return (None, tokenizer.stream.pos()); 271 | } 272 | }; 273 | 274 | match token { 275 | SelectorToken::UniversalSelector => { 276 | components.push(Component { 277 | selector: SimpleSelector { 278 | kind: SimpleSelectorType::Universal, 279 | subselectors: Vec::new(), 280 | }, 281 | combinator, 282 | }); 283 | 284 | combinator = Combinator::None; 285 | } 286 | SelectorToken::TypeSelector(ident) => { 287 | components.push(Component { 288 | selector: SimpleSelector { 289 | kind: SimpleSelectorType::Type(ident), 290 | subselectors: Vec::new(), 291 | }, 292 | combinator, 293 | }); 294 | 295 | combinator = Combinator::None; 296 | } 297 | SelectorToken::ClassSelector(ident) => { 298 | add_sub(SubSelector::Attribute( 299 | "class", 300 | AttributeOperator::Contains(ident), 301 | )); 302 | } 303 | SelectorToken::IdSelector(id) => { 304 | add_sub(SubSelector::Attribute("id", AttributeOperator::Matches(id))); 305 | } 306 | SelectorToken::AttributeSelector(name, op) => { 307 | add_sub(SubSelector::Attribute(name, op)); 308 | } 309 | SelectorToken::PseudoClass(ident) => { 310 | let class = match ident { 311 | "first-child" => PseudoClass::FirstChild, 312 | "link" => PseudoClass::Link, 313 | "visited" => PseudoClass::Visited, 314 | "hover" => PseudoClass::Hover, 315 | "active" => PseudoClass::Active, 316 | "focus" => PseudoClass::Focus, 317 | _ => { 318 | warn!("':{}' is not supported. Selector skipped.", ident); 319 | return (None, tokenizer.stream.pos()); 320 | } 321 | }; 322 | 323 | // TODO: duplicates 324 | // TODO: order 325 | 326 | add_sub(SubSelector::PseudoClass(class)); 327 | } 328 | SelectorToken::LangPseudoClass(lang) => { 329 | add_sub(SubSelector::PseudoClass(PseudoClass::Lang(lang))); 330 | } 331 | SelectorToken::DescendantCombinator => { 332 | combinator = Combinator::Descendant; 333 | } 334 | SelectorToken::ChildCombinator => { 335 | combinator = Combinator::Child; 336 | } 337 | SelectorToken::AdjacentCombinator => { 338 | combinator = Combinator::AdjacentSibling; 339 | } 340 | } 341 | } 342 | 343 | if components.is_empty() { 344 | (None, tokenizer.stream.pos()) 345 | } else if components[0].combinator != Combinator::None { 346 | debug_assert_eq!( 347 | components[0].combinator, 348 | Combinator::None, 349 | "the first component must not have a combinator" 350 | ); 351 | 352 | (None, tokenizer.stream.pos()) 353 | } else { 354 | (Some(Selector { components }), tokenizer.stream.pos()) 355 | } 356 | } 357 | 358 | impl fmt::Display for Selector<'_> { 359 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 360 | for component in &self.components { 361 | match component.combinator { 362 | Combinator::Descendant => write!(f, " ")?, 363 | Combinator::Child => write!(f, " > ")?, 364 | Combinator::AdjacentSibling => write!(f, " + ")?, 365 | Combinator::None => {} 366 | } 367 | 368 | match component.selector.kind { 369 | SimpleSelectorType::Universal => write!(f, "*")?, 370 | SimpleSelectorType::Type(ident) => write!(f, "{ident}")?, 371 | }; 372 | 373 | for sel in &component.selector.subselectors { 374 | match sel { 375 | SubSelector::Attribute(name, operator) => { 376 | match operator { 377 | AttributeOperator::Exists => { 378 | write!(f, "[{name}]")?; 379 | } 380 | AttributeOperator::Matches(value) => { 381 | write!(f, "[{name}='{value}']")?; 382 | } 383 | AttributeOperator::Contains(value) => { 384 | write!(f, "[{name}~='{value}']")?; 385 | } 386 | AttributeOperator::StartsWith(value) => { 387 | write!(f, "[{name}|='{value}']")?; 388 | } 389 | }; 390 | } 391 | SubSelector::PseudoClass(class) => write!(f, ":{class}")?, 392 | } 393 | } 394 | } 395 | 396 | Ok(()) 397 | } 398 | } 399 | 400 | /// A selector token. 401 | #[derive(Clone, Copy, PartialEq, Debug)] 402 | pub enum SelectorToken<'a> { 403 | /// `*` 404 | UniversalSelector, 405 | 406 | /// `div` 407 | TypeSelector(&'a str), 408 | 409 | /// `.class` 410 | ClassSelector(&'a str), 411 | 412 | /// `#id` 413 | IdSelector(&'a str), 414 | 415 | /// `[color=red]` 416 | AttributeSelector(&'a str, AttributeOperator<'a>), 417 | 418 | /// `:first-child` 419 | PseudoClass(&'a str), 420 | 421 | /// `:lang(en)` 422 | LangPseudoClass(&'a str), 423 | 424 | /// `a b` 425 | DescendantCombinator, 426 | 427 | /// `a > b` 428 | ChildCombinator, 429 | 430 | /// `a + b` 431 | AdjacentCombinator, 432 | } 433 | 434 | /// A selector tokenizer. 435 | /// 436 | /// # Example 437 | /// 438 | /// ``` 439 | /// use simplecss::{SelectorTokenizer, SelectorToken}; 440 | /// 441 | /// let mut t = SelectorTokenizer::from("div > p:first-child"); 442 | /// assert_eq!(t.next().unwrap().unwrap(), SelectorToken::TypeSelector("div")); 443 | /// assert_eq!(t.next().unwrap().unwrap(), SelectorToken::ChildCombinator); 444 | /// assert_eq!(t.next().unwrap().unwrap(), SelectorToken::TypeSelector("p")); 445 | /// assert_eq!(t.next().unwrap().unwrap(), SelectorToken::PseudoClass("first-child")); 446 | /// assert!(t.next().is_none()); 447 | /// ``` 448 | pub struct SelectorTokenizer<'a> { 449 | stream: Stream<'a>, 450 | after_combinator: bool, 451 | finished: bool, 452 | } 453 | 454 | impl<'a> From<&'a str> for SelectorTokenizer<'a> { 455 | fn from(text: &'a str) -> Self { 456 | SelectorTokenizer { 457 | stream: Stream::from(text), 458 | after_combinator: true, 459 | finished: false, 460 | } 461 | } 462 | } 463 | 464 | impl<'a> Iterator for SelectorTokenizer<'a> { 465 | type Item = Result, Error>; 466 | 467 | fn next(&mut self) -> Option { 468 | if self.finished || self.stream.at_end() { 469 | if self.after_combinator { 470 | self.after_combinator = false; 471 | return Some(Err(Error::SelectorMissing)); 472 | } 473 | 474 | return None; 475 | } 476 | 477 | macro_rules! try2 { 478 | ($e:expr) => { 479 | match $e { 480 | Ok(v) => v, 481 | Err(e) => { 482 | self.finished = true; 483 | return Some(Err(e)); 484 | } 485 | } 486 | }; 487 | } 488 | 489 | match self.stream.curr_byte_unchecked() { 490 | b'*' => { 491 | if !self.after_combinator { 492 | self.finished = true; 493 | return Some(Err(Error::UnexpectedSelector)); 494 | } 495 | 496 | self.after_combinator = false; 497 | self.stream.advance(1); 498 | Some(Ok(SelectorToken::UniversalSelector)) 499 | } 500 | b'#' => { 501 | self.after_combinator = false; 502 | self.stream.advance(1); 503 | let ident = try2!(self.stream.consume_ident()); 504 | Some(Ok(SelectorToken::IdSelector(ident))) 505 | } 506 | b'.' => { 507 | self.after_combinator = false; 508 | self.stream.advance(1); 509 | let ident = try2!(self.stream.consume_ident()); 510 | Some(Ok(SelectorToken::ClassSelector(ident))) 511 | } 512 | b'[' => { 513 | self.after_combinator = false; 514 | self.stream.advance(1); 515 | let ident = try2!(self.stream.consume_ident()); 516 | 517 | let op = match try2!(self.stream.curr_byte()) { 518 | b']' => AttributeOperator::Exists, 519 | b'=' => { 520 | self.stream.advance(1); 521 | let value = try2!(self.stream.consume_string()); 522 | AttributeOperator::Matches(value) 523 | } 524 | b'~' => { 525 | self.stream.advance(1); 526 | try2!(self.stream.consume_byte(b'=')); 527 | let value = try2!(self.stream.consume_string()); 528 | AttributeOperator::Contains(value) 529 | } 530 | b'|' => { 531 | self.stream.advance(1); 532 | try2!(self.stream.consume_byte(b'=')); 533 | let value = try2!(self.stream.consume_string()); 534 | AttributeOperator::StartsWith(value) 535 | } 536 | _ => { 537 | self.finished = true; 538 | return Some(Err(Error::InvalidAttributeSelector)); 539 | } 540 | }; 541 | 542 | try2!(self.stream.consume_byte(b']')); 543 | 544 | Some(Ok(SelectorToken::AttributeSelector(ident, op))) 545 | } 546 | b':' => { 547 | self.after_combinator = false; 548 | self.stream.advance(1); 549 | let ident = try2!(self.stream.consume_ident()); 550 | 551 | if ident == "lang" { 552 | try2!(self.stream.consume_byte(b'(')); 553 | let lang = self.stream.consume_bytes(|c| c != b')').trim(); 554 | try2!(self.stream.consume_byte(b')')); 555 | 556 | if lang.is_empty() { 557 | self.finished = true; 558 | return Some(Err(Error::InvalidLanguagePseudoClass)); 559 | } 560 | 561 | Some(Ok(SelectorToken::LangPseudoClass(lang))) 562 | } else { 563 | Some(Ok(SelectorToken::PseudoClass(ident))) 564 | } 565 | } 566 | b'>' => { 567 | if self.after_combinator { 568 | self.after_combinator = false; 569 | self.finished = true; 570 | return Some(Err(Error::UnexpectedCombinator)); 571 | } 572 | 573 | self.stream.advance(1); 574 | self.after_combinator = true; 575 | Some(Ok(SelectorToken::ChildCombinator)) 576 | } 577 | b'+' => { 578 | if self.after_combinator { 579 | self.after_combinator = false; 580 | self.finished = true; 581 | return Some(Err(Error::UnexpectedCombinator)); 582 | } 583 | 584 | self.stream.advance(1); 585 | self.after_combinator = true; 586 | Some(Ok(SelectorToken::AdjacentCombinator)) 587 | } 588 | b' ' | b'\t' | b'\n' | b'\r' | b'\x0C' => { 589 | self.stream.skip_spaces(); 590 | 591 | if self.after_combinator { 592 | return self.next(); 593 | } 594 | 595 | while self.stream.curr_byte() == Ok(b'/') { 596 | try2!(self.stream.skip_comment()); 597 | self.stream.skip_spaces(); 598 | } 599 | 600 | match self.stream.curr_byte() { 601 | Ok(b'>') | Ok(b'+') | Ok(b',') | Ok(b'{') | Err(_) => self.next(), 602 | _ => { 603 | if self.after_combinator { 604 | self.after_combinator = false; 605 | self.finished = true; 606 | return Some(Err(Error::UnexpectedSelector)); 607 | } 608 | 609 | self.after_combinator = true; 610 | Some(Ok(SelectorToken::DescendantCombinator)) 611 | } 612 | } 613 | } 614 | b'/' => { 615 | if self.stream.next_byte() == Ok(b'*') { 616 | try2!(self.stream.skip_comment()); 617 | } else { 618 | self.finished = true; 619 | } 620 | 621 | self.next() 622 | } 623 | b',' | b'{' => { 624 | self.finished = true; 625 | self.next() 626 | } 627 | _ => { 628 | let ident = try2!(self.stream.consume_ident()); 629 | 630 | if !self.after_combinator { 631 | self.finished = true; 632 | return Some(Err(Error::UnexpectedSelector)); 633 | } 634 | 635 | self.after_combinator = false; 636 | Some(Ok(SelectorToken::TypeSelector(ident))) 637 | } 638 | } 639 | } 640 | } 641 | --------------------------------------------------------------------------------