"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/xmldom/dom-parser.js" (7 Feb 2017, 8345 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 function DOMParser(options){
    2     this.options = options ||{locator:{}};
    3     
    4 }
    5 DOMParser.prototype.parseFromString = function(source,mimeType){
    6     var options = this.options;
    7     var sax =  new XMLReader();
    8     var domBuilder = options.domBuilder || new DOMHandler();//contentHandler and LexicalHandler
    9     var errorHandler = options.errorHandler;
   10     var locator = options.locator;
   11     var defaultNSMap = options.xmlns||{};
   12     var entityMap = {'lt':'<','gt':'>','amp':'&','quot':'"','apos':"'"}
   13     if(locator){
   14         domBuilder.setDocumentLocator(locator)
   15     }
   16     
   17     sax.errorHandler = buildErrorHandler(errorHandler,domBuilder,locator);
   18     sax.domBuilder = options.domBuilder || domBuilder;
   19     if(/\/x?html?$/.test(mimeType)){
   20         entityMap.nbsp = '\xa0';
   21         entityMap.copy = '\xa9';
   22         defaultNSMap['']= 'http://www.w3.org/1999/xhtml';
   23     }
   24     defaultNSMap.xml = defaultNSMap.xml || 'http://www.w3.org/XML/1998/namespace';
   25     if(source){
   26         sax.parse(source,defaultNSMap,entityMap);
   27     }else{
   28         sax.errorHandler.error("invalid doc source");
   29     }
   30     return domBuilder.doc;
   31 }
   32 function buildErrorHandler(errorImpl,domBuilder,locator){
   33     if(!errorImpl){
   34         if(domBuilder instanceof DOMHandler){
   35             return domBuilder;
   36         }
   37         errorImpl = domBuilder ;
   38     }
   39     var errorHandler = {}
   40     var isCallback = errorImpl instanceof Function;
   41     locator = locator||{}
   42     function build(key){
   43         var fn = errorImpl[key];
   44         if(!fn && isCallback){
   45             fn = errorImpl.length == 2?function(msg){errorImpl(key,msg)}:errorImpl;
   46         }
   47         errorHandler[key] = fn && function(msg){
   48             fn('[xmldom '+key+']\t'+msg+_locator(locator));
   49         }||function(){};
   50     }
   51     build('warning');
   52     build('error');
   53     build('fatalError');
   54     return errorHandler;
   55 }
   56 
   57 //console.log('#\n\n\n\n\n\n\n####')
   58 /**
   59  * +ContentHandler+ErrorHandler
   60  * +LexicalHandler+EntityResolver2
   61  * -DeclHandler-DTDHandler 
   62  * 
   63  * DefaultHandler:EntityResolver, DTDHandler, ContentHandler, ErrorHandler
   64  * DefaultHandler2:DefaultHandler,LexicalHandler, DeclHandler, EntityResolver2
   65  * @link http://www.saxproject.org/apidoc/org/xml/sax/helpers/DefaultHandler.html
   66  */
   67 function DOMHandler() {
   68     this.cdata = false;
   69 }
   70 function position(locator,node){
   71     node.lineNumber = locator.lineNumber;
   72     node.columnNumber = locator.columnNumber;
   73 }
   74 /**
   75  * @see org.xml.sax.ContentHandler#startDocument
   76  * @link http://www.saxproject.org/apidoc/org/xml/sax/ContentHandler.html
   77  */ 
   78 DOMHandler.prototype = {
   79     startDocument : function() {
   80         this.doc = new DOMImplementation().createDocument(null, null, null);
   81         if (this.locator) {
   82             this.doc.documentURI = this.locator.systemId;
   83         }
   84     },
   85     startElement:function(namespaceURI, localName, qName, attrs) {
   86         var doc = this.doc;
   87         var el = doc.createElementNS(namespaceURI, qName||localName);
   88         var len = attrs.length;
   89         appendElement(this, el);
   90         this.currentElement = el;
   91         
   92         this.locator && position(this.locator,el)
   93         for (var i = 0 ; i < len; i++) {
   94             var namespaceURI = attrs.getURI(i);
   95             var value = attrs.getValue(i);
   96             var qName = attrs.getQName(i);
   97             var attr = doc.createAttributeNS(namespaceURI, qName);
   98             this.locator &&position(attrs.getLocator(i),attr);
   99             attr.value = attr.nodeValue = value;
  100             el.setAttributeNode(attr)
  101         }
  102     },
  103     endElement:function(namespaceURI, localName, qName) {
  104         var current = this.currentElement
  105         var tagName = current.tagName;
  106         this.currentElement = current.parentNode;
  107     },
  108     startPrefixMapping:function(prefix, uri) {
  109     },
  110     endPrefixMapping:function(prefix) {
  111     },
  112     processingInstruction:function(target, data) {
  113         var ins = this.doc.createProcessingInstruction(target, data);
  114         this.locator && position(this.locator,ins)
  115         appendElement(this, ins);
  116     },
  117     ignorableWhitespace:function(ch, start, length) {
  118     },
  119     characters:function(chars, start, length) {
  120         chars = _toString.apply(this,arguments)
  121         //console.log(chars)
  122         if(chars){
  123             if (this.cdata) {
  124                 var charNode = this.doc.createCDATASection(chars);
  125             } else {
  126                 var charNode = this.doc.createTextNode(chars);
  127             }
  128             if(this.currentElement){
  129                 this.currentElement.appendChild(charNode);
  130             }else if(/^\s*$/.test(chars)){
  131                 this.doc.appendChild(charNode);
  132                 //process xml
  133             }
  134             this.locator && position(this.locator,charNode)
  135         }
  136     },
  137     skippedEntity:function(name) {
  138     },
  139     endDocument:function() {
  140         this.doc.normalize();
  141     },
  142     setDocumentLocator:function (locator) {
  143         if(this.locator = locator){// && !('lineNumber' in locator)){
  144             locator.lineNumber = 0;
  145         }
  146     },
  147     //LexicalHandler
  148     comment:function(chars, start, length) {
  149         chars = _toString.apply(this,arguments)
  150         var comm = this.doc.createComment(chars);
  151         this.locator && position(this.locator,comm)
  152         appendElement(this, comm);
  153     },
  154     
  155     startCDATA:function() {
  156         //used in characters() methods
  157         this.cdata = true;
  158     },
  159     endCDATA:function() {
  160         this.cdata = false;
  161     },
  162     
  163     startDTD:function(name, publicId, systemId) {
  164         var impl = this.doc.implementation;
  165         if (impl && impl.createDocumentType) {
  166             var dt = impl.createDocumentType(name, publicId, systemId);
  167             this.locator && position(this.locator,dt)
  168             appendElement(this, dt);
  169         }
  170     },
  171     /**
  172      * @see org.xml.sax.ErrorHandler
  173      * @link http://www.saxproject.org/apidoc/org/xml/sax/ErrorHandler.html
  174      */
  175     warning:function(error) {
  176         console.warn('[xmldom warning]\t'+error,_locator(this.locator));
  177     },
  178     error:function(error) {
  179         console.error('[xmldom error]\t'+error,_locator(this.locator));
  180     },
  181     fatalError:function(error) {
  182         console.error('[xmldom fatalError]\t'+error,_locator(this.locator));
  183         throw error;
  184     }
  185 }
  186 function _locator(l){
  187     if(l){
  188         return '\n@'+(l.systemId ||'')+'#[line:'+l.lineNumber+',col:'+l.columnNumber+']'
  189     }
  190 }
  191 function _toString(chars,start,length){
  192     if(typeof chars == 'string'){
  193         return chars.substr(start,length)
  194     }else{//java sax connect width xmldom on rhino(what about: "? && !(chars instanceof String)")
  195         if(chars.length >= start+length || start){
  196             return new java.lang.String(chars,start,length)+'';
  197         }
  198         return chars;
  199     }
  200 }
  201 
  202 /*
  203  * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/LexicalHandler.html
  204  * used method of org.xml.sax.ext.LexicalHandler:
  205  *  #comment(chars, start, length)
  206  *  #startCDATA()
  207  *  #endCDATA()
  208  *  #startDTD(name, publicId, systemId)
  209  *
  210  *
  211  * IGNORED method of org.xml.sax.ext.LexicalHandler:
  212  *  #endDTD()
  213  *  #startEntity(name)
  214  *  #endEntity(name)
  215  *
  216  *
  217  * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/DeclHandler.html
  218  * IGNORED method of org.xml.sax.ext.DeclHandler
  219  *  #attributeDecl(eName, aName, type, mode, value)
  220  *  #elementDecl(name, model)
  221  *  #externalEntityDecl(name, publicId, systemId)
  222  *  #internalEntityDecl(name, value)
  223  * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/EntityResolver2.html
  224  * IGNORED method of org.xml.sax.EntityResolver2
  225  *  #resolveEntity(String name,String publicId,String baseURI,String systemId)
  226  *  #resolveEntity(publicId, systemId)
  227  *  #getExternalSubset(name, baseURI)
  228  * @link http://www.saxproject.org/apidoc/org/xml/sax/DTDHandler.html
  229  * IGNORED method of org.xml.sax.DTDHandler
  230  *  #notationDecl(name, publicId, systemId) {};
  231  *  #unparsedEntityDecl(name, publicId, systemId, notationName) {};
  232  */
  233 "endDTD,startEntity,endEntity,attributeDecl,elementDecl,externalEntityDecl,internalEntityDecl,resolveEntity,getExternalSubset,notationDecl,unparsedEntityDecl".replace(/\w+/g,function(key){
  234     DOMHandler.prototype[key] = function(){return null}
  235 })
  236 
  237 /* Private static helpers treated below as private instance methods, so don't need to add these to the public API; we might use a Relator to also get rid of non-standard public properties */
  238 function appendElement (hander,node) {
  239     if (!hander.currentElement) {
  240         hander.doc.appendChild(node);
  241     } else {
  242         hander.currentElement.appendChild(node);
  243     }
  244 }//appendChild and setAttributeNS are preformance key
  245 
  246 //if(typeof require == 'function'){
  247     var XMLReader = require('./sax').XMLReader;
  248     var DOMImplementation = exports.DOMImplementation = require('./dom').DOMImplementation;
  249     exports.XMLSerializer = require('./dom').XMLSerializer ;
  250     exports.DOMParser = DOMParser;
  251 //}