Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Automatically binds the xml prefix. Fixes #39.

  • Loading branch information...
commit e49053c51a9da2deba1d529370688bbc37bea48a 1 parent e8e9bdb
@jmakeig jmakeig authored isaacs committed
View
14 lib/sax.js
@@ -36,7 +36,9 @@ function SAXParser (strict, opt) {
this.state = S.BEGIN
this.ENTITIES = Object.create(sax.ENTITIES)
- if (this.opt.xmlns) this.ns = {} // NS bindings stacks: prefix -> [uri, uri...]
+ if (this.opt.xmlns) this.ns = {
+ "xml": ["http://www.w3.org/XML/1998/namespace"]
+ } // NS bindings stacks: prefix -> [uri, uri...]
// mostly just for error reporting
this.position = this.line = this.column = 0
@@ -353,9 +355,13 @@ function attrib (parser) {
// namespace binding attribute; push the binding into scope
// and annotate tag so binding can be popped on tag close
var prefix = n == "xmlns" ? "" : qn.local
- parser.ns[prefix] = parser.ns[prefix] || []
- parser.ns[prefix].unshift(parser.attribValue)
- parser.tag.bindings.push(prefix)
+ if(prefix === "xml" && parser.attribValue !== "http://www.w3.org/XML/1998/namespace") {
+ strictFail(parser, "Cannot bind the xml prefix to " + parser.attribValue)
+ } else {
+ parser.ns[prefix] = parser.ns[prefix] || []
+ parser.ns[prefix].unshift(parser.attribValue)
+ parser.tag.bindings.push(prefix)
+ }
}
// defer onattribute events until all attributes have been seen
View
35 test/xmlns-xml-default-prefix-attribute.js
@@ -0,0 +1,35 @@
+require(__dirname).test(
+ { xml : "<root xml:lang='en'/>"
+ , expect :
+ [ [ "attribute"
+ , { name: "xml:lang"
+ , local: "lang"
+ , prefix: "xml"
+ , uri: "http://www.w3.org/XML/1998/namespace"
+ , value: "en"
+ }
+ ]
+ , [ "opentag"
+ , { name: "root"
+ , uri: ""
+ , prefix: ""
+ , local: "root"
+ , attributes:
+ { "xml:lang":
+ { name: "xml:lang"
+ , local: "lang"
+ , prefix: "xml"
+ , uri: "http://www.w3.org/XML/1998/namespace"
+ , value: "en"
+ }
+ }
+ , bindings: []
+ }
+ ]
+ , ["closetag", "root"]
+ ]
+ , strict : true
+ , opt : { xmlns: true }
+ }
+)
+
View
20 test/xmlns-xml-default-prefix.js
@@ -0,0 +1,20 @@
+require(__dirname).test(
+ { xml : "<xml:root/>"
+ , expect :
+ [
+ [ "opentag"
+ , { name: "xml:root"
+ , uri: "http://www.w3.org/XML/1998/namespace"
+ , prefix: "xml"
+ , local: "root"
+ , attributes: {}
+ , bindings: []
+ }
+ ]
+ , ["closetag", "xml:root"]
+ ]
+ , strict : true
+ , opt : { xmlns: true }
+ }
+)
+
View
39 test/xmlns-xml-default-redefine.js
@@ -0,0 +1,39 @@
+require(__dirname).test(
+ { xml : "<xml:root xmlns:xml='ERROR'/>"
+ , expect :
+ [ ["error"
+ , "Cannot bind the xml prefix to ERROR\n"
+ + "Line: 0\nColumn: 27\nChar: '"
+ ]
+ , [ "attribute"
+ , { name: "xmlns:xml"
+ , local: "xml"
+ , prefix: "xmlns"
+ , uri: ""
+ , value: "ERROR"
+ }
+ ]
+ , [ "opentag"
+ , { name: "xml:root"
+ , uri: "http://www.w3.org/XML/1998/namespace"
+ , prefix: "xml"
+ , local: "root"
+ , attributes:
+ { "xmlns:xml":
+ { name: "xmlns:xml"
+ , local: "xml"
+ , prefix: "xmlns"
+ , uri: ""
+ , value: "ERROR"
+ }
+ }
+ , bindings: []
+ }
+ ]
+ , ["closetag", "xml:root"]
+ ]
+ , strict : true
+ , opt : { xmlns: true }
+ }
+)
+
Please sign in to comment.
Something went wrong with that request. Please try again.