How to use htmlparser2

Comprehensive htmlparser2 code examples:

How to use htmlparser2.ElementType:

81
82
83
84
85
86
87
88
89
90
for (var i = 0; i < elems.length; i++) {
    var elem = elems[i];
    if (htmlparser2_1.DomUtils.isText(elem))
        ret += elem.data;
    else if (htmlparser2_1.DomUtils.hasChildren(elem) &&
        elem.type !== htmlparser2_1.ElementType.Comment &&
        elem.type !== htmlparser2_1.ElementType.Script &&
        elem.type !== htmlparser2_1.ElementType.Style) {
        ret += text(elem.children);
    }

How to use htmlparser2.parseDOM:

41
42
43
44
45
46
47
48
49
50

if ( selectedText.length == 0 ) {
	selectedText = editor.document.getText();
}

var parsedEls = htmlparser.parseDOM(selectedText);
var processedEls = processEl(parsedEls);
var outputClasses = [];

processedEls.filter((el) => {

How to use htmlparser2.DomUtils:

696
697
698
699
700
701
702
703
704
705
 */
function remove(selector) {
    // Filter if we have selector
    var elems = selector ? this.filter(selector) : this;
    utils_1.domEach(elems, function (el) {
        htmlparser2_1.DomUtils.removeElement(el);
        el.prev = el.next = el.parent = null;
    });
    return this;
}

How to use htmlparser2.DomHandler:

91
92
93
94
95
96
97
98
99
100
101
  return {htmlFile: data.toString(), htmlPath: htmlPath, rootSelector:  rootSelector, transfos: transfos, node: ast, selNode: selectorAttr.value}
}


function parseDom(jsxZ,callback){
  var parser = new htmlParser.Parser(
    new htmlParser.DomHandler(function (err, dom) {
      if (err) err("Too much malformed HTML "+jsxZ.htmlPath,jsxZ.node)
      if (jsxZ.rootSelector){
        var dom = cssSelector.selectOne(jsxZ.rootSelector,dom)
        if (!dom) error("selector "+jsxZ.rootSelector+" does not match any node in "+ jsxZ.htmlPath,jsxZ.selNode)

How to use htmlparser2.parseDocument:

1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
    `Input length ${html.length} is above allowed limit of ${maxInputLength}. Truncating without ellipsis.`
  );
  html = html.substring(0, maxInputLength);
}

const document = htmlparser2.parseDocument(html, { decodeEntities: options.decodeEntities });
const bases = findBaseElements(document.children);
const builder = new BlockTextBuilder(options, picker, metadata);
walk(bases, builder);
return builder.toString();

How to use htmlparser2.Parser:

106
107
108
109
110
111
112
113
114
115
var skipMap = {};
var transformMap = {};
var skipText = false;
var skipTextDepth = 0;

var parser = new htmlparser.Parser({
  onopentag: function(name, attribs) {
    if (skipText) {
      skipTextDepth++;
      return;