Skip to content

Commit

Permalink
bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
jindw committed Aug 21, 2016
1 parent b7a4daa commit bafdf2c
Show file tree
Hide file tree
Showing 16 changed files with 19,976 additions and 90 deletions.
36 changes: 20 additions & 16 deletions dom-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ DOMParser.prototype.parseFromString = function(source,mimeType){
if(source){
sax.parse(source,defaultNSMap,entityMap);
}else{
sax.errorHandler.error("invalid document source");
sax.errorHandler.error("invalid doc source");
}
return domBuilder.document;
return domBuilder.doc;
}
function buildErrorHandler(errorImpl,domBuilder,locator){
if(!errorImpl){
Expand Down Expand Up @@ -77,13 +77,13 @@ function position(locator,node){
*/
DOMHandler.prototype = {
startDocument : function() {
this.document = new DOMImplementation().createDocument(null, null, null);
this.doc = new DOMImplementation().createDocument(null, null, null);
if (this.locator) {
this.document.documentURI = this.locator.systemId;
this.doc.documentURI = this.locator.systemId;
}
},
startElement:function(namespaceURI, localName, qName, attrs) {
var doc = this.document;
var doc = this.doc;
var el = doc.createElementNS(namespaceURI, qName||localName);
var len = attrs.length;
appendElement(this, el);
Expand All @@ -104,15 +104,15 @@ DOMHandler.prototype = {
},
endElement:function(namespaceURI, localName, qName) {
var current = this.currentElement
var tagName = current.tagName;
this.currentElement = current.parentNode;
var tagName = current.tagName;
this.currentElement = current.parentNode;
},
startPrefixMapping:function(prefix, uri) {
},
endPrefixMapping:function(prefix) {
},
processingInstruction:function(target, data) {
var ins = this.document.createProcessingInstruction(target, data);
var ins = this.doc.createProcessingInstruction(target, data);
this.locator && position(this.locator,ins)
appendElement(this, ins);
},
Expand All @@ -121,21 +121,25 @@ DOMHandler.prototype = {
characters:function(chars, start, length) {
chars = _toString.apply(this,arguments)
//console.log(chars)
if(this.currentElement && chars){
if(chars){
if (this.cdata) {
var charNode = this.document.createCDATASection(chars);
this.currentElement.appendChild(charNode);
var charNode = this.doc.createCDATASection(chars);
} else {
var charNode = this.document.createTextNode(chars);
var charNode = this.doc.createTextNode(chars);
}
if(this.currentElement){
this.currentElement.appendChild(charNode);
}else if(/^\s*$/.test(chars)){
this.doc.appendChild(charNode);
//process xml
}
this.locator && position(this.locator,charNode)
}
},
skippedEntity:function(name) {
},
endDocument:function() {
this.document.normalize();
this.doc.normalize();
},
setDocumentLocator:function (locator) {
if(this.locator = locator){// && !('lineNumber' in locator)){
Expand All @@ -145,7 +149,7 @@ DOMHandler.prototype = {
//LexicalHandler
comment:function(chars, start, length) {
chars = _toString.apply(this,arguments)
var comm = this.document.createComment(chars);
var comm = this.doc.createComment(chars);
this.locator && position(this.locator,comm)
appendElement(this, comm);
},
Expand All @@ -159,7 +163,7 @@ DOMHandler.prototype = {
},

startDTD:function(name, publicId, systemId) {
var impl = this.document.implementation;
var impl = this.doc.implementation;
if (impl && impl.createDocumentType) {
var dt = impl.createDocumentType(name, publicId, systemId);
this.locator && position(this.locator,dt)
Expand Down Expand Up @@ -235,7 +239,7 @@ 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 */
function appendElement (hander,node) {
if (!hander.currentElement) {
hander.document.appendChild(node);
hander.doc.appendChild(node);
} else {
hander.currentElement.appendChild(node);
}
Expand Down
82 changes: 60 additions & 22 deletions dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ NodeList.prototype = {
item: function(index) {
return this[index] || null;
},
toString:function(){
toString:function(isHTML,nodeFilter){
for(var buf = [], i = 0;i<this.length;i++){
serializeToString(this[i],buf);
serializeToString(this[i],buf,isHTML,nodeFilter);
}
return buf.join('');
}
Expand Down Expand Up @@ -170,6 +170,7 @@ function _addNamedNode(el,list,newAttr,oldAttr){
}
}
function _removeNamedNode(el,list,attr){
//console.log('remove attr:'+attr)
var i = _findNodeIndex(list,attr);
if(i>=0){
var lastIndex = list.length-1
Expand All @@ -185,7 +186,7 @@ function _removeNamedNode(el,list,attr){
}
}
}else{
throw DOMException(NOT_FOUND_ERR,new Error())
throw DOMException(NOT_FOUND_ERR,new Error(el.tagName+'@'+attr))
}
}
NamedNodeMap.prototype = {
Expand All @@ -195,9 +196,11 @@ NamedNodeMap.prototype = {
// if(key.indexOf(':')>0 || key == 'xmlns'){
// return null;
// }
//console.log()
var i = this.length;
while(i--){
var attr = this[i];
//console.log(attr.nodeName,key)
if(attr.nodeName == key){
return attr;
}
Expand Down Expand Up @@ -748,6 +751,7 @@ Element.prototype = {
return this.attributes.setNamedItemNS(newAttr);
},
removeAttributeNode : function(oldAttr){
//console.log(this == oldAttr.ownerElement)
return this.attributes.removeNamedItem(oldAttr.nodeName);
},
//get real attribute name,and remove it by removeAttributeNode
Expand Down Expand Up @@ -792,6 +796,7 @@ Element.prototype = {
}
});
return ls;

});
}
};
Expand Down Expand Up @@ -908,15 +913,32 @@ function ProcessingInstruction() {
ProcessingInstruction.prototype.nodeType = PROCESSING_INSTRUCTION_NODE;
_extends(ProcessingInstruction,Node);
function XMLSerializer(){}
XMLSerializer.prototype.serializeToString = function(node,attributeSorter){
return node.toString(attributeSorter);
XMLSerializer.prototype.serializeToString = function(node,isHtml,nodeFilter){
return nodeSerializeToString.call(node,isHtml,nodeFilter);
}
Node.prototype.toString =function(attributeSorter){
Node.prototype.toString = nodeSerializeToString;
function nodeSerializeToString(isHtml,nodeFilter){
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('');
}
function needNamespaceDefine(node, visibleNamespaces) {
function needNamespaceDefine(node,isHTML, visibleNamespaces) {
var prefix = node.prefix,uri = node.namespaceURI;
if (!prefix && !uri){
return false;
Expand All @@ -925,20 +947,38 @@ function needNamespaceDefine(node, visibleNamespaces) {
|| uri == 'http://www.w3.org/2000/xmlns/'){
return false;
}

var i = visibleNamespaces.length
//console.log(visibleNamespaces)
//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,attributeSorter,isHTML,visibleNamespaces){
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){
case ELEMENT_NODE:
if (!visibleNamespaces) visibleNamespaces = [];
Expand All @@ -952,9 +992,6 @@ function serializeToString(node,buf,attributeSorter,isHTML,visibleNamespaces){
buf.push('<',nodeName);


if(attributeSorter){
buf.sort.apply(attrs, attributeSorter);
}

for(var i=0;i<len;i++){
// add namespaces for attributes
Expand All @@ -967,28 +1004,29 @@ function serializeToString(node,buf,attributeSorter,isHTML,visibleNamespaces){
}
for(var i=0;i<len;i++){
var attr = attrs.item(i);
if (needNamespaceDefine(attr, visibleNamespaces)) {
if (needNamespaceDefine(attr,isHTML, visibleNamespaces)) {
buf.push(attr.prefix ? ' xmlns:' + attr.prefix : " xmlns", "='" , attr.namespaceURI , "'");
visibleNamespaces.push({ prefix: attr.prefix, namespace: attr.namespaceURI });
}
serializeToString(attr,buf,attributeSorter,isHTML);
serializeToString(attr,buf,isHTML,nodeFilter,visibleNamespaces);
}
// add namespace for current node
if (needNamespaceDefine(node, visibleNamespaces)) {
if (needNamespaceDefine(node,isHTML, visibleNamespaces)) {
buf.push(node.prefix ? ' xmlns:' + node.prefix : " xmlns", "='" , node.namespaceURI , "'");
visibleNamespaces.push({ prefix: node.prefix, namespace: node.namespaceURI });
}

if(child || isHTML && !/^(?:meta|link|img|br|hr|input|button)$/i.test(nodeName)){
if(child || isHTML && !/^(?:meta|link|img|br|hr|input)$/i.test(nodeName)){
buf.push('>');
//if is cdata child node
if(isHTML && /^script$/i.test(nodeName)){
/*if(isHTML && /^script$/i.test(nodeName)){
if(child){
buf.push(child.data);
}
}else{
}else*/
{
while(child){
serializeToString(child,buf,attributeSorter,isHTML,visibleNamespaces);
serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
child = child.nextSibling;
}
}
Expand All @@ -1003,7 +1041,7 @@ function serializeToString(node,buf,attributeSorter,isHTML,visibleNamespaces){
case DOCUMENT_FRAGMENT_NODE:
var child = node.firstChild;
while(child){
serializeToString(child,buf,attributeSorter,isHTML);
serializeToString(child,buf,isHTML,nodeFilter,visibleNamespaces);
child = child.nextSibling;
}
return;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xmldom",
"version": "0.1.22",
"name": "xmldom-alpha",
"version": "0.1.23",
"description": "A W3C Standard XML DOM(Level2 CORE) implementation and parser(DOMParser/XMLSerializer).",
"keywords": ["w3c","dom","xml","parser","javascript","DOMParser","XMLSerializer"],
"author": "jindw <jindw@xidea.org> (http://www.xidea.org)",
Expand Down
Loading

0 comments on commit bafdf2c

Please sign in to comment.