Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

49.4 KB
Raw Blame
//! HTML — a tolerant hand-rolled tokenizer (no DOM dependency; bounded by a
//! 64MB whole-file cap). Generic extraction rule, no hardcoded names:
//! a dominant `<table>` (≥5 rows, consistent column count, header row) →
//! one record per row with header-derived field names; otherwise one
//! document record {title, headings, body}.
//!
//! Inline JavaScript in a `<script>` block of a document page is also
//! extracted as code symbols — the same definitions a standalone `.js` file
//! would yield — with line numbers in this HTML file (#2356). The page body
//! stays the visible text: script and style source is not copied into it.
//! A dominant table stays one record per row. Its scripts are not symbols:
//! those fields would land in the table's data dataset, and a sample that
//! stopped on the rows would leave them out of the frozen mapping.

use super::{
    emit_document, sanitize_field_name, ExtractStats, FieldOrigin, RawRecord, Sink,
    MAX_RECORDS_PER_FILE, MAX_WHOLE_FILE,
};
use anyhow::Result;
use serde_json::{Map, Value};
use std::path::Path;

/// What the tokenizer reads from one page. The EPUB extractor reads each
/// chapter through [`parse`] and uses `headings` and `body`.
#[derive(Default)]
pub(crate) struct Doc {
    pub(crate) title: String,
    pub(crate) headings: Vec<String>,
    pub(crate) body: String,
    /// `(id, byte offset in body)` for each element carrying an `id` (or an
    /// `<a name>`), in document order: where that element's text begins. EPUB
    /// splits a chapter file at its table-of-contents anchors with these.
    pub(crate) anchors: Vec<(String, usize)>,
    tables: Vec<Vec<Vec<String>>>, // tables → rows → cells
    header_cells: Vec<Vec<bool>>,  // per table: was first row <th>?
    /// Inline JavaScript blocks as `(content_start, content_end)` byte
    /// offsets into the source. External scripts (`src`), non-JS types, and
    /// `<style>` are not recorded. The page body still omits this text.
    scripts: Vec<(usize, usize)>,
}

pub fn extract(path: &Path, gzip: bool, sink: Sink) -> Result<ExtractStats> {
    let mut stats = ExtractStats::default();
    let Some(bytes) = super::read_whole(path, gzip, MAX_WHOLE_FILE)? else {
        stats.junk += 1;
        return Ok(stats);
    };
    let (text, _) = crate::sniff::decode_text(&bytes);
    let mut doc = parse(&text);
    let scripts = std::mem::take(&mut doc.scripts);

    // Dominant-table rule.
    if let Some((rows, first_row_th)) = dominant_table(&doc) {
        let header: Vec<String> = {
            let mut seen = std::collections::HashSet::new();
            rows[0]
                .iter()
                .enumerate()
                .map(|(i, h)| {
                    let mut name = if first_row_th || looks_like_header(&rows[0]) {
                        sanitize_field_name(h)
                    } else {
                        format!("col_{}", i + 1)
                    };
                    while !seen.insert(name.clone()) {
                        name.push('2');
                    }
                    name
                })
                .collect()
        };
        let data_rows: Box<dyn Iterator<Item = (usize, &Vec<String>)>> =
            if first_row_th || looks_like_header(&rows[0]) {
                Box::new(rows.iter().enumerate().skip(1))
            } else {
                Box::new(rows.iter().enumerate())
            };
        for (i, row) in data_rows {
            let mut fields = Map::new();
            for (j, cell) in row.iter().enumerate() {
                if cell.trim().is_empty() {
                    continue;
                }
                let name = header
                    .get(j)
                    .cloned()
                    .unwrap_or_else(|| format!("col_{}", j + 1));
                fields.insert(name, Value::String(cell.trim().to_string()));
            }
            if fields.is_empty() {
                continue;
            }
            stats.records += 1;
            if !sink(RawRecord {
                fields,
                locator: format!("row{i}"),
                group: None,
                origin: FieldOrigin::Data,
            }) {
                return Ok(stats);
            }
        }
        // Scripts stay out of this path. Symbol records share the file's
        // ungrouped sketch, so their extractor fields (`name`, `code`,
        // `defs`, …) would join the table's data dataset. Phase A stops at
        // the sample, often before those records, and phase B would then
        // index fields the frozen mapping does not name.
        drop(scripts);
        return Ok(stats);
    }

    // Document record.
    let title = if doc.title.trim().is_empty() {
        doc.headings
            .first()
            .cloned()
            .unwrap_or_else(|| stem_of(path))
    } else {
        doc.title.trim().to_string()
    };
    let keep_going = emit_document(
        &title,
        &doc.headings,
        doc.body.trim(),
        None,
        MAX_RECORDS_PER_FILE,
        sink,
        &mut stats,
    );
    if keep_going {
        super::code::emit_inline_scripts(path, &text, &scripts, &mut stats, sink);
    }
    Ok(stats)
}

/// Render a page as ONE document — `{title, headings, body}` — even when it
/// has a dominant table. This is how a page whose table was too small to be
/// a dataset of its own is indexed after the fold into the scope's document
/// dataset (`dataset::MIN_DATASET_RECORDS`). The table's rows are already in
/// `body` as ` | `-joined lines, so no cell text is lost. The title comes
/// from `logical_path`, as in `extract_as_document_with_name`.
pub(crate) fn extract_document(
    content_path: &Path,
    logical_path: &Path,
    gzip: bool,
    sink: Sink,
) -> Result<ExtractStats> {
    let mut stats = ExtractStats::default();
    let Some(bytes) = super::read_whole(content_path, gzip, MAX_WHOLE_FILE)? else {
        stats.junk += 1;
        return Ok(stats);
    };
    let (text, _) = crate::sniff::decode_text(&bytes);
    let doc = parse(&text);
    if doc.title.trim().is_empty() && doc.headings.is_empty() && doc.body.trim().is_empty() {
        stats.junk += 1;
        return Ok(stats);
    }
    let title = if doc.title.trim().is_empty() {
        doc.headings
            .first()
            .cloned()
            .unwrap_or_else(|| stem_of(logical_path))
    } else {
        doc.title.trim().to_string()
    };
    emit_document(
        &title,
        &doc.headings,
        doc.body.trim(),
        None,
        MAX_RECORDS_PER_FILE,
        sink,
        &mut stats,
    );
    Ok(stats)
}

