# All files index.js

65.03% Statements 106/163
95.56% Branches 43/45
60% Functions 12/20
65.03% Lines 106/163

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    1641x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    22x
    22x
    22x
    22x
    22x
    32x
    22x
    22x
    22x
    13x
    2x
    2x
    2x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    8x
    8x
    8x
    8x
    13x
    13x
    13x
    13x
    8x
    3x
    3x
    3x
    3x
    3x

    3x

    2x
    2x
    4x
    4x

    2x
    2x
    2x
    2x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    19x
    19x
    19x
    2x

    19x
    19x
    19x
    16x
    16x
    16x
    3x
    3x
    19x
    1x
    1x
    3x
    3x

    19x
    19x
    19x
    3x
    3x
    3x
    16x
    19x
    4x

    12x

    22x
    22x
    22x
    22x
    22x
    22x
    22x
    22x
    22x

     const {Parser} = require('htmlparser2');

    /**
     * @see https://github.com/fb55/htmlparser2/wiki/Parser-options
     */
    const defaultOptions = {lowerCaseTags: false, lowerCaseAttributeNames: false, decodeEntities: false};

    const defaultDirectives = [{name: '!doctype', start: '<', end: '>'}];

    /**
     * Parse html to PostHTMLTree
     * @param  {String} html
     * @param  {Object} [options=defaultOptions]
     * @return {PostHTMLTree}
     */
    function postHTMLParser(html, options) {
      const bufArray = [];
      const results = [];

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

      function isDirective({name}, tag) {
        if (name instanceof RegExp) {
          const regex = new RegExp(name.source, 'i');

          return regex.test(tag);
        }

        if (tag !== name) {
          return false;
        }

        return true;
      }

      function parserDirective(name, data) {
        const directives = [].concat(defaultDirectives, options.directives || []);
        const last = bufArray.last();

        for (const directive of directives) {
          const directiveText = directive.start + data + directive.end;

          name = name.toLowerCase();
          if (isDirective(directive, name)) {
            if (!last) {
              results.push(directiveText);
              return;
            }

            if (last.content === undefined) {
              last.content = [];
            }

            last.content.push(directiveText);
          }
        }
      }

      function normalizeArributes(attrs) {
        const result = {};
        Object.keys(attrs).forEach(key => {
          const object = {};
          object[key] = attrs[key].replace(/&quot;/g, '"');
          Object.assign(result, object);
        });

        return result;
      }

      const parser = new Parser({
        onprocessinginstruction: parserDirective,
        oncomment(data) {
          const comment = `<!--${data}-->`;
          const last = bufArray.last();

          if (!last) {
            results.push(comment);
            return;
          }

          if (last.content === undefined) {
            last.content = [];
          }

          last.content.push(comment);
        },
        onopentag(tag, attrs) {
          const buf = {tag};

          if (Object.keys(attrs).length > 0) {
            buf.attrs = normalizeArributes(attrs);
          }

          bufArray.push(buf);
        },
        onclosetag() {
          const buf = bufArray.pop();

          if (!bufArray.length > 0) {
            results.push(buf);
            return;
          }

          const last = bufArray.last();
          if (!Array.isArray(last.content)) {
            last.content = [];
          }

          last.content.push(buf);
        },
        ontext(text) {
          const last = bufArray.last();

          if (!last) {
            results.push(text);
            return;
          }

          if (last.content && last.content.length > 0 && typeof last.content[last.content.length - 1] === 'string') {
            last.content[last.content.length - 1] = `${last.content[last.content.length - 1]}${text}`;
            return;
          }

          if (last.content === undefined) {
            last.content = [];
          }

          last.content.push(text);
        }
      }, options || defaultOptions);

      parser.write(html);
      parser.end();

      return results;
    }

    function parserWrapper(...args) {
      let option;

      function parser(html) {
        const opt = {...defaultOptions, ...option};
        return postHTMLParser(html, opt);
      }

      if (
        args.length === 1 &&
        Boolean(args[0]) &&
        args[0].constructor.name === 'Object'
      ) {
        option = args[0];
        return parser;
      }

      option = args[1];
      return parser(args[0]);
    }

    module.exports = parserWrapper;
    module.exports.defaultOptions = defaultOptions;
    module.exports.defaultDirectives = defaultDirectives;