Skip to content

Commit a632d60

Browse files
phananyyx990803
authored andcommitted
Check and warn for falsy class names (fixes #4050) (#4051)
This commit adds a check for falsy names (null or empty string) before attempting to add or remove them, to prevent a DOM exception. A warning will also be triggered if in development env.
1 parent 8491857 commit a632d60

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/platforms/web/runtime/class-util.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
/* @flow */
22

3+
import { warn } from 'core/util/index'
4+
35
/**
46
* Add class with compatibility for SVG since classList is not supported on
57
* SVG elements in IE
68
*/
7-
export function addClass (el: Element, cls: string) {
9+
export function addClass (el: Element, cls: ?string) {
10+
if (!cls || cls.trim() === '') {
11+
process.env.NODE_ENV !== 'production' && warn('Ignoring empty class name.')
12+
return
13+
}
14+
815
/* istanbul ignore else */
916
if (el.classList) {
1017
if (cls.indexOf(' ') > -1) {
@@ -24,7 +31,12 @@ export function addClass (el: Element, cls: string) {
2431
* Remove class with compatibility for SVG since classList is not supported on
2532
* SVG elements in IE
2633
*/
27-
export function removeClass (el: Element, cls: string) {
34+
export function removeClass (el: Element, cls: ?string) {
35+
if (!cls || cls.trim() === '') {
36+
process.env.NODE_ENV !== 'production' && warn('Ignoring empty class name.')
37+
return
38+
}
39+
2840
/* istanbul ignore else */
2941
if (el.classList) {
3042
if (cls.indexOf(' ') > -1) {

0 commit comments

Comments
 (0)