Permalink
Browse files

Infer namespaces for root element.

Now if you add an "svg" element, we automatically infer the namespace because
there is an "svg" namespace prefix. In conjunction with inheriting the namespace
from the parent element, this means that namespaces are almost optional!
  • Loading branch information...
mbostock committed Nov 23, 2011
1 parent cc3f5da commit 11c1e7a906409d7748dec3193768c58739b26e45
Showing with 31 additions and 30 deletions.
  1. +12 −14 d3.js
  2. +2 −2 d3.min.js
  3. +12 −14 src/core/ns.js
  4. +5 −0 test/core/ns-test.js
View
26 d3.js
@@ -464,24 +464,22 @@ d3.xml = function(url, mime, callback) {
}
d3.xhr(url, mime, ready);
};
-d3.ns = {
-
- prefix: {
- svg: "http://www.w3.org/2000/svg",
- xhtml: "http://www.w3.org/1999/xhtml",
- xlink: "http://www.w3.org/1999/xlink",
- xml: "http://www.w3.org/XML/1998/namespace",
- xmlns: "http://www.w3.org/2000/xmlns/"
- },
+var d3_nsPrefix = {
+ svg: "http://www.w3.org/2000/svg",
+ xhtml: "http://www.w3.org/1999/xhtml",
+ xlink: "http://www.w3.org/1999/xlink",
+ xml: "http://www.w3.org/XML/1998/namespace",
+ xmlns: "http://www.w3.org/2000/xmlns/"
+};
+d3.ns = {
+ prefix: d3_nsPrefix,
qualify: function(name) {
var i = name.indexOf(":");
- return i < 0 ? name : {
- space: d3.ns.prefix[name.substring(0, i)],
- local: name.substring(i + 1)
- };
+ return i < 0 ? (name in d3_nsPrefix
+ ? {space: d3_nsPrefix[name], local: name} : name)
+ : {space: d3_nsPrefix[name.substring(0, i)], local: name.substring(i + 1)};
}
-
};
d3.dispatch = function() {
var dispatch = new d3_dispatch(),
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1,19 +1,17 @@
-d3.ns = {
-
- prefix: {
- svg: "http://www.w3.org/2000/svg",
- xhtml: "http://www.w3.org/1999/xhtml",
- xlink: "http://www.w3.org/1999/xlink",
- xml: "http://www.w3.org/XML/1998/namespace",
- xmlns: "http://www.w3.org/2000/xmlns/"
- },
+var d3_nsPrefix = {
+ svg: "http://www.w3.org/2000/svg",
+ xhtml: "http://www.w3.org/1999/xhtml",
+ xlink: "http://www.w3.org/1999/xlink",
+ xml: "http://www.w3.org/XML/1998/namespace",
+ xmlns: "http://www.w3.org/2000/xmlns/"
+};
+d3.ns = {
+ prefix: d3_nsPrefix,
qualify: function(name) {
var i = name.indexOf(":");
- return i < 0 ? name : {
- space: d3.ns.prefix[name.substring(0, i)],
- local: name.substring(i + 1)
- };
+ return i < 0 ? (name in d3_nsPrefix
+ ? {space: d3_nsPrefix[name], local: name} : name)
+ : {space: d3_nsPrefix[name.substring(0, i)], local: name.substring(i + 1)};
}
-
};
View
@@ -46,6 +46,11 @@ suite.addBatch({
var name = d3.ns.qualify("foo:bar");
assert.isUndefined(name.space);
assert.equal(name.local, "bar");
+ },
+ "known local name returns space and local": function() {
+ var name = d3.ns.qualify("svg");
+ assert.equal(name.space, "http://www.w3.org/2000/svg");
+ assert.equal(name.local, "svg");
}
}
});

0 comments on commit 11c1e7a

Please sign in to comment.