Skip to content

Commit

Permalink
merged some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jindw committed Jan 28, 2014
1 parent efee4e8 commit b05c68e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 48 deletions.
6 changes: 0 additions & 6 deletions dom-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ DOMHandler.prototype = {
this.document.documentURI = this.locator.systemId;
}
},
allText:function(source) {
var doc = this.document;
var el = doc.createTextNode(source);
appendElement(this, el);
this.currentElement = el;
},
startElement:function(namespaceURI, localName, qName, attrs) {
var doc = this.document;
var el = doc.createElementNS(namespaceURI, qName||localName);
Expand Down
10 changes: 8 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ API Reference
//errorHandler is supported
new DOMParser({
/**
* youcan override the errorHandler for xml parser
* locator is always need for error position info
*/
locator:{},
/**
* you can override the errorHandler for xml parser
* @link http://www.saxproject.org/apidoc/org/xml/sax/ErrorHandler.html
*/
errorHandler:{warning:callback,error:callback,fatalError:callback}
errorHandler:{warning:function(w){console.warn(w)},error:callback,fatalError:callback}
//only callback model
//errorHandler:function(level,msg){console.log(level,msg)}
})
```
Expand Down
92 changes: 52 additions & 40 deletions sax.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
var start = 0;
while(true){
var i = source.indexOf('<',start);
if(i<0){
if(!source.substr(start).match(/^\s*$/)){
var doc = domBuilder.document;
var text = doc.createTextNode(source.substr(start));
doc.appendChild(text);
domBuilder.currentElement = text;
}
return;
}
if(i>start){
appendText(i);
}
Expand Down Expand Up @@ -113,46 +122,41 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
end = parseDCC(source,i,domBuilder,errorHandler);
break;
default:
if(i<0){
if(!source.substr(start).match(/^\s*$/)){
domBuilder.allText(source);
}
return;
}else{
try{
locator&&position(i);
var el = new ElementAttributes();
//elStartEnd
var end = parseElementStartPart(source,i,el,entityReplacer,errorHandler);
var len = el.length;
//position fixed
if(len && locator){
var backup = copyLocator(locator,{});
for(var i = 0;i<len;i++){
var a = el[i];
position(a.offset);
a.offset = copyLocator(locator,{});
}
copyLocator(backup,locator);
}
if(!el.closed && fixSelfClosed(source,end,el.tagName,closeMap)){
el.closed = true;
if(!entityMap.nbsp){
errorHandler.warning('unclosed xml attribute');
}
try{
locator&&position(i);

var el = new ElementAttributes();

//elStartEnd
var end = parseElementStartPart(source,i,el,entityReplacer,errorHandler);
var len = el.length;
//position fixed
if(len && locator){
var backup = copyLocator(locator,{});
for(var i = 0;i<len;i++){
var a = el[i];
position(a.offset);
a.offset = copyLocator(locator,{});
}
appendElement(el,domBuilder,parseStack);


if(el.uri === 'http://www.w3.org/1999/xhtml' && !el.closed){
end = parseHtmlSpecialContent(source,end,el.tagName,entityReplacer,domBuilder)
}else{
end++;
copyLocator(backup,locator);
}
if(!el.closed && fixSelfClosed(source,end,el.tagName,closeMap)){
el.closed = true;
if(!entityMap.nbsp){
errorHandler.warning('unclosed xml attribute');
}
}catch(e){
errorHandler.error('element parse error: '+e);
end = -1;
}
appendElement(el,domBuilder,parseStack);


if(el.uri === 'http://www.w3.org/1999/xhtml' && !el.closed){
end = parseHtmlSpecialContent(source,end,el.tagName,entityReplacer,domBuilder)
}else{
end++;
}
}catch(e){
errorHandler.error('element parse error: '+e);
end = -1;
}

}
Expand Down Expand Up @@ -238,6 +242,9 @@ function parseElementStartPart(source,start,el,entityReplacer,errorHandler){
throw new Error("attribute invalid close char('/')")
}
break;
case ''://end document
//throw new Error('unexpected end of input')
errorHandler.error('unexpected end of input');
case '>':
switch(s){
case S_TAG:
Expand Down Expand Up @@ -449,8 +456,12 @@ function parseDCC(source,start,domBuilder,errorHandler){//sure start with '<!'
var end = source.indexOf('-->',start+4);
if(end === -1) errorHandler.fatalError("Unclosed comment");
//append comment source.substring(4,end)//<!--
domBuilder.comment(source,start+4,end-start-4);
return end+3;
if(end>start){
domBuilder.comment(source,start+4,end-start-4);
return end+3;
}else{
return -1;
}
}else{
//error
return -1;
Expand All @@ -472,7 +483,8 @@ function parseDCC(source,start,domBuilder,errorHandler){//sure start with '<!'
var pubid = len>3 && /^public$/i.test(matchs[2][0]) && matchs[3][0]
var sysid = len>4 && matchs[4][0];
var lastMatch = matchs[len-1]
domBuilder.startDTD(name,pubid,sysid);
domBuilder.startDTD(name,pubid && pubid.replace(/^(['"])(.*?)\1$/,'$2'),
sysid && sysid.replace(/^(['"])(.*?)\1$/,'$2'));
domBuilder.endDTD();

return lastMatch.index+lastMatch[0].length
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function include(){
include('./dom','./parse-element','./node','./namespace','./html/normalize'
,'./error','./locator'
,'./big-file-performance'
,"./xml-error"
)


Expand Down
12 changes: 12 additions & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ wows.describe('XML Node Parse').addBatch({
var doc = new DOMParser().parseFromString(source,"text/xml");
var source2 = new XMLSerializer().serializeToString(doc);
console.assert(source == source2,source2);
},
'public id && sysid':function(){
var error = []
var parser = new DOMParser({
locator:{},
errorHandler:function(msg){
error.push(msg);
}
});
var doc = parser.parseFromString('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html/>', 'text/html');
console.log(doc+'')

}
}).run(); // Run it
//var ELEMENT_NODE = NodeType.ELEMENT_NODE = 1;
Expand Down
14 changes: 14 additions & 0 deletions test/xml-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ vows.describe('errorHandle').addBatch({
//console.log(errors)
console.assert(errors.length==0,"unclosed html tag not need report!!")
},
"invalid xml node":function(){
var errors = [];
var p = new DOMParser({
errorHandler: function(key,msg){
//console.log(key,msg)
errors.push(key, msg)
}
});
console.log('loop');
var dom = new DOMParser().parseFromString('<test><!--', 'text/xml')
//var dom = new DOMParser().parseFromString('<div><p><a></a><b></b></p></div>', 'text/html');
console.log(dom+'')
var dom = p.parseFromString('<r', 'text/xml');
},
'invalid xml attribute(miss qute)': function() {
var errors = [];
var p = new DOMParser({
Expand Down

0 comments on commit b05c68e

Please sign in to comment.