Commit 42d71002 authored by David Sveningsson's avatar David Sveningsson
Browse files

fix(dom): throw if `tagName` is invalid

parent 80e332d7
......@@ -865,6 +865,25 @@ describe("HtmlElement", () => {
expect(result?.tagName).toEqual("b");
});
});
it("should not throw error if tagName is undefined", () => {
expect.assertions(1);
expect(() => new HtmlElement(undefined, null, NodeClosed.EndTag, null, location)).not.toThrow();
});
it("should throw error if tagName is empty string", () => {
expect.assertions(1);
expect(() => new HtmlElement("", null, NodeClosed.EndTag, null, location)).toThrow(
"The tag name provided ('') is not a valid name"
);
});
it("should throw error if tagName is asterisk", () => {
expect.assertions(1);
expect(() => new HtmlElement("*", null, NodeClosed.EndTag, null, location)).toThrow(
"The tag name provided ('*') is not a valid name"
);
});
});
function mockEntry(stub: Partial<MetaData> = {}): MetaData {
......
......@@ -21,6 +21,10 @@ function isElement(node: DOMNode): node is HtmlElement {
return node.nodeType === NodeType.ELEMENT_NODE;
}
function isValidTagName(tagName: string | undefined): boolean {
return Boolean(tagName !== "" && tagName !== "*");
}
export class HtmlElement extends DOMNode {
public readonly tagName: string;
public readonly parent: HtmlElement | null;
......@@ -41,6 +45,10 @@ export class HtmlElement extends DOMNode {
const nodeType = tagName ? NodeType.ELEMENT_NODE : NodeType.DOCUMENT_NODE;
super(nodeType, tagName, location);
if (!isValidTagName(tagName)) {
throw new Error(`The tag name provided ('${tagName || ""}') is not a valid name`);
}
this.tagName = tagName || "#document";
this.parent = parent ?? null;
this.attr = {};
......@@ -177,9 +185,6 @@ export class HtmlElement extends DOMNode {
* If passing "*" this test will pass if any tagname is set.
*/
public is(tagName: string): boolean {
if (!this.tagName) {
return false;
}
return tagName === "*" || this.tagName.toLowerCase() === tagName.toLowerCase();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment