Skip to content

Commit

Permalink
fix(tokenizer): Fix onattribend's endIndex (#1540)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaIT authored Jul 16, 2023
1 parent 1b25141 commit 685ccb6
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 58 deletions.
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

0 comments on commit 685ccb6

Please sign in to comment.