Skip to content

Commit dc3b829

Browse files
committed
Add d3.create. Fixes #156.
1 parent a551afe commit dc3b829

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ selection.each(function() {
428428
});
429429
```
430430

431+
<a name="create" href="#create">#</a> d3.<b>create</b>(<i>name</i>) [<>](https://github.com/d3/d3-selection/blob/master/src/create.js "Source")
432+
433+
Given the specified element *name*, returns a single-element selection containing a detached element of the given name in the current document.
434+
431435
<a name="creator" href="#creator">#</a> d3.<b>creator</b>(<i>name</i>) [<>](https://github.com/d3/d3-selection/blob/master/src/creator.js "Source")
432436

433437
Given the specified element *name*, returns a function which creates an element of the given name, assuming that `this` is the parent element. This method is used internally by [*selection*.append](#selection_append) and [*selection*.insert](#selection_insert) to create new elements. For example, this:

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export {default as create} from "./src/create";
12
export {default as creator} from "./src/creator";
23
export {default as local} from "./src/local";
34
export {default as matcher} from "./src/matcher";

src/create.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import creator from "./creator";
2+
import select from "./select";
3+
4+
export default function(name) {
5+
return select(creator(name).call(document.documentElement));
6+
}

test/create-test.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var tape = require("tape"),
2+
jsdom = require("./jsdom"),
3+
d3 = require("../");
4+
5+
tape("d3.create(name) returns a new HTML element with the given name", function(test) {
6+
var document = global.document = jsdom("");
7+
try {
8+
var h1 = d3.create("h1");
9+
test.equal(h1._groups[0][0].namespaceURI, "http://www.w3.org/1999/xhtml");
10+
test.equal(h1._groups[0][0].tagName, "H1");
11+
test.deepEqual(h1._parents, [null]);
12+
test.end();
13+
} finally {
14+
delete global.document;
15+
}
16+
});
17+
18+
tape("d3.create(name) returns a new SVG element with the given name", function(test) {
19+
var document = global.document = jsdom("");
20+
try {
21+
var svg = d3.create("svg");
22+
test.equal(svg._groups[0][0].namespaceURI, "http://www.w3.org/2000/svg");
23+
test.equal(svg._groups[0][0].tagName, "svg");
24+
test.deepEqual(svg._parents, [null]);
25+
test.end();
26+
} finally {
27+
delete global.document;
28+
}
29+
});

0 commit comments

Comments
 (0)