Skip to content

Commit

Permalink
Check and warn for falsy class names (fixes vuejs#4050) (vuejs#4051)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phanan authored and yyx990803 committed Nov 1, 2016
1 parent 8491857 commit a632d60
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/platforms/web/runtime/class-util.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/* @flow */

import { warn } from 'core/util/index'

/**
* Add class with compatibility for SVG since classList is not supported on
* SVG elements in IE
*/
export function addClass (el: Element, cls: string) {
export function addClass (el: Element, cls: ?string) {
if (!cls || cls.trim() === '') {
process.env.NODE_ENV !== 'production' && warn('Ignoring empty class name.')
return
}

/* istanbul ignore else */
if (el.classList) {
if (cls.indexOf(' ') > -1) {
Expand All @@ -24,7 +31,12 @@ export function addClass (el: Element, cls: string) {
* Remove class with compatibility for SVG since classList is not supported on
* SVG elements in IE
*/
export function removeClass (el: Element, cls: string) {
export function removeClass (el: Element, cls: ?string) {
if (!cls || cls.trim() === '') {
process.env.NODE_ENV !== 'production' && warn('Ignoring empty class name.')
return
}

/* istanbul ignore else */
if (el.classList) {
if (cls.indexOf(' ') > -1) {
Expand Down

0 comments on commit a632d60

Please sign in to comment.