@tkeron/html-parser 1.5.0 → 1.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -48,9 +48,11 @@ export const updateElementContent = (element: any): void => {
|
|
|
48
48
|
configurable: true,
|
|
49
49
|
});
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
let ancestor = element.parentElement || element.parentNode;
|
|
52
|
+
while (ancestor) {
|
|
53
|
+
ancestor._internalOuterHTML = undefined;
|
|
54
|
+
ancestor._internalInnerHTML = undefined;
|
|
55
|
+
ancestor._internalTextContent = undefined;
|
|
56
|
+
ancestor = ancestor.parentElement || ancestor.parentNode;
|
|
55
57
|
}
|
|
56
58
|
};
|
|
@@ -19,11 +19,11 @@ export const parseSelector = (selector: string): SelectorGroup[] => {
|
|
|
19
19
|
remaining = remaining.slice(tagMatch[1].length);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
const idMatches = remaining.matchAll(/#([a-zA-Z0-
|
|
22
|
+
const idMatches = remaining.matchAll(/#([a-zA-Z0-9_-][a-zA-Z0-9_-]*)/g);
|
|
23
23
|
for (const match of idMatches) {
|
|
24
24
|
tokens.push({ type: "id", value: match[1] });
|
|
25
25
|
}
|
|
26
|
-
remaining = remaining.replace(/#[a-zA-Z0-
|
|
26
|
+
remaining = remaining.replace(/#[a-zA-Z0-9_-][a-zA-Z0-9_-]*/g, "");
|
|
27
27
|
|
|
28
28
|
const classMatches = remaining.matchAll(/\.([a-zA-Z][a-zA-Z0-9_-]*)/g);
|
|
29
29
|
for (const match of classMatches) {
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, it, expect } from "bun:test";
|
|
2
|
+
import { parseHTML } from "../index";
|
|
3
|
+
|
|
4
|
+
describe("querySelector with underscore IDs", () => {
|
|
5
|
+
it("should find element with ID starting with single underscore", () => {
|
|
6
|
+
const doc = parseHTML(
|
|
7
|
+
"<html><body><div id='_test'>Content</div></body></html>",
|
|
8
|
+
);
|
|
9
|
+
const result = doc.querySelector("#_test");
|
|
10
|
+
expect(result).not.toBeNull();
|
|
11
|
+
expect(result?.tagName).toBe("DIV");
|
|
12
|
+
expect(result?.id).toBe("_test");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should find element with ID starting with double underscore", () => {
|
|
16
|
+
const doc = parseHTML(
|
|
17
|
+
"<html><body><div id='__test'>Content</div></body></html>",
|
|
18
|
+
);
|
|
19
|
+
const result = doc.querySelector("#__test");
|
|
20
|
+
expect(result).not.toBeNull();
|
|
21
|
+
expect(result?.tagName).toBe("DIV");
|
|
22
|
+
expect(result?.id).toBe("__test");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should find element with complex underscore ID", () => {
|
|
26
|
+
const doc = parseHTML(
|
|
27
|
+
"<html><body><div id='__tkeron_component_root__'>Content</div></body></html>",
|
|
28
|
+
);
|
|
29
|
+
const result = doc.querySelector("#__tkeron_component_root__");
|
|
30
|
+
expect(result).not.toBeNull();
|
|
31
|
+
expect(result?.tagName).toBe("DIV");
|
|
32
|
+
expect(result?.id).toBe("__tkeron_component_root__");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should find underscore ID from child element context", () => {
|
|
36
|
+
const doc = parseHTML(
|
|
37
|
+
"<html><body><div id='__root'><p>Nested</p></div></body></html>",
|
|
38
|
+
);
|
|
39
|
+
const body = doc.querySelector("body");
|
|
40
|
+
const result = body?.querySelector("#__root");
|
|
41
|
+
expect(result).not.toBeNull();
|
|
42
|
+
expect(result?.tagName).toBe("DIV");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("should find nested element with underscore ID", () => {
|
|
46
|
+
const doc = parseHTML(
|
|
47
|
+
"<html><body><div><span id='_nested'>Text</span></div></body></html>",
|
|
48
|
+
);
|
|
49
|
+
const result = doc.querySelector("#_nested");
|
|
50
|
+
expect(result).not.toBeNull();
|
|
51
|
+
expect(result?.tagName).toBe("SPAN");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should return null for non-existent underscore ID", () => {
|
|
55
|
+
const doc = parseHTML(
|
|
56
|
+
"<html><body><div id='other'>Content</div></body></html>",
|
|
57
|
+
);
|
|
58
|
+
const result = doc.querySelector("#_nonexistent");
|
|
59
|
+
expect(result).toBeNull();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should work with querySelectorAll for underscore IDs", () => {
|
|
63
|
+
const doc = parseHTML(
|
|
64
|
+
"<html><body><div id='_a'>A</div><div id='_b'>B</div></body></html>",
|
|
65
|
+
);
|
|
66
|
+
const resultA = doc.querySelectorAll("#_a");
|
|
67
|
+
const resultB = doc.querySelectorAll("#_b");
|
|
68
|
+
expect(resultA.length).toBe(1);
|
|
69
|
+
expect(resultB.length).toBe(1);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should find ID starting with hyphen", () => {
|
|
73
|
+
const doc = parseHTML(
|
|
74
|
+
"<html><body><div id='-test'>Content</div></body></html>",
|
|
75
|
+
);
|
|
76
|
+
const result = doc.querySelector("#-test");
|
|
77
|
+
expect(result).not.toBeNull();
|
|
78
|
+
expect(result?.tagName).toBe("DIV");
|
|
79
|
+
expect(result?.id).toBe("-test");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("should find ID with mixed underscore and hyphen at start", () => {
|
|
83
|
+
const doc = parseHTML(
|
|
84
|
+
"<html><body><div id='_-mixed'>Content</div></body></html>",
|
|
85
|
+
);
|
|
86
|
+
const result = doc.querySelector("#_-mixed");
|
|
87
|
+
expect(result).not.toBeNull();
|
|
88
|
+
expect(result?.id).toBe("_-mixed");
|
|
89
|
+
});
|
|
90
|
+
});
|