Skip to content

Commit 89dd43e

Browse files
committed
Add functional methods.
These are primarily intended for use by d3-transition, but they might have other uses (such as caching closures for optimization).
1 parent 0627356 commit 89dd43e

7 files changed

+132
-1
lines changed

README.md

+60
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,50 @@ Note that the `:nth-child` pseudo-class is a one-based index rather than a zero-
142142

143143
The returned selection may not preserve the index of the original selection, as some elements may be removed; you can use [*selection*.select](#selection_select) to preserve the index, if needed.
144144

145+
<a name="matcher" href="#matcher">#</a> d3.<b>matcher</b>(<i>selector</i>)
146+
147+
Given the specified *selector*, returns a function which returns true if `this` element [matches](https://developer.mozilla.org/en-US/docs/Web/API/Element/matches) the specified selector. This method is used internally by [*selection*.filter](#selection_filter). For example, this:
148+
149+
```js
150+
var div = selection.filter("div");
151+
```
152+
153+
Is equivalent to:
154+
155+
```js
156+
var div = selection.filter(d3.matcher("div"));
157+
```
158+
159+
(Although D3 is not a compatibility layer, this implementation does support vendor-prefixed implementations due to the recent standardization of *element*.matches.)
160+
161+
<a name="selector" href="#selector">#</a> d3.<b>selector</b>(<i>selector</i>)
162+
163+
Given the specified *selector*, returns a function which returns the first descendant of `this` element that matches the specified selector. This method is used internally by [*selection*.select](#selection_select). For example, this:
164+
165+
```js
166+
var div = selection.select("div");
167+
```
168+
169+
Is equivalent to:
170+
171+
```js
172+
var div = selection.select(d3.selector("div"));
173+
```
174+
175+
<a name="selectorAll" href="#selectorAll">#</a> d3.<b>selectorAll</b>(<i>selector</i>)
176+
177+
Given the specified *selector*, returns a function which returns all descendants of `this` element that match the specified selector. This method is used internally by [*selection*.selectAll](#selection_selectAll). For example, this:
178+
179+
```js
180+
var div = selection.selectAll("div");
181+
```
182+
183+
Is equivalent to:
184+
185+
```js
186+
var div = selection.selectAll(d3.selectorAll("div"));
187+
```
188+
145189
### Modifying Elements
146190

147191
After selecting elements, use the selection’s transformation methods to affect document content. Selection methods return the current selection, allowing the concise application of multiple methods on a given selection via method chaining. For example, to set the name attribute and color style of an anchor element:
@@ -274,6 +318,22 @@ selection.each(function() {
274318
});
275319
```
276320

321+
<a name="creator" href="#creator">#</a> d3.<b>creator</b>(<i>name</i>)
322+
323+
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) to create new elements. For example, this:
324+
325+
```js
326+
selection.append("div");
327+
```
328+
329+
Is equivalent to:
330+
331+
```js
332+
selection.append(d3.creator("div"));
333+
```
334+
335+
See [namespace](#namespace) for details on supported namespace prefixes, such as for SVG elements.
336+
277337
### Joining Data
278338

279339
For an introduction to D3’s data joins, see [Thinking With Joins](http://bost.ocks.org/mike/join/). Also see the [General Update Pattern](http://bl.ocks.org/mbostock/3808218) examples.

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
export {default as creator} from "./src/creator";
2+
export {default as matcher} from "./src/matcher";
13
export {default as mouse} from "./src/mouse";
24
export {default as namespace} from "./src/namespace";
35
export {default as namespaces} from "./src/namespaces";
46
export {default as select} from "./src/select";
57
export {default as selectAll} from "./src/selectAll";
68
export {default as selection} from "./src/selection/index";
9+
export {default as selector} from "./src/selector";
10+
export {default as selectorAll} from "./src/selectorAll";
711
export {default as touch} from "./src/touch";
812
export {default as touches} from "./src/touches";
913
export {event} from "./src/selection/on";

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "d3-selection",
3-
"version": "0.6.6",
3+
"version": "0.6.7",
44
"description": "Data-driven DOM manipulation: select elements and join them to data.",
55
"keywords": [
66
"d3",

test/creator-test.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var tape = require("tape"),
2+
jsdom = require("jsdom"),
3+
d3 = require("../");
4+
5+
tape("d3.creator(name).call(element) returns a new element with the given name", function(test) {
6+
var document = jsdom.jsdom("<body class='foo'>");
7+
test.deepEqual(type(d3.creator("h1").call(document.body)), {namespace: "http://www.w3.org/1999/xhtml", name: "H1"});
8+
test.deepEqual(type(d3.creator("xhtml:h1").call(document.body)), {namespace: "http://www.w3.org/1999/xhtml", name: "H1"});
9+
test.deepEqual(type(d3.creator("svg").call(document.body)), {namespace: "http://www.w3.org/2000/svg", name: "svg"});
10+
test.deepEqual(type(d3.creator("g").call(document.body)), {namespace: "http://www.w3.org/1999/xhtml", name: "G"});
11+
test.end();
12+
});
13+
14+
tape("d3.creator(name).call(element) can inherit the namespace from the given element", function(test) {
15+
var document = jsdom.jsdom("<body class='foo'><svg></svg>"),
16+
svg = document.querySelector("svg");
17+
test.deepEqual(type(d3.creator("g").call(document.body)), {namespace: "http://www.w3.org/1999/xhtml", name: "G"});
18+
test.deepEqual(type(d3.creator("g").call(svg)), {namespace: "http://www.w3.org/2000/svg", name: "g"});
19+
test.end();
20+
});
21+
22+
function type(element) {
23+
return {namespace: element.namespaceURI, name: element.tagName};
24+
}

test/matcher-test.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var tape = require("tape"),
2+
jsdom = require("jsdom"),
3+
d3 = require("../");
4+
5+
tape("d3.matcher(selector).call(element) returns true if the element matches the selector", function(test) {
6+
var document = jsdom.jsdom("<body class='foo'>");
7+
test.equal(d3.matcher("body").call(document.body), true);
8+
test.equal(d3.matcher(".foo").call(document.body), true);
9+
test.equal(d3.matcher("body.foo").call(document.body), true);
10+
test.equal(d3.matcher("h1").call(document.body), false);
11+
test.equal(d3.matcher("body.bar").call(document.body), false);
12+
test.end();
13+
});

test/selector-test.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var tape = require("tape"),
2+
jsdom = require("jsdom"),
3+
d3 = require("../");
4+
5+
tape("d3.selector(selector).call(element) returns the first element that matches the selector", function(test) {
6+
var document = jsdom.jsdom("<body class='foo'>");
7+
test.equal(d3.selector("body").call(document.documentElement), document.body);
8+
test.equal(d3.selector(".foo").call(document.documentElement), document.body);
9+
test.equal(d3.selector("body.foo").call(document.documentElement), document.body);
10+
test.equal(d3.selector("h1").call(document.documentElement), null);
11+
test.equal(d3.selector("body.bar").call(document.documentElement), null);
12+
test.end();
13+
});

test/selectorAll-test.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var tape = require("tape"),
2+
jsdom = require("jsdom"),
3+
d3 = require("../");
4+
5+
tape("d3.selectorAll(selector).call(element) returns all elements that match the selector", function(test) {
6+
var document = jsdom.jsdom("<body class='foo'><div class='foo'>"),
7+
body = document.body,
8+
div = document.querySelector("div");
9+
test.deepEqual(d3.selectorAll("body").call(document.documentElement), [body]);
10+
test.deepEqual(d3.selectorAll(".foo").call(document.documentElement), [body, div]);
11+
test.deepEqual(d3.selectorAll("div.foo").call(document.documentElement), [div]);
12+
test.deepEqual(d3.selectorAll("div").call(document.documentElement), [div]);
13+
test.deepEqual(d3.selectorAll("div,body").call(document.documentElement), [body,div]);
14+
test.deepEqual(d3.selectorAll("h1").call(document.documentElement), []);
15+
test.deepEqual(d3.selectorAll("body.bar").call(document.documentElement), []);
16+
test.end();
17+
});

0 commit comments

Comments
 (0)