GeezerVison is showing github.com — fix(lab-ops-hub): address Codex round-2 P2s on extractor parity (#569) · developing-today/code@17347f0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pkgs/lab-ops-hub/src/lib.ts +30-5Lines changed: 30 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -528,7 +528,12 @@ export function decodeHtmlEntities(data: string): string { | 528 | 528 | const code = Number.parseInt(body.slice(hex ? 2 : 1), hex ? 16 : 10); | 529 | 529 | return String.fromCodePoint(normalizeNumericRef(code)); | 530 | 530 | } | 531 | | - return NAMED_ENTITIES[body] ?? whole; | | 531 | + // Own-property lookup only: a plain index would resolve inherited | | 532 | + // Object properties (&constructor;, &toString;) to JavaScript | | 533 | + // functions, where HTMLParser leaves unknown names literal. | | 534 | + return Object.prototype.hasOwnProperty.call(NAMED_ENTITIES, body) | | 535 | + ? NAMED_ENTITIES[body] | | 536 | + : whole; | 532 | 537 | }); | 533 | 538 | } | 534 | 539 |
| @@ -608,8 +613,19 @@ export function extractHtml(html: string): { title: string; links: number; text: | 608 | 613 | } | 609 | 614 | handleData(html.slice(i, i + close.index)); | 610 | 615 | i += close.index; | | 616 | + // Consume the close tag here, tolerantly: HTMLParser accepts | | 617 | + // recoverable end tags carrying trailing attributes | | 618 | + // (</script foo>), which the strict end-tag pattern in the normal | | 619 | + // path rejects -- skipDepth would stay set and the rest of the | | 620 | + // document would be discarded. | | 621 | + const closeTag = new RegExp(`</${cdataTag}[^>]*>`, "i").exec(html.slice(i)); | | 622 | + const closedTag = cdataTag; | 611 | 623 | cdataTag = null; | 612 | | - continue; // the close tag itself is parsed by the normal path | | 624 | + if (closeTag !== null) { | | 625 | + handleEndTag(closedTag); | | 626 | + i += closeTag[0].length; | | 627 | + } | | 628 | + continue; | 613 | 629 | } | 614 | 630 | const lt = html.indexOf("<", i); | 615 | 631 | if (lt === -1) { | @@ -655,12 +671,21 @@ export function extractHtml(html: string): { title: string; links: number; text: | 655 | 671 | // attribute value end the tag early. | 656 | 672 | let j = i + 1; | 657 | 673 | let quote = ""; | | 674 | + let lastSig = ""; | 658 | 675 | while (j < n) { | 659 | 676 | const ch = html[j]; | 660 | 677 | if (quote !== "") { | 661 | | - if (ch === quote) quote = ""; | 662 | | - } else if (ch === '"' || ch === "'") quote = ch; | 663 | | - else if (ch === ">") break; | | 678 | + if (ch === quote) { | | 679 | + quote = ""; | | 680 | + lastSig = ch; | | 681 | + } | | 682 | + } else if ((ch === '"' || ch === "'") && lastSig === "=") { | | 683 | + // A quote opens a quoted value only where a value begins | | 684 | + // (right after '='); a quote inside an unquoted value, as in | | 685 | + // <a href=/it's>, is data, as HTMLParser tokenizes it. | | 686 | + quote = ch; | | 687 | + } else if (ch === ">") break; | | 688 | + else if (!/\s/.test(ch)) lastSig = ch; | 664 | 689 | j += 1; | 665 | 690 | } | 666 | 691 | if (j >= n) { | |
Collapse file Copy file name to clipboard +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -[{"html":"<html><head><title>Hello</title></head><body><p>World</p><a href=\"/x\">one</a></body></html>","links":1,"name":"basic page: title, paragraph, one anchor","text":"World one","title":"Hello"},{"html":"<p>x</p><a>bare</a>","links":1,"name":"bare anchor counts (Worker regex <a\\s missed it)","text":"x bare","title":""},{"html":"<a/><a href=\"/y\">two</a>","links":2,"name":"self-closing anchor counts","text":"two","title":""},{"html":"<script>var s = '<a href=\"#\">fake</a>';</script><p>real</p>","links":0,"name":"anchor markup inside a script string does not count","text":"real","title":""},{"html":"<!-- <a href=\"#\">hidden</a> --><p>shown</p>","links":0,"name":"anchor markup inside a comment does not count","text":"shown","title":""},{"html":"<title>Fish & Chips — —</title><p>body</p>","links":0,"name":"title entities are decoded (Worker regex returned them raw)","text":"body","title":"Fish & Chips — —"},{"html":"<style>a { color: red; }</style><p>body</p>","links":0,"name":"style content does not leak into text (Worker fallback leaked it)","text":"body","title":""},{"html":"<title>My Title</title>loose text","links":0,"name":"title does not leak into text","text":"loose text","title":"My Title"},{"html":"<p>before</p><script>var a = '<a href=\"#\">x</a>';","links":0,"name":"unclosed script swallows the rest of the document","text":"before","title":""},{"html":"<p>keep</p><style>.a { color: blue; }","links":0,"name":"unclosed style swallows the rest of the document","text":"keep","title":""},{"html":"<p>a b\n\tc</p>","links":0,"name":"whitespace collapses across chunks","text":"a b c","title":""},{"html":"<TITLE>Caps</TITLE><A HREF=\"#\">U</A>","links":1,"name":"uppercase tags parse like lowercase","text":"U","title":"Caps"},{"html":"<p><tag> & "q" end</p>","links":0,"name":"entities in text are decoded, nbsp collapses as whitespace","text":"<tag> & \"q\" end","title":""},{"html":"a<b>bold</b>c","links":0,"name":"inline markup splits text into space-joined chunks","text":"a bold c","title":""},{"html":"<title>x <a href=\"#\">y</a> z</title><p>out</p>","links":0,"name":"title is raw text: markup inside is literal and anchors do not count","text":"out","title":"x <a href=\"#\">y</a> z"},{"html":"<a title=\"2 > 1\" href=\"#\">go</a>","links":1,"name":"quoted attribute value may contain >","text":"go","title":""},{"html":"<script>1</script><style>2</style><a href=\"#\">after</a>","links":1,"name":"script and style both skipped, anchors after them count","text":"after","title":""},{"name":"unquoted attribute value ending in / is not a self-closing tag (script stays raw text)","html":"<script src=/x/>var x=\"<a href=x>fake</a>\";</script><p>real</p>","title":"","links":0,"text":"real"},{"name":"comment terminated by --!> does not swallow the rest of the document","html":"<!-- migration note --!><h1>Article title</h1><p>Article body</p>","title":"","links":0,"text":"Article title Article body"},{"name":"abruptly closed empty comment <!--> does not swallow the rest of the document","html":"<!--><p>shown</p>","title":"","links":0,"text":"shown"},{"name":"numeric character reference in the C1 range is remapped (€ is the euro sign)","html":"<title>Price €10</title><p>body</p>","title":"Price €10","links":0,"text":"body"},{"name":"numeric character references for NUL and a surrogate become U+FFFD","html":"<p>a�b�c</p>","title":"","links":0,"text":"a�b�c"},{"name":"out-of-range numeric character reference becomes U+FFFD","html":"<p>x�y</p>","title":"","links":0,"text":"x�y"}] | | 1 | +[{"html":"<html><head><title>Hello</title></head><body><p>World</p><a href=\"/x\">one</a></body></html>","links":1,"name":"basic page: title, paragraph, one anchor","text":"World one","title":"Hello"},{"html":"<p>x</p><a>bare</a>","links":1,"name":"bare anchor counts (Worker regex <a\\s missed it)","text":"x bare","title":""},{"html":"<a/><a href=\"/y\">two</a>","links":2,"name":"self-closing anchor counts","text":"two","title":""},{"html":"<script>var s = '<a href=\"#\">fake</a>';</script><p>real</p>","links":0,"name":"anchor markup inside a script string does not count","text":"real","title":""},{"html":"<!-- <a href=\"#\">hidden</a> --><p>shown</p>","links":0,"name":"anchor markup inside a comment does not count","text":"shown","title":""},{"html":"<title>Fish & Chips — —</title><p>body</p>","links":0,"name":"title entities are decoded (Worker regex returned them raw)","text":"body","title":"Fish & Chips — —"},{"html":"<style>a { color: red; }</style><p>body</p>","links":0,"name":"style content does not leak into text (Worker fallback leaked it)","text":"body","title":""},{"html":"<title>My Title</title>loose text","links":0,"name":"title does not leak into text","text":"loose text","title":"My Title"},{"html":"<p>before</p><script>var a = '<a href=\"#\">x</a>';","links":0,"name":"unclosed script swallows the rest of the document","text":"before","title":""},{"html":"<p>keep</p><style>.a { color: blue; }","links":0,"name":"unclosed style swallows the rest of the document","text":"keep","title":""},{"html":"<p>a b\n\tc</p>","links":0,"name":"whitespace collapses across chunks","text":"a b c","title":""},{"html":"<TITLE>Caps</TITLE><A HREF=\"#\">U</A>","links":1,"name":"uppercase tags parse like lowercase","text":"U","title":"Caps"},{"html":"<p><tag> & "q" end</p>","links":0,"name":"entities in text are decoded, nbsp collapses as whitespace","text":"<tag> & \"q\" end","title":""},{"html":"a<b>bold</b>c","links":0,"name":"inline markup splits text into space-joined chunks","text":"a bold c","title":""},{"html":"<title>x <a href=\"#\">y</a> z</title><p>out</p>","links":0,"name":"title is raw text: markup inside is literal and anchors do not count","text":"out","title":"x <a href=\"#\">y</a> z"},{"html":"<a title=\"2 > 1\" href=\"#\">go</a>","links":1,"name":"quoted attribute value may contain >","text":"go","title":""},{"html":"<script>1</script><style>2</style><a href=\"#\">after</a>","links":1,"name":"script and style both skipped, anchors after them count","text":"after","title":""},{"name":"unquoted attribute value ending in / is not a self-closing tag (script stays raw text)","html":"<script src=/x/>var x=\"<a href=x>fake</a>\";</script><p>real</p>","title":"","links":0,"text":"real"},{"name":"comment terminated by --!> does not swallow the rest of the document","html":"<!-- migration note --!><h1>Article title</h1><p>Article body</p>","title":"","links":0,"text":"Article title Article body"},{"name":"abruptly closed empty comment <!--> does not swallow the rest of the document","html":"<!--><p>shown</p>","title":"","links":0,"text":"shown"},{"name":"numeric character reference in the C1 range is remapped (€ is the euro sign)","html":"<title>Price €10</title><p>body</p>","title":"Price €10","links":0,"text":"body"},{"name":"numeric character references for NUL and a surrogate become U+FFFD","html":"<p>a�b�c</p>","title":"","links":0,"text":"a�b�c"},{"name":"out-of-range numeric character reference becomes U+FFFD","html":"<p>x�y</p>","title":"","links":0,"text":"x�y"},{"name":"apostrophe inside an unquoted attribute value does not open quote mode","html":"<a href=/it's>link</a><p>body</p>","title":"","links":1,"text":"link body"},{"name":"unknown entity names matching Object prototype properties stay literal","html":"<p>&constructor; &toString;</p>","title":"","links":0,"text":"&constructor; &toString;"},{"name":"script end tag with trailing attributes still closes the raw-text region","html":"<script>x</script foo><p>body</p>","title":"","links":0,"text":"body"}] |
|