yzhanhtmlparser
Version:
A streaming HTML parser based on HTML Standard. 基于 HTML 标准的流式 HTML 解析器
25 lines (24 loc) • 781 B
JavaScript
const HtmlParser = require('./htmlParser/HtmlParser.class')
const StringStream = require('./htmlParser/StringStream.class')
const Collector = require('./htmlParser/Collector.class')
const { isMatched, buildDOMTree } = require('../utils')
module.exports = {
HtmlParser,
parse(html) {
const htmlParser = new HtmlParser()
const stringStream = new StringStream(html)
stringStream.pipe(htmlParser)
const collector = new Collector()
htmlParser.on('data', str => collector.write(str))
stringStream.read()
return collector.data.map(data => JSON.parse(data))
},
isMatched(html) {
const tokens = this.parse(html)
return isMatched(tokens)
},
buildDOMTree(html) {
const tokens = this.parse(html)
return buildDOMTree(tokens)
}
}