From 09453ba336c1b0e231d045b256f196c1a361a2fb Mon Sep 17 00:00:00 2001 From: Simon JAILLET Date: Sun, 29 Mar 2015 16:37:34 +0200 Subject: [PATCH] Simplify a bit the ontext handler. --- index.js | 43 ++++++++------------------- test/cases/24-with-start-indices.json | 1 + 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/index.js b/index.js index 41fd0bb9..2144f13b 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ var ElementType = require("domelementtype"); - -var re_whitespace = /\s+/g; var NodePrototype = require("./lib/node"); var ElementPrototype = require("./lib/element"); +var re_whitespace = /\s+/g; + function DomHandler(callback, options, elementCB){ if(typeof callback === "object"){ elementCB = options; @@ -42,6 +42,7 @@ DomHandler.prototype.onend = function(){ if(this._done) return; this._done = true; this._parser = null; + this._activeTextNode = null; this._handleCallback(null); }; @@ -55,7 +56,7 @@ DomHandler.prototype.onerror = function(error){ }; DomHandler.prototype.onclosetag = function(){ - //if(this._tagStack.pop().name !== name) this._handleCallback(Error("Tagname didn't match!")); + this._activeTextNode = null; var elem = this._tagStack.pop(); if(this._elementCB) this._elementCB(elem); }; @@ -84,6 +85,7 @@ DomHandler.prototype._addDomElement = function(element){ siblings.push(element); element.parent = parent || null; + this._activeTextNode = element.type === ElementType.Text ? element : null; }; DomHandler.prototype.onopentag = function(name, attribs){ @@ -104,36 +106,17 @@ DomHandler.prototype.ontext = function(data){ //it's an alias for normalizeWhitespace var normalize = this._options.normalizeWhitespace || this._options.ignoreWhitespace; - var lastTag; - - if(!this._tagStack.length && this.dom.length && (lastTag = this.dom[this.dom.length-1]).type === ElementType.Text){ - if(normalize){ - lastTag.data = (lastTag.data + data).replace(re_whitespace, " "); + if (this._activeTextNode) { + if (normalize) { + this._activeTextNode.data = (this._activeTextNode.data + data).replace(re_whitespace, " "); } else { - lastTag.data += data; + this._activeTextNode.data += data; } } else { - if( - this._tagStack.length && - (lastTag = this._tagStack[this._tagStack.length - 1]) && - (lastTag = lastTag.children[lastTag.children.length - 1]) && - lastTag.type === ElementType.Text - ){ - if(normalize){ - lastTag.data = (lastTag.data + data).replace(re_whitespace, " "); - } else { - lastTag.data += data; - } - } else { - if(normalize){ - data = data.replace(re_whitespace, " "); - } - - this._addDomElement({ - data: data, - type: ElementType.Text - }); - } + this._addDomElement({ + data: normalize ? data.replace(re_whitespace, " ") : data, + type: ElementType.Text + }); } }; diff --git a/test/cases/24-with-start-indices.json b/test/cases/24-with-start-indices.json index 02228f1e..72d61967 100644 --- a/test/cases/24-with-start-indices.json +++ b/test/cases/24-with-start-indices.json @@ -11,6 +11,7 @@ "type": "directive" }, { + "startIndex": 14, "type": "text", "data": " " },