fn stem_of(path: &Path) -> String {
    path.file_stem()
        .map(|s| s.to_string_lossy().to_string())
        .unwrap_or_else(|| "untitled".into())
}

fn looks_like_header(row: &[String]) -> bool {
    let numericish = |s: &str| {
        let t = s.trim();
        !t.is_empty()
            && t.chars()
                .all(|c| c.is_ascii_digit() || matches!(c, '.' | ',' | '-'))
    };
    !row.is_empty() && row.iter().all(|c| !numericish(c) && !c.trim().is_empty())
}

fn dominant_table(doc: &Doc) -> Option<(&Vec<Vec<String>>, bool)> {
    let mut best: Option<usize> = None;
    for (i, rows) in doc.tables.iter().enumerate() {
        if rows.len() < 5 {
            continue;
        }
        let w = rows[0].len();
        if w < 2 {
            continue;
        }
        let consistent = rows.iter().filter(|r| r.len() == w).count();
        if consistent * 10 < rows.len() * 9 {
            continue;
        }
        if best
            .map(|b| rows.len() > doc.tables[b].len())
            .unwrap_or(true)
        {
            best = Some(i);
        }
    }
    best.map(|i| {
        (
            &doc.tables[i],
            doc.header_cells
                .get(i)
                .and_then(|h| h.first())
                .copied()
                .unwrap_or(false),
        )
    })
}

pub(crate) fn parse(html: &str) -> Doc {
    let mut doc = Doc::default();
    let bytes = html.as_bytes();
    let mut i = 0usize;
    let mut text_sink: Vec<&'static str> = Vec::new(); // element context stack (interned kinds)
    let mut cur_text = String::new();

    // table state
    let mut in_table = false;
    let mut cur_table: Vec<Vec<String>> = Vec::new();
    let mut cur_row: Vec<String> = Vec::new();
    let mut cur_cell: Option<String> = None;
    let mut cur_row_th: Vec<bool> = Vec::new();
    let mut table_header_flags: Vec<bool> = Vec::new();

    let flush_text = |cur_text: &mut String,
                      ctx: &Vec<&'static str>,
                      doc: &mut Doc,
                      cur_cell: &mut Option<String>| {
        let t = normalize_ws(cur_text);
        cur_text.clear();
        if t.is_empty() {
            return;
        }
        if let Some(cell) = cur_cell.as_mut() {
            if !cell.is_empty() {
                cell.push(' ');
            }
            cell.push_str(&t);
            return;
        }
        match ctx.last().copied() {
            Some("title") => {
                if !doc.title.is_empty() {
                    doc.title.push(' ');
                }
                doc.title.push_str(&t);
            }
            Some("h") => {
                doc.headings.push(t.clone());
                doc.body.push_str(&t);
                doc.body.push_str("\n\n");
            }
            _ => {
                doc.body.push_str(&t);
                doc.body.push(' ');
            }
        }
    };

    while i < bytes.len() {
        if bytes[i] == b'<' {
            // comment?
            if html[i..].starts_with("<!--") {
                i = html[i..]
                    .find("-->")
                    .map(|p| i + p + 3)
                    .unwrap_or(bytes.len());
                continue;
            }
            // parse tag
            let close = i + 1 < bytes.len() && bytes[i + 1] == b'/';
            let name_start = if close { i + 2 } else { i + 1 };
            let mut j = name_start;
            while j < bytes.len() && (bytes[j].is_ascii_alphanumeric() || bytes[j] == b'!') {
                j += 1;
            }
            let name = html[name_start..j].to_lowercase();
            let (tag_end, self_closing) = scan_tag(bytes, j);

            flush_text(&mut cur_text, &text_sink, &mut doc, &mut cur_cell);
            if !close {
                if let Some(id) = anchor_id(&html[j..tag_end], &name) {
                    doc.anchors.push((id, doc.body.len()));
                }
            }

            // Raw-text elements. Their contents are code, so jump the cursor
            // straight to the literal close tag rather than tokenizing them:
            // never buffered, never flushed, and a `<` in JS cannot be mistaken
            // for a tag and eat the rest of the page. XHTML-style `<script/>`
            // has no contents to skip.
            // The name scan above stops at any byte outside [A-Za-z0-9!], so
            // `<script-loader>` yields the name `script`. Entering raw-text
            // mode on that would skip to a `</script>` the document never
            // contains — `raw_text_end` rightly refuses `</script-loader>` —
            // and swallow everything after it. Custom element names are
            // REQUIRED to contain a hyphen, so this is a live shape. Only
            // treat the name as the raw-text element when it actually ended
            // there: whitespace, `/`, or `>`.
            let name_ended = bytes
                .get(j)
                .is_some_and(|&c| c.is_ascii_whitespace() || c == b'/' || c == b'>');
            if !close && name_ended && matches!(name.as_str(), "script" | "style") && !self_closing
            {
                let content_start = tag_end + 1;
                let content_end = raw_text_end(bytes, content_start, name.as_bytes());
                if name == "script"
                    && content_start < content_end
                    && inline_javascript_script(&html[j..tag_end])
                {
                    doc.scripts.push((content_start, content_end));
                }
                i = content_end;
                continue;
            }

            match (close, name.as_str()) {
                (false, "title") => text_sink.push("title"),
                (true, "title") => {
                    text_sink.pop();
                }
                (false, "h1") | (false, "h2") | (false, "h3") => text_sink.push("h"),
                (true, "h1") | (true, "h2") | (true, "h3") => {
                    text_sink.pop();
                }
                (false, "table") => {
                    in_table = true;
                    cur_table.clear();
                    cur_row.clear();
                    cur_row_th.clear();
                    cur_cell = None;
                    table_header_flags.clear();
                }
                (true, "table") => {
                    if let Some(c) = cur_cell.take() {
                        cur_row.push(c);
                    }
                    if !cur_row.is_empty() {
                        table_header_flags
                            .push(cur_row_th.iter().all(|&b| b) && !cur_row_th.is_empty());
                        cur_table.push(std::mem::take(&mut cur_row));
                    }
                    if !cur_table.is_empty() {
                        // The rows also go to the body, in place, one
                        // ` | `-joined line per row as PPTX tables do: cell
                        // text never reaches the body any other way, and a
                        // table that is not the dominant one is not emitted
                        // as rows. An empty cell keeps its column.
                        line_break(&mut doc.body, 2);
                        for row in &cur_table {
                            if row.iter().any(|c| !c.is_empty()) {
                                doc.body.push_str(&row.join(" | "));
                                doc.body.push('\n');
                            }
                        }
                        doc.header_cells
                            .push(vec![table_header_flags.first().copied().unwrap_or(false)]);
                        doc.tables.push(std::mem::take(&mut cur_table));
                    }
                    in_table = false;
                }
                (false, "tr") if in_table => {
                    if let Some(c) = cur_cell.take() {
                        cur_row.push(c);
                    }
                    if !cur_row.is_empty() {
                        table_header_flags
                            .push(cur_row_th.iter().all(|&b| b) && !cur_row_th.is_empty());
                        cur_table.push(std::mem::take(&mut cur_row));
                    }
                    cur_row_th.clear();
                }
                (false, "td") | (false, "th") if in_table => {
                    if let Some(c) = cur_cell.take() {
                        cur_row.push(c);
                    }
                    cur_row_th.push(name == "th");
                    cur_cell = Some(String::new());
                }
                // Block boundaries are blank lines: `split_sections` packs
                // paragraphs at `\n\n`, so a single newline here made a page
                // of paragraphs one paragraph, hard-cut mid-word. Line-level
                // breaks (`br`, list items, table rows) stay single newlines,
                // so a list or a table stays one block.
                (_, "p") | (_, "div") | (true, "ul") | (true, "ol") => {
                    line_break(&mut doc.body, 2);
                }
                (false, "br") | (false, "li") | (true, "tr") => {
                    line_break(&mut doc.body, 1);
                }
                _ => {}
            }
            i = tag_end + 1;
        } else {
            let next = memchr::memchr(b'<', &bytes[i..])
                .map(|p| i + p)
                .unwrap_or(bytes.len());
            cur_text.push_str(&decode_entities(&html[i..next]));
            i = next;
        }
    }
    flush_text(&mut cur_text, &text_sink, &mut doc, &mut cur_cell);
    doc
}

