@@ -18,38 +18,49 @@ describe('svg+text utils', function() {
1818 . attr ( 'transform' , 'translate(50,50)' ) ;
1919 }
2020
21- function assertAnchorLink ( node , href ) {
21+ function assertAnchorLink ( node , href , target , show , msg ) {
2222 var a = node . select ( 'a' ) ;
2323
24- expect ( a . attr ( 'xlink:href' ) ) . toBe ( href ) ;
25- expect ( a . attr ( 'xlink:show' ) ) . toBe ( href === null ? null : 'new' ) ;
24+ if ( target === undefined ) target = href === null ? null : '_blank' ;
25+ if ( show === undefined ) show = href === null ? null : 'new' ;
26+
27+ expect ( a . attr ( 'xlink:href' ) ) . toBe ( href , msg ) ;
28+ expect ( a . attr ( 'target' ) ) . toBe ( target , msg ) ;
29+ expect ( a . attr ( 'xlink:show' ) ) . toBe ( show , msg ) ;
2630 }
2731
28- function assertTspanStyle ( node , style ) {
32+ function assertTspanStyle ( node , style , msg ) {
2933 var tspan = node . select ( 'tspan' ) ;
30- expect ( tspan . attr ( 'style' ) ) . toBe ( style ) ;
34+ expect ( tspan . attr ( 'style' ) ) . toBe ( style , msg ) ;
3135 }
3236
33- function assertAnchorAttrs ( node , style ) {
37+ function assertAnchorAttrs ( node , expectedAttrs , msg ) {
3438 var a = node . select ( 'a' ) ;
3539
36- var WHITE_LIST = [ 'xlink:href' , 'xlink:show' , 'style' ] ,
40+ if ( ! expectedAttrs ) expectedAttrs = { } ;
41+
42+ var WHITE_LIST = [ 'xlink:href' , 'xlink:show' , 'style' , 'target' , 'onclick' ] ,
3743 attrs = listAttributes ( a . node ( ) ) ;
3844
3945 // check that no other attribute are found in anchor,
4046 // which can be lead to XSS attacks.
4147
42- var hasWrongAttr = attrs . some ( function ( attr ) {
43- return WHITE_LIST . indexOf ( attr ) === - 1 ;
48+ var wrongAttrs = [ ] ;
49+ attrs . forEach ( function ( attr ) {
50+ if ( WHITE_LIST . indexOf ( attr ) === - 1 ) wrongAttrs . push ( attr ) ;
4451 } ) ;
4552
46- expect ( hasWrongAttr ) . toBe ( false ) ;
53+ expect ( wrongAttrs ) . toEqual ( [ ] , msg ) ;
4754
55+ var style = expectedAttrs . style || '' ;
4856 var fullStyle = style || '' ;
4957 if ( style ) fullStyle += ';' ;
5058 fullStyle += 'cursor:pointer' ;
5159
52- expect ( a . attr ( 'style' ) ) . toBe ( fullStyle ) ;
60+ expect ( a . attr ( 'style' ) ) . toBe ( fullStyle , msg ) ;
61+
62+ expect ( a . attr ( 'onclick' ) ) . toBe ( expectedAttrs . onclick || null , msg ) ;
63+
5364 }
5465
5566 function listAttributes ( node ) {
@@ -137,7 +148,7 @@ describe('svg+text utils', function() {
137148 var node = mockTextSVGElement ( textCase ) ;
138149
139150 expect ( node . text ( ) ) . toEqual ( 'Subtitle' ) ;
140- assertAnchorAttrs ( node , 'font-size:300px' ) ;
151+ assertAnchorAttrs ( node , { style : 'font-size:300px' } ) ;
141152 assertAnchorLink ( node , 'XSS' ) ;
142153 } ) ;
143154 } ) ;
@@ -157,11 +168,31 @@ describe('svg+text utils', function() {
157168 var node = mockTextSVGElement ( textCase ) ;
158169
159170 expect ( node . text ( ) ) . toEqual ( 'z' ) ;
160- assertAnchorAttrs ( node , 'y' ) ;
171+ assertAnchorAttrs ( node , { style : 'y' } ) ;
161172 assertAnchorLink ( node , 'x' ) ;
162173 } ) ;
163174 } ) ;
164175
176+ it ( 'accepts `target` with links and tries to translate it to `xlink:show`' , function ( ) {
177+ var specs = [
178+ { target : '_blank' , show : 'new' } ,
179+ { target : '_self' , show : 'replace' } ,
180+ { target : '_parent' , show : 'replace' } ,
181+ { target : '_top' , show : 'replace' } ,
182+ { target : 'some_frame_name' , show : 'new' }
183+ ] ;
184+ specs . forEach ( function ( spec ) {
185+ var node = mockTextSVGElement ( '<a href="x" target="' + spec . target + '">link</a>' ) ;
186+ assertAnchorLink ( node , 'x' , spec . target , spec . show , spec . target ) ;
187+ } ) ;
188+ } ) ;
189+
190+ it ( 'attaches onclick if popup is specified' , function ( ) {
191+ var node = mockTextSVGElement ( '<a href="x" target="fred" popup="width=500,height=400">link</a>' ) ;
192+ assertAnchorLink ( node , 'x' , 'fred' , 'new' ) ;
193+ assertAnchorAttrs ( node , { onclick : 'window.open(\'x\',\'fred\',\'width=500,height=400\');return false;' } ) ;
194+ } ) ;
195+
165196 it ( 'keeps query parameters in href' , function ( ) {
166197 var textCases = [
167198 '<a href="https://abc.com/myFeature.jsp?name=abc&pwd=def">abc.com?shared-key</a>' ,
@@ -171,9 +202,9 @@ describe('svg+text utils', function() {
171202 textCases . forEach ( function ( textCase ) {
172203 var node = mockTextSVGElement ( textCase ) ;
173204
174- assertAnchorAttrs ( node ) ;
175- expect ( node . text ( ) ) . toEqual ( 'abc.com?shared-key' ) ;
176- assertAnchorLink ( node , 'https://abc.com/myFeature.jsp?name=abc&pwd=def' ) ;
205+ assertAnchorAttrs ( node , { } , textCase ) ;
206+ expect ( node . text ( ) ) . toEqual ( 'abc.com?shared-key' , textCase ) ;
207+ assertAnchorLink ( node , 'https://abc.com/myFeature.jsp?name=abc&pwd=def' , undefined , undefined , textCase ) ;
177208 } ) ;
178209 } ) ;
179210
0 commit comments