123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- var ElementType = require("domelementtype");
- var re_whitespace = /\s+/g;
- function DomHandler(callback, options, elementCB){
- if(typeof callback === "object"){
- elementCB = options;
- options = callback;
- callback = null;
- } else if(typeof options === "function"){
- elementCB = options;
- options = defaultOpts;
- }
- this._callback = callback;
- this._options = options || defaultOpts;
- this._elementCB = elementCB;
- this.dom = [];
- this._done = false;
- this._tagStack = [];
- }
- //default options
- var defaultOpts = {
- normalizeWhitespace: false //Replace all whitespace with single spaces
- };
- //Resets the handler back to starting state
- DomHandler.prototype.onreset = function(){
- DomHandler.call(this, this._callback, this._options, this._elementCB);
- };
- //Signals the handler that parsing is done
- DomHandler.prototype.onend = function(){
- if(this._done) return;
- this._done = true;
- this._handleCallback(null);
- };
- DomHandler.prototype._handleCallback =
- DomHandler.prototype.onerror = function(error){
- if(typeof this._callback === "function"){
- this._callback(error, this.dom);
- } else {
- if(error) throw error;
- }
- };
- DomHandler.prototype.onclosetag = function(name){
- //if(this._tagStack.pop().name !== name) this._handleCallback(Error("Tagname didn't match!"));
- var elem = this._tagStack.pop();
- if(this._elementCB) this._elementCB(elem);
- };
- DomHandler.prototype._addDomElement = function(element){
- var lastTag = this._tagStack[this._tagStack.length - 1];
- if(lastTag){
- lastTag.children.push(element);
- } else { //There aren't parent elements
- this.dom.push(element);
- }
- };
- DomHandler.prototype.onopentag = function(name, attribs){
- var lastTag = this._tagStack[this._tagStack.length - 1];
- var element = {
- type: name === "script" ? ElementType.Script : name === "style" ? ElementType.Style : ElementType.Tag,
- name: name,
- attribs: attribs,
- children: [],
- prev: null,
- next: null,
- parent: lastTag || null
- };
- if(lastTag){
- var idx = lastTag.children.length;
- while(idx > 0){
- if(ElementType.isTag(lastTag.children[--idx])){
- element.prev = lastTag.children[idx];
- lastTag.children[idx].next = element;
- break;
- }
- }
- lastTag.children.push(element);
- } else {
- this.dom.push(element);
- }
- this._tagStack.push(element);
- };
- DomHandler.prototype.ontext = function(data){
- //the ignoreWhitespace is officially dropped, but for now,
- //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, " ");
- } else {
- lastTag.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
- });
- }
- }
- };
- DomHandler.prototype.oncomment = function(data){
- var lastTag = this._tagStack[this._tagStack.length - 1];
- if(lastTag && lastTag.type === ElementType.Comment){
- lastTag.data += data;
- return;
- }
- var element = {
- data: data,
- type: ElementType.Comment
- };
- this._addDomElement(element);
- this._tagStack.push(element);
- };
- DomHandler.prototype.oncdatastart = function(){
- var element = {
- children: [{
- data: "",
- type: ElementType.Text
- }],
- type: ElementType.CDATA
- };
- this._addDomElement(element);
- this._tagStack.push(element);
- };
- DomHandler.prototype.oncommentend = DomHandler.prototype.oncdataend = function(){
- this._tagStack.pop();
- };
- DomHandler.prototype.onprocessinginstruction = function(name, data){
- this._addDomElement({
- name: name,
- data: data,
- type: ElementType.Directive
- });
- };
- module.exports = DomHandler;
|