goog.provide( 'htmlparser.example.handler.html2json' ); goog.requireType( 'htmlparser.typedef.Handler' ); var rootNode, currentNode, stack, xmlDeclaration; /** * @extends {htmlparser.typedef.Handler} * @const */ htmlparser.example.handler.html2json = { _init : function(){ rootNode = currentNode = []; stack = []; xmlDeclaration = ''; }, _getResult : function(){ if( typeof rootNode[ 0 ] !== 'number' ){ rootNode.unshift( 11 ); }; return rootNode; }, onParseError : function( msg ){ throw msg; }, onParseDocType : function( doctype ){ rootNode[ 0 ] = 9; rootNode[ 1 ] = xmlDeclaration + doctype; xmlDeclaration = ''; }, onParseStartTag : function( tag, attrs, empty, myIndex ){ var element = [ tag ]; if( attrs ){ element[ 1 ] = attrs; }; currentNode.push( element ); if( !empty ){ stack.push( currentNode ); currentNode = element; }; }, onParseEndTag : function( tag, isInvalidEndTagOmission, isMissingStartTag ){ if( isMissingStartTag ){ currentNode.push( '' ); } else if( !isInvalidEndTagOmission ){ if( tag === currentNode[ 0 ] ){ currentNode = stack.pop(); } else { // error }; }; }, onParseText : function( text ){ currentNode.push( text ); }, onParseComment : function( comment ){ currentNode.push( [ 8, comment ] ); }, onParseCDATASection : function( data ){ currentNode.push( [ 4, data ] ); }, onParseProcessingInstruction : function( data ){ if( rootNode.length === 0 && data.indexOf( 'xml ' ) === 0 ){ xmlDeclaration = ''; } else { currentNode.push( [ 7, data ] ); }; } };