|
|
@@ -1,26 +1,8 @@
|
|
|
/* @flow */
|
|
|
|
|
|
-import { isIE9, namespaceMap } from 'web/util/index'
|
|
|
-
|
|
|
-const svgNS = namespaceMap.svg
|
|
|
-
|
|
|
-/**
|
|
|
- * In IE9, setAttribute('class') will result in empty class
|
|
|
- * if the element also has the :class attribute; However in
|
|
|
- * PhantomJS, setting `className` does not work on SVG elements...
|
|
|
- * So we have to do a conditional check here.
|
|
|
- */
|
|
|
-export function setClass (el: Element, cls: string) {
|
|
|
- /* istanbul ignore else */
|
|
|
- if (!isIE9 || el.namespaceURI === svgNS) {
|
|
|
- el.setAttribute('class', cls)
|
|
|
- } else {
|
|
|
- el.className = cls
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
/**
|
|
|
- * Add class with compatibility for IE & SVG
|
|
|
+ * Add class with compatibility for SVG since classList is not supported on
|
|
|
+ * SVG elements in IE
|
|
|
*/
|
|
|
export function addClass (el: Element, cls: string) {
|
|
|
if (el.classList) {
|
|
|
@@ -30,15 +12,16 @@ export function addClass (el: Element, cls: string) {
|
|
|
el.classList.add(cls)
|
|
|
}
|
|
|
} else {
|
|
|
- const cur = ' ' + getClass(el) + ' '
|
|
|
+ const cur = ' ' + el.getAttribute('class') + ' '
|
|
|
if (cur.indexOf(' ' + cls + ' ') < 0) {
|
|
|
- setClass(el, (cur + cls).trim())
|
|
|
+ el.setAttribute('class', (cur + cls).trim())
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Remove class with compatibility for IE & SVG
|
|
|
+ * Remove class with compatibility for SVG since classList is not supported on
|
|
|
+ * SVG elements in IE
|
|
|
*/
|
|
|
export function removeClass (el: Element, cls: string) {
|
|
|
if (el.classList) {
|
|
|
@@ -48,27 +31,11 @@ export function removeClass (el: Element, cls: string) {
|
|
|
el.classList.remove(cls)
|
|
|
}
|
|
|
} else {
|
|
|
- let cur = ' ' + getClass(el) + ' '
|
|
|
+ let cur = ' ' + el.getAttribute('class') + ' '
|
|
|
const tar = ' ' + cls + ' '
|
|
|
while (cur.indexOf(tar) >= 0) {
|
|
|
cur = cur.replace(tar, ' ')
|
|
|
}
|
|
|
- setClass(el, cur.trim())
|
|
|
- }
|
|
|
- if (!el.className) {
|
|
|
- el.removeAttribute('class')
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * For IE9 compat: when both class and :class are present
|
|
|
- * getAttribute('class') returns wrong value... but className
|
|
|
- * on SVG elements returns an object.
|
|
|
- */
|
|
|
-function getClass (el: Element): string {
|
|
|
- let classname = el.className
|
|
|
- if (typeof classname === 'object') {
|
|
|
- classname = classname.baseVal || ''
|
|
|
+ el.setAttribute('class', cur.trim())
|
|
|
}
|
|
|
- return classname
|
|
|
}
|