@rustle/html-parse
v1.0.0
Published
simple html parser
Downloads
14
Readme
Simple html parser compatible with single tags without /.
Usage
import { parse, evaluate } from '@rustle/html-parse';
const built = parse(`
<div>
<br>
<a href="xxx"/>
</div>
`)
const ast = evaluate(built, (tag, props, ...children) => {
return { tag, props, children };
})
console.log(ast); // [{ tag: 'div', props: null, children: [...] }]// Get all `a` tags.
const a = [];
evaluate(built, (tag, props, ...children) => {
if (tag === 'a') {
a.push({ tag, props, children });
}
})CDN
<!DOCTYPE html>
<html lang='en'>
<body>
<script src='https://unpkg.com/@rustle/html-parse/dist/htmlParse.umd.js'></script>
<script>
const { parse, evaluate } = FetchNpmPackage;
// ...
</script>
</body>
</html>Caveats
The special
tagis marked with uppercase, because thehtmltags are all in lowercase, to avoid conflicts, the specialtagis marked with uppercase.- comment node:
COMMENT.
- comment node:
For the wrong
htmlsyntax, it is impossible to achieve the same behavior as the browser. To be precise, this is more similar to anxmlparser, so write standardhtmlsyntax as much as possible.
