Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Style updates

  • Loading branch information...
commit de4334c1dee6372fac0ad3709b36f901c49369e4 1 parent 9f24f34
@isaacs isaacs authored
View
52 lib/sax.js
@@ -35,7 +35,7 @@ function SAXParser (strict, opt) {
this.strict = !!strict
this.state = S.BEGIN
this.ENTITIES = Object.create(sax.ENTITIES)
-
+
if (this.opt.xmlns) this.ns = {} // NS bindings stacks: prefix -> [uri, uri...]
// mostly just for error reporting
@@ -244,7 +244,7 @@ sax.STATE =
sax.ENTITIES =
{ "apos" : "'"
-, "quot" : '"'
+, "quot" : "\""
, "amp" : "&"
, "gt" : ">"
, "lt" : "<"
@@ -321,19 +321,20 @@ function strictFail (parser, message) {
}
function qname (n, parser) {
- var i = n.indexOf(':')
- , q = i < 0 ? ['',n] : n.split(':')
- , p = q[0] , l = q[1]
- , u = parser.ns[p] && parser.ns[p][0] || ''
-
- if (!u && p && p != 'xmlns') {
+ var i = n.indexOf(":")
+ , q = i < 0 ? [ "", n ] : n.split(":")
+ , p = q[0]
+ , l = q[1]
+ , u = parser.ns[p] && parser.ns[p][0] || ""
+
+ if (!u && p && p != "xmlns") {
strictFail(parser, "Unbound namespace prefix: " + JSON.stringify(p))
parser.ns[p] = parser.ns[p] || []
parser.ns[p].push(p)
u = p
}
-
- return {prefix: p, local: l, uri: u}
+
+ return { prefix: p, local: l, uri: u }
}
function newTag (parser) {
@@ -348,10 +349,10 @@ function attrib (parser) {
var n = parser.attribName
, qn = qname(n, parser)
- if (n == 'xmlns' || qn.prefix == 'xmlns') {
+ if (n == "xmlns" || qn.prefix == "xmlns") {
// 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
+ var prefix = n == "xmlns" ? "" : qn.local
parser.ns[prefix] = parser.ns[prefix] || []
parser.ns[prefix].unshift(parser.attribValue)
parser.tag.bindings.push(prefix)
@@ -364,9 +365,10 @@ function attrib (parser) {
} else {
// in non-xmlns mode, we can emit the event right away
parser.tag.attributes[parser.attribName] = parser.attribValue
- emitNode(parser, "onattribute", {
- name:parser.attribName,
- value:parser.attribValue})
+ emitNode( parser
+ , "onattribute"
+ , { name: parser.attribName
+ , value: parser.attribValue } )
}
parser.attribName = parser.attribValue = ""
@@ -377,34 +379,36 @@ function openTag (parser, selfClosing) {
// emit namespace binding events
for (var i = 0; i < parser.tag.bindings.length; i ++) {
var p = parser.tag.bindings[i]
- emitNode(parser, "onopennamespace", {
- prefix: p, uri: parser.ns[p][0] })
+ emitNode( parser
+ , "onopennamespace"
+ , { prefix: p , uri: parser.ns[p][0] } )
}
-
+
// handle deferred onattribute events
while (parser.attribList.length) {
var nv = parser.attribList.pop()
- , n = nv[0], v = nv[1]
+ , n = nv[0]
+ , v = nv[1]
, q = qname(n, parser)
- , ns = q.prefix ? q.uri : ''
+ , ns = q.prefix ? q.uri : ""
, a = { name: n
, value: v
, prefix: q.prefix
, local: q.local
, uri: ns
}
-
+
parser.tag.attributes[n] = a
emitNode(parser, "onattribute", a)
}
-
+
// add namespace info to tag
var qn = qname(parser.tagName, parser)
parser.tag.prefix = qn.prefix
parser.tag.local = qn.local
parser.tag.uri = qn.uri
}
-
+
// process the tag
parser.sawRoot = true
parser.tags.push(parser.tag)
@@ -786,7 +790,7 @@ function write (chunk) {
} else strictFail(parser, "Invalid attribute name")
continue
- case S.ATTRIB_NAME:
+ case S.ATTRIB_NAME:
if (c === "=") parser.state = S.ATTRIB_VALUE
else if (is(whitespace, c)) parser.state = S.ATTRIB_NAME_SAW_WHITE
else if (is(nameBody, c)) parser.attribName += c
View
67 test/index.js
@@ -1,53 +1,54 @@
-var sys = require("sys"),
- assert = require("assert"),
- fs = require("fs"),
- path = require("path"),
- sax = require("../lib/sax");
+var util = require("util")
+ , assert = require("assert")
+ , fs = require("fs")
+ , path = require("path")
+ , sax = require("../lib/sax")
-exports.sax = sax;
+exports.sax = sax
// handy way to do simple unit tests
// if the options contains an xml string, it'll be written and the parser closed.
// otherwise, it's assumed that the test will write and close.
exports.test = function test (options) {
- var xml = options.xml,
- parser = sax.parser(options.strict, options.opt),
- expect = options.expect,
- e = 0;
+ var xml = options.xml
+ , parser = sax.parser(options.strict, options.opt)
+ , expect = options.expect
+ , e = 0
sax.EVENTS.forEach(function (ev) {
parser["on" + ev] = function (n) {
- if (e >= expect.length && (ev === "end" || ev === "ready")) return;
+ if (e >= expect.length && (ev === "end" || ev === "ready")) return
assert.ok( e < expect.length,
- "expectation #"+e+" "+sys.inspect(expect[e])+"\n"+
- "Unexpected event: "+ev+" "+(n ? sys.inspect(n) : ""));
- var inspected = n instanceof Error ? "\n"+ n.message : sys.inspect(n)
+ "expectation #"+e+" "+util.inspect(expect[e])+"\n"+
+ "Unexpected event: "+ev+" "+(n ? util.inspect(n) : ""))
+ var inspected = n instanceof Error ? "\n"+ n.message : util.inspect(n)
assert.equal(ev, expect[e][0],
"expectation #"+e+"\n"+
"Didn't get expected event\n"+
- "expect: "+expect[e][0] + " " +sys.inspect(expect[e][1])+"\n"+
- "actual: "+ev+" "+inspected+"\n");
- if (ev === "error") assert.equal(n.message, expect[e][1]);
+ "expect: "+expect[e][0] + " " +util.inspect(expect[e][1])+"\n"+
+ "actual: "+ev+" "+inspected+"\n")
+ if (ev === "error") assert.equal(n.message, expect[e][1])
else assert.deepEqual(n, expect[e][1],
"expectation #"+e+"\n"+
"Didn't get expected argument\n"+
- "expect: "+expect[e][0] + " " +sys.inspect(expect[e][1])+"\n"+
- "actual: "+ev+" "+inspected+"\n");
- e++;
- if (ev === "error") parser.resume();
- };
- });
- if (xml) parser.write(xml).close();
- return parser;
+ "expect: "+expect[e][0] + " " +util.inspect(expect[e][1])+"\n"+
+ "actual: "+ev+" "+inspected+"\n")
+ e++
+ if (ev === "error") parser.resume()
+ }
+ })
+ if (xml) parser.write(xml).close()
+ return parser
}
if (module === require.main) {
- var running = true,
- failures = 0;
+ var running = true
+ , failures = 0
+
function fail (file, er) {
- sys.error("Failed: "+file);
- sys.error(er.stack || er.message);
- failures ++;
+ util.error("Failed: "+file)
+ util.error(er.stack || er.message)
+ failures ++
}
fs.readdir(__dirname, function (error, files) {
@@ -67,7 +68,7 @@ if (module === require.main) {
fail(file, er)
}
})
- if (!failures) return console.log("#all pass");
- else return console.error(failures + " failure" + (failures > 1 ? "s" : ""));
- });
+ if (!failures) return console.log("#all pass")
+ else return console.error(failures + " failure" + (failures > 1 ? "s" : ""))
+ })
}
View
18 test/xmlns-rebinding.js
@@ -19,19 +19,19 @@ require(__dirname).test
attributes: { "xmlns:x": { name: "xmlns:x", value: "x1", uri: "", prefix: "xmlns", local: "x" }
, "xmlns:y": { name: "xmlns:y", value: "y1", uri: "", prefix: "xmlns", local: "y" }
, "x:a": { name: "x:a", value: "x1", uri: "x1", prefix: "x", local: "a" }
- , "y:a": { name: "y:a", value: "y1", uri: "y1", prefix: "y", local: "a" } },
+ , "y:a": { name: "y:a", value: "y1", uri: "y1", prefix: "y", local: "a" } },
bindings: [ "x", "y" ] } ]
, [ "opennamespace", { prefix: "x", uri: "x2" } ]
, [ "attribute", { name: "xmlns:x", value: "x2", uri: "", prefix: "xmlns", local: "x" } ]
- , [ "opentag", { name: "rebind", uri: "", prefix: "", local: "rebind",
- attributes: { "xmlns:x": { name: "xmlns:x", value: "x2", uri: "", prefix: "xmlns", local: "x" } },
+ , [ "opentag", { name: "rebind", uri: "", prefix: "", local: "rebind",
+ attributes: { "xmlns:x": { name: "xmlns:x", value: "x2", uri: "", prefix: "xmlns", local: "x" } },
bindings: [ "x" ] } ]
-
+
, [ "attribute", { name: "x:a", value: "x2", uri: "x2", prefix: "x", local: "a" } ]
, [ "attribute", { name: "y:a", value: "y1", uri: "y1", prefix: "y", local: "a" } ]
- , [ "opentag", { name: "check", uri: "", prefix: "", local: "check",
- attributes: { "x:a": { name: "x:a", value: "x2", uri: "x2", prefix: "x", local: "a" }
+ , [ "opentag", { name: "check", uri: "", prefix: "", local: "check",
+ attributes: { "x:a": { name: "x:a", value: "x2", uri: "x2", prefix: "x", local: "a" }
, "y:a": { name: "y:a", value: "y1", uri: "y1", prefix: "y", local: "a" } },
bindings: [] } ]
@@ -42,12 +42,12 @@ require(__dirname).test
, [ "attribute", { name: "x:a", value: "x1", uri: "x1", prefix: "x", local: "a" } ]
, [ "attribute", { name: "y:a", value: "y1", uri: "y1", prefix: "y", local: "a" } ]
- , [ "opentag", { name: "check", uri: "", prefix: "", local: "check",
- attributes: { "x:a": { name: "x:a", value: "x1", uri: "x1", prefix: "x", local: "a" }
+ , [ "opentag", { name: "check", uri: "", prefix: "", local: "check",
+ attributes: { "x:a": { name: "x:a", value: "x1", uri: "x1", prefix: "x", local: "a" }
, "y:a": { name: "y:a", value: "y1", uri: "y1", prefix: "y", local: "a" } },
bindings: [] } ]
, [ "closetag", "check" ]
-
+
, [ "closetag", "root" ]
, [ "closenamespace", { prefix: "y", uri: "y1" } ]
, [ "closenamespace", { prefix: "x", uri: "x1" } ]
View
29 test/xmlns-strict.js
@@ -18,19 +18,19 @@ require(__dirname).test
, [ "attribute", { name: "attr", value: "normal", prefix: "", local: "attr", uri: "" } ]
, [ "opentag", { name: "plain", prefix: "", local: "plain", uri: "",
- attributes: { "attr": { name: "attr", value: "normal", uri: "", prefix: "", local: "attr", uri: "" } },
+ attributes: { "attr": { name: "attr", value: "normal", uri: "", prefix: "", local: "attr", uri: "" } },
bindings: [] } ]
, [ "closetag", "plain" ]
-
+
, [ "opennamespace", { prefix: "", uri: "uri:default" } ]
-
+
, [ "attribute", { name: "xmlns", value: "uri:default", prefix: "", local: "xmlns", uri: "" } ]
, [ "opentag", { name: "ns1", prefix: "", local: "ns1", uri: "uri:default",
- attributes: { "xmlns": { name: "xmlns", value: "uri:default", prefix: "", local: "xmlns", uri: "" } },
+ attributes: { "xmlns": { name: "xmlns", value: "uri:default", prefix: "", local: "xmlns", uri: "" } },
bindings: [ "" ] } ]
, [ "attribute", { name: "attr", value: "normal", prefix: "", local: "attr", uri: "" } ]
- , [ "opentag", { name: "plain", prefix: "", local: "plain", uri: "uri:default",
+ , [ "opentag", { name: "plain", prefix: "", local: "plain", uri: "uri:default",
attributes: { "attr": { name: "attr", value: "normal", prefix: "", local: "attr", uri: "" } },
bindings: [] } ]
, [ "closetag", "plain" ]
@@ -38,22 +38,25 @@ require(__dirname).test
, [ "closetag", "ns1" ]
, [ "closenamespace", { prefix: "", uri: "uri:default" } ]
-
+
, [ "opennamespace", { prefix: "a", uri: "uri:nsa" } ]
-
- , [ "attribute", { name: "xmlns:a", value: "uri:nsa", prefix: "xmlns", local: "a", uri: "" } ] // FIXME should probably have uri= xmlns namespace (http://www.w3.org/2000/xmlns/)
- , [ "opentag", { name: "ns2", prefix: "", local: "ns2", uri: "",
- attributes: { "xmlns:a": { name: "xmlns:a", value: "uri:nsa", prefix: "xmlns", local: "a", uri: "" } }, // FIXME should probably have uri= xmlns namespace (http://www.w3.org/2000/xmlns/)
+
+ // FIXME should probably have uri= xmlns namespace (http://www.w3.org/2000/xmlns/)
+ , [ "attribute", { name: "xmlns:a", value: "uri:nsa", prefix: "xmlns", local: "a", uri: "" } ]
+
+ // FIXME should probably have uri= xmlns namespace (http://www.w3.org/2000/xmlns/)
+ , [ "opentag", { name: "ns2", prefix: "", local: "ns2", uri: "",
+ attributes: { "xmlns:a": { name: "xmlns:a", value: "uri:nsa", prefix: "xmlns", local: "a", uri: "" } },
bindings: [ "a" ] } ]
, [ "attribute", { name: "attr", value: "normal", prefix: "", local: "attr", uri: "" } ]
- , [ "opentag", { name: "plain", prefix: "", local: "plain", uri: "",
+ , [ "opentag", { name: "plain", prefix: "", local: "plain", uri: "",
attributes: { "attr": { name: "attr", value: "normal", prefix: "", local: "attr", uri: "" } },
bindings: [] } ]
, [ "closetag", "plain" ]
-
+
, [ "attribute", { name: "a:attr", value: "namespaced", prefix: "a", local: "attr", uri: "uri:nsa" } ]
- , [ "opentag", { name: "a:ns", prefix: "a", local: "ns", uri: "uri:nsa",
+ , [ "opentag", { name: "a:ns", prefix: "a", local: "ns", uri: "uri:nsa",
attributes: { "a:attr": { name: "a:attr", value: "namespaced", prefix: "a", local: "attr", uri: "uri:nsa" } },
bindings: [] } ]
, [ "closetag", "a:ns" ]
View
8 test/xmlns-unbound.js
@@ -2,14 +2,14 @@
require(__dirname).test(
{ strict : true
, opt : { xmlns: true }
- , expect :
+ , expect :
[ ["error", "Unbound namespace prefix: \"unbound\"\nLine: 0\nColumn: 26\nChar: '"]
-
+
, [ "attribute", { name: "unbound:attr", value: "value", uri: "unbound", prefix: "unbound", local: "attr" } ]
- , [ "opentag", { name: "root", uri: "", prefix: "", local: "root",
+ , [ "opentag", { name: "root", uri: "", prefix: "", local: "root",
attributes: { "unbound:attr": { name: "unbound:attr", value: "value", uri: "unbound", prefix: "unbound", local: "attr" } },
bindings: [] } ]
, [ "closetag", "root" ]
]
}
-).write("<root unbound:attr='value'/>")
+).write("<root unbound:attr='value'/>")
Please sign in to comment.
Something went wrong with that request. Please try again.