Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Tokenizer onattribend's endIndex #1540

Merged
merged 4 commits into from
Jul 16, 2023
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
15 changes: 15 additions & 0 deletions src/Tokenizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ describe("Tokenizer", () => {
});
});

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();
Expand Down
9 changes: 5 additions & 4 deletions src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ export default class Tokenizer {
private stateInAttributeName(c: number): void {
if (c === CharCodes.Eq || isEndOfTagSection(c)) {
this.cbs.onattribname(this.sectionStart, this.index);
this.sectionStart = -1;
this.sectionStart = this.index;
this.state = State.AfterAttributeName;
this.stateAfterAttributeName(c);
}
Expand All @@ -473,11 +473,12 @@ export default class Tokenizer {
if (c === CharCodes.Eq) {
this.state = State.BeforeAttributeValue;
} else if (c === CharCodes.Slash || c === CharCodes.Gt) {
this.cbs.onattribend(QuoteType.NoValue, this.index);
this.cbs.onattribend(QuoteType.NoValue, this.sectionStart);
this.sectionStart = -1;
this.state = State.BeforeAttributeName;
this.stateBeforeAttributeName(c);
} else if (!isWhitespace(c)) {
this.cbs.onattribend(QuoteType.NoValue, this.index);
this.cbs.onattribend(QuoteType.NoValue, this.sectionStart);
this.state = State.InAttributeName;
this.sectionStart = this.index;
}
Expand Down Expand Up @@ -506,7 +507,7 @@ export default class Tokenizer {
quote === CharCodes.DoubleQuote
? QuoteType.Double
: QuoteType.Single,
this.index,
this.index + 1,
);
this.state = State.BeforeAttributeName;
} else if (this.decodeEntities && c === CharCodes.Amp) {
Expand Down
125 changes: 124 additions & 1 deletion src/__snapshots__/Tokenizer.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,128 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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`] = `
[
[
Expand Down Expand Up @@ -82,7 +205,7 @@ exports[`Tokenizer should handle entities for entities in attributes (#276) 1`]
[
"onattribend",
3,
41,
42,
],
[
"onselfclosingtag",
Expand Down
Loading