@dvaji/cheerox 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +74 -0
- package/Cargo.lock +1091 -0
- package/Cargo.toml +47 -0
- package/LICENSE +21 -0
- package/README.md +121 -2
- package/RELEASING.md +114 -0
- package/THIRD_PARTY_NOTICES.md +7581 -0
- package/build.rs +3 -0
- package/cheerox.darwin-arm64.node +0 -0
- package/cheerox.darwin-x64.node +0 -0
- package/cheerox.linux-arm64-gnu.node +0 -0
- package/cheerox.linux-arm64-musl.node +0 -0
- package/cheerox.linux-x64-gnu.node +0 -0
- package/cheerox.linux-x64-musl.node +0 -0
- package/cheerox.win32-arm64-msvc.node +0 -0
- package/cheerox.win32-x64-msvc.node +0 -0
- package/dist/cheerio-compat-BKkfJEAr.mjs +1648 -0
- package/dist/cheerio-compat-BKkfJEAr.mjs.map +1 -0
- package/dist/cheerio-compat-DRLPzBdX.d.mts +88 -0
- package/dist/cheerio-compat-DRsINTwV.d.cts +88 -0
- package/dist/cheerio-compat-DRsINTwV.d.ts +88 -0
- package/dist/cheerio-compat-DcnYK9OB.cjs +1719 -0
- package/dist/cheerio-compat-DcnYK9OB.cjs.map +1 -0
- package/dist/cheerio-compat.cjs +18 -0
- package/dist/cheerio-compat.d.cts +2 -0
- package/dist/cheerio-compat.d.mts +2 -0
- package/dist/cheerio-compat.d.ts +2 -0
- package/dist/cheerio-compat.mjs +1 -0
- package/dist/index.cjs +13 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.mjs +53 -0
- package/dist/index.mjs.map +1 -0
- package/dist/legacy-parse.cjs +5 -0
- package/dist/legacy-parse.d.mts +8 -0
- package/dist/legacy-parse.d.ts +7 -0
- package/dist/legacy-parse.mjs +1 -0
- package/dist/modern-B7hyW4cI.d.cts +102 -0
- package/dist/modern-B7hyW4cI.d.ts +102 -0
- package/dist/modern-CHQGP0Yt.cjs +2858 -0
- package/dist/modern-CHQGP0Yt.cjs.map +1 -0
- package/dist/modern-CJvSD_y5.d.mts +102 -0
- package/dist/modern-CvXcTn82.mjs +2769 -0
- package/dist/modern-CvXcTn82.mjs.map +1 -0
- package/dist/modern.cjs +19 -0
- package/dist/modern.d.cts +3 -0
- package/dist/modern.d.mts +3 -0
- package/dist/modern.d.ts +3 -0
- package/dist/modern.mjs +3 -0
- package/dist/nodes-jbWWhssj.cjs +4210 -0
- package/dist/nodes-jbWWhssj.cjs.map +1 -0
- package/dist/nodes-sYxb5qyZ.mjs +3803 -0
- package/dist/nodes-sYxb5qyZ.mjs.map +1 -0
- package/dist/slim.cjs +16 -0
- package/dist/slim.cjs.map +1 -0
- package/dist/slim.d.cts +6 -0
- package/dist/slim.d.mts +6 -0
- package/dist/slim.d.ts +6 -0
- package/dist/slim.mjs +12 -0
- package/dist/slim.mjs.map +1 -0
- package/dist/types-CA01Zg3r.d.cts +526 -0
- package/dist/types-CA01Zg3r.d.mts +526 -0
- package/dist/types-CA01Zg3r.d.ts +526 -0
- package/dist/utils.cjs +31 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +8 -0
- package/dist/utils.d.mts +8 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.mjs +25 -0
- package/dist/utils.mjs.map +1 -0
- package/native/src/arena.rs +2109 -0
- package/native/src/attributes.rs +946 -0
- package/native/src/codec.rs +298 -0
- package/native/src/diagnostics.rs +1249 -0
- package/native/src/document_order.rs +711 -0
- package/native/src/extraction.rs +321 -0
- package/native/src/gc.rs +175 -0
- package/native/src/hook_sink.rs +801 -0
- package/native/src/hook_stream.rs +513 -0
- package/native/src/id_hash.rs +42 -0
- package/native/src/immutable.rs +333 -0
- package/native/src/lib.rs +4120 -0
- package/native/src/location.rs +392 -0
- package/native/src/markup_tokenizer.rs +668 -0
- package/native/src/runtime.rs +368 -0
- package/native/src/selector.rs +1596 -0
- package/native/src/sink.rs +274 -0
- package/native/src/snapshot.rs +672 -0
- package/native/src/stream.rs +334 -0
- package/native/src/tokenizer.rs +581 -0
- package/native/src/tracker.rs +232 -0
- package/native/vendor/README.md +35 -0
- package/native/vendor/html5ever-0.35.0/.cargo_vcs_info.json +6 -0
- package/native/vendor/html5ever-0.35.0/Cargo.lock +828 -0
- package/native/vendor/html5ever-0.35.0/Cargo.toml +85 -0
- package/native/vendor/html5ever-0.35.0/LICENSE-APACHE +201 -0
- package/native/vendor/html5ever-0.35.0/LICENSE-MIT +25 -0
- package/native/vendor/html5ever-0.35.0/README.md +60 -0
- package/native/vendor/html5ever-0.35.0/benches/html5ever.rs +86 -0
- package/native/vendor/html5ever-0.35.0/data/bench/lipsum-zh.html +19 -0
- package/native/vendor/html5ever-0.35.0/data/bench/lipsum.html +40 -0
- package/native/vendor/html5ever-0.35.0/data/bench/medium-fragment.html +24 -0
- package/native/vendor/html5ever-0.35.0/data/bench/small-fragment.html +7 -0
- package/native/vendor/html5ever-0.35.0/data/bench/strong.html +1 -0
- package/native/vendor/html5ever-0.35.0/data/bench/tiny-fragment.html +1 -0
- package/native/vendor/html5ever-0.35.0/examples/arena.rs +356 -0
- package/native/vendor/html5ever-0.35.0/examples/capi/tokenize.c +74 -0
- package/native/vendor/html5ever-0.35.0/examples/noop-tokenize.rs +47 -0
- package/native/vendor/html5ever-0.35.0/examples/noop-tree-builder.rs +132 -0
- package/native/vendor/html5ever-0.35.0/examples/print-tree-actions.rs +185 -0
- package/native/vendor/html5ever-0.35.0/examples/tokenize.rs +111 -0
- package/native/vendor/html5ever-0.35.0/src/driver.rs +148 -0
- package/native/vendor/html5ever-0.35.0/src/lib.rs +30 -0
- package/native/vendor/html5ever-0.35.0/src/macros.rs +34 -0
- package/native/vendor/html5ever-0.35.0/src/serialize/mod.rs +255 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/char_ref/mod.rs +530 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/interface.rs +99 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/mod.rs +2628 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/states.rs +97 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/data.rs +170 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/mod.rs +1848 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/rules.rs +1494 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/tag_sets.rs +114 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/types.rs +91 -0
- package/native/vendor/html5ever-0.35.0/src/util/str.rs +52 -0
- package/native/vendor/html5gum-0.8.4/Cargo.lock +1006 -0
- package/native/vendor/html5gum-0.8.4/Cargo.toml +109 -0
- package/native/vendor/html5gum-0.8.4/LICENSE +19 -0
- package/native/vendor/html5gum-0.8.4/README.md +110 -0
- package/native/vendor/html5gum-0.8.4/benches/files/0050.html +2131 -0
- package/native/vendor/html5gum-0.8.4/benches/files/6D30.html +1004 -0
- package/native/vendor/html5gum-0.8.4/benches/files.rs +32 -0
- package/native/vendor/html5gum-0.8.4/benches/patterns.rs +31 -0
- package/native/vendor/html5gum-0.8.4/src/arrayvec.rs +29 -0
- package/native/vendor/html5gum-0.8.4/src/char_validator.rs +134 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/callback.rs +676 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/default.rs +207 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/emitter.rs +344 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/html5ever.rs +306 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/mod.rs +25 -0
- package/native/vendor/html5gum-0.8.4/src/entities.rs +3342 -0
- package/native/vendor/html5gum-0.8.4/src/error.rs +96 -0
- package/native/vendor/html5gum-0.8.4/src/htmlstring.rs +130 -0
- package/native/vendor/html5gum-0.8.4/src/lib.rs +66 -0
- package/native/vendor/html5gum-0.8.4/src/machine.rs +2136 -0
- package/native/vendor/html5gum-0.8.4/src/machine_helper.rs +277 -0
- package/native/vendor/html5gum-0.8.4/src/read_helper.rs +244 -0
- package/native/vendor/html5gum-0.8.4/src/reader.rs +440 -0
- package/native/vendor/html5gum-0.8.4/src/span.rs +98 -0
- package/native/vendor/html5gum-0.8.4/src/state.rs +16 -0
- package/native/vendor/html5gum-0.8.4/src/testutils.rs +71 -0
- package/native/vendor/html5gum-0.8.4/src/tokenizer.rs +189 -0
- package/native/vendor/html5gum-0.8.4/src/utils.rs +88 -0
- package/native/vendor/markup5ever-0.35.0/.cargo_vcs_info.json +6 -0
- package/native/vendor/markup5ever-0.35.0/Cargo.toml +45 -0
- package/native/vendor/markup5ever-0.35.0/LICENSE-APACHE +201 -0
- package/native/vendor/markup5ever-0.35.0/LICENSE-MIT +25 -0
- package/native/vendor/markup5ever-0.35.0/interface/mod.rs +422 -0
- package/native/vendor/markup5ever-0.35.0/interface/tree_builder.rs +280 -0
- package/native/vendor/markup5ever-0.35.0/lib.rs +59 -0
- package/native/vendor/markup5ever-0.35.0/serialize.rs +77 -0
- package/native/vendor/markup5ever-0.35.0/util/buffer_queue.rs +333 -0
- package/native/vendor/markup5ever-0.35.0/util/smallcharset.rs +88 -0
- package/native/vendor/scraper-0.24.0/Cargo.toml +52 -0
- package/native/vendor/scraper-0.24.0/LICENSE +15 -0
- package/native/vendor/scraper-0.24.0/src/element_ref/element.rs +212 -0
- package/native/vendor/scraper-0.24.0/src/element_ref/mod.rs +236 -0
- package/native/vendor/scraper-0.24.0/src/element_ref/serializable.rs +15 -0
- package/native/vendor/scraper-0.24.0/src/error/utils.rs +91 -0
- package/native/vendor/scraper-0.24.0/src/error.rs +118 -0
- package/native/vendor/scraper-0.24.0/src/html/mod.rs +270 -0
- package/native/vendor/scraper-0.24.0/src/html/serializable.rs +27 -0
- package/native/vendor/scraper-0.24.0/src/html/tree_sink.rs +285 -0
- package/native/vendor/scraper-0.24.0/src/lib.rs +180 -0
- package/native/vendor/scraper-0.24.0/src/node/serializable.rs +52 -0
- package/native/vendor/scraper-0.24.0/src/node.rs +465 -0
- package/native/vendor/scraper-0.24.0/src/selectable.rs +70 -0
- package/native/vendor/scraper-0.24.0/src/selector.rs +290 -0
- package/package.json +167 -4
- package/scripts/check-native.mjs +15 -0
- package/scripts/check-release.mjs +234 -0
- package/scripts/fix-declarations.mjs +39 -0
- package/scripts/generate-notices.mjs +194 -0
- package/scripts/notices/README.md +13 -0
- package/scripts/notices/cheerio-MIT.txt +21 -0
- package/scripts/notices/html5ever-AUTHORS.txt +20 -0
- package/scripts/notices/html5ever-COPYRIGHT.txt +8 -0
- package/scripts/notices/html5ever-LICENSE-APACHE.txt +201 -0
- package/scripts/notices/html5ever-LICENSE-MIT.txt +25 -0
- package/scripts/notices/htmlparser2-MIT.txt +18 -0
- package/scripts/notices/napi-rs-MIT.txt +43 -0
- package/scripts/notices/neon-MIT.txt +19 -0
- package/scripts/notices/runtime-01c266bced4a434da005.txt +3 -0
- package/scripts/notices/runtime-0a54e647fe54104658b5.txt +14 -0
- package/scripts/notices/runtime-0b74dfa0bcee5c420c6b.txt +25 -0
- package/scripts/notices/runtime-0d542e0c8804e39aa7f3.txt +177 -0
- package/scripts/notices/runtime-0f96a83840e146e43c0e.txt +21 -0
- package/scripts/notices/runtime-123a331b5dbf04c30097.txt +25 -0
- package/scripts/notices/runtime-172020dbfd5b53a226df.txt +34 -0
- package/scripts/notices/runtime-209fbbe0ad52d9235e37.txt +26 -0
- package/scripts/notices/runtime-23f18e03dc49df91622f.txt +23 -0
- package/scripts/notices/runtime-268872b9816f90fd8e85.txt +220 -0
- package/scripts/notices/runtime-35242e7a83f69875e6ed.txt +176 -0
- package/scripts/notices/runtime-378f5840b258e2779c39.txt +25 -0
- package/scripts/notices/runtime-4108245a1f2df9d4e94d.txt +25 -0
- package/scripts/notices/runtime-4455bf75a91154108304.txt +21 -0
- package/scripts/notices/runtime-553fffcd9b1cb158bc3e.txt +13 -0
- package/scripts/notices/runtime-62c7a1e35f56406896d7.txt +176 -0
- package/scripts/notices/runtime-6485b8ed310d3f0340bf.txt +25 -0
- package/scripts/notices/runtime-69fef7b0f322a6555415.txt +201 -0
- package/scripts/notices/runtime-6df43f6f4b5d4587f3d8.txt +187 -0
- package/scripts/notices/runtime-799e9ca9d179295ef372.txt +24 -0
- package/scripts/notices/runtime-7b63ecd5f1902af1b637.txt +25 -0
- package/scripts/notices/runtime-7bc7d5de2a0b793d4e1a.txt +21 -0
- package/scripts/notices/runtime-7e12e5df4bae12cb2158.txt +24 -0
- package/scripts/notices/runtime-861399f8c21c042b1105.txt +12 -0
- package/scripts/notices/runtime-8ada45cd9f843acf64e4.txt +201 -0
- package/scripts/notices/runtime-90eb64f0279b0d9432ac.txt +12 -0
- package/scripts/notices/runtime-a60eea817514531668d7.txt +201 -0
- package/scripts/notices/runtime-a7c936ff1ed8fa340172.txt +25 -0
- package/scripts/notices/runtime-ab6eec6caf0fa5775e41.txt +275 -0
- package/scripts/notices/runtime-b1181a40b2a7b25cf66f.txt +19 -0
- package/scripts/notices/runtime-b71bd43a069ca0641a9e.txt +25 -0
- package/scripts/notices/runtime-c16f8dcf1a368b83be78.txt +201 -0
- package/scripts/notices/runtime-c2cfccb812fe482101a8.txt +21 -0
- package/scripts/notices/runtime-e99d88d232bf57d70f0f.txt +25 -0
- package/scripts/notices/runtime-f32fb3b417a194167cfa.txt +9 -0
- package/scripts/notices/runtime-f5062c9a188d81dfe66b.txt +39 -0
- package/scripts/notices/runtime-f9070a832833fa7488fd.txt +4 -0
- package/scripts/notices/runtime-fab3dd6bdab226f1c086.txt +373 -0
- package/scripts/notices/runtime-fba3318d0c600380177e.txt +62 -0
- package/scripts/notices/runtime-ff8f68cb076caf8cefe7.txt +25 -0
- package/scripts/notices/runtime-ff92bed461f50338dd70.txt +74 -0
- package/scripts/notices/runtime.json +1632 -0
- package/scripts/notices/sources.json +561 -0
- package/scripts/notices/xray-MIT.txt +21 -0
- package/src/attribute-object.ts +71 -0
- package/src/attributes.ts +308 -0
- package/src/cheerio-compat/css.ts +58 -0
- package/src/cheerio-compat/render.ts +106 -0
- package/src/cheerio-compat/selectors.ts +51 -0
- package/src/cheerio-compat/types.ts +52 -0
- package/src/cheerio-compat.ts +2055 -0
- package/src/codec.ts +96 -0
- package/src/dom.ts +87 -0
- package/src/extract-plan.ts +97 -0
- package/src/extract.ts +322 -0
- package/src/hooks.ts +369 -0
- package/src/index.ts +48 -0
- package/src/io.ts +491 -0
- package/src/match.ts +207 -0
- package/src/modern.ts +259 -0
- package/src/native.ts +149 -0
- package/src/nodes.ts +831 -0
- package/src/parse.ts +53 -0
- package/src/parser-types.ts +157 -0
- package/src/router.ts +1129 -0
- package/src/selection.ts +682 -0
- package/src/slim.ts +12 -0
- package/src/snapshot.ts +291 -0
- package/src/transport.ts +113 -0
- package/src/types.ts +186 -0
- package/src/utils.ts +29 -0
- package/src/workers.ts +21 -0
- package/tsconfig.json +15 -0
- package/tsdown.config.mjs +17 -0
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
// Copyright 2014-2017 The html5ever Project Developers. See the
|
|
2
|
+
// COPYRIGHT file at the top-level directory of this distribution.
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
5
|
+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
6
|
+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
7
|
+
// option. This file may not be copied, modified, or distributed
|
|
8
|
+
// except according to those terms.
|
|
9
|
+
|
|
10
|
+
//! The `BufferQueue` struct and helper types.
|
|
11
|
+
//!
|
|
12
|
+
//! This type is designed for the efficient parsing of string data, especially where many
|
|
13
|
+
//! significant characters are from the ascii range 0-63. This includes, for example, important
|
|
14
|
+
//! characters in xml/html parsing.
|
|
15
|
+
//!
|
|
16
|
+
//! Good and predictable performance is achieved by avoiding allocation where possible (a.k.a. zero
|
|
17
|
+
//! copy).
|
|
18
|
+
//!
|
|
19
|
+
//! [`BufferQueue`]: struct.BufferQueue.html
|
|
20
|
+
|
|
21
|
+
use std::{
|
|
22
|
+
cell::{RefCell, RefMut},
|
|
23
|
+
collections::VecDeque,
|
|
24
|
+
mem,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
use tendril::StrTendril;
|
|
28
|
+
|
|
29
|
+
pub use self::SetResult::{FromSet, NotFromSet};
|
|
30
|
+
use crate::util::smallcharset::SmallCharSet;
|
|
31
|
+
|
|
32
|
+
/// Result from [`pop_except_from`] containing either a character from a [`SmallCharSet`], or a
|
|
33
|
+
/// string buffer of characters not from the set.
|
|
34
|
+
///
|
|
35
|
+
/// [`pop_except_from`]: struct.BufferQueue.html#method.pop_except_from
|
|
36
|
+
/// [`SmallCharSet`]: ../struct.SmallCharSet.html
|
|
37
|
+
#[derive(PartialEq, Eq, Debug)]
|
|
38
|
+
pub enum SetResult {
|
|
39
|
+
/// A character from the `SmallCharSet`.
|
|
40
|
+
FromSet(char),
|
|
41
|
+
/// A string buffer containing no characters from the `SmallCharSet`.
|
|
42
|
+
NotFromSet(StrTendril),
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// A queue of owned string buffers, which supports incrementally consuming characters.
|
|
46
|
+
///
|
|
47
|
+
/// Internally it uses [`VecDeque`] and has the same complexity properties.
|
|
48
|
+
///
|
|
49
|
+
/// [`VecDeque`]: https://doc.rust-lang.org/std/collections/struct.VecDeque.html
|
|
50
|
+
#[derive(Clone, Debug)]
|
|
51
|
+
pub struct BufferQueue {
|
|
52
|
+
/// Buffers to process.
|
|
53
|
+
buffers: RefCell<VecDeque<StrTendril>>,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
impl Default for BufferQueue {
|
|
57
|
+
/// Create an empty BufferQueue.
|
|
58
|
+
#[inline]
|
|
59
|
+
fn default() -> Self {
|
|
60
|
+
Self {
|
|
61
|
+
buffers: RefCell::new(VecDeque::with_capacity(16)),
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
impl BufferQueue {
|
|
67
|
+
/// Returns whether the queue is empty.
|
|
68
|
+
#[inline]
|
|
69
|
+
pub fn is_empty(&self) -> bool {
|
|
70
|
+
self.buffers.borrow().is_empty()
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/// Get the buffer at the beginning of the queue.
|
|
74
|
+
#[inline]
|
|
75
|
+
pub fn pop_front(&self) -> Option<StrTendril> {
|
|
76
|
+
self.buffers.borrow_mut().pop_front()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/// Add a buffer to the beginning of the queue.
|
|
80
|
+
///
|
|
81
|
+
/// If the buffer is empty, it will be skipped.
|
|
82
|
+
pub fn push_front(&self, buf: StrTendril) {
|
|
83
|
+
if buf.len32() == 0 {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
self.buffers.borrow_mut().push_front(buf);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// Add a buffer to the end of the queue.
|
|
90
|
+
///
|
|
91
|
+
/// If the buffer is empty, it will be skipped.
|
|
92
|
+
pub fn push_back(&self, buf: StrTendril) {
|
|
93
|
+
if buf.len32() == 0 {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
self.buffers.borrow_mut().push_back(buf);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/// Look at the next available character without removing it, if the queue is not empty.
|
|
100
|
+
pub fn peek(&self) -> Option<char> {
|
|
101
|
+
debug_assert!(
|
|
102
|
+
!self.buffers.borrow().iter().any(|el| el.len32() == 0),
|
|
103
|
+
"invariant \"all buffers in the queue are non-empty\" failed"
|
|
104
|
+
);
|
|
105
|
+
self.buffers
|
|
106
|
+
.borrow()
|
|
107
|
+
.front()
|
|
108
|
+
.map(|b| b.chars().next().unwrap())
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/// Pops and returns either a single character from the given set, or
|
|
112
|
+
/// a buffer of characters none of which are in the set.
|
|
113
|
+
///
|
|
114
|
+
/// # Examples
|
|
115
|
+
///
|
|
116
|
+
/// ```
|
|
117
|
+
/// # #[macro_use] extern crate markup5ever;
|
|
118
|
+
/// # #[macro_use] extern crate tendril;
|
|
119
|
+
/// # fn main() {
|
|
120
|
+
/// use markup5ever::buffer_queue::{BufferQueue, SetResult};
|
|
121
|
+
///
|
|
122
|
+
/// let mut queue = BufferQueue::default();
|
|
123
|
+
/// queue.push_back(format_tendril!(r#"<some_tag attr="text">SomeText</some_tag>"#));
|
|
124
|
+
/// let set = small_char_set!(b'<' b'>' b' ' b'=' b'"' b'/');
|
|
125
|
+
/// let tag = format_tendril!("some_tag");
|
|
126
|
+
/// let attr = format_tendril!("attr");
|
|
127
|
+
/// let attr_val = format_tendril!("text");
|
|
128
|
+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::FromSet('<')));
|
|
129
|
+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::NotFromSet(tag)));
|
|
130
|
+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::FromSet(' ')));
|
|
131
|
+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::NotFromSet(attr)));
|
|
132
|
+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::FromSet('=')));
|
|
133
|
+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::FromSet('"')));
|
|
134
|
+
/// assert_eq!(queue.pop_except_from(set), Some(SetResult::NotFromSet(attr_val)));
|
|
135
|
+
/// // ...
|
|
136
|
+
/// # }
|
|
137
|
+
/// ```
|
|
138
|
+
pub fn pop_except_from(&self, set: SmallCharSet) -> Option<SetResult> {
|
|
139
|
+
let (result, now_empty) = match self.buffers.borrow_mut().front_mut() {
|
|
140
|
+
None => (None, false),
|
|
141
|
+
Some(buf) => {
|
|
142
|
+
let n = set.nonmember_prefix_len(buf);
|
|
143
|
+
if n > 0 {
|
|
144
|
+
let out;
|
|
145
|
+
unsafe {
|
|
146
|
+
out = buf.unsafe_subtendril(0, n);
|
|
147
|
+
buf.unsafe_pop_front(n);
|
|
148
|
+
}
|
|
149
|
+
(Some(NotFromSet(out)), buf.is_empty())
|
|
150
|
+
} else {
|
|
151
|
+
let c = buf.pop_front_char().expect("empty buffer in queue");
|
|
152
|
+
(Some(FromSet(c)), buf.is_empty())
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// Unborrow self for this part.
|
|
158
|
+
if now_empty {
|
|
159
|
+
self.buffers.borrow_mut().pop_front();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
result
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/// Consume bytes matching the pattern, using a custom comparison function `eq`.
|
|
166
|
+
///
|
|
167
|
+
/// Returns `Some(true)` if there is a match, `Some(false)` if there is no match, or `None` if
|
|
168
|
+
/// it wasn't possible to know (more data is needed).
|
|
169
|
+
///
|
|
170
|
+
/// The custom comparison function is used elsewhere to compare ascii-case-insensitively.
|
|
171
|
+
///
|
|
172
|
+
/// # Examples
|
|
173
|
+
///
|
|
174
|
+
/// ```
|
|
175
|
+
/// # extern crate markup5ever;
|
|
176
|
+
/// # #[macro_use] extern crate tendril;
|
|
177
|
+
/// # fn main() {
|
|
178
|
+
/// use markup5ever::buffer_queue::BufferQueue;
|
|
179
|
+
///
|
|
180
|
+
/// let mut queue = BufferQueue::default();
|
|
181
|
+
/// queue.push_back(format_tendril!("testtext"));
|
|
182
|
+
/// let test_str = "test";
|
|
183
|
+
/// assert_eq!(queue.eat("test", |&a, &b| a == b), Some(true));
|
|
184
|
+
/// assert_eq!(queue.eat("text", |&a, &b| a == b), Some(true));
|
|
185
|
+
/// assert!(queue.is_empty());
|
|
186
|
+
/// # }
|
|
187
|
+
/// ```
|
|
188
|
+
pub fn eat<F: Fn(&u8, &u8) -> bool>(&self, pat: &str, eq: F) -> Option<bool> {
|
|
189
|
+
let mut buffers_exhausted = 0;
|
|
190
|
+
let mut consumed_from_last = 0;
|
|
191
|
+
|
|
192
|
+
self.buffers.borrow().front()?;
|
|
193
|
+
|
|
194
|
+
for pattern_byte in pat.bytes() {
|
|
195
|
+
if buffers_exhausted >= self.buffers.borrow().len() {
|
|
196
|
+
return None;
|
|
197
|
+
}
|
|
198
|
+
let buf = &self.buffers.borrow()[buffers_exhausted];
|
|
199
|
+
|
|
200
|
+
if !eq(&buf.as_bytes()[consumed_from_last], &pattern_byte) {
|
|
201
|
+
return Some(false);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
consumed_from_last += 1;
|
|
205
|
+
if consumed_from_last >= buf.len() {
|
|
206
|
+
buffers_exhausted += 1;
|
|
207
|
+
consumed_from_last = 0;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// We have a match. Commit changes to the BufferQueue.
|
|
212
|
+
for _ in 0..buffers_exhausted {
|
|
213
|
+
self.buffers.borrow_mut().pop_front();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
match self.buffers.borrow_mut().front_mut() {
|
|
217
|
+
None => assert_eq!(consumed_from_last, 0),
|
|
218
|
+
Some(ref mut buf) => buf.pop_front(consumed_from_last as u32),
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
Some(true)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/// Get the next character if one is available, removing it from the queue.
|
|
225
|
+
///
|
|
226
|
+
/// This function manages the buffers, removing them as they become empty.
|
|
227
|
+
pub fn next(&self) -> Option<char> {
|
|
228
|
+
let (result, now_empty) = match self.buffers.borrow_mut().front_mut() {
|
|
229
|
+
None => (None, false),
|
|
230
|
+
Some(buf) => {
|
|
231
|
+
let c = buf.pop_front_char().expect("empty buffer in queue");
|
|
232
|
+
(Some(c), buf.is_empty())
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
if now_empty {
|
|
237
|
+
self.buffers.borrow_mut().pop_front();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
result
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
pub fn replace_with(&self, other: BufferQueue) {
|
|
244
|
+
let _ = mem::replace(&mut *self.buffers.borrow_mut(), other.buffers.take());
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
pub fn swap_with(&self, other: &BufferQueue) {
|
|
248
|
+
mem::swap(
|
|
249
|
+
&mut *self.buffers.borrow_mut(),
|
|
250
|
+
&mut *other.buffers.borrow_mut(),
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/// Return a mutable reference to the first tendril in the queue.
|
|
255
|
+
pub fn peek_front_chunk_mut(&self) -> Option<RefMut<'_, StrTendril>> {
|
|
256
|
+
let buffers = self.buffers.borrow_mut();
|
|
257
|
+
if buffers.is_empty() {
|
|
258
|
+
return None;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
let front_buffer = RefMut::map(buffers, |buffers| {
|
|
262
|
+
buffers.front_mut().expect("there is at least one buffer")
|
|
263
|
+
});
|
|
264
|
+
Some(front_buffer)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
#[cfg(test)]
|
|
269
|
+
#[allow(non_snake_case)]
|
|
270
|
+
mod test {
|
|
271
|
+
use tendril::SliceExt;
|
|
272
|
+
|
|
273
|
+
use super::BufferQueue;
|
|
274
|
+
use super::SetResult::{FromSet, NotFromSet};
|
|
275
|
+
|
|
276
|
+
#[test]
|
|
277
|
+
fn smoke_test() {
|
|
278
|
+
let bq = BufferQueue::default();
|
|
279
|
+
assert_eq!(bq.peek(), None);
|
|
280
|
+
assert_eq!(bq.next(), None);
|
|
281
|
+
|
|
282
|
+
bq.push_back("abc".to_tendril());
|
|
283
|
+
assert_eq!(bq.peek(), Some('a'));
|
|
284
|
+
assert_eq!(bq.next(), Some('a'));
|
|
285
|
+
assert_eq!(bq.peek(), Some('b'));
|
|
286
|
+
assert_eq!(bq.peek(), Some('b'));
|
|
287
|
+
assert_eq!(bq.next(), Some('b'));
|
|
288
|
+
assert_eq!(bq.peek(), Some('c'));
|
|
289
|
+
assert_eq!(bq.next(), Some('c'));
|
|
290
|
+
assert_eq!(bq.peek(), None);
|
|
291
|
+
assert_eq!(bq.next(), None);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
#[test]
|
|
295
|
+
fn can_unconsume() {
|
|
296
|
+
let bq = BufferQueue::default();
|
|
297
|
+
bq.push_back("abc".to_tendril());
|
|
298
|
+
assert_eq!(bq.next(), Some('a'));
|
|
299
|
+
|
|
300
|
+
bq.push_front("xy".to_tendril());
|
|
301
|
+
assert_eq!(bq.next(), Some('x'));
|
|
302
|
+
assert_eq!(bq.next(), Some('y'));
|
|
303
|
+
assert_eq!(bq.next(), Some('b'));
|
|
304
|
+
assert_eq!(bq.next(), Some('c'));
|
|
305
|
+
assert_eq!(bq.next(), None);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
#[test]
|
|
309
|
+
fn can_pop_except_set() {
|
|
310
|
+
let bq = BufferQueue::default();
|
|
311
|
+
bq.push_back("abc&def".to_tendril());
|
|
312
|
+
let pop = || bq.pop_except_from(small_char_set!('&'));
|
|
313
|
+
assert_eq!(pop(), Some(NotFromSet("abc".to_tendril())));
|
|
314
|
+
assert_eq!(pop(), Some(FromSet('&')));
|
|
315
|
+
assert_eq!(pop(), Some(NotFromSet("def".to_tendril())));
|
|
316
|
+
assert_eq!(pop(), None);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
#[test]
|
|
320
|
+
fn can_eat() {
|
|
321
|
+
// This is not very comprehensive. We rely on the tokenizer
|
|
322
|
+
// integration tests for more thorough testing with many
|
|
323
|
+
// different input buffer splits.
|
|
324
|
+
let bq = BufferQueue::default();
|
|
325
|
+
bq.push_back("a".to_tendril());
|
|
326
|
+
bq.push_back("bc".to_tendril());
|
|
327
|
+
assert_eq!(bq.eat("abcd", u8::eq_ignore_ascii_case), None);
|
|
328
|
+
assert_eq!(bq.eat("ax", u8::eq_ignore_ascii_case), Some(false));
|
|
329
|
+
assert_eq!(bq.eat("ab", u8::eq_ignore_ascii_case), Some(true));
|
|
330
|
+
assert_eq!(bq.next(), Some('c'));
|
|
331
|
+
assert_eq!(bq.next(), None);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Copyright 2014-2017 The html5ever Project Developers. See the
|
|
2
|
+
// COPYRIGHT file at the top-level directory of this distribution.
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
5
|
+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
6
|
+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
7
|
+
// option. This file may not be copied, modified, or distributed
|
|
8
|
+
// except according to those terms.
|
|
9
|
+
|
|
10
|
+
//! This module contains a single struct [`SmallCharSet`]. See its documentation for details.
|
|
11
|
+
//!
|
|
12
|
+
//! [`SmallCharSet`]: struct.SmallCharSet.html
|
|
13
|
+
|
|
14
|
+
/// Represents a set of "small characters", those with Unicode scalar
|
|
15
|
+
/// values less than 64.
|
|
16
|
+
///
|
|
17
|
+
/// This is stored as a bitmap, with 1 bit for each value.
|
|
18
|
+
#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
|
|
19
|
+
pub struct SmallCharSet {
|
|
20
|
+
pub bits: u64,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
impl SmallCharSet {
|
|
24
|
+
/// Checks whether a character (u8 value below 64) is stored in the SmallCharSet.
|
|
25
|
+
///
|
|
26
|
+
/// # Examples
|
|
27
|
+
///
|
|
28
|
+
/// ```ignore
|
|
29
|
+
/// # use markup5ever::SmallCharSet;
|
|
30
|
+
/// let set = SmallCharSet {
|
|
31
|
+
/// bits: 0b00000000_01000000_00000100_00000000_00000000_00000000_00010000_00000000
|
|
32
|
+
/// };
|
|
33
|
+
/// assert!(set.contains(64));
|
|
34
|
+
/// assert!(set.contains(b'6')); // `b'6'` is the same as 64u8
|
|
35
|
+
/// ```
|
|
36
|
+
#[inline]
|
|
37
|
+
fn contains(&self, n: u8) -> bool {
|
|
38
|
+
0 != (self.bits & (1 << (n as usize)))
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/// Count the number of bytes of characters at the beginning of `buf` which are not in the set.
|
|
42
|
+
///
|
|
43
|
+
/// This functionality is used in [`BufferQueue::pop_except_from`].
|
|
44
|
+
///
|
|
45
|
+
/// # Examples
|
|
46
|
+
///
|
|
47
|
+
/// ```
|
|
48
|
+
/// # #[macro_use] extern crate markup5ever;
|
|
49
|
+
/// # fn main() {
|
|
50
|
+
/// let set = small_char_set!(48 49 50); // '0' '1' '2'
|
|
51
|
+
/// // `test` is 4 chars, 😁 is 4 chars, then we meet a character in the set
|
|
52
|
+
/// let test_str = "test😁01232afd";
|
|
53
|
+
/// assert_eq!(set.nonmember_prefix_len(test_str), 8);
|
|
54
|
+
/// # }
|
|
55
|
+
/// ```
|
|
56
|
+
///
|
|
57
|
+
/// [`BufferQueue::pop_except_from`]: buffer_queue/struct.BufferQueue.html#method.pop_except_from
|
|
58
|
+
pub fn nonmember_prefix_len(&self, buf: &str) -> u32 {
|
|
59
|
+
let mut n = 0;
|
|
60
|
+
for b in buf.bytes() {
|
|
61
|
+
if b >= 64 || !self.contains(b) {
|
|
62
|
+
n += 1;
|
|
63
|
+
} else {
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
n
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#[cfg(test)]
|
|
72
|
+
mod test {
|
|
73
|
+
#[test]
|
|
74
|
+
fn nonmember_prefix() {
|
|
75
|
+
for &c in ['&', '\0'].iter() {
|
|
76
|
+
for x in 0..48u32 {
|
|
77
|
+
for y in 0..48u32 {
|
|
78
|
+
let mut s = "x".repeat(x as usize);
|
|
79
|
+
s.push(c);
|
|
80
|
+
s.push_str(&"x".repeat(y as usize));
|
|
81
|
+
let set = small_char_set!('&' '\0');
|
|
82
|
+
|
|
83
|
+
assert_eq!(x, set.nonmember_prefix_len(&s));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Vendored from crates.io scraper 0.24.0.
|
|
2
|
+
#
|
|
3
|
+
# Local changes versus the published crate, matching cheerox's needs:
|
|
4
|
+
# - `Attributes` is an insertion-ordered vector-backed map instead of
|
|
5
|
+
# `indexmap::IndexMap`, removing per-element hashing and index allocation
|
|
6
|
+
# from HTML tree construction.
|
|
7
|
+
# - The `main`, `deterministic` and `default` features, the `getopts` and
|
|
8
|
+
# `indexmap` dependencies, the binary and the examples are removed.
|
|
9
|
+
[package]
|
|
10
|
+
edition = "2021"
|
|
11
|
+
name = "scraper"
|
|
12
|
+
version = "0.24.0"
|
|
13
|
+
license = "ISC"
|
|
14
|
+
repository = "https://github.com/causal-agent/scraper"
|
|
15
|
+
description = "HTML parsing and querying with CSS selectors (vendored, vector-backed attributes)"
|
|
16
|
+
build = false
|
|
17
|
+
autolib = false
|
|
18
|
+
autobins = false
|
|
19
|
+
autoexamples = false
|
|
20
|
+
autotests = false
|
|
21
|
+
autobenches = false
|
|
22
|
+
|
|
23
|
+
[lib]
|
|
24
|
+
name = "scraper"
|
|
25
|
+
path = "src/lib.rs"
|
|
26
|
+
|
|
27
|
+
[features]
|
|
28
|
+
atomic = []
|
|
29
|
+
errors = []
|
|
30
|
+
serde = ["dep:serde"]
|
|
31
|
+
|
|
32
|
+
[dependencies.cssparser]
|
|
33
|
+
version = "0.35.0"
|
|
34
|
+
|
|
35
|
+
[dependencies.ego-tree]
|
|
36
|
+
version = "0.10.0"
|
|
37
|
+
|
|
38
|
+
[dependencies.html5ever]
|
|
39
|
+
version = "0.35.0"
|
|
40
|
+
|
|
41
|
+
[dependencies.precomputed-hash]
|
|
42
|
+
version = "0.1.1"
|
|
43
|
+
|
|
44
|
+
[dependencies.selectors]
|
|
45
|
+
version = "0.31.0"
|
|
46
|
+
|
|
47
|
+
[dependencies.serde]
|
|
48
|
+
version = "1.0.218"
|
|
49
|
+
optional = true
|
|
50
|
+
|
|
51
|
+
[dependencies.tendril]
|
|
52
|
+
version = "0.4.3"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Copyright © 2016, June McEnroe <june@causal.agency>
|
|
2
|
+
Copyright © 2017, Vivek Kushwaha <yoursvivek@gmail.com>
|
|
3
|
+
Copyright © 2024-2025, rust-scraper Contributors
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
use html5ever::Namespace;
|
|
2
|
+
use selectors::{
|
|
3
|
+
attr::{AttrSelectorOperation, CaseSensitivity, NamespaceConstraint},
|
|
4
|
+
bloom::BloomFilter,
|
|
5
|
+
matching, Element, OpaqueElement,
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
use super::ElementRef;
|
|
9
|
+
use crate::selector::{CssLocalName, CssString, NonTSPseudoClass, PseudoElement, Simple};
|
|
10
|
+
|
|
11
|
+
/// Note: will never match against non-tree-structure pseudo-classes.
|
|
12
|
+
impl Element for ElementRef<'_> {
|
|
13
|
+
type Impl = Simple;
|
|
14
|
+
|
|
15
|
+
fn opaque(&self) -> OpaqueElement {
|
|
16
|
+
OpaqueElement::new(self.node.value())
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
fn parent_element(&self) -> Option<Self> {
|
|
20
|
+
self.parent().and_then(ElementRef::wrap)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
fn parent_node_is_shadow_root(&self) -> bool {
|
|
24
|
+
false
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
fn containing_shadow_host(&self) -> Option<Self> {
|
|
28
|
+
None
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
fn is_pseudo_element(&self) -> bool {
|
|
32
|
+
false
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fn is_part(&self, _name: &CssLocalName) -> bool {
|
|
36
|
+
false
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
fn is_same_type(&self, other: &Self) -> bool {
|
|
40
|
+
self.value().name == other.value().name
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
fn imported_part(&self, _: &CssLocalName) -> Option<CssLocalName> {
|
|
44
|
+
None
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
fn prev_sibling_element(&self) -> Option<Self> {
|
|
48
|
+
self.prev_siblings()
|
|
49
|
+
.find(|sibling| sibling.value().is_element())
|
|
50
|
+
.map(ElementRef::new)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fn next_sibling_element(&self) -> Option<Self> {
|
|
54
|
+
self.next_siblings()
|
|
55
|
+
.find(|sibling| sibling.value().is_element())
|
|
56
|
+
.map(ElementRef::new)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
fn first_element_child(&self) -> Option<Self> {
|
|
60
|
+
self.children()
|
|
61
|
+
.find(|child| child.value().is_element())
|
|
62
|
+
.map(ElementRef::new)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
fn is_html_element_in_html_document(&self) -> bool {
|
|
66
|
+
// FIXME: Is there more to this?
|
|
67
|
+
self.value().name.ns == ns!(html)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
fn has_local_name(&self, name: &CssLocalName) -> bool {
|
|
71
|
+
self.value().name.local == name.0
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
fn has_namespace(&self, namespace: &Namespace) -> bool {
|
|
75
|
+
&self.value().name.ns == namespace
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fn attr_matches(
|
|
79
|
+
&self,
|
|
80
|
+
ns: &NamespaceConstraint<&Namespace>,
|
|
81
|
+
local_name: &CssLocalName,
|
|
82
|
+
operation: &AttrSelectorOperation<&CssString>,
|
|
83
|
+
) -> bool {
|
|
84
|
+
self.value().attrs.iter().any(|(key, value)| {
|
|
85
|
+
!matches!(*ns, NamespaceConstraint::Specific(url) if *url != key.ns)
|
|
86
|
+
&& local_name.0 == key.local
|
|
87
|
+
&& operation.eval_str(value)
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn match_non_ts_pseudo_class(
|
|
92
|
+
&self,
|
|
93
|
+
_pc: &NonTSPseudoClass,
|
|
94
|
+
_context: &mut matching::MatchingContext<'_, Self::Impl>,
|
|
95
|
+
) -> bool {
|
|
96
|
+
false
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
fn match_pseudo_element(
|
|
100
|
+
&self,
|
|
101
|
+
_pe: &PseudoElement,
|
|
102
|
+
_context: &mut matching::MatchingContext<Self::Impl>,
|
|
103
|
+
) -> bool {
|
|
104
|
+
false
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
fn is_link(&self) -> bool {
|
|
108
|
+
self.value().name() == "link"
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
fn is_html_slot_element(&self) -> bool {
|
|
112
|
+
true
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
fn has_id(&self, id: &CssLocalName, case_sensitivity: CaseSensitivity) -> bool {
|
|
116
|
+
match self.value().id() {
|
|
117
|
+
Some(val) => case_sensitivity.eq(id.0.as_bytes(), val.as_bytes()),
|
|
118
|
+
None => false,
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
fn has_class(&self, name: &CssLocalName, case_sensitivity: CaseSensitivity) -> bool {
|
|
123
|
+
self.value().has_class(&name.0, case_sensitivity)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
fn has_custom_state(&self, _name: &CssLocalName) -> bool {
|
|
127
|
+
false
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
fn is_empty(&self) -> bool {
|
|
131
|
+
!self
|
|
132
|
+
.children()
|
|
133
|
+
.any(|child| child.value().is_element() || child.value().is_text())
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
fn is_root(&self) -> bool {
|
|
137
|
+
self.parent()
|
|
138
|
+
.is_some_and(|parent| parent.value().is_document())
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
fn apply_selector_flags(&self, _flags: matching::ElementSelectorFlags) {}
|
|
142
|
+
|
|
143
|
+
fn add_element_unique_hashes(&self, _filter: &mut BloomFilter) -> bool {
|
|
144
|
+
// FIXME: Do we want to add `self.node.id()` here?
|
|
145
|
+
false
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
#[cfg(test)]
|
|
150
|
+
mod tests {
|
|
151
|
+
use crate::html::Html;
|
|
152
|
+
use crate::selector::{CssLocalName, Selector};
|
|
153
|
+
use selectors::attr::CaseSensitivity;
|
|
154
|
+
use selectors::Element;
|
|
155
|
+
|
|
156
|
+
#[test]
|
|
157
|
+
fn test_has_id() {
|
|
158
|
+
let html = "<p id='link_id_456'>hey there</p>";
|
|
159
|
+
let fragment = Html::parse_fragment(html);
|
|
160
|
+
let sel = Selector::parse("p").unwrap();
|
|
161
|
+
|
|
162
|
+
let element = fragment.select(&sel).next().unwrap();
|
|
163
|
+
assert!(element.has_id(
|
|
164
|
+
&CssLocalName::from("link_id_456"),
|
|
165
|
+
CaseSensitivity::CaseSensitive
|
|
166
|
+
));
|
|
167
|
+
|
|
168
|
+
let html = "<p>hey there</p>";
|
|
169
|
+
let fragment = Html::parse_fragment(html);
|
|
170
|
+
let element = fragment.select(&sel).next().unwrap();
|
|
171
|
+
assert!(!element.has_id(
|
|
172
|
+
&CssLocalName::from("any_link_id"),
|
|
173
|
+
CaseSensitivity::CaseSensitive
|
|
174
|
+
));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
#[test]
|
|
178
|
+
fn test_is_link() {
|
|
179
|
+
let html = "<link href='https://www.example.com'>";
|
|
180
|
+
let fragment = Html::parse_fragment(html);
|
|
181
|
+
let sel = Selector::parse("link").unwrap();
|
|
182
|
+
let element = fragment.select(&sel).next().unwrap();
|
|
183
|
+
assert!(element.is_link());
|
|
184
|
+
|
|
185
|
+
let html = "<p>hey there</p>";
|
|
186
|
+
let fragment = Html::parse_fragment(html);
|
|
187
|
+
let sel = Selector::parse("p").unwrap();
|
|
188
|
+
let element = fragment.select(&sel).next().unwrap();
|
|
189
|
+
assert!(!element.is_link());
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
#[test]
|
|
193
|
+
fn test_has_class() {
|
|
194
|
+
let html = "<p class='my_class'>hey there</p>";
|
|
195
|
+
let fragment = Html::parse_fragment(html);
|
|
196
|
+
let sel = Selector::parse("p").unwrap();
|
|
197
|
+
let element = fragment.select(&sel).next().unwrap();
|
|
198
|
+
assert!(element.has_class(
|
|
199
|
+
&CssLocalName::from("my_class"),
|
|
200
|
+
CaseSensitivity::CaseSensitive
|
|
201
|
+
));
|
|
202
|
+
|
|
203
|
+
let html = "<p>hey there</p>";
|
|
204
|
+
let fragment = Html::parse_fragment(html);
|
|
205
|
+
let sel = Selector::parse("p").unwrap();
|
|
206
|
+
let element = fragment.select(&sel).next().unwrap();
|
|
207
|
+
assert!(!element.has_class(
|
|
208
|
+
&CssLocalName::from("my_class"),
|
|
209
|
+
CaseSensitivity::CaseSensitive
|
|
210
|
+
));
|
|
211
|
+
}
|
|
212
|
+
}
|