mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-23 10:07:19 +00:00
Upgrade to latest version of xmldom
This commit is contained in:
parent
d9fd722e50
commit
f4a015f120
0
plugins/tiddlywiki/xmldom/files/LICENSE
Normal file → Executable file
0
plugins/tiddlywiki/xmldom/files/LICENSE
Normal file → Executable file
82
plugins/tiddlywiki/xmldom/files/dom-parser.js
Normal file → Executable file
82
plugins/tiddlywiki/xmldom/files/dom-parser.js
Normal file → Executable file
@ -2,32 +2,31 @@ function DOMParser(options){
|
|||||||
this.options = options ||{locator:{}};
|
this.options = options ||{locator:{}};
|
||||||
|
|
||||||
}
|
}
|
||||||
DOMParser.prototype.parseFromString = function(source,mimeType){
|
DOMParser.prototype.parseFromString = function(source,mimeType){
|
||||||
var options = this.options;
|
var options = this.options;
|
||||||
var sax = new XMLReader();
|
var sax = new XMLReader();
|
||||||
var domBuilder = options.domBuilder || new DOMHandler();//contentHandler and LexicalHandler
|
var domBuilder = options.domBuilder || new DOMHandler();//contentHandler and LexicalHandler
|
||||||
var errorHandler = options.errorHandler;
|
var errorHandler = options.errorHandler;
|
||||||
var locator = options.locator;
|
var locator = options.locator;
|
||||||
var defaultNSMap = options.xmlns||{};
|
var defaultNSMap = options.xmlns||{};
|
||||||
var entityMap = {'lt':'<','gt':'>','amp':'&','quot':'"','apos':"'"}
|
var isHTML = /\/x?html?$/.test(mimeType);//mimeType.toLowerCase().indexOf('html') > -1;
|
||||||
|
var entityMap = isHTML?require('./entities').entityMap:{'lt':'<','gt':'>','amp':'&','quot':'"','apos':"'"};
|
||||||
if(locator){
|
if(locator){
|
||||||
domBuilder.setDocumentLocator(locator)
|
domBuilder.setDocumentLocator(locator)
|
||||||
}
|
}
|
||||||
|
|
||||||
sax.errorHandler = buildErrorHandler(errorHandler,domBuilder,locator);
|
sax.errorHandler = buildErrorHandler(errorHandler,domBuilder,locator);
|
||||||
sax.domBuilder = options.domBuilder || domBuilder;
|
sax.domBuilder = options.domBuilder || domBuilder;
|
||||||
if(/\/x?html?$/.test(mimeType)){
|
if(isHTML){
|
||||||
entityMap.nbsp = '\xa0';
|
|
||||||
entityMap.copy = '\xa9';
|
|
||||||
defaultNSMap['']= 'http://www.w3.org/1999/xhtml';
|
defaultNSMap['']= 'http://www.w3.org/1999/xhtml';
|
||||||
}
|
}
|
||||||
defaultNSMap.xml = defaultNSMap.xml || 'http://www.w3.org/XML/1998/namespace';
|
defaultNSMap.xml = defaultNSMap.xml || 'http://www.w3.org/XML/1998/namespace';
|
||||||
if(source){
|
if(source){
|
||||||
sax.parse(source,defaultNSMap,entityMap);
|
sax.parse(source,defaultNSMap,entityMap);
|
||||||
}else{
|
}else{
|
||||||
sax.errorHandler.error("invalid document source");
|
sax.errorHandler.error("invalid doc source");
|
||||||
}
|
}
|
||||||
return domBuilder.document;
|
return domBuilder.doc;
|
||||||
}
|
}
|
||||||
function buildErrorHandler(errorImpl,domBuilder,locator){
|
function buildErrorHandler(errorImpl,domBuilder,locator){
|
||||||
if(!errorImpl){
|
if(!errorImpl){
|
||||||
@ -41,27 +40,20 @@ function buildErrorHandler(errorImpl,domBuilder,locator){
|
|||||||
locator = locator||{}
|
locator = locator||{}
|
||||||
function build(key){
|
function build(key){
|
||||||
var fn = errorImpl[key];
|
var fn = errorImpl[key];
|
||||||
if(!fn){
|
if(!fn && isCallback){
|
||||||
if(isCallback){
|
fn = errorImpl.length == 2?function(msg){errorImpl(key,msg)}:errorImpl;
|
||||||
fn = errorImpl.length == 2?function(msg){errorImpl(key,msg)}:errorImpl;
|
|
||||||
}else{
|
|
||||||
var i=arguments.length;
|
|
||||||
while(--i){
|
|
||||||
if(fn = errorImpl[arguments[i]]){
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
errorHandler[key] = fn && function(msg){
|
errorHandler[key] = fn && function(msg){
|
||||||
fn(msg+_locator(locator));
|
fn('[xmldom '+key+']\t'+msg+_locator(locator));
|
||||||
}||function(){};
|
}||function(){};
|
||||||
}
|
}
|
||||||
build('warning','warn');
|
build('warning');
|
||||||
build('error','warn','warning');
|
build('error');
|
||||||
build('fatalError','warn','warning','error');
|
build('fatalError');
|
||||||
return errorHandler;
|
return errorHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//console.log('#\n\n\n\n\n\n\n####')
|
||||||
/**
|
/**
|
||||||
* +ContentHandler+ErrorHandler
|
* +ContentHandler+ErrorHandler
|
||||||
* +LexicalHandler+EntityResolver2
|
* +LexicalHandler+EntityResolver2
|
||||||
@ -84,13 +76,13 @@ function position(locator,node){
|
|||||||
*/
|
*/
|
||||||
DOMHandler.prototype = {
|
DOMHandler.prototype = {
|
||||||
startDocument : function() {
|
startDocument : function() {
|
||||||
this.document = new DOMImplementation().createDocument(null, null, null);
|
this.doc = new DOMImplementation().createDocument(null, null, null);
|
||||||
if (this.locator) {
|
if (this.locator) {
|
||||||
this.document.documentURI = this.locator.systemId;
|
this.doc.documentURI = this.locator.systemId;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
startElement:function(namespaceURI, localName, qName, attrs) {
|
startElement:function(namespaceURI, localName, qName, attrs) {
|
||||||
var doc = this.document;
|
var doc = this.doc;
|
||||||
var el = doc.createElementNS(namespaceURI, qName||localName);
|
var el = doc.createElementNS(namespaceURI, qName||localName);
|
||||||
var len = attrs.length;
|
var len = attrs.length;
|
||||||
appendElement(this, el);
|
appendElement(this, el);
|
||||||
@ -102,24 +94,22 @@ DOMHandler.prototype = {
|
|||||||
var value = attrs.getValue(i);
|
var value = attrs.getValue(i);
|
||||||
var qName = attrs.getQName(i);
|
var qName = attrs.getQName(i);
|
||||||
var attr = doc.createAttributeNS(namespaceURI, qName);
|
var attr = doc.createAttributeNS(namespaceURI, qName);
|
||||||
if( attr.getOffset){
|
this.locator &&position(attrs.getLocator(i),attr);
|
||||||
position(attr.getOffset(1),attr)
|
|
||||||
}
|
|
||||||
attr.value = attr.nodeValue = value;
|
attr.value = attr.nodeValue = value;
|
||||||
el.setAttributeNode(attr)
|
el.setAttributeNode(attr)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
endElement:function(namespaceURI, localName, qName) {
|
endElement:function(namespaceURI, localName, qName) {
|
||||||
var current = this.currentElement
|
var current = this.currentElement
|
||||||
var tagName = current.tagName;
|
var tagName = current.tagName;
|
||||||
this.currentElement = current.parentNode;
|
this.currentElement = current.parentNode;
|
||||||
},
|
},
|
||||||
startPrefixMapping:function(prefix, uri) {
|
startPrefixMapping:function(prefix, uri) {
|
||||||
},
|
},
|
||||||
endPrefixMapping:function(prefix) {
|
endPrefixMapping:function(prefix) {
|
||||||
},
|
},
|
||||||
processingInstruction:function(target, data) {
|
processingInstruction:function(target, data) {
|
||||||
var ins = this.document.createProcessingInstruction(target, data);
|
var ins = this.doc.createProcessingInstruction(target, data);
|
||||||
this.locator && position(this.locator,ins)
|
this.locator && position(this.locator,ins)
|
||||||
appendElement(this, ins);
|
appendElement(this, ins);
|
||||||
},
|
},
|
||||||
@ -128,13 +118,17 @@ DOMHandler.prototype = {
|
|||||||
characters:function(chars, start, length) {
|
characters:function(chars, start, length) {
|
||||||
chars = _toString.apply(this,arguments)
|
chars = _toString.apply(this,arguments)
|
||||||
//console.log(chars)
|
//console.log(chars)
|
||||||
if(this.currentElement && chars){
|
if(chars){
|
||||||
if (this.cdata) {
|
if (this.cdata) {
|
||||||
var charNode = this.document.createCDATASection(chars);
|
var charNode = this.doc.createCDATASection(chars);
|
||||||
this.currentElement.appendChild(charNode);
|
|
||||||
} else {
|
} else {
|
||||||
var charNode = this.document.createTextNode(chars);
|
var charNode = this.doc.createTextNode(chars);
|
||||||
|
}
|
||||||
|
if(this.currentElement){
|
||||||
this.currentElement.appendChild(charNode);
|
this.currentElement.appendChild(charNode);
|
||||||
|
}else if(/^\s*$/.test(chars)){
|
||||||
|
this.doc.appendChild(charNode);
|
||||||
|
//process xml
|
||||||
}
|
}
|
||||||
this.locator && position(this.locator,charNode)
|
this.locator && position(this.locator,charNode)
|
||||||
}
|
}
|
||||||
@ -142,7 +136,7 @@ DOMHandler.prototype = {
|
|||||||
skippedEntity:function(name) {
|
skippedEntity:function(name) {
|
||||||
},
|
},
|
||||||
endDocument:function() {
|
endDocument:function() {
|
||||||
this.document.normalize();
|
this.doc.normalize();
|
||||||
},
|
},
|
||||||
setDocumentLocator:function (locator) {
|
setDocumentLocator:function (locator) {
|
||||||
if(this.locator = locator){// && !('lineNumber' in locator)){
|
if(this.locator = locator){// && !('lineNumber' in locator)){
|
||||||
@ -152,7 +146,7 @@ DOMHandler.prototype = {
|
|||||||
//LexicalHandler
|
//LexicalHandler
|
||||||
comment:function(chars, start, length) {
|
comment:function(chars, start, length) {
|
||||||
chars = _toString.apply(this,arguments)
|
chars = _toString.apply(this,arguments)
|
||||||
var comm = this.document.createComment(chars);
|
var comm = this.doc.createComment(chars);
|
||||||
this.locator && position(this.locator,comm)
|
this.locator && position(this.locator,comm)
|
||||||
appendElement(this, comm);
|
appendElement(this, comm);
|
||||||
},
|
},
|
||||||
@ -166,7 +160,7 @@ DOMHandler.prototype = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
startDTD:function(name, publicId, systemId) {
|
startDTD:function(name, publicId, systemId) {
|
||||||
var impl = this.document.implementation;
|
var impl = this.doc.implementation;
|
||||||
if (impl && impl.createDocumentType) {
|
if (impl && impl.createDocumentType) {
|
||||||
var dt = impl.createDocumentType(name, publicId, systemId);
|
var dt = impl.createDocumentType(name, publicId, systemId);
|
||||||
this.locator && position(this.locator,dt)
|
this.locator && position(this.locator,dt)
|
||||||
@ -178,13 +172,13 @@ DOMHandler.prototype = {
|
|||||||
* @link http://www.saxproject.org/apidoc/org/xml/sax/ErrorHandler.html
|
* @link http://www.saxproject.org/apidoc/org/xml/sax/ErrorHandler.html
|
||||||
*/
|
*/
|
||||||
warning:function(error) {
|
warning:function(error) {
|
||||||
console.warn(error,_locator(this.locator));
|
console.warn('[xmldom warning]\t'+error,_locator(this.locator));
|
||||||
},
|
},
|
||||||
error:function(error) {
|
error:function(error) {
|
||||||
console.error(error,_locator(this.locator));
|
console.error('[xmldom error]\t'+error,_locator(this.locator));
|
||||||
},
|
},
|
||||||
fatalError:function(error) {
|
fatalError:function(error) {
|
||||||
console.error(error,_locator(this.locator));
|
console.error('[xmldom fatalError]\t'+error,_locator(this.locator));
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -242,15 +236,15 @@ function _toString(chars,start,length){
|
|||||||
/* 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 */
|
/* 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 */
|
||||||
function appendElement (hander,node) {
|
function appendElement (hander,node) {
|
||||||
if (!hander.currentElement) {
|
if (!hander.currentElement) {
|
||||||
hander.document.appendChild(node);
|
hander.doc.appendChild(node);
|
||||||
} else {
|
} else {
|
||||||
hander.currentElement.appendChild(node);
|
hander.currentElement.appendChild(node);
|
||||||
}
|
}
|
||||||
}//appendChild and setAttributeNS are preformance key
|
}//appendChild and setAttributeNS are preformance key
|
||||||
|
|
||||||
if(typeof require == 'function'){
|
//if(typeof require == 'function'){
|
||||||
var XMLReader = require('./sax').XMLReader;
|
var XMLReader = require('./sax').XMLReader;
|
||||||
var DOMImplementation = exports.DOMImplementation = require('./dom').DOMImplementation;
|
var DOMImplementation = exports.DOMImplementation = require('./dom').DOMImplementation;
|
||||||
exports.XMLSerializer = require('./dom').XMLSerializer ;
|
exports.XMLSerializer = require('./dom').XMLSerializer ;
|
||||||
exports.DOMParser = DOMParser;
|
exports.DOMParser = DOMParser;
|
||||||
}
|
//}
|
||||||
|
165
plugins/tiddlywiki/xmldom/files/dom.js
Normal file → Executable file
165
plugins/tiddlywiki/xmldom/files/dom.js
Normal file → Executable file
@ -109,6 +109,12 @@ NodeList.prototype = {
|
|||||||
*/
|
*/
|
||||||
item: function(index) {
|
item: function(index) {
|
||||||
return this[index] || null;
|
return this[index] || null;
|
||||||
|
},
|
||||||
|
toString:function(isHTML,nodeFilter){
|
||||||
|
for(var buf = [], i = 0;i<this.length;i++){
|
||||||
|
serializeToString(this[i],buf,isHTML,nodeFilter);
|
||||||
|
}
|
||||||
|
return buf.join('');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
function LiveNodeList(node,refresh){
|
function LiveNodeList(node,refresh){
|
||||||
@ -164,6 +170,7 @@ function _addNamedNode(el,list,newAttr,oldAttr){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
function _removeNamedNode(el,list,attr){
|
function _removeNamedNode(el,list,attr){
|
||||||
|
//console.log('remove attr:'+attr)
|
||||||
var i = _findNodeIndex(list,attr);
|
var i = _findNodeIndex(list,attr);
|
||||||
if(i>=0){
|
if(i>=0){
|
||||||
var lastIndex = list.length-1
|
var lastIndex = list.length-1
|
||||||
@ -179,7 +186,7 @@ function _removeNamedNode(el,list,attr){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
throw DOMException(NOT_FOUND_ERR,new Error())
|
throw DOMException(NOT_FOUND_ERR,new Error(el.tagName+'@'+attr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NamedNodeMap.prototype = {
|
NamedNodeMap.prototype = {
|
||||||
@ -189,9 +196,11 @@ NamedNodeMap.prototype = {
|
|||||||
// if(key.indexOf(':')>0 || key == 'xmlns'){
|
// if(key.indexOf(':')>0 || key == 'xmlns'){
|
||||||
// return null;
|
// return null;
|
||||||
// }
|
// }
|
||||||
|
//console.log()
|
||||||
var i = this.length;
|
var i = this.length;
|
||||||
while(i--){
|
while(i--){
|
||||||
var attr = this[i];
|
var attr = this[i];
|
||||||
|
//console.log(attr.nodeName,key)
|
||||||
if(attr.nodeName == key){
|
if(attr.nodeName == key){
|
||||||
return attr;
|
return attr;
|
||||||
}
|
}
|
||||||
@ -373,7 +382,7 @@ Node.prototype = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
el = el.nodeType == 2?el.ownerDocument : el.parentNode;
|
el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
@ -388,7 +397,7 @@ Node.prototype = {
|
|||||||
return map[prefix] ;
|
return map[prefix] ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
el = el.nodeType == 2?el.ownerDocument : el.parentNode;
|
el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
@ -573,7 +582,7 @@ Document.prototype = {
|
|||||||
}
|
}
|
||||||
return newChild;
|
return newChild;
|
||||||
}
|
}
|
||||||
if(this.documentElement == null && newChild.nodeType == 1){
|
if(this.documentElement == null && newChild.nodeType == ELEMENT_NODE){
|
||||||
this.documentElement = newChild;
|
this.documentElement = newChild;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -593,7 +602,7 @@ Document.prototype = {
|
|||||||
getElementById : function(id){
|
getElementById : function(id){
|
||||||
var rtv = null;
|
var rtv = null;
|
||||||
_visitNode(this.documentElement,function(node){
|
_visitNode(this.documentElement,function(node){
|
||||||
if(node.nodeType == 1){
|
if(node.nodeType == ELEMENT_NODE){
|
||||||
if(node.getAttribute('id') == id){
|
if(node.getAttribute('id') == id){
|
||||||
rtv = node;
|
rtv = node;
|
||||||
return true;
|
return true;
|
||||||
@ -742,6 +751,7 @@ Element.prototype = {
|
|||||||
return this.attributes.setNamedItemNS(newAttr);
|
return this.attributes.setNamedItemNS(newAttr);
|
||||||
},
|
},
|
||||||
removeAttributeNode : function(oldAttr){
|
removeAttributeNode : function(oldAttr){
|
||||||
|
//console.log(this == oldAttr.ownerElement)
|
||||||
return this.attributes.removeNamedItem(oldAttr.nodeName);
|
return this.attributes.removeNamedItem(oldAttr.nodeName);
|
||||||
},
|
},
|
||||||
//get real attribute name,and remove it by removeAttributeNode
|
//get real attribute name,and remove it by removeAttributeNode
|
||||||
@ -786,6 +796,7 @@ Element.prototype = {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
return ls;
|
return ls;
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -817,10 +828,7 @@ CharacterData.prototype = {
|
|||||||
|
|
||||||
},
|
},
|
||||||
appendChild:function(newChild){
|
appendChild:function(newChild){
|
||||||
//if(!(newChild instanceof CharacterData)){
|
throw new Error(ExceptionMessage[HIERARCHY_REQUEST_ERR])
|
||||||
throw new Error(ExceptionMessage[3])
|
|
||||||
//}
|
|
||||||
return Node.prototype.appendChild.apply(this,arguments)
|
|
||||||
},
|
},
|
||||||
deleteData: function(offset, count) {
|
deleteData: function(offset, count) {
|
||||||
this.replaceData(offset,count,"");
|
this.replaceData(offset,count,"");
|
||||||
@ -902,39 +910,132 @@ function ProcessingInstruction() {
|
|||||||
ProcessingInstruction.prototype.nodeType = PROCESSING_INSTRUCTION_NODE;
|
ProcessingInstruction.prototype.nodeType = PROCESSING_INSTRUCTION_NODE;
|
||||||
_extends(ProcessingInstruction,Node);
|
_extends(ProcessingInstruction,Node);
|
||||||
function XMLSerializer(){}
|
function XMLSerializer(){}
|
||||||
XMLSerializer.prototype.serializeToString = function(node,attributeSorter){
|
XMLSerializer.prototype.serializeToString = function(node,isHtml,nodeFilter){
|
||||||
return node.toString(attributeSorter);
|
return nodeSerializeToString.call(node,isHtml,nodeFilter);
|
||||||
}
|
}
|
||||||
Node.prototype.toString =function(attributeSorter){
|
Node.prototype.toString = nodeSerializeToString;
|
||||||
|
function nodeSerializeToString(isHtml,nodeFilter){
|
||||||
var buf = [];
|
var buf = [];
|
||||||
serializeToString(this,buf,attributeSorter);
|
var refNode = this.nodeType == 9 && this.documentElement || this;
|
||||||
|
var prefix = refNode.prefix;
|
||||||
|
var uri = refNode.namespaceURI;
|
||||||
|
|
||||||
|
if(uri && prefix == null){
|
||||||
|
//console.log(prefix)
|
||||||
|
var prefix = refNode.lookupPrefix(uri);
|
||||||
|
if(prefix == null){
|
||||||
|
//isHTML = true;
|
||||||
|
var visibleNamespaces=[
|
||||||
|
{namespace:uri,prefix:null}
|
||||||
|
//{namespace:uri,prefix:''}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
serializeToString(this,buf,isHtml,nodeFilter,visibleNamespaces);
|
||||||
|
//console.log('###',this.nodeType,uri,prefix,buf.join(''))
|
||||||
return buf.join('');
|
return buf.join('');
|
||||||
}
|
}
|
||||||
function serializeToString(node,buf,attributeSorter,isHTML){
|
function needNamespaceDefine(node,isHTML, visibleNamespaces) {
|
||||||
|
var prefix = node.prefix||'';
|
||||||
|
var uri = node.namespaceURI;
|
||||||
|
if (!prefix && !uri){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (prefix === "xml" && uri === "http://www.w3.org/XML/1998/namespace"
|
||||||
|
|| uri == 'http://www.w3.org/2000/xmlns/'){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var i = visibleNamespaces.length
|
||||||
|
//console.log('@@@@',node.tagName,prefix,uri,visibleNamespaces)
|
||||||
|
while (i--) {
|
||||||
|
var ns = visibleNamespaces[i];
|
||||||
|
// get namespace prefix
|
||||||
|
//console.log(node.nodeType,node.tagName,ns.prefix,prefix)
|
||||||
|
if (ns.prefix == prefix){
|
||||||
|
return ns.namespace != uri;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//console.log(isHTML,uri,prefix=='')
|
||||||
|
//if(isHTML && prefix ==null && uri == 'http://www.w3.org/1999/xhtml'){
|
||||||
|
// return false;
|
||||||
|
//}
|
||||||
|
//node.flag = '11111'
|
||||||
|
//console.error(3,true,node.flag,node.prefix,node.namespaceURI)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
|
||||||
|
if(nodeFilter){
|
||||||
|
node = nodeFilter(node);
|
||||||
|
if(node){
|
||||||
|
if(typeof node == 'string'){
|
||||||
|
buf.push(node);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//buf.sort.apply(attrs, attributeSorter);
|
||||||
|
}
|
||||||
switch(node.nodeType){
|
switch(node.nodeType){
|
||||||
case ELEMENT_NODE:
|
case ELEMENT_NODE:
|
||||||
|
if (!visibleNamespaces) visibleNamespaces = [];
|
||||||
|
var startVisibleNamespaces = visibleNamespaces.length;
|
||||||
var attrs = node.attributes;
|
var attrs = node.attributes;
|
||||||
var len = attrs.length;
|
var len = attrs.length;
|
||||||
var child = node.firstChild;
|
var child = node.firstChild;
|
||||||
var nodeName = node.tagName;
|
var nodeName = node.tagName;
|
||||||
|
|
||||||
isHTML = (htmlns === node.namespaceURI) ||isHTML
|
isHTML = (htmlns === node.namespaceURI) ||isHTML
|
||||||
buf.push('<',nodeName);
|
buf.push('<',nodeName);
|
||||||
if(attributeSorter){
|
|
||||||
buf.sort.apply(attrs, attributeSorter);
|
|
||||||
|
|
||||||
|
for(var i=0;i<len;i++){
|
||||||
|
// add namespaces for attributes
|
||||||
|
var attr = attrs.item(i);
|
||||||
|
if (attr.prefix == 'xmlns') {
|
||||||
|
visibleNamespaces.push({ prefix: attr.localName, namespace: attr.value });
|
||||||
|
}else if(attr.nodeName == 'xmlns'){
|
||||||
|
visibleNamespaces.push({ prefix: '', namespace: attr.value });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for(var i=0;i<len;i++){
|
for(var i=0;i<len;i++){
|
||||||
serializeToString(attrs.item(i),buf,attributeSorter,isHTML);
|
var attr = attrs.item(i);
|
||||||
|
if (needNamespaceDefine(attr,isHTML, visibleNamespaces)) {
|
||||||
|
var prefix = attr.prefix||'';
|
||||||
|
var uri = attr.namespaceURI;
|
||||||
|
var ns = prefix ? ' xmlns:' + prefix : " xmlns";
|
||||||
|
buf.push(ns, '="' , uri , '"');
|
||||||
|
visibleNamespaces.push({ prefix: prefix, namespace:uri });
|
||||||
|
}
|
||||||
|
serializeToString(attr,buf,isHTML,nodeFilter,visibleNamespaces);
|
||||||
}
|
}
|
||||||
if(child || isHTML && !/^(?:meta|link|img|br|hr|input|button)$/i.test(nodeName)){
|
// add namespace for current node
|
||||||
|
if (needNamespaceDefine(node,isHTML, visibleNamespaces)) {
|
||||||
|
var prefix = node.prefix||'';
|
||||||
|
var uri = node.namespaceURI;
|
||||||
|
var ns = prefix ? ' xmlns:' + prefix : " xmlns";
|
||||||
|
buf.push(ns, '="' , uri , '"');
|
||||||
|
visibleNamespaces.push({ prefix: prefix, namespace:uri });
|
||||||
|
}
|
||||||
|
|
||||||
|
if(child || isHTML && !/^(?:meta|link|img|br|hr|input)$/i.test(nodeName)){
|
||||||
buf.push('>');
|
buf.push('>');
|
||||||
//if is cdata child node
|
//if is cdata child node
|
||||||
if(isHTML && /^script$/i.test(nodeName)){
|
if(isHTML && /^script$/i.test(nodeName)){
|
||||||
if(child){
|
|
||||||
buf.push(child.data);
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
while(child){
|
while(child){
|
||||||
serializeToString(child,buf,attributeSorter,isHTML);
|
if(child.data){
|
||||||
|
buf.push(child.data);
|
||||||
|
}else{
|
||||||
|
serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
|
||||||
|
}
|
||||||
|
child = child.nextSibling;
|
||||||
|
}
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
while(child){
|
||||||
|
serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
|
||||||
child = child.nextSibling;
|
child = child.nextSibling;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -942,12 +1043,14 @@ function serializeToString(node,buf,attributeSorter,isHTML){
|
|||||||
}else{
|
}else{
|
||||||
buf.push('/>');
|
buf.push('/>');
|
||||||
}
|
}
|
||||||
|
// remove added visible namespaces
|
||||||
|
//visibleNamespaces.length = startVisibleNamespaces;
|
||||||
return;
|
return;
|
||||||
case DOCUMENT_NODE:
|
case DOCUMENT_NODE:
|
||||||
case DOCUMENT_FRAGMENT_NODE:
|
case DOCUMENT_FRAGMENT_NODE:
|
||||||
var child = node.firstChild;
|
var child = node.firstChild;
|
||||||
while(child){
|
while(child){
|
||||||
serializeToString(child,buf,attributeSorter,isHTML);
|
serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
|
||||||
child = child.nextSibling;
|
child = child.nextSibling;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -1092,8 +1195,8 @@ try{
|
|||||||
},
|
},
|
||||||
set:function(data){
|
set:function(data){
|
||||||
switch(this.nodeType){
|
switch(this.nodeType){
|
||||||
case 1:
|
case ELEMENT_NODE:
|
||||||
case 11:
|
case DOCUMENT_FRAGMENT_NODE:
|
||||||
while(this.firstChild){
|
while(this.firstChild){
|
||||||
this.removeChild(this.firstChild);
|
this.removeChild(this.firstChild);
|
||||||
}
|
}
|
||||||
@ -1104,7 +1207,7 @@ try{
|
|||||||
default:
|
default:
|
||||||
//TODO:
|
//TODO:
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.value = value;
|
this.value = data;
|
||||||
this.nodeValue = data;
|
this.nodeValue = data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1112,8 +1215,8 @@ try{
|
|||||||
|
|
||||||
function getTextContent(node){
|
function getTextContent(node){
|
||||||
switch(node.nodeType){
|
switch(node.nodeType){
|
||||||
case 1:
|
case ELEMENT_NODE:
|
||||||
case 11:
|
case DOCUMENT_FRAGMENT_NODE:
|
||||||
var buf = [];
|
var buf = [];
|
||||||
node = node.firstChild;
|
node = node.firstChild;
|
||||||
while(node){
|
while(node){
|
||||||
@ -1135,7 +1238,7 @@ try{
|
|||||||
}catch(e){//ie8
|
}catch(e){//ie8
|
||||||
}
|
}
|
||||||
|
|
||||||
if(typeof require == 'function'){
|
//if(typeof require == 'function'){
|
||||||
exports.DOMImplementation = DOMImplementation;
|
exports.DOMImplementation = DOMImplementation;
|
||||||
exports.XMLSerializer = XMLSerializer;
|
exports.XMLSerializer = XMLSerializer;
|
||||||
}
|
//}
|
||||||
|
244
plugins/tiddlywiki/xmldom/files/entities.js
Executable file
244
plugins/tiddlywiki/xmldom/files/entities.js
Executable file
@ -0,0 +1,244 @@
|
|||||||
|
exports.entityMap = {
|
||||||
|
lt: '<',
|
||||||
|
gt: '>',
|
||||||
|
amp: '&',
|
||||||
|
quot: '"',
|
||||||
|
apos: "'",
|
||||||
|
Agrave: "À",
|
||||||
|
Aacute: "Á",
|
||||||
|
Acirc: "Â",
|
||||||
|
Atilde: "Ã",
|
||||||
|
Auml: "Ä",
|
||||||
|
Aring: "Å",
|
||||||
|
AElig: "Æ",
|
||||||
|
Ccedil: "Ç",
|
||||||
|
Egrave: "È",
|
||||||
|
Eacute: "É",
|
||||||
|
Ecirc: "Ê",
|
||||||
|
Euml: "Ë",
|
||||||
|
Igrave: "Ì",
|
||||||
|
Iacute: "Í",
|
||||||
|
Icirc: "Î",
|
||||||
|
Iuml: "Ï",
|
||||||
|
ETH: "Ð",
|
||||||
|
Ntilde: "Ñ",
|
||||||
|
Ograve: "Ò",
|
||||||
|
Oacute: "Ó",
|
||||||
|
Ocirc: "Ô",
|
||||||
|
Otilde: "Õ",
|
||||||
|
Ouml: "Ö",
|
||||||
|
Oslash: "Ø",
|
||||||
|
Ugrave: "Ù",
|
||||||
|
Uacute: "Ú",
|
||||||
|
Ucirc: "Û",
|
||||||
|
Uuml: "Ü",
|
||||||
|
Yacute: "Ý",
|
||||||
|
THORN: "Þ",
|
||||||
|
szlig: "ß",
|
||||||
|
agrave: "à",
|
||||||
|
aacute: "á",
|
||||||
|
acirc: "â",
|
||||||
|
atilde: "ã",
|
||||||
|
auml: "ä",
|
||||||
|
aring: "å",
|
||||||
|
aelig: "æ",
|
||||||
|
ccedil: "ç",
|
||||||
|
egrave: "è",
|
||||||
|
eacute: "é",
|
||||||
|
ecirc: "ê",
|
||||||
|
euml: "ë",
|
||||||
|
igrave: "ì",
|
||||||
|
iacute: "í",
|
||||||
|
icirc: "î",
|
||||||
|
iuml: "ï",
|
||||||
|
eth: "ð",
|
||||||
|
ntilde: "ñ",
|
||||||
|
ograve: "ò",
|
||||||
|
oacute: "ó",
|
||||||
|
ocirc: "ô",
|
||||||
|
otilde: "õ",
|
||||||
|
ouml: "ö",
|
||||||
|
oslash: "ø",
|
||||||
|
ugrave: "ù",
|
||||||
|
uacute: "ú",
|
||||||
|
ucirc: "û",
|
||||||
|
uuml: "ü",
|
||||||
|
yacute: "ý",
|
||||||
|
thorn: "þ",
|
||||||
|
yuml: "ÿ",
|
||||||
|
nbsp: " ",
|
||||||
|
iexcl: "¡",
|
||||||
|
cent: "¢",
|
||||||
|
pound: "£",
|
||||||
|
curren: "¤",
|
||||||
|
yen: "¥",
|
||||||
|
brvbar: "¦",
|
||||||
|
sect: "§",
|
||||||
|
uml: "¨",
|
||||||
|
copy: "©",
|
||||||
|
ordf: "ª",
|
||||||
|
laquo: "«",
|
||||||
|
not: "¬",
|
||||||
|
shy: "",
|
||||||
|
reg: "®",
|
||||||
|
macr: "¯",
|
||||||
|
deg: "°",
|
||||||
|
plusmn: "±",
|
||||||
|
sup2: "²",
|
||||||
|
sup3: "³",
|
||||||
|
acute: "´",
|
||||||
|
micro: "µ",
|
||||||
|
para: "¶",
|
||||||
|
middot: "·",
|
||||||
|
cedil: "¸",
|
||||||
|
sup1: "¹",
|
||||||
|
ordm: "º",
|
||||||
|
raquo: "»",
|
||||||
|
frac14: "¼",
|
||||||
|
frac12: "½",
|
||||||
|
frac34: "¾",
|
||||||
|
iquest: "¿",
|
||||||
|
times: "×",
|
||||||
|
divide: "÷",
|
||||||
|
forall: "∀",
|
||||||
|
part: "∂",
|
||||||
|
exist: "∃",
|
||||||
|
empty: "∅",
|
||||||
|
nabla: "∇",
|
||||||
|
isin: "∈",
|
||||||
|
notin: "∉",
|
||||||
|
ni: "∋",
|
||||||
|
prod: "∏",
|
||||||
|
sum: "∑",
|
||||||
|
minus: "−",
|
||||||
|
lowast: "∗",
|
||||||
|
radic: "√",
|
||||||
|
prop: "∝",
|
||||||
|
infin: "∞",
|
||||||
|
ang: "∠",
|
||||||
|
and: "∧",
|
||||||
|
or: "∨",
|
||||||
|
cap: "∩",
|
||||||
|
cup: "∪",
|
||||||
|
'int': "∫",
|
||||||
|
there4: "∴",
|
||||||
|
sim: "∼",
|
||||||
|
cong: "≅",
|
||||||
|
asymp: "≈",
|
||||||
|
ne: "≠",
|
||||||
|
equiv: "≡",
|
||||||
|
le: "≤",
|
||||||
|
ge: "≥",
|
||||||
|
sub: "⊂",
|
||||||
|
sup: "⊃",
|
||||||
|
nsub: "⊄",
|
||||||
|
sube: "⊆",
|
||||||
|
supe: "⊇",
|
||||||
|
oplus: "⊕",
|
||||||
|
otimes: "⊗",
|
||||||
|
perp: "⊥",
|
||||||
|
sdot: "⋅",
|
||||||
|
Alpha: "Α",
|
||||||
|
Beta: "Β",
|
||||||
|
Gamma: "Γ",
|
||||||
|
Delta: "Δ",
|
||||||
|
Epsilon: "Ε",
|
||||||
|
Zeta: "Ζ",
|
||||||
|
Eta: "Η",
|
||||||
|
Theta: "Θ",
|
||||||
|
Iota: "Ι",
|
||||||
|
Kappa: "Κ",
|
||||||
|
Lambda: "Λ",
|
||||||
|
Mu: "Μ",
|
||||||
|
Nu: "Ν",
|
||||||
|
Xi: "Ξ",
|
||||||
|
Omicron: "Ο",
|
||||||
|
Pi: "Π",
|
||||||
|
Rho: "Ρ",
|
||||||
|
Sigma: "Σ",
|
||||||
|
Tau: "Τ",
|
||||||
|
Upsilon: "Υ",
|
||||||
|
Phi: "Φ",
|
||||||
|
Chi: "Χ",
|
||||||
|
Psi: "Ψ",
|
||||||
|
Omega: "Ω",
|
||||||
|
alpha: "α",
|
||||||
|
beta: "β",
|
||||||
|
gamma: "γ",
|
||||||
|
delta: "δ",
|
||||||
|
epsilon: "ε",
|
||||||
|
zeta: "ζ",
|
||||||
|
eta: "η",
|
||||||
|
theta: "θ",
|
||||||
|
iota: "ι",
|
||||||
|
kappa: "κ",
|
||||||
|
lambda: "λ",
|
||||||
|
mu: "μ",
|
||||||
|
nu: "ν",
|
||||||
|
xi: "ξ",
|
||||||
|
omicron: "ο",
|
||||||
|
pi: "π",
|
||||||
|
rho: "ρ",
|
||||||
|
sigmaf: "ς",
|
||||||
|
sigma: "σ",
|
||||||
|
tau: "τ",
|
||||||
|
upsilon: "υ",
|
||||||
|
phi: "φ",
|
||||||
|
chi: "χ",
|
||||||
|
psi: "ψ",
|
||||||
|
omega: "ω",
|
||||||
|
thetasym: "ϑ",
|
||||||
|
upsih: "ϒ",
|
||||||
|
piv: "ϖ",
|
||||||
|
OElig: "Œ",
|
||||||
|
oelig: "œ",
|
||||||
|
Scaron: "Š",
|
||||||
|
scaron: "š",
|
||||||
|
Yuml: "Ÿ",
|
||||||
|
fnof: "ƒ",
|
||||||
|
circ: "ˆ",
|
||||||
|
tilde: "˜",
|
||||||
|
ensp: " ",
|
||||||
|
emsp: " ",
|
||||||
|
thinsp: " ",
|
||||||
|
zwnj: "",
|
||||||
|
zwj: "",
|
||||||
|
lrm: "",
|
||||||
|
rlm: "",
|
||||||
|
ndash: "–",
|
||||||
|
mdash: "—",
|
||||||
|
lsquo: "‘",
|
||||||
|
rsquo: "’",
|
||||||
|
sbquo: "‚",
|
||||||
|
ldquo: "“",
|
||||||
|
rdquo: "”",
|
||||||
|
bdquo: "„",
|
||||||
|
dagger: "†",
|
||||||
|
Dagger: "‡",
|
||||||
|
bull: "•",
|
||||||
|
hellip: "…",
|
||||||
|
permil: "‰",
|
||||||
|
prime: "′",
|
||||||
|
Prime: "″",
|
||||||
|
lsaquo: "‹",
|
||||||
|
rsaquo: "›",
|
||||||
|
oline: "‾",
|
||||||
|
euro: "€",
|
||||||
|
trade: "™",
|
||||||
|
larr: "←",
|
||||||
|
uarr: "↑",
|
||||||
|
rarr: "→",
|
||||||
|
darr: "↓",
|
||||||
|
harr: "↔",
|
||||||
|
crarr: "↵",
|
||||||
|
lceil: "⌈",
|
||||||
|
rceil: "⌉",
|
||||||
|
lfloor: "⌊",
|
||||||
|
rfloor: "⌋",
|
||||||
|
loz: "◊",
|
||||||
|
spades: "♠",
|
||||||
|
clubs: "♣",
|
||||||
|
hearts: "♥",
|
||||||
|
diams: "♦"
|
||||||
|
};
|
||||||
|
//for(var n in exports.entityMap){console.log(exports.entityMap[n].charCodeAt())}
|
0
plugins/tiddlywiki/xmldom/files/readme.md
Normal file → Executable file
0
plugins/tiddlywiki/xmldom/files/readme.md
Normal file → Executable file
217
plugins/tiddlywiki/xmldom/files/sax.js
Normal file → Executable file
217
plugins/tiddlywiki/xmldom/files/sax.js
Normal file → Executable file
@ -2,21 +2,21 @@
|
|||||||
//[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
|
//[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
|
||||||
//[5] Name ::= NameStartChar (NameChar)*
|
//[5] Name ::= NameStartChar (NameChar)*
|
||||||
var nameStartChar = /[A-Z_a-z\xC0-\xD6\xD8-\xF6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]///\u10000-\uEFFFF
|
var nameStartChar = /[A-Z_a-z\xC0-\xD6\xD8-\xF6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]///\u10000-\uEFFFF
|
||||||
var nameChar = new RegExp("[\\-\\.0-9"+nameStartChar.source.slice(1,-1)+"\u00B7\u0300-\u036F\\u203F-\u2040]");
|
var nameChar = new RegExp("[\\-\\.0-9"+nameStartChar.source.slice(1,-1)+"\\u00B7\\u0300-\\u036F\\u203F-\\u2040]");
|
||||||
var tagNamePattern = new RegExp('^'+nameStartChar.source+nameChar.source+'*(?:\:'+nameStartChar.source+nameChar.source+'*)?$');
|
var tagNamePattern = new RegExp('^'+nameStartChar.source+nameChar.source+'*(?:\:'+nameStartChar.source+nameChar.source+'*)?$');
|
||||||
//var tagNamePattern = /^[a-zA-Z_][\w\-\.]*(?:\:[a-zA-Z_][\w\-\.]*)?$/
|
//var tagNamePattern = /^[a-zA-Z_][\w\-\.]*(?:\:[a-zA-Z_][\w\-\.]*)?$/
|
||||||
//var handlers = 'resolveEntity,getExternalSubset,characters,endDocument,endElement,endPrefixMapping,ignorableWhitespace,processingInstruction,setDocumentLocator,skippedEntity,startDocument,startElement,startPrefixMapping,notationDecl,unparsedEntityDecl,error,fatalError,warning,attributeDecl,elementDecl,externalEntityDecl,internalEntityDecl,comment,endCDATA,endDTD,endEntity,startCDATA,startDTD,startEntity'.split(',')
|
//var handlers = 'resolveEntity,getExternalSubset,characters,endDocument,endElement,endPrefixMapping,ignorableWhitespace,processingInstruction,setDocumentLocator,skippedEntity,startDocument,startElement,startPrefixMapping,notationDecl,unparsedEntityDecl,error,fatalError,warning,attributeDecl,elementDecl,externalEntityDecl,internalEntityDecl,comment,endCDATA,endDTD,endEntity,startCDATA,startDTD,startEntity'.split(',')
|
||||||
|
|
||||||
//S_TAG, S_ATTR, S_EQ, S_V
|
//S_TAG, S_ATTR, S_EQ, S_ATTR_NOQUOT_VALUE
|
||||||
//S_ATTR_S, S_E, S_S, S_C
|
//S_ATTR_SPACE, S_ATTR_END, S_TAG_SPACE, S_TAG_CLOSE
|
||||||
var S_TAG = 0;//tag name offerring
|
var S_TAG = 0;//tag name offerring
|
||||||
var S_ATTR = 1;//attr name offerring
|
var S_ATTR = 1;//attr name offerring
|
||||||
var S_ATTR_S=2;//attr name end and space offer
|
var S_ATTR_SPACE=2;//attr name end and space offer
|
||||||
var S_EQ = 3;//=space?
|
var S_EQ = 3;//=space?
|
||||||
var S_V = 4;//attr value(no quot value only)
|
var S_ATTR_NOQUOT_VALUE = 4;//attr value(no quot value only)
|
||||||
var S_E = 5;//attr value end and no space(quot end)
|
var S_ATTR_END = 5;//attr value end and no space(quot end)
|
||||||
var S_S = 6;//(attr value end || tag end ) && (space offer)
|
var S_TAG_SPACE = 6;//(attr value end || tag end ) && (space offer)
|
||||||
var S_C = 7;//closed el<el />
|
var S_TAG_CLOSE = 7;//closed el<el />
|
||||||
|
|
||||||
function XMLReader(){
|
function XMLReader(){
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ XMLReader.prototype = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
|
function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
|
||||||
function fixedFromCharCode(code) {
|
function fixedFromCharCode(code) {
|
||||||
// String.prototype.fromCharCode does not supports
|
// String.prototype.fromCharCode does not supports
|
||||||
// > 2 bytes unicode chars directly
|
// > 2 bytes unicode chars directly
|
||||||
if (code > 0xffff) {
|
if (code > 0xffff) {
|
||||||
@ -76,7 +76,7 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
|
|||||||
}
|
}
|
||||||
var lineStart = 0;
|
var lineStart = 0;
|
||||||
var lineEnd = 0;
|
var lineEnd = 0;
|
||||||
var linePattern = /.+(?:\r\n?|\n)|.*$/g
|
var linePattern = /.*(?:\r\n?|\n)|.*$/g
|
||||||
var locator = domBuilder.locator;
|
var locator = domBuilder.locator;
|
||||||
|
|
||||||
var parseStack = [{currentNSMap:defaultNSMapCopy}]
|
var parseStack = [{currentNSMap:defaultNSMapCopy}]
|
||||||
@ -87,7 +87,7 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
|
|||||||
var tagStart = source.indexOf('<',start);
|
var tagStart = source.indexOf('<',start);
|
||||||
if(tagStart<0){
|
if(tagStart<0){
|
||||||
if(!source.substr(start).match(/^\s*$/)){
|
if(!source.substr(start).match(/^\s*$/)){
|
||||||
var doc = domBuilder.document;
|
var doc = domBuilder.doc;
|
||||||
var text = doc.createTextNode(source.substr(start));
|
var text = doc.createTextNode(source.substr(start));
|
||||||
doc.appendChild(text);
|
doc.appendChild(text);
|
||||||
domBuilder.currentElement = text;
|
domBuilder.currentElement = text;
|
||||||
@ -102,16 +102,36 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
|
|||||||
var end = source.indexOf('>',tagStart+3);
|
var end = source.indexOf('>',tagStart+3);
|
||||||
var tagName = source.substring(tagStart+2,end);
|
var tagName = source.substring(tagStart+2,end);
|
||||||
var config = parseStack.pop();
|
var config = parseStack.pop();
|
||||||
var localNSMap = config.localNSMap;
|
if(end<0){
|
||||||
if(config.tagName != tagName){
|
|
||||||
errorHandler.fatalError("end tag name: "+tagName+' is not match the current start tagName:'+config.tagName );
|
tagName = source.substring(tagStart+2).replace(/[\s<].*/,'');
|
||||||
}
|
//console.error('#@@@@@@'+tagName)
|
||||||
domBuilder.endElement(config.uri,config.localName,tagName);
|
errorHandler.error("end tag name: "+tagName+' is not complete:'+config.tagName);
|
||||||
if(localNSMap){
|
end = tagStart+1+tagName.length;
|
||||||
for(var prefix in localNSMap){
|
}else if(tagName.match(/\s</)){
|
||||||
domBuilder.endPrefixMapping(prefix) ;
|
tagName = tagName.replace(/[\s<].*/,'');
|
||||||
}
|
errorHandler.error("end tag name: "+tagName+' maybe not complete');
|
||||||
|
end = tagStart+1+tagName.length;
|
||||||
}
|
}
|
||||||
|
//console.error(parseStack.length,parseStack)
|
||||||
|
//console.error(config);
|
||||||
|
var localNSMap = config.localNSMap;
|
||||||
|
var endMatch = config.tagName == tagName;
|
||||||
|
var endIgnoreCaseMach = endMatch || config.tagName&&config.tagName.toLowerCase() == tagName.toLowerCase()
|
||||||
|
if(endIgnoreCaseMach){
|
||||||
|
domBuilder.endElement(config.uri,config.localName,tagName);
|
||||||
|
if(localNSMap){
|
||||||
|
for(var prefix in localNSMap){
|
||||||
|
domBuilder.endPrefixMapping(prefix) ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!endMatch){
|
||||||
|
errorHandler.fatalError("end tag name: "+tagName+' is not match the current start tagName:'+config.tagName );
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
parseStack.push(config)
|
||||||
|
}
|
||||||
|
|
||||||
end++;
|
end++;
|
||||||
break;
|
break;
|
||||||
// end elment
|
// end elment
|
||||||
@ -124,33 +144,40 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
|
|||||||
end = parseDCC(source,tagStart,domBuilder,errorHandler);
|
end = parseDCC(source,tagStart,domBuilder,errorHandler);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
||||||
locator&&position(tagStart);
|
locator&&position(tagStart);
|
||||||
|
|
||||||
var el = new ElementAttributes();
|
var el = new ElementAttributes();
|
||||||
|
var currentNSMap = parseStack[parseStack.length-1].currentNSMap;
|
||||||
//elStartEnd
|
//elStartEnd
|
||||||
var end = parseElementStartPart(source,tagStart,el,entityReplacer,errorHandler);
|
var end = parseElementStartPart(source,tagStart,el,currentNSMap,entityReplacer,errorHandler);
|
||||||
var len = el.length;
|
var len = el.length;
|
||||||
|
|
||||||
if(locator){
|
|
||||||
if(len){
|
|
||||||
//attribute position fixed
|
|
||||||
for(var i = 0;i<len;i++){
|
|
||||||
var a = el[i];
|
|
||||||
position(a.offset);
|
|
||||||
a.offset = copyLocator(locator,{});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
position(end);
|
|
||||||
}
|
|
||||||
if(!el.closed && fixSelfClosed(source,end,el.tagName,closeMap)){
|
if(!el.closed && fixSelfClosed(source,end,el.tagName,closeMap)){
|
||||||
el.closed = true;
|
el.closed = true;
|
||||||
if(!entityMap.nbsp){
|
if(!entityMap.nbsp){
|
||||||
errorHandler.warning('unclosed xml attribute');
|
errorHandler.warning('unclosed xml attribute');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
appendElement(el,domBuilder,parseStack);
|
if(locator && len){
|
||||||
|
var locator2 = copyLocator(locator,{});
|
||||||
|
//try{//attribute position fixed
|
||||||
|
for(var i = 0;i<len;i++){
|
||||||
|
var a = el[i];
|
||||||
|
position(a.offset);
|
||||||
|
a.locator = copyLocator(locator,{});
|
||||||
|
}
|
||||||
|
//}catch(e){console.error('@@@@@'+e)}
|
||||||
|
domBuilder.locator = locator2
|
||||||
|
if(appendElement(el,domBuilder,currentNSMap)){
|
||||||
|
parseStack.push(el)
|
||||||
|
}
|
||||||
|
domBuilder.locator = locator;
|
||||||
|
}else{
|
||||||
|
if(appendElement(el,domBuilder,currentNSMap)){
|
||||||
|
parseStack.push(el)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(el.uri === 'http://www.w3.org/1999/xhtml' && !el.closed){
|
if(el.uri === 'http://www.w3.org/1999/xhtml' && !el.closed){
|
||||||
@ -160,8 +187,10 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch(e){
|
}catch(e){
|
||||||
errorHandler.error('element parse error: '+e);
|
errorHandler.error('element parse error: '+e)
|
||||||
|
//errorHandler.error('element parse error: '+e);
|
||||||
end = -1;
|
end = -1;
|
||||||
|
//throw e;
|
||||||
}
|
}
|
||||||
if(end>start){
|
if(end>start){
|
||||||
start = end;
|
start = end;
|
||||||
@ -181,7 +210,7 @@ function copyLocator(f,t){
|
|||||||
* @see #appendElement(source,elStartEnd,el,selfClosed,entityReplacer,domBuilder,parseStack);
|
* @see #appendElement(source,elStartEnd,el,selfClosed,entityReplacer,domBuilder,parseStack);
|
||||||
* @return end of the elementStartPart(end of elementEndPart for selfClosed el)
|
* @return end of the elementStartPart(end of elementEndPart for selfClosed el)
|
||||||
*/
|
*/
|
||||||
function parseElementStartPart(source,start,el,entityReplacer,errorHandler){
|
function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,errorHandler){
|
||||||
var attrName;
|
var attrName;
|
||||||
var value;
|
var value;
|
||||||
var p = ++start;
|
var p = ++start;
|
||||||
@ -193,7 +222,7 @@ function parseElementStartPart(source,start,el,entityReplacer,errorHandler){
|
|||||||
if(s === S_ATTR){//attrName
|
if(s === S_ATTR){//attrName
|
||||||
attrName = source.slice(start,p);
|
attrName = source.slice(start,p);
|
||||||
s = S_EQ;
|
s = S_EQ;
|
||||||
}else if(s === S_ATTR_S){
|
}else if(s === S_ATTR_SPACE){
|
||||||
s = S_EQ;
|
s = S_EQ;
|
||||||
}else{
|
}else{
|
||||||
//fatalError: equal must after attrName or space after attrName
|
//fatalError: equal must after attrName or space after attrName
|
||||||
@ -202,25 +231,30 @@ function parseElementStartPart(source,start,el,entityReplacer,errorHandler){
|
|||||||
break;
|
break;
|
||||||
case '\'':
|
case '\'':
|
||||||
case '"':
|
case '"':
|
||||||
if(s === S_EQ){//equal
|
if(s === S_EQ || s === S_ATTR //|| s == S_ATTR_SPACE
|
||||||
|
){//equal
|
||||||
|
if(s === S_ATTR){
|
||||||
|
errorHandler.warning('attribute value must after "="')
|
||||||
|
attrName = source.slice(start,p)
|
||||||
|
}
|
||||||
start = p+1;
|
start = p+1;
|
||||||
p = source.indexOf(c,start)
|
p = source.indexOf(c,start)
|
||||||
if(p>0){
|
if(p>0){
|
||||||
value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
|
value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
|
||||||
el.add(attrName,value,start-1);
|
el.add(attrName,value,start-1);
|
||||||
s = S_E;
|
s = S_ATTR_END;
|
||||||
}else{
|
}else{
|
||||||
//fatalError: no end quot match
|
//fatalError: no end quot match
|
||||||
throw new Error('attribute value no end \''+c+'\' match');
|
throw new Error('attribute value no end \''+c+'\' match');
|
||||||
}
|
}
|
||||||
}else if(s == S_V){
|
}else if(s == S_ATTR_NOQUOT_VALUE){
|
||||||
value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
|
value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
|
||||||
//console.log(attrName,value,start,p)
|
//console.log(attrName,value,start,p)
|
||||||
el.add(attrName,value,start);
|
el.add(attrName,value,start);
|
||||||
//console.dir(el)
|
//console.dir(el)
|
||||||
errorHandler.warning('attribute "'+attrName+'" missed start quot('+c+')!!');
|
errorHandler.warning('attribute "'+attrName+'" missed start quot('+c+')!!');
|
||||||
start = p+1;
|
start = p+1;
|
||||||
s = S_E
|
s = S_ATTR_END
|
||||||
}else{
|
}else{
|
||||||
//fatalError: no equal before
|
//fatalError: no equal before
|
||||||
throw new Error('attribute value must after "="');
|
throw new Error('attribute value must after "="');
|
||||||
@ -230,14 +264,14 @@ function parseElementStartPart(source,start,el,entityReplacer,errorHandler){
|
|||||||
switch(s){
|
switch(s){
|
||||||
case S_TAG:
|
case S_TAG:
|
||||||
el.setTagName(source.slice(start,p));
|
el.setTagName(source.slice(start,p));
|
||||||
case S_E:
|
case S_ATTR_END:
|
||||||
case S_S:
|
case S_TAG_SPACE:
|
||||||
case S_C:
|
case S_TAG_CLOSE:
|
||||||
s = S_C;
|
s =S_TAG_CLOSE;
|
||||||
el.closed = true;
|
el.closed = true;
|
||||||
case S_V:
|
case S_ATTR_NOQUOT_VALUE:
|
||||||
case S_ATTR:
|
case S_ATTR:
|
||||||
case S_ATTR_S:
|
case S_ATTR_SPACE:
|
||||||
break;
|
break;
|
||||||
//case S_EQ:
|
//case S_EQ:
|
||||||
default:
|
default:
|
||||||
@ -247,30 +281,36 @@ function parseElementStartPart(source,start,el,entityReplacer,errorHandler){
|
|||||||
case ''://end document
|
case ''://end document
|
||||||
//throw new Error('unexpected end of input')
|
//throw new Error('unexpected end of input')
|
||||||
errorHandler.error('unexpected end of input');
|
errorHandler.error('unexpected end of input');
|
||||||
|
if(s == S_TAG){
|
||||||
|
el.setTagName(source.slice(start,p));
|
||||||
|
}
|
||||||
|
return p;
|
||||||
case '>':
|
case '>':
|
||||||
switch(s){
|
switch(s){
|
||||||
case S_TAG:
|
case S_TAG:
|
||||||
el.setTagName(source.slice(start,p));
|
el.setTagName(source.slice(start,p));
|
||||||
case S_E:
|
case S_ATTR_END:
|
||||||
case S_S:
|
case S_TAG_SPACE:
|
||||||
case S_C:
|
case S_TAG_CLOSE:
|
||||||
break;//normal
|
break;//normal
|
||||||
case S_V://Compatible state
|
case S_ATTR_NOQUOT_VALUE://Compatible state
|
||||||
case S_ATTR:
|
case S_ATTR:
|
||||||
value = source.slice(start,p);
|
value = source.slice(start,p);
|
||||||
if(value.slice(-1) === '/'){
|
if(value.slice(-1) === '/'){
|
||||||
el.closed = true;
|
el.closed = true;
|
||||||
value = value.slice(0,-1)
|
value = value.slice(0,-1)
|
||||||
}
|
}
|
||||||
case S_ATTR_S:
|
case S_ATTR_SPACE:
|
||||||
if(s === S_ATTR_S){
|
if(s === S_ATTR_SPACE){
|
||||||
value = attrName;
|
value = attrName;
|
||||||
}
|
}
|
||||||
if(s == S_V){
|
if(s == S_ATTR_NOQUOT_VALUE){
|
||||||
errorHandler.warning('attribute "'+value+'" missed quot(")!!');
|
errorHandler.warning('attribute "'+value+'" missed quot(")!!');
|
||||||
el.add(attrName,value.replace(/&#?\w+;/g,entityReplacer),start)
|
el.add(attrName,value.replace(/&#?\w+;/g,entityReplacer),start)
|
||||||
}else{
|
}else{
|
||||||
errorHandler.warning('attribute "'+value+'" missed value!! "'+value+'" instead!!')
|
if(currentNSMap[''] !== 'http://www.w3.org/1999/xhtml' || !value.match(/^(?:disabled|checked|selected)$/i)){
|
||||||
|
errorHandler.warning('attribute "'+value+'" missed value!! "'+value+'" instead!!')
|
||||||
|
}
|
||||||
el.add(value,value,start)
|
el.add(value,value,start)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -287,64 +327,68 @@ function parseElementStartPart(source,start,el,entityReplacer,errorHandler){
|
|||||||
switch(s){
|
switch(s){
|
||||||
case S_TAG:
|
case S_TAG:
|
||||||
el.setTagName(source.slice(start,p));//tagName
|
el.setTagName(source.slice(start,p));//tagName
|
||||||
s = S_S;
|
s = S_TAG_SPACE;
|
||||||
break;
|
break;
|
||||||
case S_ATTR:
|
case S_ATTR:
|
||||||
attrName = source.slice(start,p)
|
attrName = source.slice(start,p)
|
||||||
s = S_ATTR_S;
|
s = S_ATTR_SPACE;
|
||||||
break;
|
break;
|
||||||
case S_V:
|
case S_ATTR_NOQUOT_VALUE:
|
||||||
var value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
|
var value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
|
||||||
errorHandler.warning('attribute "'+value+'" missed quot(")!!');
|
errorHandler.warning('attribute "'+value+'" missed quot(")!!');
|
||||||
el.add(attrName,value,start)
|
el.add(attrName,value,start)
|
||||||
case S_E:
|
case S_ATTR_END:
|
||||||
s = S_S;
|
s = S_TAG_SPACE;
|
||||||
break;
|
break;
|
||||||
//case S_S:
|
//case S_TAG_SPACE:
|
||||||
//case S_EQ:
|
//case S_EQ:
|
||||||
//case S_ATTR_S:
|
//case S_ATTR_SPACE:
|
||||||
// void();break;
|
// void();break;
|
||||||
//case S_C:
|
//case S_TAG_CLOSE:
|
||||||
//ignore warning
|
//ignore warning
|
||||||
}
|
}
|
||||||
}else{//not space
|
}else{//not space
|
||||||
//S_TAG, S_ATTR, S_EQ, S_V
|
//S_TAG, S_ATTR, S_EQ, S_ATTR_NOQUOT_VALUE
|
||||||
//S_ATTR_S, S_E, S_S, S_C
|
//S_ATTR_SPACE, S_ATTR_END, S_TAG_SPACE, S_TAG_CLOSE
|
||||||
switch(s){
|
switch(s){
|
||||||
//case S_TAG:void();break;
|
//case S_TAG:void();break;
|
||||||
//case S_ATTR:void();break;
|
//case S_ATTR:void();break;
|
||||||
//case S_V:void();break;
|
//case S_ATTR_NOQUOT_VALUE:void();break;
|
||||||
case S_ATTR_S:
|
case S_ATTR_SPACE:
|
||||||
errorHandler.warning('attribute "'+attrName+'" missed value!! "'+attrName+'" instead!!')
|
var tagName = el.tagName;
|
||||||
|
if(currentNSMap[''] !== 'http://www.w3.org/1999/xhtml' || !attrName.match(/^(?:disabled|checked|selected)$/i)){
|
||||||
|
errorHandler.warning('attribute "'+attrName+'" missed value!! "'+attrName+'" instead2!!')
|
||||||
|
}
|
||||||
el.add(attrName,attrName,start);
|
el.add(attrName,attrName,start);
|
||||||
start = p;
|
start = p;
|
||||||
s = S_ATTR;
|
s = S_ATTR;
|
||||||
break;
|
break;
|
||||||
case S_E:
|
case S_ATTR_END:
|
||||||
errorHandler.warning('attribute space is required"'+attrName+'"!!')
|
errorHandler.warning('attribute space is required"'+attrName+'"!!')
|
||||||
case S_S:
|
case S_TAG_SPACE:
|
||||||
s = S_ATTR;
|
s = S_ATTR;
|
||||||
start = p;
|
start = p;
|
||||||
break;
|
break;
|
||||||
case S_EQ:
|
case S_EQ:
|
||||||
s = S_V;
|
s = S_ATTR_NOQUOT_VALUE;
|
||||||
start = p;
|
start = p;
|
||||||
break;
|
break;
|
||||||
case S_C:
|
case S_TAG_CLOSE:
|
||||||
throw new Error("elements closed character '/' and '>' must be connected to");
|
throw new Error("elements closed character '/' and '>' must be connected to");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}//end outer switch
|
||||||
|
//console.log('p++',p)
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @return end of the elementStartPart(end of elementEndPart for selfClosed el)
|
* @return true if has new namespace define
|
||||||
*/
|
*/
|
||||||
function appendElement(el,domBuilder,parseStack){
|
function appendElement(el,domBuilder,currentNSMap){
|
||||||
var tagName = el.tagName;
|
var tagName = el.tagName;
|
||||||
var localNSMap = null;
|
var localNSMap = null;
|
||||||
var currentNSMap = parseStack[parseStack.length-1].currentNSMap;
|
//var currentNSMap = parseStack[parseStack.length-1].currentNSMap;
|
||||||
var i = el.length;
|
var i = el.length;
|
||||||
while(i--){
|
while(i--){
|
||||||
var a = el[i];
|
var a = el[i];
|
||||||
@ -383,7 +427,7 @@ function appendElement(el,domBuilder,parseStack){
|
|||||||
if(prefix === 'xml'){
|
if(prefix === 'xml'){
|
||||||
a.uri = 'http://www.w3.org/XML/1998/namespace';
|
a.uri = 'http://www.w3.org/XML/1998/namespace';
|
||||||
}if(prefix !== 'xmlns'){
|
}if(prefix !== 'xmlns'){
|
||||||
a.uri = currentNSMap[prefix]
|
a.uri = currentNSMap[prefix || '']
|
||||||
|
|
||||||
//{console.log('###'+a.qName,domBuilder.locator.systemId+'',currentNSMap,a.uri)}
|
//{console.log('###'+a.qName,domBuilder.locator.systemId+'',currentNSMap,a.uri)}
|
||||||
}
|
}
|
||||||
@ -412,7 +456,8 @@ function appendElement(el,domBuilder,parseStack){
|
|||||||
}else{
|
}else{
|
||||||
el.currentNSMap = currentNSMap;
|
el.currentNSMap = currentNSMap;
|
||||||
el.localNSMap = localNSMap;
|
el.localNSMap = localNSMap;
|
||||||
parseStack.push(el);
|
//parseStack.push(el);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function parseHtmlSpecialContent(source,elStartEnd,tagName,entityReplacer,domBuilder){
|
function parseHtmlSpecialContent(source,elStartEnd,tagName,entityReplacer,domBuilder){
|
||||||
@ -442,7 +487,11 @@ function fixSelfClosed(source,elStartEnd,tagName,closeMap){
|
|||||||
var pos = closeMap[tagName];
|
var pos = closeMap[tagName];
|
||||||
if(pos == null){
|
if(pos == null){
|
||||||
//console.log(tagName)
|
//console.log(tagName)
|
||||||
pos = closeMap[tagName] = source.lastIndexOf('</'+tagName+'>')
|
pos = source.lastIndexOf('</'+tagName+'>')
|
||||||
|
if(pos<elStartEnd){//忘记闭合
|
||||||
|
pos = source.lastIndexOf('</'+tagName)
|
||||||
|
}
|
||||||
|
closeMap[tagName] =pos
|
||||||
}
|
}
|
||||||
return pos<elStartEnd;
|
return pos<elStartEnd;
|
||||||
//}
|
//}
|
||||||
@ -533,7 +582,7 @@ ElementAttributes.prototype = {
|
|||||||
},
|
},
|
||||||
length:0,
|
length:0,
|
||||||
getLocalName:function(i){return this[i].localName},
|
getLocalName:function(i){return this[i].localName},
|
||||||
getOffset:function(i){return this[i].offset},
|
getLocator:function(i){return this[i].locator},
|
||||||
getQName:function(i){return this[i].qName},
|
getQName:function(i){return this[i].qName},
|
||||||
getURI:function(i){return this[i].uri},
|
getURI:function(i){return this[i].uri},
|
||||||
getValue:function(i){return this[i].value}
|
getValue:function(i){return this[i].value}
|
||||||
@ -580,7 +629,5 @@ function split(source,start){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(typeof require == 'function'){
|
exports.XMLReader = XMLReader;
|
||||||
exports.XMLReader = XMLReader;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -18,6 +18,15 @@
|
|||||||
},
|
},
|
||||||
"prefix": "",
|
"prefix": "",
|
||||||
"suffix": ""
|
"suffix": ""
|
||||||
|
},{
|
||||||
|
"file": "entities.js",
|
||||||
|
"fields": {
|
||||||
|
"type": "application/javascript",
|
||||||
|
"title": "$:/plugins/tiddlywiki/xmldom/entities",
|
||||||
|
"module-type": "library"
|
||||||
|
},
|
||||||
|
"prefix": "",
|
||||||
|
"suffix": ""
|
||||||
},{
|
},{
|
||||||
"file": "sax.js",
|
"file": "sax.js",
|
||||||
"fields": {
|
"fields": {
|
||||||
|
Loading…
Reference in New Issue
Block a user