/// Where the attribute scanner stands, which is what decides whether a `/`
/// closes the tag or is just a byte of an attribute value.
#[derive(PartialEq)]
enum Attr {
    /// Between attributes, or inside an attribute name.
    Bare,
    /// Just past `=`, with the value not yet started.
    BeforeValue,
    /// Inside an unquoted value, where every byte up to the next whitespace
    /// or `>` belongs to the value.
    Unquoted,
}

/// Offset of the tag's `>` (or EOF) given `from` at the end of its name, plus
/// whether the tag self-closes.
///
/// The byte before `>` cannot answer the second question on its own: a slash
/// is also legal inside an unquoted attribute value, as in
/// `<script src=/cdn/lib/>`. Reading that as `/>` would skip raw-text mode for
/// an element that is never void and does have contents to skip. Only a slash
/// outside quotes and outside an unquoted value closes the tag.
fn scan_tag(bytes: &[u8], from: usize) -> (usize, bool) {
    let mut k = from;
    let mut quote: Option<u8> = None;
    let mut attr = Attr::Bare;
    let mut self_closing = false;
    while k < bytes.len() {
        let c = bytes[k];
        match quote {
            Some(q) => {
                if c == q {
                    quote = None;
                    attr = Attr::Bare;
                }
            }
            None => {
                if c == b'>' {
                    break;
                }
                // Only a slash immediately before `>` closes the tag; anything
                // following one reopens attribute scanning.
                self_closing = false;
                match c {
                    b'"' | b'\'' => quote = Some(c),
                    b'=' if attr != Attr::Unquoted => attr = Attr::BeforeValue,
                    // `<p a= />` makes the slash itself the value.
                    b'/' if attr == Attr::BeforeValue => attr = Attr::Unquoted,
                    b'/' if attr == Attr::Bare => self_closing = true,
                    _ if c.is_ascii_whitespace() => {
                        if attr == Attr::Unquoted {
                            attr = Attr::Bare;
                        }
                    }
                    _ => {
                        if attr == Attr::BeforeValue {
                            attr = Attr::Unquoted;
                        }
                    }
                }
            }
        }
        k += 1;
    }
    (k, self_closing)
}

/// Offset of the `<` opening `</name...>` at or after `from`, or EOF if there
/// is none — an unterminated raw-text element runs to the end of the file, as
/// it does in a browser.
///
/// Only an *appropriate* end tag terminates raw text: HTML5 lets the name be
/// followed by whitespace, `/` or `>` and nothing else, so `</script-foo>` is
/// script text that the surrounding code goes on hiding. Accepting any
/// non-alphanumeric follower would end the skip at `-`, `_` or `.` and index
/// the code after it.
fn raw_text_end(bytes: &[u8], from: usize, name: &[u8]) -> usize {
    let mut p = from.min(bytes.len());
    while let Some(off) = memchr::memchr(b'<', &bytes[p..]) {
        let at = p + off;
        let after = at + 2 + name.len();
        if bytes.get(at + 1) == Some(&b'/')
            && after <= bytes.len()
            && bytes[at + 2..after].eq_ignore_ascii_case(name)
            && bytes
                .get(after)
                .is_some_and(|&c| c.is_ascii_whitespace() || c == b'/' || c == b'>')
        {
            return at;
        }
        p = at + 1;
    }
    bytes.len()
}

