-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
120 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "xmldom-alpha", | ||
"version": "0.1.24", | ||
"name": "xmldom", | ||
"version": "0.1.27", | ||
"description": "A W3C Standard XML DOM(Level2 CORE) implementation and parser(DOMParser/XMLSerializer).", | ||
"keywords": ["w3c","dom","xml","parser","javascript","DOMParser","XMLSerializer"], | ||
"author": "jindw <[email protected]> (http://www.xidea.org)", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"use strict"; | ||
var xdom = require('xmldom'); | ||
var wows = require('vows'); | ||
var assert = require('assert'); | ||
var DOMParser = require('xmldom').DOMParser; | ||
var XMLSerializer = require('xmldom').XMLSerializer; | ||
|
||
|
||
// Create a Test Suite | ||
wows.describe('XML Namespace Parse').addBatch({ | ||
"testlitecns":function(){ | ||
var assert = assert || {equal:function(v1,v2){console.assert(v1==v2,v1+'!='+v2)}} | ||
var doc = new DOMParser({ | ||
xmlns:{'c':'http://www.xidea.org/lite/core','':'http://www.w3.org/1999/xhtml'} | ||
}).parseFromString('<html><body><c:var name="a" value="${1}"/></body></html>', "text/xml"); | ||
//console.log(String(doc)) | ||
var el = doc.getElementsByTagName('c:var')[0]; | ||
console.log(String(el.namespaceURI)) | ||
console.log(String(doc)) | ||
}, | ||
//ignore default prefix xml attribute | ||
"test":function(){ | ||
var assert = assert || {equal:function(v1,v2){console.assert(v1==v2,v1+'!='+v2)}} | ||
// Just for debugging | ||
var w3 = "http://www.w3.org/1999/xhtml"; | ||
var n1 = "http://www.frankston.com/public"; | ||
var n2 = "http://rmf.vc/n2"; | ||
var n3 = "http://rmf.vc/n3"; | ||
var hx = '<html test="a" xmlns="' + w3 + '" xmlns:rmf="' + n1 + '"><rmf:foo hello="asdfa"/></html>'; | ||
|
||
var doc = new DOMParser().parseFromString(hx, "text/xml"); | ||
//console.log(de.prefix,de.getAttributeNode('xmlns').prefix) | ||
var els = [].slice.call(doc.documentElement.getElementsByTagNameNS(n1, "foo")); | ||
for (var _i = 0, els_1 = els; _i < els_1.length; _i++) { | ||
var el = els_1[_i]; | ||
|
||
var te = doc.createElementNS(n1, "test"); | ||
te.setAttributeNS(n1, "bar", "valx"); | ||
var te = doc.createElementNS(n1, "test"); | ||
te.setAttributeNS(n1, "bar", "valx"); | ||
//console.log("New Elm: " + ss(te)); | ||
assert.equal(String(te),'<test xmlns="'+n1+'" bar="valx"/>'); | ||
el.appendChild(te); | ||
var tx = doc.createElementNS(n2, "test"); | ||
tx.setAttributeNS(n2, "bar", "valx"); | ||
//console.log("New Elm: " + String(tx)); | ||
assert.equal(String(tx),'<test xmlns="'+n2+'" bar="valx"/>'); | ||
el.appendChild(tx); | ||
|
||
//console.log("Element: " + ss(tx)); | ||
} | ||
var sr = String(doc); | ||
//console.log("Serialized: " + sr.replace(/>/g, ">\n ")); | ||
|
||
} | ||
}).run(); | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
var XMLSerializer = require('xmldom').XMLSerializer | ||
var DOMParser = require('xmldom').DOMParser; | ||
var domParser = new DOMParser({xmlns:{'':'http://www.w3.org/1999/xhtml'}}); | ||
|
||
var excludeTags = new RegExp('^(?:'+['javascript', 'vbscript', 'expression', | ||
'meta', 'xml', 'blink', 'link', | ||
'script', 'applet','embed', 'object', | ||
'iframe', 'frame', 'frameset','ilayer', 'layer', 'bgsound', 'base', | ||
].join('|') | ||
+')$','i'); | ||
var excludeAttrs = /^on|style/i | ||
var urlAttrs = /(?:href|src)/i | ||
var invalidURL = /^(data|javascript|vbscript|ftp)\:/ | ||
|
||
function xss(html){ | ||
var dom = domParser.parseFromString(html,'text/html') | ||
return dom.documentElement.toString(true,function(node){ | ||
switch(node.nodeType){ | ||
case 1://element | ||
var tagName = node.tagName; | ||
if(excludeTags.test(tagName)){ | ||
return ''; | ||
} | ||
return node; | ||
case 2: | ||
var attrName = node.name | ||
if(excludeAttrs.test(attrName)){ | ||
return null; | ||
} | ||
if(urlAttrs.test(attrName)){ | ||
var value = node.value; | ||
if(invalidURL.test(value)){ | ||
return null; | ||
} | ||
} | ||
return node; | ||
case 3: | ||
return node; | ||
} | ||
}) | ||
} | ||
|
||
var html = '<div onclick="alert(123)" title="32323"><script>alert(123)</script></div>'; | ||
var result = xss(html); | ||
console.log(result) |