Cleverly compacted `classed` code.

We can take advantage of JavaScript's invisible hoisting of variables to set
them before they are defined. This simplifies the code for the various special
cases in the `classed` operator, such as for SVG elements and browsers that
support the tokenized class list.
This commit is contained in:
Mike Bostock 2011-05-07 11:21:59 -07:00
Родитель 8d2bc4b1b3
Коммит 99e2a55614
3 изменённых файлов: 32 добавлений и 34 удалений

31
d3.js поставляемый
Просмотреть файл

@ -1305,37 +1305,36 @@ function d3_selection(groups) {
// If no value is specified, return the first value. // If no value is specified, return the first value.
if (arguments.length < 2) { if (arguments.length < 2) {
return first(function() { return first(function() {
var c;
if (c = this.classList) return c.contains(name); if (c = this.classList) return c.contains(name);
var c = this.className;
re.lastIndex = 0; re.lastIndex = 0;
return re.test((c = this.className).baseVal != null return re.test(c.baseVal != null ? c.baseVal : c);
? c.baseVal : c);
}); });
} }
/** @this {Element} */ /** @this {Element} */
function classedAdd() { function classedAdd() {
var c;
if (c = this.classList) return c.add(name); if (c = this.classList) return c.add(name);
var isAnimatedString = (c = this.className).baseVal != null, var c = this.className,
classes = isAnimatedString ? c.baseVal : c; cb = c.baseVal != null,
cv = cb ? c.baseVal : c;
re.lastIndex = 0; re.lastIndex = 0;
if (!re.test(classes)) { if (!re.test(cv)) {
classes = d3_collapse(classes + " " + name); cv = d3_collapse(cv + " " + name);
if (isAnimatedString) c.baseVal = classes; if (cb) c.baseVal = cv;
else this.className = classes; else this.className = cv;
} }
} }
/** @this {Element} */ /** @this {Element} */
function classedRemove() { function classedRemove() {
var c;
if (c = this.classList) return c.remove(name); if (c = this.classList) return c.remove(name);
var isAnimatedString = (c = this.className).baseVal != null, var c = this.className,
classes = isAnimatedString ? c.baseVal : c; cb = c.baseVal != null,
classes = d3_collapse(classes.replace(re, " ")); cv = cb ? c.baseVal : c;
if (isAnimatedString) c.baseVal = classes; cv = d3_collapse(cv.replace(re, " "));
else this.className = classes; if (cb) c.baseVal = cv;
else this.className = cv;
} }
/** @this {Element} */ /** @this {Element} */

4
d3.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -305,37 +305,36 @@ function d3_selection(groups) {
// If no value is specified, return the first value. // If no value is specified, return the first value.
if (arguments.length < 2) { if (arguments.length < 2) {
return first(function() { return first(function() {
var c;
if (c = this.classList) return c.contains(name); if (c = this.classList) return c.contains(name);
var c = this.className;
re.lastIndex = 0; re.lastIndex = 0;
return re.test((c = this.className).baseVal != null return re.test(c.baseVal != null ? c.baseVal : c);
? c.baseVal : c);
}); });
} }
/** @this {Element} */ /** @this {Element} */
function classedAdd() { function classedAdd() {
var c;
if (c = this.classList) return c.add(name); if (c = this.classList) return c.add(name);
var isAnimatedString = (c = this.className).baseVal != null, var c = this.className,
classes = isAnimatedString ? c.baseVal : c; cb = c.baseVal != null,
cv = cb ? c.baseVal : c;
re.lastIndex = 0; re.lastIndex = 0;
if (!re.test(classes)) { if (!re.test(cv)) {
classes = d3_collapse(classes + " " + name); cv = d3_collapse(cv + " " + name);
if (isAnimatedString) c.baseVal = classes; if (cb) c.baseVal = cv;
else this.className = classes; else this.className = cv;
} }
} }
/** @this {Element} */ /** @this {Element} */
function classedRemove() { function classedRemove() {
var c;
if (c = this.classList) return c.remove(name); if (c = this.classList) return c.remove(name);
var isAnimatedString = (c = this.className).baseVal != null, var c = this.className,
classes = isAnimatedString ? c.baseVal : c; cb = c.baseVal != null,
classes = d3_collapse(classes.replace(re, " ")); cv = cb ? c.baseVal : c;
if (isAnimatedString) c.baseVal = classes; cv = d3_collapse(cv.replace(re, " "));
else this.className = classes; if (cb) c.baseVal = cv;
else this.className = cv;
} }
/** @this {Element} */ /** @this {Element} */