Skip to content
This repository was archived by the owner on Jan 23, 2020. It is now read-only.

Commit e102b7b

Browse files
author
adon
committed
make parser's method resilient when state tracking is off
1 parent ed7605d commit e102b7b

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/context-parser.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ function FastParser(config) {
4040

4141
/**
4242
* @function FastParser#reset
43+
* @param {integer} initState (optional) - set the initial state, if it is not stateMachine.State.STATE_DATA
4344
*
4445
* @description
4546
* Reset all internal states, as if being created with the new operator
4647
*/
47-
FastParser.prototype.reset = function () {
48+
FastParser.prototype.reset = function (initState) {
4849
var self = this;
4950

50-
self.state = stateMachine.State.STATE_DATA; /* Save the current status */
51+
self.state = initState || stateMachine.State.STATE_DATA; /* Save the current status */
5152
self.tags = ['', '']; /* Save the current tag name */
5253
self.tagIdx = 0;
5354
self.attrName = ''; /* Save the current attribute name */
@@ -475,14 +476,15 @@ Parser.prototype.constructor = Parser;
475476

476477
/**
477478
* @function Parser#reset
479+
* @param {integer} initState (optional) - set the initial state, if it is not stateMachine.State.STATE_DATA
478480
*
479481
* @description
480482
* Reset all internal states, as if being created with the new operator
481483
*/
482-
Parser.prototype.reset = function () {
484+
Parser.prototype.reset = function (initState) {
483485
var self = this;
484486

485-
FastParser.prototype.reset.call(self);
487+
FastParser.prototype.reset.call(self, initState);
486488

487489
if (self.config.enableStateTracking) {
488490
self.states = [this.state];
@@ -590,8 +592,11 @@ Parser.prototype.getModifiedInput = function() {
590592
*
591593
*/
592594
Parser.prototype.setCurrentState = function(state) {
593-
this.states.pop();
594-
this.states.push(this.state = state);
595+
this.state = state;
596+
if (this.config.enableStateTracking) {
597+
this.states.pop();
598+
this.states.push(state);
599+
}
595600
return this;
596601
};
597602

@@ -611,55 +616,52 @@ Parser.prototype.getCurrentState = function() {
611616
/**
612617
* @function Parser#getStates
613618
*
614-
* @returns {Array} An array of states.
619+
* @returns {Array} An array of states, or undefined if enableStateTracking is off
615620
*
616621
* @description
617622
* Get the states of the HTML5 page
618623
*
619624
*/
620625
Parser.prototype.getStates = function() {
621-
return this.states.slice();
626+
return this.states && this.states.slice();
622627
};
623628

624629
/**
625630
* @function Parser#setInitState
631+
* @alias Parser#reset
626632
*
627633
* @param {integer} state - The initial state of the HTML5 Context Parser.
628634
*
629635
* @description
630-
* Set the init state of HTML5 Context Parser.
636+
* Set the init state of HTML5 Context Parser. Once this is called, any existing states will be reset, in order to make use of the init state for processing.
631637
*
632638
*/
633-
Parser.prototype.setInitState = function(state) {
634-
this.states = [state];
635-
return this;
636-
};
639+
Parser.prototype.setInitState = Parser.prototype.reset;
637640

638641
/**
639642
* @function Parser#getInitState
640643
*
641-
* @returns {integer} The initial state of the HTML5 Context Parser.
644+
* @returns {integer} The initial state of the HTML5 Context Parser, or undefined if enableStateTracking is off
642645
*
643646
* @description
644647
* Get the init state of HTML5 Context Parser.
645648
*
646649
*/
647650
Parser.prototype.getInitState = function() {
648-
return this.states[0];
651+
return this.states && this.states[0];
649652
};
650653

651654
/**
652655
* @function Parser#getLastState
653656
*
654-
* @returns {integer} The last state of the HTML5 Context Parser.
657+
* @returns {integer} The last state of the HTML5 Context Parser, or undefined if enableStateTracking is off
655658
*
656659
* @description
657660
* Get the last state of HTML5 Context Parser.
658661
*
659662
*/
660663
Parser.prototype.getLastState = function() {
661-
// * undefined if length = 0
662-
return this.states[ this.states.length - 1 ];
664+
return this.states && this.states[ this.states.length - 1 ];
663665
};
664666

665667
/**

0 commit comments

Comments
 (0)