# All files / src/patches htmlparser2.js

82.92% Statements 204/246
70.5% Branches 98/139
92.3% Functions 12/13
82.78% Lines 202/244

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
    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
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488

    18x

    18x
    230x

    18x
    467x

    18x

    18x
    18x
    18x
    18x
    18x
    18x
    18x

    18x
    18x
    18x
    18x
    18x
    18x
    18x

    18x
    18x

    18x

    18x
    18x
    18x
    18x

    18x
    18x
    18x
    18x
    18x
    18x
    18x
    18x
    18x

    18x
    18x

    18x
    18x
    18x
    18x
    18x
    18x
    18x
    18x
    18x
    18x

    18x
    18x
    18x
    18x
    18x
    18x
    18x
    18x

    18x
    18x
    18x
    18x
    18x

    18x

    18x
    18x
    18x

    18x

    1678x

    18x

    18x
    949x
    949x
    949x
    949x
    5694x
    137x

    949x
    53x
    53x

    896x

    18x
    140x
    140x
    140x

    140x
    224x
    224x

    224x
    9x
    9x

    215x
    87x

    131x

    131x

    18x
    1670x
    720x
    950x

    950x
    1x
    949x

    949x

    949x

    949x

    18x
    140x
    140x
    87x
    87x

    53x

    53x
    53x

    53x
    44x

    9x

    18x
    230x
    230x
    230x
    230x

    230x

    18x
    82x

    82x
    9x
    9x

    73x
    72x
    72x

    1x
    1x

    18x
    720x
    720x

    720x
    10x
    9x

    1x
    1x

    710x
    710x

    18x
    72x
    72x
    63x

    9x
    9x
    9x
    9x
    9x
    9x

    18x
    569x
    31061x

    31061x
    10743x
    20318x
    1670x
    18648x
    4110x
    14538x
    720x
    13818x
    2494x
    11324x
    719x
    10605x
    230x

    10375x
    2133x
    8242x
    1916x
    6326x
    467x
    5859x
    411x
    5448x
    5236x
    212x

    212x

    212x

    212x

    212x

    212x

    212x

    212x

    212x

    212x

    212x

    212x

    212x

    212x

    212x

    212x

    212x

    212x

    212x
    140x
    72x
    72x

    31061x

    569x

    18x

     /*
     * Three behaviours of htmlparser2 are patched here, the first 2 being 'convenience' patches
     * to avoid repeated passing of lowerCaseAttributeNames and recognizeSelfClosing options.
     * 1. Defaulting to self closing tag recognition
     * 2. Disabling automatic attribute name conversion to lower case
     * 3. Ability to inject/whitelist certain tags to be parsed like script/style tags do. ('special' tags)
     */

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

    /*
     Enable any self closing tags '<xx />' to be parsed.
     This allows MarkBind's own components to be recognised as such (e.g. '<panel />').
     This is equivalent to the { recognizeSelfClosing: true } option of htmlparser2;
     We modify the relevant code to avoid passing it in repeatedly.
     */
    Parser.prototype.onselfclosingtag = function () {
      this._closeCurrentTag();
    };

    /*
     Disable automatic lower case conversion.
     This is equivalent to the { lowerCaseAttributeNames: false } option of htmlparser2;
     We modify the relevant code to avoid passing it in repeatedly as well.
     */
    Parser.prototype.onattribname = function (name) {
      this._attribname = name;
    };

    /* eslint-disable
        brace-style,
        indent,
        keyword-spacing,
        max-len,
        no-mixed-spaces-and-tabs,
        no-multi-spaces,
        no-plusplus,
        no-tabs,
        no-unused-vars,
        no-var,
        one-var,
        quotes,
        semi,
        space-before-blocks,
        space-before-function-paren,
        spaced-comment,
        vars-on-top,
    */

    var i = 0,

        TEXT                      = i++,
        BEFORE_TAG_NAME           = i++, //after <
        IN_TAG_NAME               = i++,
        IN_SELF_CLOSING_TAG       = i++,
        BEFORE_CLOSING_TAG_NAME   = i++,
        IN_CLOSING_TAG_NAME       = i++,
        AFTER_CLOSING_TAG_NAME    = i++,

        //attributes
        BEFORE_ATTRIBUTE_NAME     = i++,
        IN_ATTRIBUTE_NAME         = i++,
        AFTER_ATTRIBUTE_NAME      = i++,
        BEFORE_ATTRIBUTE_VALUE    = i++,
        IN_ATTRIBUTE_VALUE_DQ     = i++, // "
        IN_ATTRIBUTE_VALUE_SQ     = i++, // '
        IN_ATTRIBUTE_VALUE_NQ     = i++,

        //declarations
        BEFORE_DECLARATION        = i++, // !
        IN_DECLARATION            = i++,

        //processing instructions
        IN_PROCESSING_INSTRUCTION = i++, // ?

        //comments
        BEFORE_COMMENT            = i++,
        IN_COMMENT                = i++,
        AFTER_COMMENT_1           = i++,
        AFTER_COMMENT_2           = i++,

        //cdata
        BEFORE_CDATA_1            = i++, // [
        BEFORE_CDATA_2            = i++, // C
        BEFORE_CDATA_3            = i++, // D
        BEFORE_CDATA_4            = i++, // A
        BEFORE_CDATA_5            = i++, // T
        BEFORE_CDATA_6            = i++, // A
        IN_CDATA                  = i++, // [
        AFTER_CDATA_1             = i++, // ]
        AFTER_CDATA_2             = i++, // ]

        //special tags
        BEFORE_SPECIAL            = i++, //S
        BEFORE_SPECIAL_END        = i++,   //S

    	/*
    	 SCRIPT and STYLE states are preserved to maintain consistency of
    	 un-patched methods using state numbers greater than them in htmlparser2.
    	 */
        BEFORE_SCRIPT_1           = i++, //C
        BEFORE_SCRIPT_2           = i++, //R
        BEFORE_SCRIPT_3           = i++, //I
        BEFORE_SCRIPT_4           = i++, //P
        BEFORE_SCRIPT_5           = i++, //T
        AFTER_SCRIPT_1            = i++, //C
        AFTER_SCRIPT_2            = i++, //R
        AFTER_SCRIPT_3            = i++, //I
        AFTER_SCRIPT_4            = i++, //P
        AFTER_SCRIPT_5            = i++, //T

        BEFORE_STYLE_1            = i++, //T
        BEFORE_STYLE_2            = i++, //Y
        BEFORE_STYLE_3            = i++, //L
        BEFORE_STYLE_4            = i++, //E
        AFTER_STYLE_1             = i++, //T
        AFTER_STYLE_2             = i++, //Y
        AFTER_STYLE_3             = i++, //L
        AFTER_STYLE_4             = i++, //E

        BEFORE_ENTITY             = i++, //&
        BEFORE_NUMERIC_ENTITY     = i++, //#
        IN_NAMED_ENTITY           = i++,
        IN_NUMERIC_ENTITY         = i++,
        IN_HEX_ENTITY             = i++, //X

    	SPECIAL_NONE              = 0,

    	/*
    	 Non parser-state constants
    	 */

    	// _matchSpecialTagsNextCharacters, _matchNextSpecialTagClosingCharacter
    	HAS_MATCHING              = -1,
    	NO_MATCH                  = -2,
    	HAS_MATCHED               = -3;

    const DEFAULT_SPECIAL_TAGS = [
    	'script',
    	'site-nav',
    	'style',
    	'variable',
    	'markdown',
    	'md',
    ];

    function whitespace(c) {
    	return c === " " || c === "\n" || c === "\t" || c === "\f" || c === "\r";
    }

    Tokenizer.prototype.specialTagNames = [...DEFAULT_SPECIAL_TAGS];

    /**
     * Checks whether the token matches one of the first characters of the special tags,
     * and initialises the _matchingSpecialTagIndexes array with the matches' indexes if so.
     */
    Tokenizer.prototype._matchSpecialTagsFirstCharacters = function(c) {
    	this._matchingSpecialTagIndexes = [];
    	const numSpecialTags = this.specialTagNames.length;
    	const lowerCaseChar = c.toLowerCase();
    	for (let j = 0; j < numSpecialTags; j += 1) {
    		if (lowerCaseChar === this.specialTagNames[j][0]) {
    			this._matchingSpecialTagIndexes.push(j)
    		}
    	}

    	if (this._matchingSpecialTagIndexes.length > 0) {
    		this._nextSpecialTagMatchIndex = 1;
    		return true;
    	}
    	return false;
    };

    /**
     * Processes the _matchingSpecialTagIndexes array, filtering out previous match indexes
     * that do not match the current token.
     * If one of the previous matches successfully matched, the match index is returned.
     */
    Tokenizer.prototype._matchSpecialTagsNextCharacters = function(c) {
    	const matchingSpecialTags = [];
    	const numMatchingTags = this._matchingSpecialTagIndexes.length;
    	const lowerCaseChar = c.toLowerCase();

    	for (let j = 0; j < numMatchingTags; j += 1) {
    		const tagIndex = this._matchingSpecialTagIndexes[j];
    		const testChar = this.specialTagNames[tagIndex][this._nextSpecialTagMatchIndex];

    		if (testChar === undefined) {
    			Eif (c === "/" || c === ">" || whitespace(c)) {
    				return tagIndex;
    			}
    		} else if (testChar === lowerCaseChar) {
    			matchingSpecialTags.push(tagIndex);
    		}
    	}

    	this._matchingSpecialTagIndexes = matchingSpecialTags;

    	return this._matchingSpecialTagIndexes.length > 0
    		? HAS_MATCHING
    		: NO_MATCH;
    };

    /**
     * Changes the Tokenizer state to BEFORE_SPECIAL if the token matches one of
     * the first characters of _specialTagNames.
     */
    Tokenizer.prototype._stateBeforeTagName = function(c) {
    	if (c === "/") {
    		this._state = BEFORE_CLOSING_TAG_NAME;
    	} else Iif (c === "<") {
    		this._cbs.ontext(this._getSection());
    		this._sectionStart = this._index;
    	} else if (c === ">" || this._special !== SPECIAL_NONE || whitespace(c)) {
    		this._state = TEXT;
    	} else Iif (c === "!") {
    		this._state = BEFORE_DECLARATION;
    		this._sectionStart = this._index + 1;
    	} else Iif (c === "?") {
    		this._state = IN_PROCESSING_INSTRUCTION;
    		this._sectionStart = this._index + 1;
    	} else {
    		// We want special tag treatment to be xmlMode independent for MarkBind.
    		this._state = this._matchSpecialTagsFirstCharacters(c)
    			? BEFORE_SPECIAL
    			: IN_TAG_NAME;
    		this._sectionStart = this._index;
    	}
    };

    /**
     * Changes the Tokenizer state to IN_TAG_NAME or BEFORE_SPECIAL state again depending
     * on whether there are still matches in _matchingSpecialTagIndexes.
     */
    Tokenizer.prototype._stateBeforeSpecial = function(c) {
    	const result = this._matchSpecialTagsNextCharacters(c);
    	if (result === HAS_MATCHING) {
    		this._nextSpecialTagMatchIndex += 1;
    		return;
    	}

    	// Reset for _matchNextSpecialTagClosingCharacter later
    	this._nextSpecialTagMatchIndex = 0;

    	this._state = IN_TAG_NAME;
    	this._index--; //consume the token again

    	if (result === NO_MATCH) {
    		return;
    	}

    	/*
    	 Assign the one-based index of the matching tag in the special tags array.
    	 This is due to htmlparser2 using 0 for SPECIAL_NONE,
    	 which would conflict with a zero-based index.
    	*/
    	this._special = result + 1;
    };

    /**
     * Patched self closing tag state handler that removes the special state
     * if the special tag was self-closed.
     */
    Tokenizer.prototype._stateInSelfClosingTag = function(c) {
    	if (c === ">") {
    		this._cbs.onselfclosingtag();
    		this._state = TEXT;
    		this._sectionStart = this._index + 1;
    		/*
    		 Allow all special tags to be self-closed.
    		 Script and style tags are also allowed to be self-closed,
    		 which breaks from the default html spec-compliant of behaviour of htmlparser2.
    		 We allow this as such tags would be expanded upon re-rendering the html anyway.
    		 ie. '<script ... />' would end up as '<script ...></script>'
    		 */
    		this._special = SPECIAL_NONE;
    	} else Eif (!whitespace(c)) {
    		this._state = BEFORE_ATTRIBUTE_NAME;
    		this._index--;
    	}
    };

    /**
     * Processes the _special flag and _nextSpecialTagMatchIndex state variable,
     * returning a flag indicating whether the current special tag has finished matching or not.
     */
    Tokenizer.prototype._matchNextSpecialTagClosingCharacter = function(c) {
    	const nextTestChar = this.specialTagNames[this._special - 1][this._nextSpecialTagMatchIndex];

    	if (nextTestChar === undefined) {
    		this._nextSpecialTagMatchIndex = 0;
    		return (c === ">" || whitespace(c))
    			? HAS_MATCHED
    			: NO_MATCH;
    	} else if (nextTestChar === c.toLowerCase()) {
    		this._nextSpecialTagMatchIndex += 1;
    		return HAS_MATCHING;
    	}

    	this._nextSpecialTagMatchIndex = 0;
    	return NO_MATCH;
    };

    /**
     * Changes the Tokenizer state to BEFORE_SPECIAL_END if the token matches one of
     * the first character of the currently matched special tag.
     */
    Tokenizer.prototype._stateBeforeCloseingTagName = function(c) {
    	Iif (whitespace(c));
    	else Iif (c === ">") {
    		this._state = TEXT;
    	} else if (this._special !== SPECIAL_NONE) {
    		if (this._matchNextSpecialTagClosingCharacter(c) !== NO_MATCH) {
    			this._state = BEFORE_SPECIAL_END;
    		} else {
    			this._state = TEXT;
    			this._index--;
    		}
    	} else {
    		this._state = IN_CLOSING_TAG_NAME;
    		this._sectionStart = this._index;
    	}
    };

    /**
     * Changes the Tokenizer state back to TEXT, IN_TAG_NAME depending
     * on whether the token has finished or is still matching
     * the currently matched special tag.
     */
    Tokenizer.prototype._stateBeforeSpecialEnd = function(c) {
    	const result = this._matchNextSpecialTagClosingCharacter(c);
    	if (result === HAS_MATCHING) {
    		return;
    	}

    	Eif (result === HAS_MATCHED) {
    		this._sectionStart = this._index - this.specialTagNames[this._special - 1].length;
    		this._special = SPECIAL_NONE;
    		this._state = IN_CLOSING_TAG_NAME;
    		this._index--; //reconsume the token
    		return;
    	}

    	this._index--;
    	this._state = TEXT;
    };

    /**
     * Altered _parse handler that handles the extra MARKDOWN state.
     * BEFORE/AFTER SCRIPT and STYLE state handlers are made redundant
     * by the above patches and removed as well.
     */
    Tokenizer.prototype._parse = function(){
    	while(this._index < this._buffer.length && this._running){
    		var c = this._buffer.charAt(this._index);

    		if(this._state === TEXT){
    			this._stateText(c);
    		} else if(this._state === BEFORE_TAG_NAME){
    			this._stateBeforeTagName(c);
    		} else if(this._state === IN_TAG_NAME){
    			this._stateInTagName(c);
    		} else if(this._state === BEFORE_CLOSING_TAG_NAME){
    			this._stateBeforeCloseingTagName(c);
    		} else if(this._state === IN_CLOSING_TAG_NAME){
    			this._stateInCloseingTagName(c);
    		} else if(this._state === AFTER_CLOSING_TAG_NAME){
    			this._stateAfterCloseingTagName(c);
    		} else if(this._state === IN_SELF_CLOSING_TAG){
    			this._stateInSelfClosingTag(c);
    		}

    		/*
    		*	attributes
    		*/
    		else if(this._state === BEFORE_ATTRIBUTE_NAME){
    			this._stateBeforeAttributeName(c);
    		} else if(this._state === IN_ATTRIBUTE_NAME){
    			this._stateInAttributeName(c);
    		} else if(this._state === AFTER_ATTRIBUTE_NAME){
    			this._stateAfterAttributeName(c);
    		} else if(this._state === BEFORE_ATTRIBUTE_VALUE){
    			this._stateBeforeAttributeValue(c);
    		} else if(this._state === IN_ATTRIBUTE_VALUE_DQ){
    			this._stateInAttributeValueDoubleQuotes(c);
    		} else Iif(this._state === IN_ATTRIBUTE_VALUE_SQ){
    			this._stateInAttributeValueSingleQuotes(c);
    		} else Iif(this._state === IN_ATTRIBUTE_VALUE_NQ){
    			this._stateInAttributeValueNoQuotes(c);
    		}

    		/*
    		*	declarations
    		*/
    		else Iif(this._state === BEFORE_DECLARATION){
    			this._stateBeforeDeclaration(c);
    		} else Iif(this._state === IN_DECLARATION){
    			this._stateInDeclaration(c);
    		}

    		/*
    		*	processing instructions
    		*/
    		else Iif(this._state === IN_PROCESSING_INSTRUCTION){
    			this._stateInProcessingInstruction(c);
    		}

    		/*
    		*	comments
    		*/
    		else Iif(this._state === BEFORE_COMMENT){
    			this._stateBeforeComment(c);
    		} else Iif(this._state === IN_COMMENT){
    			this._stateInComment(c);
    		} else Iif(this._state === AFTER_COMMENT_1){
    			this._stateAfterComment1(c);
    		} else Iif(this._state === AFTER_COMMENT_2){
    			this._stateAfterComment2(c);
    		}

    		/*
    		*	cdata
    		*/
    		else Iif(this._state === BEFORE_CDATA_1){
    			this._stateBeforeCdata1(c);
    		} else Iif(this._state === BEFORE_CDATA_2){
    			this._stateBeforeCdata2(c);
    		} else Iif(this._state === BEFORE_CDATA_3){
    			this._stateBeforeCdata3(c);
    		} else Iif(this._state === BEFORE_CDATA_4){
    			this._stateBeforeCdata4(c);
    		} else Iif(this._state === BEFORE_CDATA_5){
    			this._stateBeforeCdata5(c);
    		} else Iif(this._state === BEFORE_CDATA_6){
    			this._stateBeforeCdata6(c);
    		} else Iif(this._state === IN_CDATA){
    			this._stateInCdata(c);
    		} else Iif(this._state === AFTER_CDATA_1){
    			this._stateAfterCdata1(c);
    		} else Iif(this._state === AFTER_CDATA_2){
    			this._stateAfterCdata2(c);
    		}

    		/*
    		* special tags
    		*/
    		else if(this._state === BEFORE_SPECIAL){
    			this._stateBeforeSpecial(c);
    		} else if(this._state === BEFORE_SPECIAL_END){
    			this._stateBeforeSpecialEnd(c);
    		}

    		/*
    		* entities
    		*/
    		else Eif(this._state === BEFORE_ENTITY){
    			this._stateBeforeEntity(c);
    		} else if(this._state === BEFORE_NUMERIC_ENTITY){
    			this._stateBeforeNumericEntity(c);
    		} else if(this._state === IN_NAMED_ENTITY){
    			this._stateInNamedEntity(c);
    		} else if(this._state === IN_NUMERIC_ENTITY){
    			this._stateInNumericEntity(c);
    		} else if(this._state === IN_HEX_ENTITY){
    			this._stateInHexEntity(c);
    		}

    		else {
    			this._cbs.onerror(Error("unknown _state"), this._state);
    		}

    		this._index++;
    	}

    	this._cleanup();
    };

    /**
     * Injects the tagsToIgnore into the Tokenizer's specialTagNames.
     */
    function injectIgnoreTags(tagsToIgnore) {
    	Tokenizer.prototype.specialTagNames = [...DEFAULT_SPECIAL_TAGS, ...tagsToIgnore];
    }

    module.exports = {
      injectIgnoreTags,
    };