You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hunjianghu/江可飞/小程序开发/html-view/index.js

53 lines
1.1 KiB

const himalaya = require('./vendors/himalaya')
const defaultEachFn = require('./each/default')
const resolveUrl = require('./each/resolveUrl')
class HtmlParser {
constructor(html, { baseUrl } = {}) {
this.nodes = himalaya.parse(html)
if (baseUrl) this.each(resolveUrl(baseUrl))
this.each(defaultEachFn)
}
each(fn) {
this._each(fn, this.nodes)
return this
}
_each(fn, nodes) {
nodes.forEach((item, ...args) => {
fn(item, ...args)
if (item.children) this._each(fn, item.children)
})
}
filter(fn) {
this.nodes = this._filter(fn, this.nodes)
return this
}
_filter(fn, nodes) {
return nodes.filter((item, ...args) => {
const result = fn(item, ...args)
if (result && item.children) item.children = this._filter(fn, item.children)
return result
})
}
map(fn) {
this.nodes = this._map(fn, this.nodes)
return this
}
_map(fn, nodes) {
return nodes.map((item, ...args) => {
item = fn(item, ...args)
if (item.children) item.children = this._map(fn, item.children)
return item
})
}
}
module.exports = HtmlParser