From a632d601476d582c612de90dfcbade1b372012ce Mon Sep 17 00:00:00 2001 From: Phan An Date: Tue, 1 Nov 2016 22:30:43 +0700 Subject: [PATCH] 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. --- src/platforms/web/runtime/class-util.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/platforms/web/runtime/class-util.js b/src/platforms/web/runtime/class-util.js index 0c166ab4..fa32283a 100644 --- a/src/platforms/web/runtime/class-util.js +++ b/src/platforms/web/runtime/class-util.js @@ -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) { @@ -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) {