# all files / src/ fixedReadTokenFactory.js

83.05% Statements 49/59
72.22% Branches 26/36
91.67% Functions 11/12
82.76% Lines 48/58

    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

    3×

    3×

    1×
    98×
    38×
    38×

    98×

    1×
    50×
    50×
    50×
    50×

    1×
    8×

    8×

    1×
    12×

    12×
    30×

    12×
    14×
    14×

    12×

    12×

    1×
    12×

    12×

    19×

    19×

    19×

    19×
    19×

    16×
    16×
    14×

    8×

    6×

    2×

    2×
    2×

    1×
    50×
    50×
    35×

    12×
    48×
    48×

     /**
     * Empty Elements - HTML 4.01
     *
     * @type {RegExp}
     */
    const EMPTY = /^(AREA|BASE|BASEFONT|BR|COL|FRAME|HR|IMG|INPUT|ISINDEX|LINK|META|PARAM|EMBED)$/i;

    /**
     * Elements that you can intentionally leave open (and which close themselves)
     *
     * @type {RegExp}
     */
    const CLOSESELF = /^(COLGROUP|DD|DT|LI|OPTIONS|P|TD|TFOOT|TH|THEAD|TR)$/i;

    /**
     * Corrects a token.
     *
     * @param {Token} tok The token to correct
     * @returns {Token} The corrected token
     */
    function correct(tok) {
      if (tok && tok.type === 'startTag') {
        tok.unary = EMPTY.test(tok.tagName) || tok.unary;
        tok.html5Unary = !(/\/>$/).test(tok.text);
      }
      return tok;
    }

    /**
     * Peeks at the next token in the parser.
     *
     * @param {HtmlParser} parser The parser
     * @param {Function} readTokenImpl The underlying readToken implementation
     * @returns {Token} The next token
     */
    function peekToken(parser, readTokenImpl) {
      const tmp = parser.stream;
      const tok = correct(readTokenImpl());
      parser.stream = tmp;
      return tok;
    }

    /**
     * Closes the last token.
     *
     * @param {HtmlParser} parser The parser
     * @param {Array<Token>} stack The stack
     */
    function closeLast(parser, stack) {
      const tok = stack.pop();

      // prepend close tag to stream.
      parser.prepend(`</${tok.tagName}>`);
    }

    /**
     * Create a new token stack.
     *
     * @returns {Array<Token>}
     */
    function newStack() {
      const stack = [];

      stack.last = function() {
        return this[this.length - 1];
      };

      stack.lastTagNameEq = function(tagName) {
        const last = this.last();
        return last && last.tagName &&
          last.tagName.toUpperCase() === tagName.toUpperCase();
      };

      stack.containsTagName = function(tagName) {
        for (let i = 0, tok; (tok = this[i]); i++) {
          if (tok.tagName === tagName) {
            return true;
          }
        }
        return false;
      };

      return stack;
    }

    /**
     * Return a readToken implementation that fixes input.
     *
     * @param {HtmlParser} parser The parser
     * @param {Object} options Options for fixing
     * @param {boolean} options.tagSoupFix True to fix tag soup scenarios
     * @param {boolean} options.selfCloseFix True to fix self-closing tags
     * @param {Function} readTokenImpl The underlying readToken implementation
     * @returns {Function}
     */
    export default function fixedReadTokenFactory(parser, options, readTokenImpl) {
      const stack = newStack();

      const handlers = {
        startTag(tok) {
          const tagName = tok.tagName;

          Iif (tagName.toUpperCase() === 'TR' && stack.lastTagNameEq('TABLE')) {
            parser.prepend('<TBODY>');
            prepareNextToken();
          } else Iif (options.selfCloseFix && CLOSESELF.test(tagName) &&
              stack.containsTagName(tagName)) {
            if (stack.lastTagNameEq(tagName)) {
              closeLast(parser, stack);
            } else {
              parser.prepend(`</${tok.tagName}>`);
              prepareNextToken();
            }
          } else Eif (!tok.unary) {
            stack.push(tok);
          }
        },

        endTag(tok) {
          const last = stack.last();
          if (last) {
            if (options.tagSoupFix && !stack.lastTagNameEq(tok.tagName)) {
              // cleanup tag soup
              closeLast(parser, stack);
            } else {
              stack.pop();
            }
          } else Eif (options.tagSoupFix) {
            // cleanup tag soup part 2: skip this token
            readTokenImpl();
            prepareNextToken();
          }
        }
      };

      function prepareNextToken() {
        const tok = peekToken(parser, readTokenImpl);
        if (tok && handlers[tok.type]) {
          handlers[tok.type](tok);
        }
      }

      return function fixedReadToken() {
        prepareNextToken();
        return correct(readTokenImpl());
      };
    }