From 6585609d74ae2679103a8a73e58696c4bd2f9219 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Thu, 6 Feb 2014 13:48:32 -0700 Subject: [PATCH 1/6] #73 Added support for recognizing self-closing tags and CDATA in non-XML mode --- lib/Parser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Parser.js b/lib/Parser.js index e94e346f..3a24e191 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -174,7 +174,7 @@ Parser.prototype.onclosetag = function(name){ }; Parser.prototype.onselfclosingtag = function(){ - if(this._options.xmlMode){ + if(this._options.xmlMode || this._options.recognizeSelfClosing){ this._closeCurrentTag(); } else { this.onopentagend(); @@ -253,7 +253,7 @@ Parser.prototype.oncomment = function(value){ Parser.prototype.oncdata = function(value){ this._updatePosition(1); - if(this._options.xmlMode){ + if(this._options.xmlMode || this._options.recognizeCDATA){ if(this._cbs.oncdatastart) this._cbs.oncdatastart(); if(this._cbs.ontext) this._cbs.ontext(value); if(this._cbs.oncdataend) this._cbs.oncdataend(); From 6c173b888a1b37d4f245e10f513c41f6c6266799 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Thu, 6 Feb 2014 20:17:57 -0700 Subject: [PATCH 2/6] Fix option to disable lower case tags and attars in non-XML mode --- lib/Parser.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/Parser.js b/lib/Parser.js index 3a24e191..76a24b66 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -111,7 +111,7 @@ Parser.prototype.ontext = function(data){ }; Parser.prototype.onopentagname = function(name){ - if(!(this._options.xmlMode || "lowerCaseTags" in this._options) || this._options.lowerCaseTags){ + if(this.isLowerCaseTagsEnabled()){ name = name.toLowerCase(); } @@ -151,7 +151,7 @@ Parser.prototype.onopentagend = function(){ Parser.prototype.onclosetag = function(name){ this._updatePosition(1); - if(!(this._options.xmlMode || "lowerCaseTags" in this._options) || this._options.lowerCaseTags){ + if(this.isLowerCaseTagsEnabled()){ name = name.toLowerCase(); } @@ -197,7 +197,7 @@ Parser.prototype._closeCurrentTag = function(){ }; Parser.prototype.onattribname = function(name){ - if(!(this._options.xmlMode || "lowerCaseAttributeNames" in this._options) || this._options.lowerCaseAttributeNames){ + if(this.isLowerCaseAttributeNamesEnabled()){ name = name.toLowerCase(); } this._attribname = name; @@ -224,7 +224,7 @@ Parser.prototype.ondeclaration = function(value){ var idx = value.search(re_nameEnd), name = idx < 0 ? value : value.substr(0, idx); - if(!(this._options.xmlMode || "lowerCaseTags" in this._options) || this._options.lowerCaseTags){ + if(this.isLowerCaseTagsEnabled()){ name = name.toLowerCase(); } this._cbs.onprocessinginstruction("!" + name, "!" + value); @@ -236,7 +236,7 @@ Parser.prototype.onprocessinginstruction = function(value){ var idx = value.search(re_nameEnd), name = idx < 0 ? value : value.substr(0, idx); - if(!(this._options.xmlMode || "lowerCaseTags" in this._options) || this._options.lowerCaseTags){ + if(this.isLowerCaseTagsEnabled()){ name = name.toLowerCase(); } this._cbs.onprocessinginstruction("?" + name, "?" + value); @@ -307,6 +307,14 @@ Parser.prototype.end = function(chunk){ this._done = true; }; +Parser.prototype.isLowerCaseTagsEnabled = function() { + return this._options.lowerCaseTags == null ? !this._options.xmlMode : this._options.lowerCaseTags === true; +}; + +Parser.prototype.isLowerCaseAttributeNamesEnabled = function() { + return this._options.lowerCaseAttributeNames == null ? !this._options.xmlMode : this._options.lowerCaseAttributeNames === true; +}; + //alias for backwards compat Parser.prototype.parseChunk = Parser.prototype.write; Parser.prototype.done = Parser.prototype.end; From 357be1d1990bb2073146a163cbe04f8a65828f9b Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Mon, 10 Feb 2014 16:54:40 -0700 Subject: [PATCH 3/6] Added this._lowerCaseTagNames and this._lowerCaseAttributeNames --- lib/Parser.js | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/Parser.js b/lib/Parser.js index 76a24b66..fed637b3 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -89,6 +89,9 @@ function Parser(cbs, options){ this.startIndex = 0; this.endIndex = null; + this._lowerCaseTagNames = options.lowerCaseTags == null ? !options.xmlMode : options.lowerCaseTags === true; + this._lowerCaseAttributeNames = options.lowerCaseAttributeNames == null ? !options.xmlMode : options.lowerCaseAttributeNames === true; + this._tokenizer = new Tokenizer(options, this); } @@ -111,7 +114,7 @@ Parser.prototype.ontext = function(data){ }; Parser.prototype.onopentagname = function(name){ - if(this.isLowerCaseTagsEnabled()){ + if(this._lowerCaseTagNames){ name = name.toLowerCase(); } @@ -151,7 +154,7 @@ Parser.prototype.onopentagend = function(){ Parser.prototype.onclosetag = function(name){ this._updatePosition(1); - if(this.isLowerCaseTagsEnabled()){ + if(this._lowerCaseTagNames){ name = name.toLowerCase(); } @@ -197,7 +200,7 @@ Parser.prototype._closeCurrentTag = function(){ }; Parser.prototype.onattribname = function(name){ - if(this.isLowerCaseAttributeNamesEnabled()){ + if(this._lowerCaseAttributeNames){ name = name.toLowerCase(); } this._attribname = name; @@ -224,7 +227,7 @@ Parser.prototype.ondeclaration = function(value){ var idx = value.search(re_nameEnd), name = idx < 0 ? value : value.substr(0, idx); - if(this.isLowerCaseTagsEnabled()){ + if(this._lowerCaseTagNames){ name = name.toLowerCase(); } this._cbs.onprocessinginstruction("!" + name, "!" + value); @@ -236,7 +239,7 @@ Parser.prototype.onprocessinginstruction = function(value){ var idx = value.search(re_nameEnd), name = idx < 0 ? value : value.substr(0, idx); - if(this.isLowerCaseTagsEnabled()){ + if(this._lowerCaseTagNames){ name = name.toLowerCase(); } this._cbs.onprocessinginstruction("?" + name, "?" + value); @@ -307,14 +310,6 @@ Parser.prototype.end = function(chunk){ this._done = true; }; -Parser.prototype.isLowerCaseTagsEnabled = function() { - return this._options.lowerCaseTags == null ? !this._options.xmlMode : this._options.lowerCaseTags === true; -}; - -Parser.prototype.isLowerCaseAttributeNamesEnabled = function() { - return this._options.lowerCaseAttributeNames == null ? !this._options.xmlMode : this._options.lowerCaseAttributeNames === true; -}; - //alias for backwards compat Parser.prototype.parseChunk = Parser.prototype.write; Parser.prototype.done = Parser.prototype.end; From bdb127331d920e99aea4b1affdb17621bf8e9eaf Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Tue, 11 Feb 2014 07:50:41 -0700 Subject: [PATCH 4/6] Handle case where options is null and allow truthy values --- lib/Parser.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/Parser.js b/lib/Parser.js index fed637b3..ff113916 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -76,7 +76,8 @@ var voidElements = { var re_nameEnd = /\s|\//; function Parser(cbs, options){ - this._options = options || {}; + options = options || {}; + this._options = options; this._cbs = cbs || {}; this._tagname = ""; @@ -89,8 +90,8 @@ function Parser(cbs, options){ this.startIndex = 0; this.endIndex = null; - this._lowerCaseTagNames = options.lowerCaseTags == null ? !options.xmlMode : options.lowerCaseTags === true; - this._lowerCaseAttributeNames = options.lowerCaseAttributeNames == null ? !options.xmlMode : options.lowerCaseAttributeNames === true; + this._lowerCaseTagNames = options.lowerCaseTags == null ? !options.xmlMode : !!options.lowerCaseTags; + this._lowerCaseAttributeNames = options.lowerCaseAttributeNames == null ? !options.xmlMode : !!options.lowerCaseAttributeNames; this._tokenizer = new Tokenizer(options, this); } From adfaafb31d5569b03552d929b7c1a1b3517ac738 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Fri, 14 Feb 2014 12:49:21 -0700 Subject: [PATCH 5/6] Switched to using "in" operator for options --- lib/Parser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Parser.js b/lib/Parser.js index ff113916..56254c60 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -90,8 +90,8 @@ function Parser(cbs, options){ this.startIndex = 0; this.endIndex = null; - this._lowerCaseTagNames = options.lowerCaseTags == null ? !options.xmlMode : !!options.lowerCaseTags; - this._lowerCaseAttributeNames = options.lowerCaseAttributeNames == null ? !options.xmlMode : !!options.lowerCaseAttributeNames; + this._lowerCaseTagNames = "lowerCaseTags" in this._options ? !!this._options.lowerCaseTags : !this._options.xmlMode; + this._lowerCaseAttributeNames = "lowerCaseAttributeNames" in this._options ? !!this._options.lowerCaseAttributeNames : !this._options.xmlMode; this._tokenizer = new Tokenizer(options, this); } From 54f33adf1c323acb67f0357b6db4ebbe42bc9edf Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Fri, 14 Feb 2014 12:57:19 -0700 Subject: [PATCH 6/6] Merged options initialization into a single line --- lib/Parser.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Parser.js b/lib/Parser.js index 56254c60..cb018fbc 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -76,8 +76,7 @@ var voidElements = { var re_nameEnd = /\s|\//; function Parser(cbs, options){ - options = options || {}; - this._options = options; + this._options = options || {}; this._cbs = cbs || {}; this._tagname = ""; @@ -93,7 +92,7 @@ function Parser(cbs, options){ this._lowerCaseTagNames = "lowerCaseTags" in this._options ? !!this._options.lowerCaseTags : !this._options.xmlMode; this._lowerCaseAttributeNames = "lowerCaseAttributeNames" in this._options ? !!this._options.lowerCaseAttributeNames : !this._options.xmlMode; - this._tokenizer = new Tokenizer(options, this); + this._tokenizer = new Tokenizer(this._options, this); } require("util").inherits(Parser, require("events").EventEmitter);