all files / src/ HtmlParser.js

83.87% Statements 78/93
79.25% Branches 42/53
83.33% Functions 15/18
76.79% Lines 43/56
8 statements, 3 functions, 7 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195                                                         37× 37×   37× 37×   37× 111× 111× 18×   111×       37× 100×   31× 31×                                                       160× 160× 134× 134×                   163× 614× 614× 144×   144× 140×     136× 136×                                                   108×                                                                                                            
import * as supports from './supports';
import * as streamReaders from './streamReaders';
import fixedReadTokenFactory from './fixedReadTokenFactory';
import {escapeQuotes} from './utils';
 
/**
 * Detection regular expressions.
 *
 * Order of detection matters: detection of one can only
 * succeed if detection of previous didn't
 
 * @type {Object}
 */
const detect = {
  comment: /^<!--/,
  endTag: /^<\//,
  atomicTag: /^<\s*(script|style|noscript|iframe|textarea)[\s\/>]/i,
  startTag: /^</,
  chars: /^[^<]/
};
 
/**
 * HtmlParser provides the capability to parse HTML and return tokens
 * representing the tags and content.
 */
export default class HtmlParser {
  /**
   * Constructor.
   *
   * @param {string} stream The initial parse stream contents.
   * @param {Object} options The options
   * @param {boolean} options.autoFix Set to true to automatically fix errors
   */
  constructor(stream = '', options = {}) {
    this.stream = stream;
 
    let fix = false;
    const fixedTokenOptions = {};
 
    for (let key in supports) {
      Eif (supports.hasOwnProperty(key)) {
        if (options.autoFix) {
          fixedTokenOptions[`${key}Fix`] = true; // !supports[key];
        }
        fix = fix || fixedTokenOptions[`${key}Fix`];
      }
    }
 
    if (fix) {
      this._readToken = fixedReadTokenFactory(this, fixedTokenOptions, () => this._readTokenImpl());
      this._peekToken = fixedReadTokenFactory(this, fixedTokenOptions, () => this._peekTokenImpl());
    } else {
      this._readToken = this._readTokenImpl;
      this._peekToken = this._peekTokenImpl;
    }
  }
 
  /**
   * Appends the given string to the parse stream.
   *
   * @param {string} str The string to append
   */
  append(str) {
    this.stream += str;
  }
 
  /**
   * Prepends the given string to the parse stream.
   *
   * @param {string} str The string to prepend
   */
  prepend(str) {
    this.stream = str + this.stream;
  }
 
  /**
   * The implementation of the token reading.
   *
   * @private
   * @returns {?Token}
   */
  _readTokenImpl() {
    const token = this._peekTokenImpl();
    if (token) {
      this.stream = this.stream.slice(token.length);
      return token;
    }
  }
 
  /**
   * The implementation of token peeking.
   *
   * @returns {?Token}
   */
  _peekTokenImpl() {
    for (let type in detect) {
      Eif (detect.hasOwnProperty(type)) {
        if (detect[type].test(this.stream)) {
          const token = streamReaders[type](this.stream);
 
          if (token) {
            if (token.type === 'startTag' &&
                (/script|style/i).test(token.tagName)) {
              return null;
            } else {
              token.text = this.stream.substr(0, token.length);
              return token;
            }
          }
        }
      }
    }
  }
 
  /**
   * The public token peeking interface.  Delegates to the basic token peeking
   * or a version that performs fixups depending on the `autoFix` setting in
   * options.
   *
   * @returns {object}
   */
  peekToken() {
    return this._peekToken();
  }
 
  /**
   * The public token reading interface.  Delegates to the basic token reading
   * or a version that performs fixups depending on the `autoFix` setting in
   * options.
   *
   * @returns {object}
   */
  readToken() {
    return this._readToken();
  }
 
  /**
   * Read tokens and hand to the given handlers.
   *
   * @param {Object} handlers The handlers to use for the different tokens.
   */
  readTokens(handlers) {
    let tok;
    while ((tok = this.readToken())) {
      // continue until we get an explicit "false" return
      if (handlers[tok.type] && handlers[tok.type](tok) === false) {
        return;
      }
    }
  }
 
  /**
   * Clears the parse stream.
   *
   * @returns {string} The contents of the parse stream before clearing.
   */
  clear() {
    const rest = this.stream;
    this.stream = '';
    return rest;
  }
 
  /**
   * Returns the rest of the parse stream.
   *
   * @returns {string} The contents of the parse stream.
   */
  rest() {
    return this.stream;
  }
}
 
HtmlParser.tokenToString = tok => tok.toString();
 
HtmlParser.escapeAttributes = attrs => {
  const escapedAttrs = {};
 
  for (let name in attrs) {
    if (attrs.hasOwnProperty(name)) {
      escapedAttrs[name] = escapeQuotes(attrs[name], null);
    }
  }
 
  return escapedAttrs;
};
 
HtmlParser.supports = supports;
 
for (let key in supports) {
  Eif (supports.hasOwnProperty(key)) {
    HtmlParser.browserHasFlaw = HtmlParser.browserHasFlaw || (!supports[key]) && key;
  }
}