11var vows = require ( "vows" ) ,
2- d3 = require ( "../../" ) ,
2+ interpolateRgb = require ( "../../" ) . interpolateRgb ,
33 load = require ( "../load" ) ,
4- assert = require ( "../env-assert" ) ,
5- document = d3 . selection ( ) . node ( ) . _ownerDocument ,
6- window = document . defaultView ;
4+ assert = require ( "../env-assert" ) ;
75
86var suite = vows . describe ( "selection.attr" ) ;
97
108suite . addBatch ( {
119 "select(body)" : {
12- topic : load ( "selection/attr" ) . sandbox ( {
13- document : document ,
14- window : window
15- } ) ,
10+ topic : load ( "selection/attr" ) . document ( ) ,
1611 "on a simple page" : {
1712 topic : function ( d3 ) {
1813 return d3 . select ( "body" ) ;
1914 } ,
2015 "sets an attribute as a string" : function ( body ) {
2116 body . attr ( "bgcolor" , "red" ) ;
22- assert . equal ( document . body . getAttribute ( "bgcolor" ) , "red" ) ;
17+ assert . equal ( body . node ( ) . getAttribute ( "bgcolor" ) , "red" ) ;
2318 } ,
2419 "sets an attribute as a number" : function ( body ) {
2520 body . attr ( "opacity" , 1 ) ;
26- assert . equal ( document . body . getAttribute ( "opacity" ) , "1" ) ;
21+ assert . equal ( body . node ( ) . getAttribute ( "opacity" ) , "1" ) ;
2722 } ,
2823 "sets an attribute as a function" : function ( body ) {
2924 body . attr ( "bgcolor" , function ( ) { return "orange" ; } ) ;
30- assert . equal ( document . body . getAttribute ( "bgcolor" ) , "orange" ) ;
25+ assert . equal ( body . node ( ) . getAttribute ( "bgcolor" ) , "orange" ) ;
3126 } ,
3227 "sets an attribute as a function of data" : function ( body ) {
3328 body . data ( [ "cyan" ] ) . attr ( "bgcolor" , String ) ;
34- assert . equal ( document . body . getAttribute ( "bgcolor" ) , "cyan" ) ;
29+ assert . equal ( body . node ( ) . getAttribute ( "bgcolor" ) , "cyan" ) ;
3530 } ,
3631 "sets an attribute as a function of index" : function ( body ) {
3732 body . attr ( "bgcolor" , function ( d , i ) { return "orange-" + i ; } ) ;
38- assert . equal ( document . body . getAttribute ( "bgcolor" ) , "orange-0" ) ;
33+ assert . equal ( body . node ( ) . getAttribute ( "bgcolor" ) , "orange-0" ) ;
3934 } ,
4035 "sets a namespaced attribute as a string" : function ( body ) {
4136 body . attr ( "xlink:href" , "url" ) ;
42- assert . equal ( document . body . getAttributeNS ( "http://www.w3.org/1999/xlink" , "href" ) , "url" ) ;
37+ assert . equal ( body . node ( ) . getAttributeNS ( "http://www.w3.org/1999/xlink" , "href" ) , "url" ) ;
4338 } ,
4439 "sets a namespaced attribute as a function" : function ( body ) {
4540 body . data ( [ "orange" ] ) . attr ( "xlink:href" , function ( d , i ) { return d + "-" + i ; } ) ;
46- assert . equal ( document . body . getAttributeNS ( "http://www.w3.org/1999/xlink" , "href" ) , "orange-0" ) ;
41+ assert . equal ( body . node ( ) . getAttributeNS ( "http://www.w3.org/1999/xlink" , "href" ) , "orange-0" ) ;
4742 } ,
4843 "sets attributes as a map of constants" : function ( body ) {
4944 body . attr ( { bgcolor : "white" , "xlink:href" : "url.png" } ) ;
50- assert . equal ( document . body . getAttribute ( "bgcolor" ) , "white" ) ;
51- assert . equal ( document . body . getAttributeNS ( "http://www.w3.org/1999/xlink" , "href" ) , "url.png" ) ;
45+ assert . equal ( body . node ( ) . getAttribute ( "bgcolor" ) , "white" ) ;
46+ assert . equal ( body . node ( ) . getAttributeNS ( "http://www.w3.org/1999/xlink" , "href" ) , "url.png" ) ;
5247 } ,
5348 "sets attributes as a map of functions" : function ( body ) {
5449 body . data ( [ "orange" ] ) . attr ( { "xlink:href" : function ( d , i ) { return d + "-" + i + ".png" ; } } ) ;
55- assert . equal ( document . body . getAttributeNS ( "http://www.w3.org/1999/xlink" , "href" ) , "orange-0.png" ) ;
50+ assert . equal ( body . node ( ) . getAttributeNS ( "http://www.w3.org/1999/xlink" , "href" ) , "orange-0.png" ) ;
5651 } ,
5752 "gets an attribute value" : function ( body ) {
58- document . body . setAttribute ( "bgcolor" , "yellow" ) ;
53+ body . node ( ) . setAttribute ( "bgcolor" , "yellow" ) ;
5954 assert . equal ( body . attr ( "bgcolor" ) , "yellow" ) ;
6055 } ,
6156 "gets a namespaced attribute value" : function ( body ) {
62- document . body . setAttributeNS ( "http://www.w3.org/1999/xlink" , "foo" , "bar" ) ;
57+ body . node ( ) . setAttributeNS ( "http://www.w3.org/1999/xlink" , "foo" , "bar" ) ;
6358 assert . equal ( body . attr ( "xlink:foo" ) , "bar" ) ;
6459 } ,
6560 "removes an attribute as null" : function ( body ) {
@@ -79,18 +74,18 @@ suite.addBatch({
7974 assert . isNull ( body . attr ( "xlink:href" ) ) ;
8075 } ,
8176 "removes attributes as a map of null" : function ( body ) {
82- document . body . setAttribute ( "bgcolor" , "white" ) ;
83- document . body . setAttributeNS ( "http://www.w3.org/1999/xlink" , "href" , "foo.png" ) ;
77+ body . node ( ) . setAttribute ( "bgcolor" , "white" ) ;
78+ body . node ( ) . setAttributeNS ( "http://www.w3.org/1999/xlink" , "href" , "foo.png" ) ;
8479 body . attr ( { bgcolor : null , "xlink:href" : null } ) ;
85- assert . isNull ( document . body . getAttribute ( "bgcolor" ) ) ;
86- assert . isNull ( document . body . getAttributeNS ( "http://www.w3.org/1999/xlink" , "href" ) ) ;
80+ assert . isNull ( body . node ( ) . getAttribute ( "bgcolor" ) ) ;
81+ assert . isNull ( body . node ( ) . getAttributeNS ( "http://www.w3.org/1999/xlink" , "href" ) ) ;
8782 } ,
8883 "removes attributes as a map of functions that return null" : function ( body ) {
89- document . body . setAttribute ( "bgcolor" , "white" ) ;
90- document . body . setAttributeNS ( "http://www.w3.org/1999/xlink" , "href" , "foo.png" ) ;
84+ body . node ( ) . setAttribute ( "bgcolor" , "white" ) ;
85+ body . node ( ) . setAttributeNS ( "http://www.w3.org/1999/xlink" , "href" , "foo.png" ) ;
9186 body . attr ( { bgcolor : function ( ) { } , "xlink:href" : function ( ) { } } ) ;
92- assert . isNull ( document . body . getAttribute ( "bgcolor" ) ) ;
93- assert . isNull ( document . body . getAttributeNS ( "http://www.w3.org/1999/xlink" , "href" ) ) ;
87+ assert . isNull ( body . node ( ) . getAttribute ( "bgcolor" ) ) ;
88+ assert . isNull ( body . node ( ) . getAttributeNS ( "http://www.w3.org/1999/xlink" , "href" ) ) ;
9489 } ,
9590 "returns the current selection" : function ( body ) {
9691 assert . isTrue ( body . attr ( "foo" , "bar" ) === body ) ;
@@ -101,13 +96,10 @@ suite.addBatch({
10196
10297suite . addBatch ( {
10398 "selectAll(div)" : {
104- topic : load ( "selection/attr" ) . sandbox ( {
105- document : document ,
106- window : window
107- } ) ,
99+ topic : load ( "selection/attr" ) . document ( ) ,
108100 "on a simple page" : {
109101 topic : function ( d3 ) {
110- return d3 . select ( "body" ) . html ( "" ) . selectAll ( "div" ) . data ( [ 0 , 1 ] ) . enter ( ) . append ( "div" ) ;
102+ return d3 . select ( "body" ) . selectAll ( "div" ) . data ( [ 0 , 1 ] ) . enter ( ) . append ( "div" ) ;
111103 } ,
112104 "sets an attribute as a string" : function ( div ) {
113105 div . attr ( "bgcolor" , "red" ) ;
@@ -125,7 +117,7 @@ suite.addBatch({
125117 assert . equal ( div [ 0 ] [ 1 ] . getAttribute ( "bgcolor" ) , "coral" ) ;
126118 } ,
127119 "sets an attribute as a function of data" : function ( div ) {
128- div . attr ( "bgcolor" , d3 . interpolateRgb ( "brown" , "steelblue" ) ) ;
120+ div . attr ( "bgcolor" , interpolateRgb ( "brown" , "steelblue" ) ) ;
129121 assert . equal ( div [ 0 ] [ 0 ] . getAttribute ( "bgcolor" ) , "#a52a2a" ) ;
130122 assert . equal ( div [ 0 ] [ 1 ] . getAttribute ( "bgcolor" ) , "#4682b4" ) ;
131123 } ,
@@ -174,15 +166,15 @@ suite.addBatch({
174166 } ,
175167 "returns the current selection" : function ( div ) {
176168 assert . isTrue ( div . attr ( "foo" , "bar" ) === div ) ;
169+ } ,
170+ "ignores null nodes" : function ( div ) {
171+ var node = div [ 0 ] [ 1 ] ;
172+ div . attr ( "href" , null ) ;
173+ div [ 0 ] [ 1 ] = null ;
174+ div . attr ( "href" , "url" ) ;
175+ assert . equal ( div [ 0 ] [ 0 ] . getAttribute ( "href" ) , "url" ) ;
176+ assert . isNull ( node . getAttribute ( "href" ) ) ;
177177 }
178- } ,
179- "ignores null nodes" : function ( d3 ) {
180- var div = d3 . select ( "body" ) . html ( "" ) . selectAll ( "div" ) . data ( [ 0 , 1 ] ) . enter ( ) . append ( "div" ) ,
181- some = d3 . selectAll ( "div" ) ;
182- some [ 0 ] [ 1 ] = null ;
183- some . attr ( "href" , null ) . attr ( "href" , "url" ) ;
184- assert . equal ( div [ 0 ] [ 0 ] . getAttribute ( "href" ) , "url" ) ;
185- assert . isNull ( div [ 0 ] [ 1 ] . getAttribute ( "href" ) ) ;
186178 }
187179 }
188180} ) ;
0 commit comments