File tree Expand file tree Collapse file tree 2 files changed +10
-7
lines changed Expand file tree Collapse file tree 2 files changed +10
-7
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,10 @@ export default function addClass(element, className) {
14
14
// svg
15
15
const pre = element . getAttribute ( "class" ) || "" ;
16
16
17
- element . setAttribute ( "class" , `${ pre } ${ className } ` ) ;
17
+ if ( ! pre . match ( className ) ) {
18
+ // check if element doesn't already have className
19
+ element . setAttribute ( "class" , `${ pre } ${ className } ` ) ;
20
+ }
18
21
} else {
19
22
if ( element . classList !== undefined ) {
20
23
// check for modern classList property
Original file line number Diff line number Diff line change @@ -17,21 +17,21 @@ describe("addClass", () => {
17
17
} ) ;
18
18
19
19
test ( "should append when element is SVG" , ( ) => {
20
- const el = document . createElement ( " svg" ) ;
21
- el . className = "firstClass" ;
20
+ const el = document . createElementNS ( 'http://www.w3.org/2000/%3C/span%3Esvg%3Cspan%20class="x x-first">' , 'svg' ) ;
21
+ el . setAttribute ( 'class' , "firstClass" ) ;
22
22
23
23
addClass ( el , "secondClass" ) ;
24
24
25
- expect ( el . className ) . toBe ( "firstClass secondClass" ) ;
25
+ expect ( el . getAttribute ( 'class' ) ) . toBe ( "firstClass secondClass" ) ;
26
26
} ) ;
27
27
28
28
test ( "should not append duplicate classNames to svg elements" , ( ) => {
29
- const el = document . createElement ( " svg" ) ;
30
- el . className = "firstClass" ;
29
+ const el = document . createElementNS ( 'http://www.w3.org/2000/%3C/span%3Esvg%3Cspan%20class="x x-first">' , 'svg' ) ;
30
+ el . setAttribute ( 'class' , "firstClass" ) ;
31
31
32
32
addClass ( el , "firstClass" ) ;
33
33
34
- expect ( el . className ) . toBe ( "firstClass" ) ;
34
+ expect ( el . getAttribute ( 'class' ) ) . toBe ( "firstClass" ) ;
35
35
} ) ;
36
36
37
37
test ( "should not append duplicate classNames to elements" , ( ) => {
You can’t perform that action at this time.
0 commit comments