{"record":{"id":"61f94f2111b4c77c","repo":"Hmbown/CodeWhale","slug":"html-entity-regex","errorCode":null,"errorMessage":"HTML entity regex","messagePattern":"HTML entity regex","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/tools/web/scrape.rs","lineNumber":234,"sourceCode":"    }\n    href.to_string()\n}\n\nfn normalize_text(text: &str) -> String {\n    let stripped = strip_html_tags(text);\n    let decoded = decode_html_entities(&stripped);\n    decoded.split_whitespace().collect::<Vec<_>>().join(\" \")\n}\n\nfn strip_html_tags(text: &str) -> String {\n    get_tag_re().replace_all(text, \"\").to_string()\n}\n\n/// Decode common HTML named and numeric character references.\npub fn decode_html_entities(text: &str) -> String {\n    static ENTITY_RE: OnceLock<Regex> = OnceLock::new();\n    let re = ENTITY_RE.get_or_init(|| {\n        Regex::new(r\"&(?:#(\\d+)|#x([0-9A-Fa-f]+)|([a-zA-Z]+));\").expect(\"HTML entity regex\")\n    });\n\n    re.replace_all(text, |caps: &regex::Captures| {\n        if let Some(dec) = caps.get(1) {\n            return dec\n                .as_str()\n                .parse::<u32>()\n                .ok()\n                .and_then(std::char::from_u32)\n                .unwrap_or('\\u{FFFD}')\n                .to_string();\n        }\n        if let Some(hex) = caps.get(2) {\n            return u32::from_str_radix(hex.as_str(), 16)\n                .ok()\n                .and_then(std::char::from_u32)\n                .unwrap_or('\\u{FFFD}')\n                .to_string();","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/0c42157ee52f9d55af2b506d71b46249910f77d3/crates/tui/src/tools/web/scrape.rs#L216-L252","documentation":"decode_html_entities() compiles its static HTML character-reference regex inside a OnceLock and unwraps the Regex::new result. The pattern is a fixed literal known to be valid, so failure would indicate a corrupted regex string in the source — never a runtime input problem.","triggerScenarios":"Thrown at crates/tui/src/tools/web/scrape.rs:234 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Verify the ENTITY_RE pattern literal was not corrupted by an edit","Add a test that forces the OnceLock initialization so bad patterns fail in CI"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0c42157ee52f9d55af2b506d71b46249910f77d3","analyzedAt":"2026-08-20T21:50:45.477Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}