`;
expect(tokens).toEqual([
{
type: OPEN_TAG_TOKEN
},
{
type: IDENTIFIER_TOKEN,
value: "div"
},
{
type: IDENTIFIER_TOKEN,
value: "id"
},
{
type: EQUALS_TOKEN
},
{
type: EXPRESSION_TOKEN,
value: 0
},
{
type: CLOSE_TAG_TOKEN
}
]);
});
it("should handle mixed text and expressions", () => {
const name = "World";
const tokens = tokenizeTemplate`Hello ${name}!`;
expect(tokens).toEqual([
{
type: TEXT_TOKEN,
value: "Hello "
},
{
type: EXPRESSION_TOKEN,
value: 0
},
{
type: TEXT_TOKEN,
value: "!"
}
]);
});
it("should handle data attributes with hyphens and underscores", () => {
const tokens = tokenizeTemplate`
`;
const attrNames = tokens.filter(
t => t.type === IDENTIFIER_TOKEN && (t.value as string).includes("data")
);
expect(attrNames.length).toBeGreaterThanOrEqual(2);
});
});
describe("whitespace handling", () => {
it("should skip whitespace inside tags", () => {
const tokens = tokenizeTemplate`< \n div id = "app" >`;
// Should not have whitespace tokens in tag context
expect(tokens).toEqual([
{
type: OPEN_TAG_TOKEN
},
{
type: IDENTIFIER_TOKEN,
value: "div"
},
{
type: IDENTIFIER_TOKEN,
value: "id"
},
{
type: EQUALS_TOKEN
},
{
type: STRING_TOKEN,
value: "app",
quote: '"'
},
{
type: CLOSE_TAG_TOKEN
}
]);
});
it("should preserve text content whitespace", () => {
const tokens = tokenizeTemplate` Hello World `;
expect(tokens).toEqual([
{
type: TEXT_TOKEN,
value: " Hello World "
}
]);
});
it("should handle multiline content with preserved whitespace", () => {
const tokens = tokenizeTemplate`
Hello
`;
expect(tokens).toEqual([
{
type: OPEN_TAG_TOKEN
},
{
type: IDENTIFIER_TOKEN,
value: "div"
},
{
type: CLOSE_TAG_TOKEN
},
{
type: TEXT_TOKEN,
value: "\n Hello\n "
},
{
type: OPEN_TAG_TOKEN
},
{
type: SLASH_TOKEN
},
{
type: IDENTIFIER_TOKEN,
value: "div"
},
{
type: CLOSE_TAG_TOKEN
}
]);
});
it("should handle tabs and mixed whitespace", () => {
const tokens = tokenizeTemplate`\tHello\nWorld `;
expect(tokens).toEqual([
{
type: TEXT_TOKEN,
value: "\tHello\nWorld "
}
]);
});
it("should handle whitespace around expressions", () => {
const name = "test";
const tokens = tokenizeTemplate` ${name} `;
expect(tokens).toEqual([
{
type: TEXT_TOKEN,
value: " "
},
{
type: EXPRESSION_TOKEN,
value: 0
},
{
type: TEXT_TOKEN,
value: " "
}
]);
});
});
describe("edge cases", () => {
it("should handle empty template", () => {
const tokens = tokenizeTemplate``;
expect(tokens).toEqual([]);
});
it("should handle only whitespace", () => {
const tokens = tokenizeTemplate` `;
expect(tokens).toEqual([
{
type: TEXT_TOKEN,
value: " "
}
]);
});
it("should handle special characters in text", () => {
const tokens = tokenizeTemplate`Hello & goodbye`;
expect(tokens).toEqual([
{
type: TEXT_TOKEN,
value: "Hello & goodbye"
}
]);
});
it("should handle consecutive expressions", () => {
const a = "first";
const b = "second";
const tokens = tokenizeTemplate`${a}${b}`;
expect(tokens).toEqual([
{
type: EXPRESSION_TOKEN,
value: 0
},
{
type: EXPRESSION_TOKEN,
value: 1
}
]);
});
it("should handle 1 letter tags", () => {
const tokens = tokenizeTemplate`
|
|
|
|
`;
expect(tokens.filter(t => t.type === IDENTIFIER_TOKEN && t.value === "a").length).toBe(3);
});
});
describe("special characters in names", () => {
it("should tokenize tag with hyphens", () => {
const tokens = tokenizeTemplate`
`;
expect(tokens).toEqual([
{
type: OPEN_TAG_TOKEN
},
{
type: IDENTIFIER_TOKEN,
value: "my-component"
},
{
type: SLASH_TOKEN
},
{
type: CLOSE_TAG_TOKEN
}
]);
});
it("should tokenize tag with periods", () => {
const tokens = tokenizeTemplate`
`;
expect(tokens).toEqual([
{
type: OPEN_TAG_TOKEN
},
{
type: IDENTIFIER_TOKEN,
value: "my.component"
},
{
type: SLASH_TOKEN
},
{
type: CLOSE_TAG_TOKEN
}
]);
});
it("should tokenize tag with colons", () => {
const tokens = tokenizeTemplate`
`;
expect(tokens).toEqual([
{
type: OPEN_TAG_TOKEN
},
{
type: IDENTIFIER_TOKEN,
value: "svg:rect"
},
{
type: SLASH_TOKEN
},
{
type: CLOSE_TAG_TOKEN
}
]);
});
it("should tokenize tag with underscores", () => {
const tokens = tokenizeTemplate`
`;
expect(tokens).toEqual([
{
type: OPEN_TAG_TOKEN
},
{
type: IDENTIFIER_TOKEN,
value: "my_component"
},
{
type: SLASH_TOKEN
},
{
type: CLOSE_TAG_TOKEN
}
]);
});
it("should tokenize attribute with -_.:$", () => {
const tokens = tokenizeTemplate`
`;
expect(tokens).toEqual([
{
type: OPEN_TAG_TOKEN
},
{
type: IDENTIFIER_TOKEN,
value: "div"
},
{
type: IDENTIFIER_TOKEN,
value: "data-id"
},
{
type: IDENTIFIER_TOKEN,
value: "data_id"
},
{
type: IDENTIFIER_TOKEN,
value: "data.id"
},
{
type: IDENTIFIER_TOKEN,
value: "data:id"
},
{
type: IDENTIFIER_TOKEN,
value: "dataid$"
},
{
type: CLOSE_TAG_TOKEN
}
]);
});
});
describe("invalid syntax", () => {
it("explains how to write a literal less-than sign in text", () => {
expect(() => tokenizeTemplate`
1 < 2
`).toThrow(
'To include a literal "<" in text, use < or ${"<"}.'
);
});
it("keeps the generic error for an invalid character inside a tag", () => {
expect(() => tokenizeTemplate`
`).toThrow("Unexpected Character: @");
});
it("should throw with extra <", () => {
expect(() => tokenizeTemplate`<
`).toThrow();
});
it("should throw with extra <", () => {
expect(() => tokenizeTemplate`
`).toThrow();
});
it("should throw on invalid identofier", () => {
expect(() => tokenizeTemplate`<.div />`).toThrow();
});
it("should throw on invalid identofier", () => {
expect(() => tokenizeTemplate`
`).toThrow();
});
it("should throw on invalid identofier", () => {
expect(() => tokenizeTemplate`
`).toThrow();
});
});
describe("bad but valid syntaxes", () => {
it("should handle multiple attributes in tight syntax", () => {
const tokens = tokenizeTemplate`
`;
const attrNames = tokens.filter(
t => t.type === IDENTIFIER_TOKEN && t.value && /^[abc]$/.test(t.value as string)
);
expect(attrNames).toHaveLength(3);
});
it("should handle attribute without value but with slash", () => {
const tokens = tokenizeTemplate`
`;
expect(tokens).toContainEqual(
expect.objectContaining({
type: IDENTIFIER_TOKEN,
value: "required"
})
);
expect(tokens).toContainEqual(
expect.objectContaining({
type: SLASH_TOKEN
})
);
});
it("should handle whitespace variations", () => {
const tokens = tokenizeTemplate`
`;
expect(tokens).toContainEqual(
expect.objectContaining({
type: IDENTIFIER_TOKEN,
value: "id"
})
);
expect(tokens).toContainEqual(
expect.objectContaining({
type: STRING_TOKEN,
value: "value"
})
);
});
});
describe("handling of raw text elements", () => {
it("should tokenize content inside `;
expect(tokens).toEqual([
{ type: OPEN_TAG_TOKEN },
{ type: IDENTIFIER_TOKEN, value: "script" },
{ type: CLOSE_TAG_TOKEN },
{ type: TEXT_TOKEN, value: "const a = 5<10;" },
{ type: OPEN_TAG_TOKEN },
{ type: SLASH_TOKEN },
{ type: IDENTIFIER_TOKEN, value: "script" },
{ type: CLOSE_TAG_TOKEN }
]);
});
it("should tokenize content inside `;
expect(tokens).toEqual([
{ type: OPEN_TAG_TOKEN },
{ type: IDENTIFIER_TOKEN, value: "style" },
{ type: CLOSE_TAG_TOKEN },
{ type: TEXT_TOKEN, value: ".class > span { color: red; }" },
{ type: OPEN_TAG_TOKEN },
{ type: SLASH_TOKEN },
{ type: IDENTIFIER_TOKEN, value: "style" },
{ type: CLOSE_TAG_TOKEN }
]);
});
it("should handle nested raw text elements ", () => {
const tokens = tokenizeTemplate`
`;
expect(tokens).toEqual([
{ type: OPEN_TAG_TOKEN },
{ type: IDENTIFIER_TOKEN, value: "div" },
{ type: CLOSE_TAG_TOKEN },
{ type: OPEN_TAG_TOKEN },
{ type: IDENTIFIER_TOKEN, value: "style" },
{ type: CLOSE_TAG_TOKEN },
{ type: TEXT_TOKEN, value: ".class > span { color: red; }" },
{ type: OPEN_TAG_TOKEN },
{ type: SLASH_TOKEN },
{ type: IDENTIFIER_TOKEN, value: "style" },
{ type: CLOSE_TAG_TOKEN },
{ type: OPEN_TAG_TOKEN },
{ type: SLASH_TOKEN },
{ type: IDENTIFIER_TOKEN, value: "div" },
{ type: CLOSE_TAG_TOKEN }
]);
});
it("should tokenize content inside