From 9d0caf6de070145c4af618847b27e24c49027b8e Mon Sep 17 00:00:00 2001 From: Ely Lucas Date: Tue, 29 Oct 2019 10:36:28 -0600 Subject: [PATCH] fix(react): checking if node is actually an element before treating it like one, fixes #19769 (#19783) --- .../react/src/components/utils/attachProps.ts | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/packages/react/src/components/utils/attachProps.ts b/packages/react/src/components/utils/attachProps.ts index 4020413a9b8..0f7dfda9e45 100644 --- a/packages/react/src/components/utils/attachProps.ts +++ b/packages/react/src/components/utils/attachProps.ts @@ -1,33 +1,36 @@ import { camelToDashCase } from './case'; export const attachProps = (node: HTMLElement, newProps: any, oldProps: any = {}) => { - // add any classes in className to the class list - const className = getClassName(node.classList, newProps, oldProps); - if (className !== '') { - node.className = className; - } - - Object.keys(newProps).forEach(name => { - if (name === 'children' || name === 'style' || name === 'ref' || name === 'class' || name === 'className' || name === 'forwardedRef') { - return; + // some test frameworks don't render DOM elements, so we test here to make sure we are dealing with DOM first + if (node instanceof Element) { + // add any classes in className to the class list + const className = getClassName(node.classList, newProps, oldProps); + if (className !== '') { + node.className = className; } - if (name.indexOf('on') === 0 && name[2] === name[2].toUpperCase()) { - const eventName = name.substring(2); - const eventNameLc = eventName[0].toLowerCase() + eventName.substring(1); - if (!isCoveredByReact(eventNameLc)) { - syncEvent(node, eventNameLc, newProps[name]); + Object.keys(newProps).forEach(name => { + if (name === 'children' || name === 'style' || name === 'ref' || name === 'class' || name === 'className' || name === 'forwardedRef') { + return; } - } else { - (node as any)[name] = newProps[name]; - const propType = typeof newProps[name]; - if (propType === 'string') { - node.setAttribute(camelToDashCase(name), newProps[name]); + if (name.indexOf('on') === 0 && name[2] === name[2].toUpperCase()) { + const eventName = name.substring(2); + const eventNameLc = eventName[0].toLowerCase() + eventName.substring(1); + + if (!isCoveredByReact(eventNameLc)) { + syncEvent(node, eventNameLc, newProps[name]); + } } else { (node as any)[name] = newProps[name]; + const propType = typeof newProps[name]; + if (propType === 'string') { + node.setAttribute(camelToDashCase(name), newProps[name]); + } else { + (node as any)[name] = newProps[name]; + } } - } - }); + }); + } }; export const getClassName = (classList: DOMTokenList, newProps: any, oldProps: any) => {