taoqf / node-html-parser

A very fast HTML parser, generating a simplified DOM, with basic element query support.
MIT License
1.12k stars 112 forks source link

Square brackets are not included in attribute key #200

Closed dagi12 closed 2 years ago

dagi12 commented 2 years ago

Square brackets are not included in attribute key. When trying to access below

<input [(ngModel)]="foo">

Attribute key is parsed as (ngmodel) same goes for output from outerHTML I'm trying to parse and refactor some angular templates. I'm unable to do that with parser working like that.

I think problem lies in below regular expression

 /**
   * Get escaped (as-is) attributes
   * @return {Object} parsed attributes
   */
  get: function () {
    if (this._rawAttrs) {
      return this._rawAttrs;
    }

    var attrs = {};

    if (this.rawAttrs) {
      var re = /([a-zA-Z()#][a-zA-Z0-9-_:()#]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/g;
      var match = void 0;

      while (match = re.exec(this.rawAttrs)) {
        var key = match[1];
        var val = match[2] || null;
        if (val && (val[0] === "'" || val[0] === "\"")) val = val.slice(1, val.length - 1);
        attrs[key] = val;
      }
    }

    this._rawAttrs = attrs;
    return attrs;
  }