parse method

List parse(
  1. String html
)

Implementation

List parse(String html) {
  String last = html;
  Match? match;
  int index;
  bool chars;

  while (html.length > 0) {
    chars = true;

    // Make sure we're not in a script or style element
    if (this._getStackLastItem() == null ||
        !this._specialTags.contains(this._getStackLastItem())) {
      // Comment
      if (html.indexOf('<!--') == 0) {
        index = html.indexOf('-->');

        if (index >= 0) {
          html = html.substring(index + 3);
          chars = false;
        }
      }
      // End tag
      else if (html.indexOf('</') == 0) {
        match = this._endTag.firstMatch(html);

        if (match != null) {
          String tag = match[0]!;

          html = html.substring(tag.length);
          chars = false;

          this._parseEndTag(tag);
        }
      }
      // Start tag
      else if (html.indexOf('<') == 0) {
        match = this._startTag.firstMatch(html);

        if (match != null) {
          String tag = match[0]!;

          html = html.substring(tag.length);
          chars = false;

          this._parseStartTag(tag, match[1]!, match[2]!, match.start);
        }
      }

      if (chars) {
        index = html.indexOf('<');

        String text = (index < 0) ? html : html.substring(0, index);

        html = (index < 0) ? '' : html.substring(index);

        this._appendNode(text);
      }
    } else {
      RegExp re =
      new RegExp(r'(.*)<\/' + this._getStackLastItem()! + r'[^>]*>');

      html = html.replaceAllMapped(re, (Match match) {
        String text = match[0]!
          ..replaceAll(new RegExp('<!--(.*?)-->'), '\$1')
          ..replaceAll(new RegExp('<!\[CDATA\[(.*?)]]>'), '\$1');

        this._appendNode(text);

        return '';
      });

      this._parseEndTag(this._getStackLastItem());
    }

    if (html == last) {
      throw 'Parse Error: ' + html;
    }

    last = html;
  }

  // Cleanup any remaining tags
  this._parseEndTag();

  List result = this._result;

  // Cleanup internal variables
  this._stack = [];
  this._result = [];

  return result;
}