/// The anchor an opening tag defines: its `id`, or the `name` of an `<a>`
/// (the pre-HTML5 form many EPUB 2 books still link to). `attrs` is the tag
/// text after the element name.
fn anchor_id(attrs: &str, element: &str) -> Option<String> {
    let b = attrs.as_bytes();
    let mut i = 0;
    while i < b.len() {
        let start = i;
        while i < b.len() && (b[i].is_ascii_whitespace() || b[i] == b'/') {
            i += 1;
        }
        let key_start = i;
        while i < b.len() && !b[i].is_ascii_whitespace() && !matches!(b[i], b'=' | b'/') {
            i += 1;
        }
        let key = &attrs[key_start..i];
        while i < b.len() && b[i].is_ascii_whitespace() {
            i += 1;
        }
        let mut value = None;
        if i < b.len() && b[i] == b'=' {
            i += 1;
            while i < b.len() && b[i].is_ascii_whitespace() {
                i += 1;
            }
            if i < b.len() && matches!(b[i], b'"' | b'\'') {
                let q = b[i];
                let v = i + 1;
                i = v;
                while i < b.len() && b[i] != q {
                    i += 1;
                }
                value = Some(&attrs[v..i]);
                i = (i + 1).min(b.len());
            } else {
                let v = i;
                while i < b.len() && !b[i].is_ascii_whitespace() {
                    i += 1;
                }
                value = Some(&attrs[v..i]);
            }
        }
        if let Some(v) = value {
            if key.eq_ignore_ascii_case("id")
                || (element == "a" && key.eq_ignore_ascii_case("name"))
            {
                let v = decode_entities(v.trim());
                if !v.is_empty() {
                    return Some(v);
                }
            }
        }
        if i == start {
            i += 1;
        }
    }
    None
}

/// End the body's current line (`n` = 1) or paragraph (`n` = 2): drop the
/// space `flush_text` leaves after a run of text, then make the body end in
/// at least `n` newlines. Nothing is added to an empty body.
fn line_break(body: &mut String, n: usize) {
    body.truncate(body.trim_end_matches(' ').len());
    if body.is_empty() {
        return;
    }
    let have = body.len() - body.trim_end_matches('\n').len();
    for _ in have..n {
        body.push('\n');
    }
}

fn normalize_ws(s: &str) -> String {
    s.split_whitespace().collect::<Vec<_>>().join(" ")
}

fn decode_entities(s: &str) -> String {
    if !s.contains('&') {
        return s.to_string();
    }
    s.replace("&amp;", "&")
        .replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "\"")
        .replace("&#39;", "'")
        .replace("&apos;", "'")
        .replace("&nbsp;", " ")
}

/// HTML spec JavaScript MIME type essences (mimesniff). `type=module` is a
/// module script, not one of these, and is handled beside them.
const JAVASCRIPT_MIME_TYPES: &[&str] = &[
    "application/ecmascript",
    "application/javascript",
    "application/x-ecmascript",
    "application/x-javascript",
    "text/ecmascript",
    "text/javascript",
    "text/javascript1.0",
    "text/javascript1.1",
    "text/javascript1.2",
    "text/javascript1.3",
    "text/javascript1.4",
    "text/javascript1.5",
    "text/jscript",
    "text/livescript",
    "text/x-ecmascript",
    "text/x-javascript",
];

/// True when this `<script>` element's inline text is JavaScript the page
/// runs. A `src` script's inline text is not executed. A `type` that is not
/// a JavaScript MIME type (JSON-LD, templates, import maps) is data, not
/// code. With no `type`, the legacy `language` attribute is consulted; with
/// neither, the element is a classic script.
fn inline_javascript_script(attrs: &str) -> bool {
    if attr_value(attrs, "src").is_some() {
        return false;
    }
    match attr_value(attrs, "type") {
        Some(ty) => is_javascript_type(ty),
        None => match attr_value(attrs, "language") {
            Some(lang) => is_javascript_language(lang),
            None => true,
        },
    }
}

fn is_javascript_type(ty: &str) -> bool {
    let essence = ty.split(';').next().unwrap_or("").trim();
    if essence.is_empty() {
        return false;
    }
    if essence.eq_ignore_ascii_case("module") {
        return true;
    }
    JAVASCRIPT_MIME_TYPES
        .iter()
        .any(|mime| essence.eq_ignore_ascii_case(mime))
}

fn is_javascript_language(lang: &str) -> bool {
    matches!(
        lang.trim().to_ascii_lowercase().as_str(),
        "javascript" | "js" | "ecmascript" | "jscript" | "livescript"
    )
}

