From 4a2d8974bbdacc4a799d1c867bf7ef5aec5c72a0 Mon Sep 17 00:00:00 2001 From: upstream Date: Fri, 2 Oct 2026 16:23:03 +0200 Subject: [PATCH] Upstream Pagefind/pagefind state of the touched files before c0afa1bc49 --- pagefind/src/fossick/parser.rs | 124 +-------------------------------- 1 file changed, 2 insertions(+), 122 deletions(-) diff --git a/pagefind/src/fossick/parser.rs b/pagefind/src/fossick/parser.rs index aaaeba5..ac0136a 100644 --- a/pagefind/src/fossick/parser.rs +++ b/pagefind/src/fossick/parser.rs @@ -154,41 +154,7 @@ impl<'a> DomParser<'a> { .join(", "); let mut anchor_counter = 0; - // Config rules run before every other handler, so the attributes they set are - // read by the handlers below exactly as if the page had carried them itself. - let rule_handlers = options - .rules - .iter() - .filter_map(|rule| match rule.selector.parse::() { - Ok(_) => { - let attributes = rule.attributes.clone(); - Some(element!(rule.selector.clone(), move |el| { - for (name, value) in attributes.iter() { - // An attribute written in the HTML is more specific than a - // rule matching a whole selector, so the page wins. - if el.has_attribute(name) { - continue; - } - if el.set_attribute(name, value).is_err() { - // Only an invalid attribute name can fail here, and that - // is reported once when the rules are read. - } - } - Ok(()) - })) - } - Err(error) => { - options.logger.warn(format!( - "Ignoring the rule selector {:?}, which Pagefind can't parse: {error}. Indexing will continue without it.", - rule.selector - )); - None - } - }) - .collect::>(); - let rewriter = HtmlRewriter::new( - rule_handlers.into_iter().chain( vec![ enclose! { (data) element!("html", move |el| { let mut data = data.borrow_mut(); @@ -597,7 +563,8 @@ impl<'a> DomParser<'a> { } Ok(()) })}, - ]) + ] + .into_iter() .fold(Settings::new().with_strict(false), |settings, handler| { settings.append_element_content_handler(handler) }), @@ -804,93 +771,6 @@ mod tests { rewriter.wrap() } - fn test_parse_with_rules( - mut input: Vec<&'static str>, - rules: &[(&'static str, &[(&'static str, &'static str)])], - ) -> DomParserResult { - use clap::CommandFactory; - let config_args = vec![twelf::Layer::Clap( - crate::PagefindInboundConfig::command().get_matches_from(vec![ - "pagefind", - "--source", - "not_important", - ]), - )]; - let mut config = - SearchOptions::load(crate::PagefindInboundConfig::with_layers(&config_args).unwrap()) - .unwrap(); - config.rules = rules - .iter() - .map(|(selector, attributes)| crate::options::PagefindRule { - selector: selector.to_string(), - attributes: attributes - .iter() - .map(|(name, value)| (name.to_string(), value.to_string())) - .collect(), - }) - .collect(); - - let mut rewriter = DomParser::new(&config); - input.insert(0, ""); - input.push(""); - for line in input { - let _ = rewriter.write(line.as_bytes()); - } - rewriter.wrap() - } - - #[test] - fn rules_apply_pagefind_attributes_by_selector() { - let data = test_parse_with_rules( - vec!["", "

Kept

"], - &[("#nav", &[("data-pagefind-ignore", "all")])], - ); - - assert_eq!(data.digest, "Kept."); - } - - #[test] - fn rules_can_pick_the_body() { - let data = test_parse_with_rules( - vec![ - "
Chrome
", - "
Real content
", - ], - &[(".main", &[("data-pagefind-body", "")])], - ); - - assert_eq!(data.digest, "Real content."); - } - - #[test] - fn an_attribute_in_the_page_wins_over_a_rule() { - let data = test_parse_with_rules( - vec![ - "

Own weight

", - "

Rule weight

", - ], - &[(".weighted", &[("data-pagefind-weight", "5")])], - ); - - // The first paragraph keeps the weight written in the page, the second one - // takes the weight from the rule, so the rule applies without overriding. - assert!(data.digest.contains("___PAGEFIND_WEIGHT___2 Own weight.")); - assert!(data.digest.contains("___PAGEFIND_WEIGHT___5 Rule weight.")); - } - - #[test] - fn unsupported_rule_selector_does_not_halt_indexing() { - let data = test_parse_with_rules( - vec!["

Kept

", "
Dropped
"], - &[ - (":not(.a b)", &[("data-pagefind-ignore", "all")]), - (".ad", &[("data-pagefind-ignore", "all")]), - ], - ); - - assert_eq!(data.digest, "Kept."); - } - #[test] fn unsupported_exclude_selector_does_not_halt_indexing() { let data = test_parse_with_exclusions(