Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions pkgs/lab-ops-hub/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,13 @@ export async function fileResponseForKey(
// whitespace collapsed — the same `" ".join` + `\s+` collapse app.py
// applies. Paired fixtures: test/extractor-fixtures.json, exercised by
// both test/extractor.test.ts and test/test_extractor_parity.py.
// - <title> and <textarea> are RCDATA elements (#4008): content is
// character data (entities decoded, markup literal) up to the matching
// close tag — title data goes to title, textarea data to text.
// - <iframe>, <xmp>, <noembed>, and <noframes> are raw-text elements
// (#4008): content is likewise literal up to the matching close tag
// (anchors inside never count), and HTMLParser reports it verbatim —
// no entity decoding — as ordinary character data.
const NAMED_ENTITIES: Record<string, string> = {
amp: "&",
lt: "<",
Expand Down Expand Up @@ -700,20 +707,33 @@ export function extractHtml(html: string): { title: string; links: number; text:
let links = 0;
const textChunks: string[] = [];
let skipDepth = 0;
// Raw-text mode: script/style are HTMLParser CDATA elements, and
// HTMLParser likewise reports <title> content as raw character data
// (markup inside a title is literal and never counts as links).
let cdataTag: "script" | "style" | "title" | null = null;
// Raw-text modes: script/style are HTMLParser CDATA elements;
// <title>/<textarea> are RCDATA (character data, entities decoded,
// markup literal); <iframe>/<xmp>/<noembed>/<noframes> are raw text
// (character data verbatim). In every mode only the matching close
// tag is markup, so anchors inside never count as links (#4008).
let cdataTag: string | null = null;

const handleData = (raw: string) => {
if (raw === "") return;
const data = decodeHtmlEntities(raw);
if (cdataTag === "title") title += data;
else if (skipDepth === 0) textChunks.push(data);
};
// Raw-text element data: HTMLParser reports it verbatim — no entity
// decoding — and the container Extractor appends it to text like any
// other character data.
const handleRawData = (raw: string) => {
if (raw === "") return;
if (skipDepth === 0) textChunks.push(raw);
};
const handleCdataData = (raw: string) => {
if (cdataTag === "iframe" || cdataTag === "xmp" || cdataTag === "noembed" || cdataTag === "noframes") handleRawData(raw);
else handleData(raw);
};
const handleStartTag = (tag: string, selfClosing: boolean) => {
if (tag === "title") {
if (!selfClosing) cdataTag = "title";
if (tag === "title" || tag === "textarea" || tag === "iframe" || tag === "xmp" || tag === "noembed" || tag === "noframes") {
if (!selfClosing) cdataTag = tag;
} else if (tag === "a") links += 1;
else if (tag === "script" || tag === "style") {
skipDepth += 1;
Expand All @@ -732,11 +752,18 @@ export function extractHtml(html: string): { title: string; links: number; text:
// HTMLParser CDATA mode: only the matching close tag is markup.
const close = new RegExp(`</${cdataTag}(?=[\\s/>])`, "i").exec(html.slice(i));
if (close === null) {
handleData(html.slice(i));
// EOF inside the element. For the #4008 elements (textarea and
// the raw-text four) the container never sees the tail:
// HTMLParser fed without close() — app.py only calls feed() —
// leaves an unterminated raw-text/RCDATA region unflushed, so
// its content is dropped, not delivered.
if (cdataTag !== "textarea" && cdataTag !== "iframe" && cdataTag !== "xmp" && cdataTag !== "noembed" && cdataTag !== "noframes") {
handleCdataData(html.slice(i));
}
i = n;
break;
}
handleData(html.slice(i, i + close.index));
handleCdataData(html.slice(i, i + close.index));
i += close.index;
// Consume the close tag here, tolerantly: HTMLParser accepts
// recoverable end tags carrying trailing attributes
Expand Down
70 changes: 70 additions & 0 deletions pkgs/lab-ops-hub/test/extractor-fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,75 @@
"title": "",
"links": 0,
"text": "a"
},
{
"name": "reported case: script markup inside textarea is literal text, not a script element",
"html": "<textarea><script></textarea><p>Article body</p>",
"title": "",
"links": 0,
"text": "<script> Article body"
},
{
"name": "textarea content is RCDATA: entities decoded, markup literal, contributes body text",
"html": "<textarea>Tom &amp; Jerry <b>not bold</b></textarea><p>after</p>",
"title": "",
"links": 0,
"text": "Tom & Jerry <b>not bold</b> after"
},
{
"name": "iframe content is raw text: anchors inside do not count, markup is literal text",
"html": "<p>before</p><iframe><a href=\"/x\">frame link</a> frame text</iframe><p>after</p>",
"title": "",
"links": 0,
"text": "before <a href=\"/x\">frame link</a> frame text after"
},
{
"name": "xmp content is raw text: anchors inside do not count, markup is literal text",
"html": "<p>before</p><xmp><a href=\"/x\">xmp link</a> xmp text</xmp><p>after</p>",
"title": "",
"links": 0,
"text": "before <a href=\"/x\">xmp link</a> xmp text after"
},
{
"name": "noembed content is raw text: markup inside is literal text",
"html": "<p>before</p><noembed><p>embed text</p></noembed><p>after</p>",
"title": "",
"links": 0,
"text": "before <p>embed text</p> after"
},
{
"name": "noframes content is raw text: markup inside is literal text",
"html": "<p>before</p><noframes><p>frames text</p></noframes><p>after</p>",
"title": "",
"links": 0,
"text": "before <p>frames text</p> after"
},
{
"name": "raw-text content is verbatim: entities are not decoded inside iframe",
"html": "<iframe>a &amp; b</iframe><p>x</p>",
"title": "",
"links": 0,
"text": "a &amp; b x"
},
{
"name": "unclosed textarea: the unterminated RCDATA tail is dropped, as the container's feed-only Extractor leaves it unflushed",
"html": "<p>before</p><textarea>never closed",
"title": "",
"links": 0,
"text": "before"
},
{
"name": "unclosed iframe: the unterminated raw-text tail is dropped, as the container's feed-only Extractor leaves it unflushed",
"html": "<p>before</p><iframe>never closed <p>lost</p>",
"title": "",
"links": 0,
"text": "before"
},
{
"name": "raw-text end tag with trailing attributes still closes the region",
"html": "<iframe>one</iframe foo><p>x</p>",
"title": "",
"links": 0,
"text": "one x"
}
]
Loading