forked from taoqf/node-html-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.ts
More file actions
40 lines (39 loc) · 1.19 KB
/
Copy pathparse.ts
File metadata and controls
40 lines (39 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import arr_back from './back';
import { base_parse, Options } from './nodes/html';
/**
* Parses HTML and returns a root element
* Parse a chuck of HTML source.
*/
export default function parse(data: string, options = { lowerCaseTagName: false, comment: false } as Partial<Options>) {
const stack = base_parse(data, options);
const [root] = stack;
while (stack.length > 1) {
// Handle each error elements.
const last = stack.pop();
const oneBefore = arr_back(stack);
if (last.parentNode && last.parentNode.parentNode) {
if (last.parentNode === oneBefore && last.tagName === oneBefore.tagName) {
// Pair error case <h3> <h3> handle : Fixes to <h3> </h3>
oneBefore.removeChild(last);
last.childNodes.forEach((child) => {
oneBefore.parentNode.appendChild(child);
});
stack.pop();
} else {
// Single error <div> <h3> </div> handle: Just removes <h3>
oneBefore.removeChild(last);
last.childNodes.forEach((child) => {
oneBefore.appendChild(child);
});
}
} else {
// If it's final element just skip.
}
}
// response.childNodes.forEach((node) => {
// if (node instanceof HTMLElement) {
// node.parentNode = null;
// }
// });
return root;
}