/// The value of attribute `want`, or `None` when it is absent. A present
/// attribute with no value is `Some("")`. `attrs` is the tag text after the
/// element name and before `>`.
fn attr_value<'a>(attrs: &'a str, want: &str) -> Option<&'a str> {
    let b = attrs.as_bytes();
    let mut i = 0;
    while i < b.len() {
        while i < b.len() && (b[i].is_ascii_whitespace() || b[i] == b'/') {
            i += 1;
        }
        if i >= b.len() || b[i] == b'>' {
            break;
        }
        let key_start = i;
        while i < b.len() && !b[i].is_ascii_whitespace() && !matches!(b[i], b'=' | b'/' | b'>') {
            i += 1;
        }
        let key = &attrs[key_start..i];
        while i < b.len() && b[i].is_ascii_whitespace() {
            i += 1;
        }
        let value = if i < b.len() && b[i] == b'=' {
            i += 1;
            while i < b.len() && b[i].is_ascii_whitespace() {
                i += 1;
            }
            if i < b.len() && matches!(b[i], b'"' | b'\'') {
                let q = b[i];
                let v = i + 1;
                i = v;
                while i < b.len() && b[i] != q {
                    i += 1;
                }
                let value = &attrs[v..i];
                if i < b.len() {
                    i += 1;
                }
                value
            } else {
                let v = i;
                while i < b.len() && !b[i].is_ascii_whitespace() && b[i] != b'>' {
                    i += 1;
                }
                &attrs[v..i]
            }
        } else {
            ""
        };
        if key.eq_ignore_ascii_case(want) {
            return Some(value);
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    fn run(name: &str, html: &str) -> (ExtractStats, Vec<RawRecord>) {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join(name);
        std::fs::write(&path, html).unwrap();
        let mut recs = Vec::new();
        let stats = extract(&path, false, &mut |r| {
            recs.push(r);
            true
        })
        .unwrap();
        (stats, recs)
    }

    fn body_of(rec: &RawRecord) -> &str {
        rec.fields["body"].as_str().unwrap()
    }

    fn table_html(header: Option<[&str; 2]>, data_rows: usize) -> String {
        let mut h = String::from("<html><body><table>");
        if let Some([a, b]) = header {
            h.push_str(&format!("<tr><th>{a}</th><th>{b}</th></tr>"));
        }
        for i in 1..=data_rows {
            h.push_str(&format!("<tr><td>item{i}</td><td>{i}</td></tr>"));
        }
        h.push_str("</table></body></html>");
        h
    }

    #[test]
    fn a_prose_page_becomes_one_document_of_title_headings_and_tag_free_body() {
        let (stats, recs) = run(
            "page.html",
            "<html><head><title>Quarterly Report</title></head><body>\
             <h1>Revenue</h1><p>Growth was steady.</p>\
             <h2>Costs</h2><div>Cloud costs declined.</div></body></html>",
        );
        assert_eq!(stats.records, 1);
        assert_eq!(recs.len(), 1);
        assert_eq!(recs[0].locator, "s0");
        assert_eq!(
            recs[0].fields["title"],
            serde_json::json!("Quarterly Report")
        );
        assert_eq!(
            recs[0].fields["headings"],
            serde_json::json!(["Revenue", "Costs"])
        );
        let body = body_of(&recs[0]);
        assert!(body.contains("Growth was steady."));
        assert!(body.contains("Cloud costs declined."));
        assert!(
            !body.contains('<') && !body.contains("div"),
            "markup leaked into the body: {body:?}"
        );
    }

    #[test]
    fn attributes_and_comments_never_reach_the_body() {
        let (_, recs) = run(
            "attrs.html",
            "<html><body><p class=\"lede\">visible</p><!-- hidden note -->\
             <a href=\"https://example.com/hidden-path\" title=\"hidden title\">link text</a>\
             </body></html>",
        );
        let body = body_of(&recs[0]);
        assert!(body.contains("visible") && body.contains("link text"));
        assert!(
            !body.contains("hidden") && !body.contains("class") && !body.contains("href"),
            "attribute or comment text was indexed: {body:?}"
        );
    }

    #[test]
    fn character_entities_are_unescaped_in_the_title_and_the_body() {
        let (_, recs) = run(
            "ents.html",
            "<html><head><title>Q4 &amp; FY</title></head><body>\
             <p>5 &lt; 10 &gt; 2, &quot;quoted&quot; &amp; &#39;single&#39;&nbsp;spaced</p>\
             </body></html>",
        );
        assert_eq!(recs[0].fields["title"], serde_json::json!("Q4 & FY"));
        let body = body_of(&recs[0]);
        assert!(body.contains("5 < 10 > 2"), "{body:?}");
        assert!(body.contains("\"quoted\" & 'single' spaced"), "{body:?}");
        assert!(!body.contains("&amp;") && !body.contains("&lt;"));
    }

    /// Inline scripts routinely carry API keys and endpoints; indexing them
    /// would put those in `_source`. Prose on both sides of a skipped element
    /// still has to survive.
    #[test]
    fn script_and_style_text_never_reaches_the_body() {
        let (_, recs) = run(
            "skip.html",
            "<html><head><title>Doc</title><style>.lede{color:#fff}</style>\
             <script>var token=1;</script></head><body><p>before</p>\
             <script>cfg={api:'https://secret.example/v1',key:'abc123'}</script>\
             <p>after</p></body></html>",
        );
        assert_eq!(recs[0].fields["title"], serde_json::json!("Doc"));
        let body = body_of(&recs[0]);
        assert!(
            body.contains("before") && body.contains("after"),
            "{body:?}"
        );
        for leak in ["lede", "color", "token", "secret.example", "abc123", "cfg"] {
            assert!(
                !body.contains(leak),
                "{leak:?} was indexed as prose: {body:?}"
            );
        }
    }

    /// `<` inside JS/CSS is an operator, not a tag. Tokenizing it would run the
    /// tag scan past `</script>` and drop the remainder of the page.
    #[test]
    fn a_less_than_inside_a_script_does_not_swallow_the_rest_of_the_page() {
        let (_, recs) = run(
            "lt.html",
            "<html><body><p>alpha</p><script>if(a<b){s=\"x>y\";}</script>\
             <style>@media (max-width:40em){.a{top:0}}</style>\
             <p>omega</p></body></html>",
        );
        let body = body_of(&recs[0]);
        assert!(body.contains("alpha") && body.contains("omega"), "{body:?}");
        assert!(
            !body.contains("max-width") && !body.contains('{'),
            "{body:?}"
        );
    }

    /// `</SCRIPT >` is the same terminator; an XHTML-style `<script src=… />`
    /// has no contents, so it must not put the tokenizer into raw-text mode.
    #[test]
    fn raw_text_ends_at_a_case_insensitive_or_spaced_close_and_self_closing_skips_nothing() {
        let (_, recs) = run(
            "case.html",
            "<html><body><SCRIPT>hidden=1;</SCRIPT >kept one\
             <script src=\"app.js\"/><p>kept two</p></body></html>",
        );
        let body = body_of(&recs[0]);
        assert!(
            body.contains("kept one") && body.contains("kept two"),
            "{body:?}"
        );
        assert!(!body.contains("hidden"), "{body:?}");
    }

    /// A slash also ends an unquoted attribute value, and `script` is never a
    /// void element — so the byte before `>` cannot decide self-closing on its
    /// own, and `<script src=/cdn/lib/>` still has contents to skip.
    #[test]
    fn a_slash_ending_an_unquoted_attribute_value_does_not_read_as_self_closing() {
        let (_, recs) = run(
            "unquoted-slash.html",
            "<html><body><p>alpha</p><script src=/cdn/lib/>var leak=\"SEKRIT\";</script>\
             <p>omega</p></body></html>",
        );
        let body = body_of(&recs[0]);
        assert!(body.contains("alpha") && body.contains("omega"), "{body:?}");
        for leak in ["SEKRIT", "leak", "cdn"] {
            assert!(
                !body.contains(leak),
                "{leak:?} was indexed as prose: {body:?}"
            );
        }
    }

    /// Raw text ends only at an *appropriate* end tag: HTML5 requires
    /// whitespace, `/` or `>` after the name, so `</script-foo>` is script
    /// text. Any other follower — `_` and `.` alike — is the same case.
    #[test]
    fn a_run_on_end_tag_name_does_not_end_raw_text_early() {
        let (_, recs) = run(
            "runon.html",
            "<p>alpha</p><script>a=1;</script-foo>b=\"LEAK2\";</script><p>omega</p>",
        );
        let body = body_of(&recs[0]);
        assert!(body.contains("alpha") && body.contains("omega"), "{body:?}");
        for leak in ["LEAK2", "a=1", "script"] {
            assert!(
                !body.contains(leak),
                "{leak:?} was indexed as prose: {body:?}"
            );
        }
    }

    /// A custom element whose name merely STARTS with a raw-text element's
    /// name is an ordinary element. Custom element names are required to
    /// contain a hyphen, so `<script-loader>` is a real shape — and reading
    /// it as `script` would enter raw-text mode looking for a `</script>`
    /// the document does not contain, discarding everything after it.
    #[test]
    fn a_custom_element_named_after_a_raw_text_element_does_not_swallow_the_page() {
        for (tag, marker) in [("script-loader", "SLOAD"), ("style-sheet", "SSHEET")] {
            let (_, recs) = run(
                "custom.html",
                &format!("<p>alpha</p><{tag} data-k=\"{marker}\">middle</{tag}><p>omega</p>"),
            );
            let body = body_of(&recs[0]);
            assert!(
                body.contains("alpha") && body.contains("omega"),
                "<{tag}> swallowed the page: {body:?}"
            );
            assert!(
                body.contains("middle"),
                "<{tag}> content was dropped: {body:?}"
            );
            assert!(
                !body.contains(marker),
                "attribute leaked into body: {body:?}"
            );
        }
    }

    /// An unclosed `<script>` swallows the rest of the file, as a real HTML
    /// parser does — there is no terminator, so the tail is code. It is
    /// bounded to the tail: text before the tag was already flushed.
    #[test]
    fn an_unclosed_script_drops_the_tail_but_keeps_the_text_before_it() {
        let (stats, recs) = run(
            "trunc.html",
            "<html><body><h1>Head</h1><p>kept</p><script>var token=1;<p>lost</p>",
        );
        assert_eq!(stats.records, 1);
        let body = body_of(&recs[0]);
        assert!(body.contains("kept"), "{body:?}");
        assert!(
            !body.contains("token") && !body.contains("lost"),
            "{body:?}"
        );
    }

    #[test]
    fn a_dominant_table_becomes_one_record_per_row_named_from_its_header_cells() {
        let (stats, recs) = run("t.html", &table_html(Some(["Product Name", "Qty"]), 5));
        assert_eq!(stats.records, 5);
        assert_eq!(recs.len(), 5);
        assert_eq!(recs[0].fields["Product_Name"], serde_json::json!("item1"));
        assert_eq!(recs[0].fields["Qty"], serde_json::json!("1"));
        assert_eq!(recs[4].fields["Product_Name"], serde_json::json!("item5"));
        assert!(
            recs.iter().all(|r| r.fields.len() == 2),
            "the header row must not become a record"
        );
        let locs: Vec<&str> = recs.iter().map(|r| r.locator.as_str()).collect();
        assert_eq!(locs, ["row1", "row2", "row3", "row4", "row5"]);
        assert!(recs.iter().all(|r| r.group.is_none()));
    }

    /// A dominant table and an inline function share one ungrouped sketch.
    /// Symbol fields must not join the row dataset: phase A often samples
    /// only the rows, and the frozen mapping would then lack `name`/`code`.
    #[test]
    fn a_dominant_table_does_not_emit_inline_script_symbols() {
        let mut html = table_html(Some(["Product Name", "Qty"]), 5);
        html.push_str("<script>function openDrawer(id) { return id; }</script>");
        let (stats, recs) = run("t.html", &html);
        assert_eq!(stats.records, 5);
        assert!(recs.iter().all(|r| r.locator.starts_with("row")));
        assert!(recs.iter().all(|r| r.fields.get("name").is_none()));
        assert!(recs.iter().all(|r| r.fields.get("defs").is_none()));
        assert!(recs.iter().all(|r| !r
            .fields
            .values()
            .any(|v| v.as_str().is_some_and(|s| s.contains("openDrawer")))));
    }

    #[test]
    fn a_headerless_table_gets_positional_column_names_and_keeps_its_first_row() {
        let (stats, recs) = run("t.html", &table_html(None, 6));
        assert_eq!(stats.records, 6, "no header row means no row is consumed");
        assert_eq!(recs[0].locator, "row0");
        assert_eq!(recs[0].fields["col_1"], serde_json::json!("item1"));
        assert_eq!(recs[0].fields["col_2"], serde_json::json!("1"));
    }

    #[test]
    fn empty_cells_are_omitted_rather_than_indexed_as_blank_values() {
        let mut h = String::from("<html><body><table><tr><th>A &amp; B</th><th>Note</th></tr>");
        for i in 1..=5 {
            h.push_str(&format!("<tr><td>x&lt;{i}</td><td>  </td></tr>"));
        }
        h.push_str("</table></body></html>");
        let (stats, recs) = run("cells.html", &h);
        assert_eq!(stats.records, 5);
        assert_eq!(recs[0].fields["A_B"], serde_json::json!("x<1"));
        assert_eq!(
            recs[0].fields.len(),
            1,
            "the blank Note cell must not be stored"
        );
    }

    /// A table that is not emitted as rows (under the 5-row dominance
    /// threshold here) used to lose its content twice over: `flush_text`
    /// diverts every cell into `cur_cell`, so the cells never reached
    /// `doc.body` either. Its rows now land in the body where the table
    /// stood, one ` | `-joined line per row, as PPTX tables do.
    #[test]
    fn a_table_below_the_dominance_threshold_keeps_its_rows_in_the_body() {
        let (stats, recs) = run(
            "small.html",
            "<html><body><h1>Head</h1><p>prose</p>\
             <table><tr><th>Region</th><th>Q3</th></tr>\
             <tr><td>North</td><td>12</td></tr>\
             <tr><td>  </td><td></td></tr>\
             <tr><td>South <b>East</b></td><td></td></tr></table>\
             <p>after</p></body></html>",
        );
        assert_eq!(stats.records, 1, "falls back to the document record");
        let body = body_of(&recs[0]);
        let lines: Vec<&str> = body.lines().map(str::trim).collect();
        let at = |s: &str| {
            lines
                .iter()
                .position(|l| *l == s)
                .unwrap_or_else(|| panic!("no line {s:?} in {body:?}"))
        };
        // In place, in order: after the prose before it, before the prose
        // after it. An empty cell keeps its column; an all-empty row is
        // dropped.
        let (head, north, south) = (at("Region | Q3"), at("North | 12"), at("South East |"));
        assert!(at("prose") < head && head < north && north < south && south < at("after"));
        assert_eq!(
            lines.iter().filter(|l| l.contains('|')).count(),
            3,
            "{body:?}"
        );
    }

    /// `split_sections` packs paragraphs at blank lines. A `<p>` used to end
    /// with a single newline, so a page of paragraphs was ONE paragraph to it
    /// and was hard-cut every `SECTION_CHARS` bytes, mid-word; the heading
    /// before such a run was emitted as a section on its own. Paragraph and
    /// div ends are now blank lines, so sections end where paragraphs do.
    #[test]
    fn sections_of_a_long_page_end_at_paragraph_ends() {
        let para = |i: usize| format!("Paragraph {i} {} end{i}.", "lorem ipsum dolor ".repeat(24));
        let html: String = std::iter::once("<html><body><h1>Title</h1>".to_string())
            .chain((0..10).map(|i| format!("<p>{}</p>", para(i))))
            .chain(std::iter::once(
                "<ul><li>one</li><li>two</li></ul><p>tail</p></body></html>".into(),
            ))
            .collect();
        let (stats, recs) = run("long.html", &html);
        assert!(stats.records > 1, "{} bytes split", html.len());
        for r in &recs[..recs.len() - 1] {
            let body = body_of(r).trim_end();
            assert!(
                (0..10).any(|i| body.ends_with(&format!("end{i}."))),
                "section ends mid-paragraph: ...{:?}",
                &body[body.len().saturating_sub(40)..]
            );
        }
        assert!(
            body_of(&recs[0]).len() > 100,
            "the heading is not a section of its own"
        );
        let last = body_of(recs.last().unwrap());
        assert!(last.contains("one\ntwo"), "list items stay lines: {last:?}");
    }

    /// EPUB splits chapter files at table-of-contents anchors, so `parse`
    /// records where each `id` (and each `<a name>`) begins in the body.
    #[test]
    fn anchors_record_ids_and_a_names_where_their_text_begins() {
        let doc = parse(
            "<p>intro</p><h2 id=\"c1\">One</h2><p data-id=\"no\" class=x>a</p>             <a name='c2'></a><p id=c3 >b</p><span name=\"no\">c</span>             <div ID = \"Q&amp;A\">d</div>",
        );
        let ids: Vec<&str> = doc.anchors.iter().map(|(id, _)| id.as_str()).collect();
        assert_eq!(ids, ["c1", "c2", "c3", "Q&A"]);
        for (id, at) in &doc.anchors {
            let rest = doc.body[*at..].trim_start();
            let want = match id.as_str() {
                "c1" => "One",
                "c2" | "c3" => "b",
                _ => "d",
            };
            assert!(rest.starts_with(want), "{id} at {at}: {rest:?}");
        }
        assert_eq!(anchor_id(" id", "p"), None, "a bare attribute has no value");
        assert_eq!(anchor_id(" id=\"\"", "p"), None);
    }

    #[test]
    fn a_page_without_a_title_falls_back_to_a_heading_then_to_the_file_stem() {
        let (_, recs) = run(
            "fallback.html",
            "<html><body><h2>Only Heading</h2><p>x</p></body></html>",
        );
        assert_eq!(recs[0].fields["title"], serde_json::json!("Only Heading"));

        let (_, recs) = run("stem-name.html", "<html><body><p>x</p></body></html>");
        assert_eq!(recs[0].fields["title"], serde_json::json!("stem-name"));
        assert!(recs[0].fields.get("headings").is_none());
    }

    #[test]
    fn unclosed_and_nonsense_markup_is_never_fatal() {
        let (stats, recs) = run(
            "junk.html",
            "<<<>>> <div class=\"unclosed <p>dangling &amp; text <script>x=1;",
        );
        assert_eq!(stats.junk, 0);
        assert_eq!(stats.records, 1, "a broken page still yields a document");
        assert_eq!(recs[0].fields["title"], serde_json::json!("junk"));

        let (stats, recs) = run("empty.html", "");
        assert_eq!(stats.records, 1);
        assert_eq!(body_of(&recs[0]), "");
    }

    /// #2356: a function inside an inline `<script>` is a code symbol at the
    /// line it occupies in the HTML file. The page's prose record stays the
    /// visible text — the script is not copied into `body`. `.html` and
    /// `.htm` take the same extractor. A `src` script, a JSON-LD block, a
    /// template, a style block, and a comment are not definitions.
    #[test]
    fn inline_script_functions_become_symbols_at_their_html_lines() {
        let page = "\
<!DOCTYPE html>
<html>
<head><title>Drawer</title></head>
<body>
<p>The drawer holds the menu.</p>
<script>
function openDrawer(id) {
  document.getElementById(id).hidden = false;
}
</script>
<script type=\"text/javascript\">
const toggleDrawer = () => {
  return true;
};
</script>
<script type=\"module\">
export function openPanel() {
  return 1;
}
</script>
<script type=\"application/ld+json\">
function ignoredJson() {}
</script>
<script src=\"app.js\">
function fromSrc() {}
</script>
<script type=\"text/template\">
function notATemplateFn() {}
</script>
<style>
function notCss() {}
</style>
<!-- <script>function inComment() {}</script> -->
<p>After the scripts.</p>
</body>
</html>
";
        let line_of = |needle: &str| {
            page.lines()
                .position(|line| line.contains(needle))
                .map(|i| i + 1)
                .unwrap_or_else(|| panic!("{needle} is not in the fixture"))
        };
        for name in ["page.html", "page.htm"] {
            let (stats, recs) = run(name, page);
            let prose = recs
                .iter()
                .find(|r| r.locator == "s0")
                .expect("prose record");
            let body = body_of(prose);
            assert!(body.contains("The drawer holds the menu."), "{body:?}");
            assert!(body.contains("After the scripts."), "{body:?}");
            assert_eq!(prose.fields["title"], serde_json::json!("Drawer"));
            for leak in [
                "openDrawer",
                "getElementById",
                "toggleDrawer",
                "openPanel",
                "ignoredJson",
                "fromSrc",
                "notATemplateFn",
                "notCss",
                "inComment",
            ] {
                assert!(
                    !body.contains(leak),
                    "{leak} leaked into the page body: {body:?}"
                );
            }

            let catalog = recs
                .iter()
                .find(|r| r.locator == "code")
                .expect("defs catalog");
            let defs = catalog.fields["defs"].as_str().unwrap();
            for expect in [
                "function openDrawer",
                "function toggleDrawer",
                "function openPanel",
            ] {
                assert!(
                    defs.contains(expect),
                    "{name} defs missing {expect}: {defs}"
                );
            }
            for absent in [
                "ignoredJson",
                "fromSrc",
                "notATemplateFn",
                "notCss",
                "inComment",
            ] {
                assert!(!defs.contains(absent), "{name} extracted {absent}: {defs}");
            }
            assert!(
                catalog.fields.get("body").is_none(),
                "script text must not become body"
            );
            assert_eq!(catalog.fields["language"], serde_json::json!("javascript"));

            let symbol = |fn_name: &str| {
                recs.iter()
                    .find(|r| {
                        r.fields.get("name").and_then(|v| v.as_str()) == Some(fn_name)
                            && r.locator.starts_with("code:")
                    })
                    .unwrap_or_else(|| panic!("{name} has no symbol {fn_name}"))
            };
            let open = symbol("openDrawer");
            let open_line = line_of("function openDrawer");
            assert_eq!(open.fields["line"], serde_json::json!(open_line));
            assert_eq!(open.fields["kind"], serde_json::json!("function"));
            assert_eq!(open.fields["language"], serde_json::json!("javascript"));
            assert_eq!(open.locator, format!("code:{open_line}:openDrawer"));
            let code = open.fields["code"].as_str().unwrap();
            assert!(code.contains("function openDrawer"), "{code}");
            let impl_text = open.fields["impl"].as_str().unwrap();
            assert!(impl_text.contains("getElementById"), "{impl_text}");
            assert!(
                page.lines()
                    .nth(open_line - 1)
                    .unwrap()
                    .contains("openDrawer"),
                "line {open_line} is not the definition"
            );

            let toggle = symbol("toggleDrawer");
            assert_eq!(
                toggle.fields["line"],
                serde_json::json!(line_of("const toggleDrawer"))
            );
            assert_eq!(toggle.fields["kind"], serde_json::json!("function"));

            let panel = symbol("openPanel");
            assert_eq!(
                panel.fields["line"],
                serde_json::json!(line_of("export function openPanel"))
            );
            assert_eq!(panel.fields["kind"], serde_json::json!("function"));

            let names: Vec<&str> = recs
                .iter()
                .filter_map(|r| r.fields.get("name").and_then(|v| v.as_str()))
                .collect();
            assert!(
                !names.iter().any(|n| {
                    matches!(
                        *n,
                        "ignoredJson" | "fromSrc" | "notATemplateFn" | "notCss" | "inComment"
                    )
                }),
                "{name} symbols: {names:?}"
            );
            assert!(stats.records >= 4, "{name} records {}", stats.records);
        }
    }

    /// A page whose small table folded into the document dataset is indexed
    /// as the page: its title, and a body holding the table's rows as text,
    /// with no markup and no per-row records.
    #[test]
    fn a_folded_table_page_renders_as_one_tag_free_document() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("report.html");
        let page = table_html(Some(["Measure", "Value"]), 5)
            .replace("<html>", "<html><head><title>Port audit</title></head>");
        std::fs::write(&path, page).unwrap();
        let mut recs = Vec::new();
        let stats = crate::extract::extract_as_document(&path, false, &mut |r| {
            recs.push(r);
            true
        })
        .unwrap();
        assert_eq!(stats.records, 1, "{recs:?}");
        assert_eq!(recs[0].fields["title"], "Port audit");
        let body = body_of(&recs[0]);
        assert!(body.contains("item3 | 3"), "{body}");
        assert!(!body.contains('<'), "{body}");
        assert!(!recs[0].fields.contains_key("Measure"));
    }
}