avancement planning

This commit is contained in:
2026-05-26 11:58:39 +02:00
parent 619a2b240a
commit 150b97cd2e
4892 changed files with 99214 additions and 429382 deletions
-37
View File
@@ -1,37 +0,0 @@
import fs from "node:fs/promises";
import { describe, it, expect } from "vitest";
import { parseFeed } from "./index.js";
const documents = new URL("__fixtures__/Documents/", import.meta.url);
describe("parseFeed", () => {
it("(rssFeed)", async () =>
expect(
parseFeed(
await fs.readFile(
new URL("RSS_Example.xml", documents),
"utf8",
),
),
).toMatchSnapshot());
it("(atomFeed)", async () =>
expect(
parseFeed(
await fs.readFile(
new URL("Atom_Example.xml", documents),
"utf8",
),
),
).toMatchSnapshot());
it("(rdfFeed)", async () =>
expect(
parseFeed(
await fs.readFile(
new URL("RDF_Example.xml", documents),
"utf8",
),
),
).toMatchSnapshot());
});
-228
View File
@@ -1,228 +0,0 @@
import { describe, it, expect, vi } from "vitest";
import { Parser, type ParserOptions } from "./Parser.js";
import * as helper from "./__fixtures__/testHelper.js";
/**
* Write to the parser twice, once a bytes, once as a single blob. Then check
* that we received the expected events.
*
* @internal
* @param input Data to write.
* @param options Parser options.
* @returns Promise that resolves if the test passes.
*/
function runTest(input: string, options?: ParserOptions) {
let firstResult: unknown[] | undefined;
return new Promise<void>((resolve, reject) => {
const handler = helper.getEventCollector((error, actual) => {
if (error) {
return reject(error);
}
if (firstResult) {
expect(actual).toEqual(firstResult);
resolve();
} else {
firstResult = actual;
expect(actual).toMatchSnapshot();
}
});
const parser = new Parser(handler, options);
// First, try to run the test via chunks
for (let index = 0; index < input.length; index++) {
parser.write(input.charAt(index));
}
parser.end();
// Then, parse everything
parser.parseComplete(input);
});
}
describe("Events", () => {
it("simple", () => runTest("<h1 class=test>adsf</h1>"));
it("Template script tags", () =>
runTest(
'<p><script type="text/template"><h1>Heading1</h1></script></p>',
));
it("Lowercase tags", () =>
runTest("<H1 class=test>adsf</H1>", { lowerCaseTags: true }));
it("CDATA", () =>
runTest("<tag><![CDATA[ asdf ><asdf></adsf><> fo]]></tag><![CD>", {
xmlMode: true,
}));
it("CDATA (inside special)", () =>
runTest(
"<script>/*<![CDATA[*/ asdf ><asdf></adsf><> fo/*]]>*/</script>",
));
it("leading lt", () => runTest(">a>"));
it("end slash: void element ending with />", () =>
runTest("<hr / ><p>Hold the line."));
it("end slash: void element ending with >", () =>
runTest("<hr ><p>Hold the line."));
it("end slash: void element ending with >, xmlMode=true", () =>
runTest("<hr ><p>Hold the line.", { xmlMode: true }));
it("end slash: non-void element ending with />", () =>
runTest("<xx / ><p>Hold the line."));
it("end slash: non-void element ending with />, xmlMode=true", () =>
runTest("<xx / ><p>Hold the line.", { xmlMode: true }));
it("end slash: non-void element ending with />, recognizeSelfClosing=true", () =>
runTest("<xx / ><p>Hold the line.", { recognizeSelfClosing: true }));
it("end slash: as part of attrib value of void element", () =>
runTest("<img src=gif.com/123/><p>Hold the line."));
it("end slash: as part of attrib value of non-void element", () =>
runTest("<a href=http://test.com/>Foo</a><p>Hold the line."));
it("Implicit close tags", () =>
runTest(
"<ol><li class=test><div><table style=width:100%><tr><th>TH<td colspan=2><h3>Heading</h3><tr><td><div>Div</div><td><div>Div2</div></table></div><li><div><h3>Heading 2</h3></div></li></ol><p>Para<h4>Heading 4</h4><p><ul><li>Hi<li>bye</ul>",
));
it("attributes (no white space, no value, no quotes)", () =>
runTest(
'<button class="test0"title="test1" disabled value=test2>adsf</button>',
));
it("crazy attribute", () => runTest("<p < = '' FAIL>stuff</p><a"));
it("Scripts creating other scripts", () =>
runTest("<p><script>var str = '<script></'+'script>';</script></p>"));
it("Long comment ending", () =>
runTest("<meta id='before'><!-- text ---><meta id='after'>"));
it("Long CDATA ending", () =>
runTest("<before /><tag><![CDATA[ text ]]]></tag><after />", {
xmlMode: true,
}));
it("Implicit open p and br tags", () =>
runTest("<div>Hallo</p>World</br></ignore></div></p></br>"));
it("lt followed by whitespace", () => runTest("a < b"));
it("double attribute", () => runTest("<h1 class=test class=boo></h1>"));
it("numeric entities", () =>
runTest("&#x61;&#x62&#99;&#100&#x66g&#x;&#x68"));
it("legacy entities", () => runTest("&AMPel&iacutee&ampeer;s&lter&sum"));
it("named entities", () =>
runTest("&amp;el&lt;er&CounterClockwiseContourIntegral;foo&bar"));
it("xml entities", () =>
runTest("&amp;&gt;&amp&lt;&uuml;&#x61;&#x62&#99;&#100&#101", {
xmlMode: true,
}));
it("entity in attribute", () =>
runTest(
"<a href='http://example.com/p&#x61;#x61ge?param=value&param2&param3=&lt;val&; & &'>",
));
it("double brackets", () =>
runTest("<<princess-purpose>>testing</princess-purpose>"));
it("legacy entities fail", () => runTest("M&M"));
it("Special special tags", () =>
runTest(
"<tItLe><b>foo</b><title></TiTlE><sitle><b></b></sitle><ttyle><b></b></ttyle><sCriPT></scripter</soo</sCript><STyLE></styler</STylE><sCiPt><stylee><scriptee><soo>",
));
it("Empty tag name", () => runTest("< ></ >"));
it("Not quite closed", () => runTest("<foo /bar></foo bar>"));
it("Entities in attributes", () =>
runTest("<foo bar=&amp; baz=\"&amp;\" boo='&amp;' noo=>"));
it("CDATA in HTML", () => runTest("<![CDATA[ foo ]]>"));
it("Comment edge-cases", () => runTest("<!-foo><!-- --- --><!--foo"));
it("CDATA edge-cases", () =>
runTest("<![CDATA><![CDATA[[]]sdaf]]><![CDATA[foo", {
recognizeCDATA: true,
}));
it("Comment false ending", () => runTest("<!-- a-b-> -->"));
it("Scripts ending with <", () => runTest("<script><</script>"));
it("CDATA more edge-cases", () =>
runTest("<![CDATA[foo]bar]>baz]]>", { recognizeCDATA: true }));
it("tag names are not ASCII alpha", () => runTest("<12>text</12>"));
it("open-implies-close case of (non-br) void close tag in non-XML mode", () =>
runTest("<select><input></select>", { lowerCaseAttributeNames: true }));
it("entity in attribute (#276)", () =>
runTest(
'<img src="?&image_uri=1&&image;=2&image=3"/>?&image_uri=1&&image;=2&image=3',
));
it("entity in title (#592)", () => runTest("<title>the &quot;title&quot"));
it("entity in title - decodeEntities=false (#592)", () =>
runTest("<title>the &quot;title&quot;", { decodeEntities: false }));
it("</title> in <script> (#745)", () =>
runTest("<script>'</title>'</script>"));
it("XML tags", () => runTest("<:foo><_bar>", { xmlMode: true }));
it("Trailing legacy entity", () => runTest("&timesbar;&timesbar"));
it("Trailing numeric entity", () => runTest("&#53&#53"));
it("Multi-byte entity", () => runTest("&NotGreaterFullEqual;"));
it("Start & end indices from domhandler", () =>
runTest(
"<!DOCTYPE html> <html> <title>The Title</title> <body class='foo'>Hello world <p></p></body> <!-- the comment --> </html> ",
));
it("Self-closing indices (#941)", () =>
runTest("<xml><a/><b/></xml>", { xmlMode: true }));
it("Entity after <", () => runTest("<&amp;"));
it("Attribute in XML (see #1350)", () =>
runTest(
'<Page\n title="Hello world"\n actionBarVisible="false"/>',
{ xmlMode: true },
));
});
describe("Helper", () => {
it("should handle errors", () => {
const eventCallback = vi.fn();
const parser = new Parser(helper.getEventCollector(eventCallback));
parser.end();
parser.write("foo");
expect(eventCallback).toHaveBeenCalledTimes(2);
expect(eventCallback).toHaveBeenNthCalledWith(1, null, []);
expect(eventCallback).toHaveBeenLastCalledWith(
new Error(".write() after done!"),
);
});
});
-164
View File
@@ -1,164 +0,0 @@
import { describe, it, expect, vi } from "vitest";
import { Parser, Tokenizer } from "./index.js";
import type { Handler } from "./Parser.js";
describe("API", () => {
it("should work without callbacks", () => {
const cbs: Partial<Handler> = { onerror: vi.fn() };
const p = new Parser(cbs, {
xmlMode: true,
lowerCaseAttributeNames: true,
});
p.end("<a foo><bar></a><!-- --><![CDATA[]]]><?foo?><!bar><boo/>boohay");
p.write("foo");
// Check for an error
p.end();
p.write("foo");
expect(cbs.onerror).toHaveBeenLastCalledWith(
new Error(".write() after done!"),
);
p.end();
expect(cbs.onerror).toHaveBeenLastCalledWith(
new Error(".end() after done!"),
);
// Should ignore the error if there is no callback
delete cbs.onerror;
p.write("foo");
p.reset();
// Remove method
cbs.onopentag = vi.fn();
p.write("<a foo");
delete cbs.onopentag;
p.write(">");
// Pause/resume
const onText = vi.fn();
cbs.ontext = onText;
p.pause();
p.write("foo");
expect(onText).not.toHaveBeenCalled();
p.resume();
expect(onText).toHaveBeenLastCalledWith("foo");
p.pause();
expect(onText).toHaveBeenCalledTimes(1);
p.resume();
expect(onText).toHaveBeenCalledTimes(1);
p.pause();
p.end("bar");
expect(onText).toHaveBeenCalledTimes(1);
p.resume();
expect(onText).toHaveBeenCalledTimes(2);
expect(onText).toHaveBeenLastCalledWith("bar");
});
it("should back out of numeric entities (#125)", () => {
const onend = vi.fn();
let text = "";
const p = new Parser({
ontext(data) {
text += data;
},
onend,
});
p.end("id=770&#anchor");
expect(onend).toHaveBeenCalledTimes(1);
expect(text).toBe("id=770&#anchor");
p.reset();
text = "";
p.end("0&#xn");
expect(onend).toHaveBeenCalledTimes(2);
expect(text).toBe("0&#xn");
});
it("should not have the start index be greater than the end index", () => {
const onopentag = vi.fn();
const onclosetag = vi.fn();
const p = new Parser({
onopentag(tag) {
expect(p.startIndex).toBeLessThanOrEqual(p.endIndex);
onopentag(tag, p.startIndex, p.endIndex);
},
onclosetag(tag) {
expect(p.startIndex).toBeLessThanOrEqual(p.endIndex);
onclosetag(tag, p.endIndex);
},
});
p.write("<p>");
expect(onopentag).toHaveBeenLastCalledWith("p", 0, 2);
expect(onclosetag).not.toHaveBeenCalled();
p.write("Foo");
p.write("<hr>");
expect(onopentag).toHaveBeenLastCalledWith("hr", 6, 9);
expect(onclosetag).toHaveBeenCalledTimes(2);
expect(onclosetag).toHaveBeenNthCalledWith(1, "p", 9);
expect(onclosetag).toHaveBeenNthCalledWith(2, "hr", 9);
});
it("should update the position when a single tag is spread across multiple chunks", () => {
let called = false;
const p = new Parser({
onopentag() {
called = true;
expect(p.startIndex).toBe(0);
expect(p.endIndex).toBe(12);
},
});
p.write("<div ");
p.write("foo=bar>");
expect(called).toBe(true);
});
it("should have the correct position for implied opening tags", () => {
let called = false;
const p = new Parser({
onopentag() {
called = true;
expect(p.startIndex).toBe(0);
expect(p.endIndex).toBe(3);
},
});
p.write("</p>");
expect(called).toBe(true);
});
it("should parse <__proto__> (#387)", () => {
const p = new Parser(null);
// Should not throw
p.parseChunk("<__proto__>");
});
it("should support custom tokenizer", () => {
class CustomTokenizer extends Tokenizer {}
const p = new Parser(
{
onparserinit(parser: Parser) {
// @ts-expect-error Accessing private tokenizer here
expect(parser.tokenizer).toBeInstanceOf(CustomTokenizer);
},
},
{ Tokenizer: CustomTokenizer },
);
p.done();
});
});
-162
View File
@@ -1,162 +0,0 @@
import { describe, it, expect } from "vitest";
import { Tokenizer } from "./index.js";
import type { Callbacks } from "./Tokenizer.js";
function tokenize(data: string, options = {}) {
const log: unknown[][] = [];
const tokenizer = new Tokenizer(
options,
new Proxy(
{},
{
get(_, property) {
return (...values: unknown[]) =>
log.push([property, ...values]);
},
},
) as Callbacks,
);
tokenizer.write(data);
tokenizer.end();
return log;
}
describe("Tokenizer", () => {
describe("should support self-closing special tags", () => {
it("for self-closing script tag", () => {
expect(tokenize("<script /><div></div>")).toMatchSnapshot();
});
it("for self-closing style tag", () => {
expect(tokenize("<style /><div></div>")).toMatchSnapshot();
});
it("for self-closing title tag", () => {
expect(tokenize("<title /><div></div>")).toMatchSnapshot();
});
it("for self-closing textarea tag", () => {
expect(tokenize("<textarea /><div></div>")).toMatchSnapshot();
});
it("for self-closing xmp tag", () => {
expect(tokenize("<xmp /><div></div>")).toMatchSnapshot();
});
});
describe("should support standard special tags", () => {
it("for normal script tag", () => {
expect(tokenize("<script></script><div></div>")).toMatchSnapshot();
});
it("for normal style tag", () => {
expect(tokenize("<style></style><div></div>")).toMatchSnapshot();
});
it("for normal sitle tag", () => {
expect(tokenize("<title></title><div></div>")).toMatchSnapshot();
});
it("for normal textarea tag", () => {
expect(
tokenize("<textarea></textarea><div></div>"),
).toMatchSnapshot();
});
it("for normal xmp tag", () => {
expect(tokenize("<xmp></xmp><div></div>")).toMatchSnapshot();
});
});
describe("should treat html inside special tags as text", () => {
it("for div inside script tag", () => {
expect(tokenize("<script><div></div></script>")).toMatchSnapshot();
});
it("for div inside style tag", () => {
expect(tokenize("<style><div></div></style>")).toMatchSnapshot();
});
it("for div inside title tag", () => {
expect(tokenize("<title><div></div></title>")).toMatchSnapshot();
});
it("for div inside textarea tag", () => {
expect(
tokenize("<textarea><div></div></textarea>"),
).toMatchSnapshot();
});
it("for div inside xmp tag", () => {
expect(tokenize("<xmp><div></div></xmp>")).toMatchSnapshot();
});
});
describe("should correctly mark attributes", () => {
it("for no value attribute", () => {
expect(tokenize("<div aaaaaaa >")).toMatchSnapshot();
});
it("for no quotes attribute", () => {
expect(tokenize("<div aaa=aaa >")).toMatchSnapshot();
});
it("for single quotes attribute", () => {
expect(tokenize("<div aaa='a' >")).toMatchSnapshot();
});
it("for double quotes attribute", () => {
expect(tokenize('<div aaa="a" >')).toMatchSnapshot();
});
});
describe("should not break after special tag followed by an entity", () => {
it("for normal special tag", () => {
expect(tokenize("<style>a{}</style>&apos;<br/>")).toMatchSnapshot();
});
it("for self-closing special tag", () => {
expect(tokenize("<style />&apos;<br/>")).toMatchSnapshot();
});
});
describe("should handle entities", () => {
it("for XML entities", () =>
expect(
tokenize("&amp;&gt;&amp&lt;&uuml;&#x61;&#x62&#99;&#100&#101", {
xmlMode: true,
}),
).toMatchSnapshot());
it("for entities in attributes (#276)", () =>
expect(
tokenize(
'<img src="?&image_uri=1&&image;=2&image=3"/>?&image_uri=1&&image;=2&image=3',
),
).toMatchSnapshot());
it("for trailing legacy entity", () =>
expect(tokenize("&timesbar;&timesbar")).toMatchSnapshot());
it("for multi-byte entities", () =>
expect(tokenize("&NotGreaterFullEqual;")).toMatchSnapshot());
});
it("should not lose data when pausing", () => {
const log: unknown[][] = [];
const tokenizer = new Tokenizer(
{},
new Proxy(
{},
{
get(_, property) {
return (...values: unknown[]) => {
if (property === "ontext") {
tokenizer.pause();
}
log.push([property, ...values]);
};
},
},
) as Callbacks,
);
tokenizer.write("&am");
tokenizer.write("p; it up!");
tokenizer.resume();
tokenizer.resume();
// Tokenizer shouldn't be paused
expect(tokenizer).toHaveProperty("running", true);
tokenizer.end();
expect(log).toMatchSnapshot();
});
});
+12 -5
View File
@@ -635,19 +635,26 @@ export default class Tokenizer {
}
private stateInEntity(): void {
const length = this.entityDecoder.write(
this.buffer,
this.index - this.offset,
);
const indexInBuffer = this.index - this.offset;
const length = this.entityDecoder.write(this.buffer, indexInBuffer);
// If `length` is positive, we are done with the entity.
if (length >= 0) {
this.state = this.baseState;
if (length === 0) {
this.index = this.entityStart;
this.index -= 1;
}
} else {
if (
indexInBuffer < this.buffer.length &&
this.buffer.charCodeAt(indexInBuffer) === CharCodes.Amp
) {
this.state = this.baseState;
this.index -= 1;
return;
}
// Mark buffer as consumed.
this.index = this.offset + this.buffer.length - 1;
}
-82
View File
@@ -1,82 +0,0 @@
import { createReadStream } from "node:fs";
import * as fs from "node:fs/promises";
import * as stream from "node:stream";
import { describe, it, expect, vi } from "vitest";
import type { Handler, ParserOptions } from "./Parser.js";
import { WritableStream } from "./WritableStream.js";
import * as helper from "./__fixtures__/testHelper.js";
describe("WritableStream", () => {
it("should decode fragmented unicode characters", () => {
const ontext = vi.fn();
const stream = new WritableStream({ ontext });
stream.write(Buffer.from([0xe2, 0x82]));
stream.write(Buffer.from([0xac]));
stream.write("");
stream.end();
expect(ontext).toHaveBeenCalledWith("€");
});
it("Basic html", () => testStream("Basic.html"));
it("Attributes", () => testStream("Attributes.html"));
it("SVG", () => testStream("Svg.html"));
it("RSS feed", () => testStream("RSS_Example.xml", { xmlMode: true }));
it("Atom feed", () => testStream("Atom_Example.xml", { xmlMode: true }));
it("RDF feed", () => testStream("RDF_Example.xml", { xmlMode: true }));
});
function getPromiseEventCollector(): [
handler: Partial<Handler>,
promise: Promise<unknown>,
] {
let handler: Partial<Handler> | undefined;
const promise = new Promise<unknown>((resolve, reject) => {
handler = helper.getEventCollector((error, events) => {
if (error) {
reject(error);
} else {
resolve(events);
}
});
});
return [handler!, promise];
}
// TODO[engine:node@>=16]: Use promise version of `stream.finished` instead.
function finished(input: Parameters<typeof stream.finished>[0]): Promise<void> {
return new Promise((resolve, reject) => {
stream.finished(input, (error) => (error ? reject(error) : resolve()));
});
}
async function testStream(
file: string,
options?: ParserOptions,
): Promise<void> {
const filePath = new URL(`__fixtures__/Documents/${file}`, import.meta.url);
const [streamHandler, eventsPromise] = getPromiseEventCollector();
const fsStream = createReadStream(filePath).pipe(
new WritableStream(streamHandler, options),
);
await finished(fsStream);
const events = await eventsPromise;
expect(events).toMatchSnapshot();
const [singlePassHandler, singlePassPromise] = getPromiseEventCollector();
const singlePassStream = new WritableStream(singlePassHandler, options).end(
await fs.readFile(filePath),
);
await finished(singlePassStream);
expect(await singlePassPromise).toStrictEqual(events);
}
-27
View File
@@ -1,27 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- http://en.wikipedia.org/wiki/Atom_%28standard%29 -->
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Example Feed</title>
<subtitle>A subtitle.</subtitle>
<link href="http://example.org/feed/" rel="self" />
<link href="http://example.org/" />
<id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id>
<updated>2003-12-13T18:30:02Z</updated>
<author>
<name>John Doe</name>
<email>johndoe@example.com</email>
</author>
<entry>
<title>Atom-Powered Robots Run Amok</title>
<link href="http://example.org/2003/12/13/atom03" />
<link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/>
<link rel="edit" href="http://example.org/2003/12/13/atom03/edit"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<content type="html"><p>Some content.</p></content>
</entry>
<entry/>
</feed>
-16
View File
@@ -1,16 +0,0 @@
<!doctype html>
<html>
<head>
<title>Attributes test</title>
</head>
<body>
<!-- Normal attributes -->
<button id="test0" class="value0" title="value1">class="value0" title="value1"</button>
<!-- Attributes with no quotes or value -->
<button id="test1" class=value2 disabled>class=value2 disabled</button>
<!-- Attributes with no space between them. No valid, but accepted by the browser -->
<button id="test2" class="value4"title="value5">class="value4"title="value5"</button>
</body>
</html>
-1
View File
@@ -1 +0,0 @@
<!DOCTYPE html><html><title>The Title</title><body>Hello world</body></html>
-63
View File
@@ -1,63 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:ev="http://purl.org/rss/1.0/modules/event/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:admin="http://webns.net/mvcb/">
<channel rdf:about="https://github.com/fb55/htmlparser2/">
<title>A title to parse and remember</title>
<link>https://github.com/fb55/htmlparser2/</link>
<description/>
<dc:language>en-us</dc:language>
<dc:rights>Copyright 2015 the authors</dc:rights>
<dc:publisher>webmaster@thisisafakedoma.in</dc:publisher>
<dc:creator>webmaster@thisisafakedoma.in</dc:creator>
<dc:source>https://github.com/fb55/htmlparser2/</dc:source>
<dc:title>A title to parse and remember</dc:title>
<dc:type>Collection</dc:type>
<syn:updateBase>2011-11-04T09:39:10-07:00</syn:updateBase>
<syn:updateFrequency>4</syn:updateFrequency>
<syn:updatePeriod>hourly</syn:updatePeriod>
<items>
<rdf:Seq>
<rdf:li rdf:resource="http://somefakesite/path/to/something.html"/>
</rdf:Seq>
</items>
</channel>
<item rdf:about="http://somefakesite/path/to/something.html">
<title><![CDATA[ Fast HTML Parsing ]]></title>
<link>
http://somefakesite/path/to/something.html
</link>
<description><![CDATA[
Great test content<br>A link: <a href="http://github.com">Github</a>
]]></description>
<dc:date>2011-11-04T09:35:17-07:00</dc:date>
<dc:language>en-us</dc:language>
<dc:rights>Copyright 2015 the authors</dc:rights>
<dc:source>
http://somefakesite/path/to/something.html
</dc:source>
<dc:title><![CDATA[ Fast HTML Parsing ]]></dc:title>
<dc:type>text</dc:type>
<dcterms:issued>2011-11-04T09:35:17-07:00</dcterms:issued>
</item>
<item rdf:about="http://somefakesite/path/to/something-else.html">
<title><![CDATA[
This space intentionally left blank
]]></title>
<link>
http://somefakesite/path/to/something-else.html
</link>
<description><![CDATA[
The early bird gets the worm
]]></description>
<dc:date>2011-11-04T09:34:54-07:00</dc:date>
<dc:language>en-us</dc:language>
<dc:rights>Copyright 2015 the authors</dc:rights>
<dc:source>
http://somefakesite/path/to/something-else.html
</dc:source>
<dc:title><![CDATA[
This space intentionally left blank
]]></dc:title>
<dc:type>text</dc:type>
<dcterms:issued>2011-11-04T09:34:54-07:00</dcterms:issued>
</item>
</rdf:RDF>
-49
View File
@@ -1,49 +0,0 @@
<?xml version="1.0"?>
<!-- http://cyber.law.harvard.edu/rss/examples/rss2sample.xml -->
<rss version="2.0">
<channel>
<title>Liftoff News</title>
<link>http://liftoff.msfc.nasa.gov/</link>
<description>Liftoff to Space Exploration.</description>
<language>en-us</language>
<pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
<lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<generator>Weblog Editor 2.0</generator>
<managingEditor>editor@example.com</managingEditor>
<webMaster>webmaster@example.com</webMaster>
<item>
<title>Star City</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
<description>How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's &lt;a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm"&gt;Star City&lt;/a&gt;.</description>
<pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
</item>
<item>
<description>Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a &lt;a href="http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm"&gt;partial eclipse of the Sun&lt;/a&gt; on Saturday, May 31st.</description>
<pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>
</item>
<item>
<title>The Engine That Does More</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>
<description>Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly. The proposed VASIMR engine would do that.</description>
<pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid>
</item>
<item>
<title>Astronauts' Dirty Laundry</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>
<description>Compared to earlier spacecraft, the International Space Station has many luxuries, but laundry facilities are not one of them. Instead, astronauts have other options.</description>
<pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid>
<media:content height="200" medium="image" url="https://picsum.photos/200" width="200"/>
</item>
</channel>
</rss>
-19
View File
@@ -1,19 +0,0 @@
<!doctype html>
<html>
<head>
<title>SVG test</title>
</head>
<body>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Test</title>
<animate />
<polygon />
<g>
<path>
<title>x</title>
<animate />
</path>
</g>
</svg>
</body>
</html>
-89
View File
@@ -1,89 +0,0 @@
import type { Parser, Handler } from "../Parser.js";
interface Event {
$event: string;
data: unknown[];
startIndex: number;
endIndex: number;
}
/**
* Creates a handler that calls the supplied callback with simplified events on
* completion.
*
* @internal
* @param callback Function to call with all events.
*/
export function getEventCollector(
callback: (error: Error | null, events?: Event[]) => void,
): Partial<Handler> {
const events: Event[] = [];
let parser: Parser;
function handle(event: string, data: unknown[]): void {
switch (event) {
case "onerror": {
callback(data[0] as Error);
break;
}
case "onend": {
callback(null, events);
break;
}
case "onreset": {
events.length = 0;
break;
}
case "onparserinit": {
parser = data[0] as Parser;
// Don't collect event
break;
}
default: {
// eslint-disable-next-line unicorn/prefer-at
const last = events[events.length - 1];
// Combine text nodes
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (event === "ontext" && last && last.$event === "text") {
(last.data[0] as string) += data[0];
last.endIndex = parser.endIndex;
break;
}
// Remove `undefined`s from attribute responses, as they cannot be represented in JSON.
if (event === "onattribute" && data[2] === undefined) {
data.pop();
}
if (!(parser.startIndex <= parser.endIndex)) {
throw new Error(
`Invalid start/end index ${parser.startIndex} > ${parser.endIndex}`,
);
}
events.push({
$event: event.slice(2),
startIndex: parser.startIndex,
endIndex: parser.endIndex,
data,
});
}
}
}
return new Proxy(
{},
{
get:
(_, event: string) =>
(...data: unknown[]) =>
handle(event, data),
},
);
}
-103
View File
@@ -1,103 +0,0 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`parseFeed > (atomFeed) 1`] = `
{
"author": "johndoe@example.com",
"description": "A subtitle.",
"id": "urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6",
"items": [
{
"description": "Some content.",
"id": "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a",
"link": "http://example.org/2003/12/13/atom03",
"media": [],
"pubDate": 2003-12-13T18:30:02.000Z,
"title": "Atom-Powered Robots Run Amok",
},
{
"media": [],
},
],
"link": "http://example.org/feed/",
"title": "Example Feed",
"type": "atom",
"updated": 2003-12-13T18:30:02.000Z,
}
`;
exports[`parseFeed > (rdfFeed) 1`] = `
{
"id": "",
"items": [
{
"description": "Great test content<br>A link: <a href="http://github.com">Github</a>",
"link": "http://somefakesite/path/to/something.html",
"media": [],
"pubDate": 2011-11-04T16:35:17.000Z,
"title": "Fast HTML Parsing",
},
{
"description": "The early bird gets the worm",
"link": "http://somefakesite/path/to/something-else.html",
"media": [],
"pubDate": 2011-11-04T16:34:54.000Z,
"title": "This space intentionally left blank",
},
],
"link": "https://github.com/fb55/htmlparser2/",
"title": "A title to parse and remember",
"type": "rdf",
}
`;
exports[`parseFeed > (rssFeed) 1`] = `
{
"author": "editor@example.com",
"description": "Liftoff to Space Exploration.",
"id": "",
"items": [
{
"description": "How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's <a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm">Star City</a>.",
"id": "http://liftoff.msfc.nasa.gov/2003/06/03.html#item573",
"link": "http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp",
"media": [],
"pubDate": 2003-06-03T09:39:21.000Z,
"title": "Star City",
},
{
"description": "Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a <a href="http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm">partial eclipse of the Sun</a> on Saturday, May 31st.",
"id": "http://liftoff.msfc.nasa.gov/2003/05/30.html#item572",
"media": [],
"pubDate": 2003-05-30T11:06:42.000Z,
},
{
"description": "Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly. The proposed VASIMR engine would do that.",
"id": "http://liftoff.msfc.nasa.gov/2003/05/27.html#item571",
"link": "http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp",
"media": [],
"pubDate": 2003-05-27T08:37:32.000Z,
"title": "The Engine That Does More",
},
{
"description": "Compared to earlier spacecraft, the International Space Station has many luxuries, but laundry facilities are not one of them. Instead, astronauts have other options.",
"id": "http://liftoff.msfc.nasa.gov/2003/05/20.html#item570",
"link": "http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp",
"media": [
{
"height": 200,
"isDefault": false,
"medium": "image",
"url": "https://picsum.photos/200",
"width": 200,
},
],
"pubDate": 2003-05-20T08:56:02.000Z,
"title": "Astronauts' Dirty Laundry",
},
],
"link": "http://liftoff.msfc.nasa.gov/",
"title": "Liftoff News",
"type": "rss",
"updated": 2003-06-10T09:41:01.000Z,
}
`;
File diff suppressed because it is too large Load Diff
-834
View File
@@ -1,834 +0,0 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`Tokenizer > should correctly mark attributes > for double quotes attribute 1`] = `
[
[
"onopentagname",
1,
4,
],
[
"onattribname",
5,
8,
],
[
"onattribdata",
10,
11,
],
[
"onattribend",
3,
12,
],
[
"onopentagend",
13,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should correctly mark attributes > for no quotes attribute 1`] = `
[
[
"onopentagname",
1,
4,
],
[
"onattribname",
5,
8,
],
[
"onattribdata",
9,
12,
],
[
"onattribend",
1,
12,
],
[
"onopentagend",
13,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should correctly mark attributes > for no value attribute 1`] = `
[
[
"onopentagname",
1,
4,
],
[
"onattribname",
5,
12,
],
[
"onattribend",
0,
12,
],
[
"onopentagend",
13,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should correctly mark attributes > for single quotes attribute 1`] = `
[
[
"onopentagname",
1,
4,
],
[
"onattribname",
5,
8,
],
[
"onattribdata",
10,
11,
],
[
"onattribend",
2,
12,
],
[
"onopentagend",
13,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should handle entities > for XML entities 1`] = `
[
[
"ontextentity",
38,
5,
],
[
"ontextentity",
62,
9,
],
[
"ontext",
9,
13,
],
[
"ontextentity",
60,
17,
],
[
"ontext",
17,
23,
],
[
"ontextentity",
97,
29,
],
[
"ontext",
29,
34,
],
[
"ontextentity",
99,
39,
],
[
"ontext",
39,
49,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should handle entities > for entities in attributes (#276) 1`] = `
[
[
"onopentagname",
1,
4,
],
[
"onattribname",
5,
8,
],
[
"onattribdata",
10,
24,
],
[
"onattribentity",
8465,
],
[
"onattribdata",
31,
41,
],
[
"onattribend",
3,
42,
],
[
"onselfclosingtag",
43,
],
[
"ontext",
44,
58,
],
[
"ontextentity",
8465,
65,
],
[
"ontext",
65,
75,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should handle entities > for multi-byte entities 1`] = `
[
[
"ontextentity",
8807,
21,
],
[
"ontextentity",
824,
21,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should handle entities > for trailing legacy entity 1`] = `
[
[
"ontextentity",
10801,
10,
],
[
"ontextentity",
215,
16,
],
[
"ontext",
16,
19,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should not break after special tag followed by an entity > for normal special tag 1`] = `
[
[
"onopentagname",
1,
6,
],
[
"onopentagend",
6,
],
[
"ontext",
7,
10,
],
[
"onclosetag",
12,
17,
],
[
"ontextentity",
39,
24,
],
[
"onopentagname",
25,
27,
],
[
"onselfclosingtag",
28,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should not break after special tag followed by an entity > for self-closing special tag 1`] = `
[
[
"onopentagname",
1,
6,
],
[
"onselfclosingtag",
8,
],
[
"ontextentity",
39,
15,
],
[
"onopentagname",
16,
18,
],
[
"onselfclosingtag",
19,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should not lose data when pausing 1`] = `
[
[
"ontextentity",
38,
5,
],
[
"ontext",
5,
12,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should support self-closing special tags > for self-closing script tag 1`] = `
[
[
"onopentagname",
1,
7,
],
[
"onselfclosingtag",
9,
],
[
"onopentagname",
11,
14,
],
[
"onopentagend",
14,
],
[
"onclosetag",
17,
20,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should support self-closing special tags > for self-closing style tag 1`] = `
[
[
"onopentagname",
1,
6,
],
[
"onselfclosingtag",
8,
],
[
"onopentagname",
10,
13,
],
[
"onopentagend",
13,
],
[
"onclosetag",
16,
19,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should support self-closing special tags > for self-closing textarea tag 1`] = `
[
[
"onopentagname",
1,
9,
],
[
"onselfclosingtag",
11,
],
[
"onopentagname",
13,
16,
],
[
"onopentagend",
16,
],
[
"onclosetag",
19,
22,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should support self-closing special tags > for self-closing title tag 1`] = `
[
[
"onopentagname",
1,
6,
],
[
"onselfclosingtag",
8,
],
[
"onopentagname",
10,
13,
],
[
"onopentagend",
13,
],
[
"onclosetag",
16,
19,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should support self-closing special tags > for self-closing xmp tag 1`] = `
[
[
"onopentagname",
1,
4,
],
[
"onselfclosingtag",
6,
],
[
"onopentagname",
8,
11,
],
[
"onopentagend",
11,
],
[
"onclosetag",
14,
17,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should support standard special tags > for normal script tag 1`] = `
[
[
"onopentagname",
1,
7,
],
[
"onopentagend",
7,
],
[
"onclosetag",
10,
16,
],
[
"onopentagname",
18,
21,
],
[
"onopentagend",
21,
],
[
"onclosetag",
24,
27,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should support standard special tags > for normal sitle tag 1`] = `
[
[
"onopentagname",
1,
6,
],
[
"onopentagend",
6,
],
[
"onclosetag",
9,
14,
],
[
"onopentagname",
16,
19,
],
[
"onopentagend",
19,
],
[
"onclosetag",
22,
25,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should support standard special tags > for normal style tag 1`] = `
[
[
"onopentagname",
1,
6,
],
[
"onopentagend",
6,
],
[
"onclosetag",
9,
14,
],
[
"onopentagname",
16,
19,
],
[
"onopentagend",
19,
],
[
"onclosetag",
22,
25,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should support standard special tags > for normal textarea tag 1`] = `
[
[
"onopentagname",
1,
9,
],
[
"onopentagend",
9,
],
[
"onclosetag",
12,
20,
],
[
"onopentagname",
22,
25,
],
[
"onopentagend",
25,
],
[
"onclosetag",
28,
31,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should support standard special tags > for normal xmp tag 1`] = `
[
[
"onopentagname",
1,
4,
],
[
"onopentagend",
4,
],
[
"onclosetag",
7,
10,
],
[
"onopentagname",
12,
15,
],
[
"onopentagend",
15,
],
[
"onclosetag",
18,
21,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should treat html inside special tags as text > for div inside script tag 1`] = `
[
[
"onopentagname",
1,
7,
],
[
"onopentagend",
7,
],
[
"ontext",
8,
19,
],
[
"onclosetag",
21,
27,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should treat html inside special tags as text > for div inside style tag 1`] = `
[
[
"onopentagname",
1,
6,
],
[
"onopentagend",
6,
],
[
"ontext",
7,
18,
],
[
"onclosetag",
20,
25,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should treat html inside special tags as text > for div inside textarea tag 1`] = `
[
[
"onopentagname",
1,
9,
],
[
"onopentagend",
9,
],
[
"ontext",
10,
21,
],
[
"onclosetag",
23,
31,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should treat html inside special tags as text > for div inside title tag 1`] = `
[
[
"onopentagname",
1,
6,
],
[
"onopentagend",
6,
],
[
"ontext",
7,
18,
],
[
"onclosetag",
20,
25,
],
[
"onend",
],
]
`;
exports[`Tokenizer > should treat html inside special tags as text > for div inside xmp tag 1`] = `
[
[
"onopentagname",
1,
4,
],
[
"onopentagend",
4,
],
[
"ontext",
5,
16,
],
[
"onclosetag",
18,
21,
],
[
"onend",
],
]
`;
File diff suppressed because it is too large Load Diff
-87
View File
@@ -1,87 +0,0 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`Index > createDocumentStream 1`] = `
Document {
"children": [
&This is text,
<!-- and comments -->,
<tags />,
],
"endIndex": null,
"next": null,
"parent": null,
"prev": null,
"startIndex": null,
"type": "root",
}
`;
exports[`Index > createDomStream 1`] = `
[
&This is text,
<!-- and comments -->,
<tags />,
]
`;
exports[`Index > parseDOM 1`] = `
[
<a
foo=""
>
<b>
<c>
ProcessingInstruction {
"data": "?foo",
"endIndex": null,
"name": "?foo",
"next": Yay!,
"parent": <c>
[Circular]
Yay!
</c>,
"prev": null,
"startIndex": null,
"type": "directive",
}
Yay!
</c>
</b>
</a>,
]
`;
exports[`Index > parseDocument 1`] = `
Document {
"children": [
<a
foo=""
>
<b>
<c>
ProcessingInstruction {
"data": "?foo",
"endIndex": null,
"name": "?foo",
"next": Yay!,
"parent": <c>
[Circular]
Yay!
</c>,
"prev": null,
"startIndex": null,
"type": "directive",
}
Yay!
</c>
</b>
</a>,
],
"endIndex": null,
"next": null,
"parent": null,
"prev": null,
"startIndex": null,
"type": "root",
}
`;
-79
View File
@@ -1,79 +0,0 @@
import { describe, it, expect } from "vitest";
import {
parseDocument,
parseDOM,
createDocumentStream,
createDomStream,
DomHandler,
DefaultHandler,
type Parser,
} from "./index.js";
import { Element } from "domhandler";
// Add an `attributes` prop to the Element for now, to make it possible for Jest to render DOM nodes.
Object.defineProperty(Element.prototype, "attributes", {
get() {
return Object.keys(this.attribs).map((name) => ({
name,
value: this.attribs[name],
}));
},
configurable: true,
enumerable: false,
});
describe("Index", () => {
it("parseDocument", () => {
const dom = parseDocument("<a foo><b><c><?foo>Yay!");
expect(dom).toMatchSnapshot();
});
it("parseDOM", () => {
const dom = parseDOM("<a foo><b><c><?foo>Yay!");
expect(dom).toMatchSnapshot();
});
it("createDocumentStream", () => {
let documentStream!: Parser;
const documentPromise = new Promise(
(resolve, reject) =>
(documentStream = createDocumentStream((error, dom) =>
error ? reject(error) : resolve(dom),
)),
);
for (const c of "&amp;This is text<!-- and comments --><tags>") {
documentStream.write(c);
}
documentStream.end();
return expect(documentPromise).resolves.toMatchSnapshot();
});
it("createDomStream", () => {
let domStream!: Parser;
const domPromise = new Promise(
(resolve, reject) =>
(domStream = createDomStream((error, dom) =>
error ? reject(error) : resolve(dom),
)),
);
for (const c of "&amp;This is text<!-- and comments --><tags>") {
domStream.write(c);
}
domStream.end();
return expect(domPromise).resolves.toMatchSnapshot();
});
describe("API", () => {
it("should export the appropriate APIs", () => {
expect(DomHandler).toEqual(DefaultHandler);
});
});
});