Skip to content

Commit

Permalink
fix issue: #114
Browse files Browse the repository at this point in the history
  • Loading branch information
taoqf committed Apr 22, 2021
1 parent 39ed43a commit bb4add0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 41 deletions.
39 changes: 38 additions & 1 deletion src/nodes/html.ts
Expand Up @@ -6,7 +6,6 @@ import TextNode from './text';
import Matcher from '../matcher';
import arr_back from '../back';
import CommentNode from './comment';
import parse from '../parse';

// const { decode } = he;

Expand Down Expand Up @@ -987,3 +986,41 @@ export function base_parse(data: string, options = { lowerCaseTagName: false, co
}
return stack;
}

/**
* Parses HTML and returns a root element
* Parse a chuck of HTML source.
*/
export 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;
}
41 changes: 1 addition & 40 deletions src/parse.ts
@@ -1,40 +1 @@
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;
}
export { parse as default } from './nodes/html';

0 comments on commit bb4add0

Please sign in to comment.