Skip to content

Commit

Permalink
Merge branch 'master' into fix-tokenizer-onattribend
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 authored Jul 12, 2023
2 parents c0569fc + cd2336c commit 25daba5
Show file tree
Hide file tree
Showing 15 changed files with 1,081 additions and 977 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-automerge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v1.5.1
uses: dependabot/fetch-metadata@v1.6.0
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Enable auto-merge for Dependabot PRs
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nodejs-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
if: matrix.node == env.NODE_COV

- name: Run Coveralls
uses: coverallsapp/github-action@v2.1.2
uses: coverallsapp/github-action@v2.2.0
if: matrix.node == env.NODE_COV
continue-on-error: true
with:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const parser = new htmlparser2.Parser({
},
});
parser.write(
"Xyz <script type='text/javascript'>const foo = '<<bar>>';</script>"
"Xyz <script type='text/javascript'>const foo = '<<bar>>';</script>",
);
parser.end();
```
Expand Down
1,925 changes: 1,014 additions & 911 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@
"entities": "^4.5.0"
},
"devDependencies": {
"@types/jest": "^29.5.2",
"@types/node": "^20.2.5",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"eslint": "^8.42.0",
"@types/jest": "^29.5.3",
"@types/node": "^20.4.1",
"@typescript-eslint/eslint-plugin": "^5.61.0",
"@typescript-eslint/parser": "^5.62.0",
"eslint": "^8.44.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-n": "^16.0.0",
"eslint-plugin-n": "^16.0.1",
"eslint-plugin-unicorn": "^47.0.0",
"jest": "^29.5.0",
"prettier": "^2.8.8",
"ts-jest": "^29.1.0",
"typescript": "^5.1.3"
"jest": "^29.6.1",
"prettier": "^3.0.0",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
},
"jest": {
"preset": "ts-jest",
Expand Down
18 changes: 9 additions & 9 deletions src/FeedHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ describe("parseFeed", () => {
parseFeed(
await fs.readFile(
path.join(documents, "RSS_Example.xml"),
"utf8"
)
)
"utf8",
),
),
).toMatchSnapshot());

it("(atomFeed)", async () =>
expect(
parseFeed(
await fs.readFile(
path.join(documents, "Atom_Example.xml"),
"utf8"
)
)
"utf8",
),
),
).toMatchSnapshot());

it("(rdfFeed)", async () =>
expect(
parseFeed(
await fs.readFile(
path.join(documents, "RDF_Example.xml"),
"utf8"
)
)
"utf8",
),
),
).toMatchSnapshot());
});
6 changes: 3 additions & 3 deletions src/Parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ describe("API", () => {
p.end();
p.write("foo");
expect(cbs.onerror).toHaveBeenLastCalledWith(
new Error(".write() after done!")
new Error(".write() after done!"),
);
p.end();
expect(cbs.onerror).toHaveBeenLastCalledWith(
new Error(".end() after done!")
new Error(".end() after done!"),
);

// Should ignore the error if there is no callback
Expand Down Expand Up @@ -156,7 +156,7 @@ describe("API", () => {
expect(parser.tokenizer).toBeInstanceOf(CustomTokenizer);
},
},
{ Tokenizer: CustomTokenizer }
{ Tokenizer: CustomTokenizer },
);
p.done();
});
Expand Down
12 changes: 6 additions & 6 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ export interface Handler {
onattribute(
name: string,
value: string,
quote?: string | undefined | null
quote?: string | undefined | null,
): void;
onopentag(
name: string,
attribs: { [s: string]: string },
isImplied: boolean
isImplied: boolean,
): void;
ontext(data: string): void;
oncomment(data: string): void;
Expand Down Expand Up @@ -230,7 +230,7 @@ export class Parser implements Callbacks {

constructor(
cbs?: Partial<Handler> | null,
private readonly options: ParserOptions = {}
private readonly options: ParserOptions = {},
) {
this.cbs = cbs ?? {};
this.htmlMode = !this.options.xmlMode;
Expand All @@ -239,7 +239,7 @@ export class Parser implements Callbacks {
options.lowerCaseAttributeNames ?? this.htmlMode;
this.tokenizer = new (options.Tokenizer ?? Tokenizer)(
this.options,
this
this,
);
this.foreignContext = [!this.htmlMode];
this.cbs.onparserinit?.(this);
Expand Down Expand Up @@ -434,7 +434,7 @@ export class Parser implements Callbacks {
? "'"
: quote === QuoteType.NoValue
? undefined
: null
: null,
);

if (
Expand Down Expand Up @@ -565,7 +565,7 @@ export class Parser implements Callbacks {

let slice = this.buffers[0].slice(
start - this.bufferOffset,
end - this.bufferOffset
end - this.bufferOffset,
);

while (end - this.bufferOffset > this.buffers[0].length) {
Expand Down
14 changes: 7 additions & 7 deletions src/Tokenizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ function tokenize(data: string, options = {}) {
return (...values: unknown[]) =>
log.push([property, ...values]);
},
}
) as Callbacks
},
) as Callbacks,
);

tokenizer.write(data);
Expand Down Expand Up @@ -76,14 +76,14 @@ describe("Tokenizer", () => {
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'
)
'<img src="?&image_uri=1&&image;=2&image=3"/>?&image_uri=1&&image;=2&image=3',
),
).toMatchSnapshot());

it("for trailing legacy entity", () =>
Expand All @@ -108,8 +108,8 @@ describe("Tokenizer", () => {
log.push([property, ...values]);
};
},
}
) as Callbacks
},
) as Callbacks,
);

tokenizer.write("&am");
Expand Down
10 changes: 5 additions & 5 deletions src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ export default class Tokenizer {
xmlMode = false,
decodeEntities = true,
}: { xmlMode?: boolean; decodeEntities?: boolean },
private readonly cbs: Callbacks
private readonly cbs: Callbacks,
) {
this.xmlMode = xmlMode;
this.decodeEntities = decodeEntities;
this.entityDecoder = new EntityDecoder(
xmlMode ? xmlDecodeTree : htmlDecodeTree,
(cp, consumed) => this.emitCodePoint(cp, consumed)
(cp, consumed) => this.emitCodePoint(cp, consumed),
);
}

Expand Down Expand Up @@ -507,7 +507,7 @@ export default class Tokenizer {
quote === CharCodes.DoubleQuote
? QuoteType.Double
: QuoteType.Single,
this.index + 1
this.index + 1,
);
this.state = State.BeforeAttributeName;
} else if (this.decodeEntities && c === CharCodes.Amp) {
Expand Down Expand Up @@ -596,14 +596,14 @@ export default class Tokenizer {
: this.baseState === State.Text ||
this.baseState === State.InSpecialTag
? DecodingMode.Legacy
: DecodingMode.Attribute
: DecodingMode.Attribute,
);
}

private stateInEntity(): void {
const length = this.entityDecoder.write(
this.buffer,
this.index - this.offset
this.index - this.offset,
);

// If `length` is positive, we are done with the entity.
Expand Down
8 changes: 4 additions & 4 deletions src/WritableStream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("WritableStream", () => {

function getPromiseEventCollector(): [
handler: Partial<Handler>,
promise: Promise<unknown>
promise: Promise<unknown>,
] {
let handler: Partial<Handler> | undefined;
const promise = new Promise<unknown>((resolve, reject) => {
Expand All @@ -54,14 +54,14 @@ function finished(input: Parameters<typeof stream.finished>[0]): Promise<void> {

async function testStream(
file: string,
options?: ParserOptions
options?: ParserOptions,
): Promise<void> {
const filePath = path.join(__dirname, "__fixtures__", "Documents", file);

const [streamHandler, eventsPromise] = getPromiseEventCollector();

const fsStream = createReadStream(filePath).pipe(
new WritableStream(streamHandler, options)
new WritableStream(streamHandler, options),
);

await finished(fsStream);
Expand All @@ -73,7 +73,7 @@ async function testStream(
const [singlePassHandler, singlePassPromise] = getPromiseEventCollector();

const singlePassStream = new WritableStream(singlePassHandler, options).end(
await fs.readFile(filePath)
await fs.readFile(filePath),
);

await finished(singlePassStream);
Expand Down
4 changes: 2 additions & 2 deletions src/WritableStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export class WritableStream extends Writable {
override _write(
chunk: string | Buffer,
encoding: string,
callback: () => void
callback: () => void,
): void {
this._parser.write(
isBuffer(chunk, encoding) ? this._decoder.write(chunk) : chunk
isBuffer(chunk, encoding) ? this._decoder.write(chunk) : chunk,
);
callback();
}
Expand Down
6 changes: 3 additions & 3 deletions src/__fixtures__/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface Event {
* @param callback Function to call with all events.
*/
export function getEventCollector(
callback: (error: Error | null, events?: Event[]) => void
callback: (error: Error | null, events?: Event[]) => void,
): Partial<Handler> {
const events: Event[] = [];
let parser: Parser;
Expand Down Expand Up @@ -63,7 +63,7 @@ export function getEventCollector(

if (!(parser.startIndex <= parser.endIndex)) {
throw new Error(
`Invalid start/end index ${parser.startIndex} > ${parser.endIndex}`
`Invalid start/end index ${parser.startIndex} > ${parser.endIndex}`,
);
}

Expand All @@ -86,6 +86,6 @@ export function getEventCollector(
(_, event: string) =>
(...data: unknown[]) =>
handle(event, data),
}
},
);
}
Loading

0 comments on commit 25daba5

Please sign in to comment.