使用antlrv4的js api 解析html

下载依赖

$ npm i antlrv4-js-html

解析HTML文本

const  antlrv4_js_html = require('antlrv4-js-html')

class  Visitor  extends  antlrv4_js_html.Visitor {

constructor() {

super()

}

visitTerminal(ctx) {

let  text = ctx.getText()

console.log(text)

return  this.visitChildren(ctx);

}

}

antlrv4_js_html.transform(`<div><span id="12312" checked>2</span><p>1<span></span></p></div>`, new  Visitor())

浏览器中使用

<script  src="../dist/index-umd.js"></script>
<script>
console.log(antlrv4_js_html)
</script>

其他模块的支持

  • [v] es

  • [v] esm

  • [v] umd

API

// Visit a parse tree produced by HTMLParser#htmlDocument.
visitHtmlDocument(ctx) {

return  this.visitChildren(ctx);

}

// Visit a parse tree produced by HTMLParser#scriptletOrSeaWs.

visitScriptletOrSeaWs(ctx) {

return  this.visitChildren(ctx);

}

// Visit a parse tree produced by HTMLParser#htmlElements.

visitHtmlElements(ctx) {

return  this.visitChildren(ctx);

}

// Visit a parse tree produced by HTMLParser#htmlElement.

visitHtmlElement(ctx) {

return  this.visitChildren(ctx);

}

// Visit a parse tree produced by HTMLParser#htmlContent.

visitHtmlContent(ctx) {

return  this.visitChildren(ctx);

}

// Visit a parse tree produced by HTMLParser#htmlAttribute.

visitHtmlAttribute(ctx) {

return  this.visitChildren(ctx);

}

// Visit a parse tree produced by HTMLParser#htmlChardata.

visitHtmlChardata(ctx) {

return  this.visitChildren(ctx);

}

// Visit a parse tree produced by HTMLParser#htmlMisc.

visitHtmlMisc(ctx) {

return  this.visitChildren(ctx);

}

// Visit a parse tree produced by HTMLParser#htmlComment.

visitHtmlComment(ctx) {

return  this.visitChildren(ctx);

}

// Visit a parse tree produced by HTMLParser#script.

visitScript(ctx) {

return  this.visitChildren(ctx);

}

// Visit a parse tree produced by HTMLParser#style.

visitStyle(ctx) {

return  this.visitChildren(ctx);

}

其他库

如果你喜欢我的作品,请考虑赞助,以保持它们的可持续性。

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!