Skip to content

Commit

Permalink
Fix #9 - fallback for node.matches.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed May 22, 2015
1 parent ae2c40f commit 25fc8f7
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions lib/d3/selection.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
var namespace = require("./namespace");

var document = global.document,
var Map = global.Map,
document = global.document,
CustomEvent = global.CustomEvent,
Map = global.Map,
filterEvents = {mouseenter: "mouseover", mouseleave: "mouseout"},
filterOf = function(selector) { return function() { return this.matches(selector); }; },
requoteRe = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g;

// Initialize polyfills.
if (document) {
var node = document.documentElement;

if (!node.matches) {
var vendorMatches = node.webkitMatchesSelector || node.msMatchesSelector || node.mozMatchesSelector || node.oMatchesSelector;
filterOf = function(selector) { return function() { return vendorMatches.call(this, selector); }; };
}

for (var type in filterEvents) {
if ("on" + type in document) {
delete filterEvents[type];
Expand All @@ -24,14 +31,16 @@ if (document) {
CustomEvent.prototype = global.Event.prototype;
}

if (!Map) {
Map = function() {};
Map.prototype = {
set: function(key, value) { this["$" + key] = value; return this; },
get: function(key) { return this["$" + key]; },
has: function(key) { return "$" + key in this; }
};
}
node = type = null;
}

if (!Map) {
Map = function() {};
Map.prototype = {
set: function(key, value) { this["$" + key] = value; return this; },
get: function(key) { return this["$" + key]; },
has: function(key) { return "$" + key in this; }
};
}

// When depth = 1, root = [Node, …].
Expand All @@ -54,7 +63,7 @@ Selection.prototype = {
var depth = this._depth,
stack = new Array(depth * 2);

selector = selectorOf(selector);
if (typeof selector !== "function") selector = selectorOf(selector);

function visit(nodes, depth) {
var i = -1,
Expand Down Expand Up @@ -107,7 +116,7 @@ Selection.prototype = {
var depth = this._depth,
stack = new Array(depth * 2);

selector = selectorAllOf(selector);
if (typeof selector !== "function") selector = selectorAllOf(selector);

function visit(nodes, depth) {
var i = -1,
Expand Down Expand Up @@ -152,7 +161,7 @@ Selection.prototype = {
var depth = this._depth,
stack = new Array(depth * 2);

filter = filterOf(filter);
if (typeof filter !== "function") filter = filterOf(filter);

function visit(nodes, depth) {
var i = -1,
Expand Down Expand Up @@ -210,7 +219,7 @@ Selection.prototype = {

this.enter(); // initializes _enter and _update references
this.exit(); // initializes _exit references
value = valueOf(value);
if (typeof value !== "function") value = valueOf(value);
visit(this._root, depth);

function visit(nodes, depth) {
Expand Down Expand Up @@ -743,7 +752,7 @@ Selection.prototype = {

return this.select(arguments.length < 2
? append
: (selector = selectorOf(selector)), insert);
: (typeof selector !== "function" && (selector = selectorOf(selector)), insert));
},

remove: function() {
Expand Down Expand Up @@ -879,25 +888,19 @@ function wordsOf(string) {
}

function selectorOf(selector) {
return typeof selector === "function" ? selector : function() {
return function() {
return this.querySelector(selector);
};
}

function selectorAllOf(selector) {
return typeof selector === "function" ? selector : function() {
return function() {
return this.querySelectorAll(selector);
};
}

function filterOf(filter) {
return typeof filter === "function" ? filter : function() {
return this.matches(filter); // TODO vendor-specific matchesSelector
};
}

function valueOf(value) {
return typeof value === "function" ? value : function() {
return function() {
return value;
};
}
Expand Down

0 comments on commit 25fc8f7

Please sign in to comment.