Skip to content

Commit

Permalink
fix: update node end on attribute value handler (fixes #16) (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan committed Jul 21, 2023
1 parent 99ceac7 commit 6dcc1c3
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a target=”_blank”></a>
94 changes: 94 additions & 0 deletions src/tokenizer/__tests__/__output__/attributes-bare-wrong-quote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { TokenTypes } from "../../../constants";

export default [
{
type: TokenTypes.OpenTagStart,
value: "<a",
range: [0, 2],
loc: {
start: {
line: 1,
column: 0,
},
end: {
line: 1,
column: 2,
},
},
},
{
type: TokenTypes.AttributeKey,
value: "target",
range: [3, 9],
loc: {
start: {
line: 1,
column: 3,
},
end: {
line: 1,
column: 9,
},
},
},
{
type: TokenTypes.AttributeAssignment,
value: "=",
range: [9, 10],
loc: {
start: {
line: 1,
column: 9,
},
end: {
line: 1,
column: 10,
},
},
},
{
type: TokenTypes.AttributeValue,
value: "”_blank”",
range: [10, 18],
loc: {
start: {
line: 1,
column: 10,
},
end: {
line: 1,
column: 18,
},
},
},
{
type: TokenTypes.OpenTagEnd,
value: ">",
range: [18, 19],
loc: {
start: {
line: 1,
column: 18,
},
end: {
line: 1,
column: 19,
},
},
},
{
type: TokenTypes.CloseTag,
value: "</a>",
range: [19, 23],
loc: {
start: {
line: 1,
column: 19,
},
end: {
line: 1,
column: 23,
},
},
},
];
6 changes: 6 additions & 0 deletions src/tokenizer/__tests__/tokenize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import VOID_TAGS from "./__output__/void-tags";
import EMPTY from "./__output__/empty";
import SVG from "./__output__/svg";
import ATTRIBUTES_MULTILINE_CRLF from "./__output__/attributes-multiline-crlf";
import ATTRIBUTES_BARE_WRONG_QUOTE from "./__output__/attributes-bare-wrong-quote";

describe("tokenize", () => {
test.each(
Expand All @@ -34,6 +35,11 @@ describe("tokenize", () => {
"attributes-bare.html",
ATTRIBUTES_BARE
],
[
"Attributes bare wrong quotes",
"attributes-bare-wrong-quote.html",
ATTRIBUTES_BARE_WRONG_QUOTE
],
[
"Attributes empty",
"attributes-empty.html",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
export default {
type: "Document",
range: [0, 23],
children: [
{
type: "Tag",
range: [0, 23],
loc: {
start: {
line: 1,
column: 0,
},
end: {
line: 1,
column: 23,
},
},
name: "a",
children: [],
openStart: {
type: "OpenTagStart",
value: "<a",
loc: {
start: {
line: 1,
column: 0,
},
end: {
line: 1,
column: 2,
},
},
range: [0, 2],
},
openEnd: {
type: "OpenTagEnd",
value: ">",
loc: {
start: {
line: 1,
column: 18,
},
end: {
line: 1,
column: 19,
},
},
range: [18, 19],
},
selfClosing: false,
attributes: [
{
type: "Attribute",
loc: {
start: { line: 1, column: 3 },
end: { line: 1, column: 18 },
},
range: [3, 18],
key: {
type: "AttributeKey",
value: "target",
loc: {
start: { line: 1, column: 3 },
end: { line: 1, column: 9 },
},
range: [3, 9],
},
value: {
type: "AttributeValue",
value: "”_blank”",
loc: {
start: { line: 1, column: 10 },
end: { line: 1, column: 18 },
},
range: [10, 18],
},
},
],
close: {
type: "CloseTag",
value: "</a>",
loc: {
start: { line: 1, column: 19 },
end: { line: 1, column: 23 },
},
range: [19, 23],
},
},
],
loc: {
start: {
line: 1,
column: 0,
},
end: {
line: 1,
column: 23,
},
},
};
8 changes: 8 additions & 0 deletions src/tree-constructor/__tests__/construct-tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ import SVG_OUTPUT from "./__output__/svg";
import ATTRIBUTES_MULTILINE_CRLF_INPUT from "../../tokenizer/__tests__/__output__/attributes-multiline-crlf";
import ATTRIBUTES_MULTILINE_CRLF_OUTPUT from "./__output__/attributes-multiline-crlf";

import ATTRIBUTES_BARE_WRONG_QUOTE_INPUT from "../../tokenizer/__tests__/__output__/attributes-bare-wrong-quote";
import ATTRIBUTES_BARE_WRONG_QUOTE_OUTPUT from "./__output__/attributes-bare-wrong-quote";

import { clearParent } from "../../utils";

describe("construct-tree", () => {
Expand All @@ -58,6 +61,11 @@ describe("construct-tree", () => {
ATTRIBUTES_APOSTROPHE_OUTPUT,
],
["Attributes empty", ATTRIBUTES_EMPTY_INPUT, ATTRIBUTES_EMPTY_OUTPUT],
[
"Attributes bare wrong quote",
ATTRIBUTES_BARE_WRONG_QUOTE_INPUT,
ATTRIBUTES_BARE_WRONG_QUOTE_OUTPUT,
],
["Comments", COMMENTS_INPUT, COMMENTS_OUTPUT],
[
"Opening closing text",
Expand Down
1 change: 1 addition & 0 deletions src/tree-constructor/handlers/attribute-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function handleAttributeValue(
const attribute = getLastAttribute(state);

attribute.value = createNodeFrom(token) as AttributeValueNode;
updateNodeEnd(attribute, token);
state.caretPosition++;
return state;
}
Expand Down

0 comments on commit 6dcc1c3

Please sign in to comment.