Skip to content

Commit

Permalink
Fix both class and className being enumerable (#2280)
Browse files Browse the repository at this point in the history
* Fix both class and className being enumerable

* Use loose equality (-1 B)
  • Loading branch information
marvinhagemeister authored and JoviDeCroock committed Jan 26, 2020
1 parent 14d8523 commit cfaa1ca
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compat/src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ options.vnode = vnode => {
let props = vnode.props;

// Alias `class` prop to `className` if available
if (props.class || props.className) {
if (props.class != props.className) {
classNameDescriptor.enumerable = 'className' in props;
if (props.className) props.class = props.className;
Object.defineProperty(props, 'className', classNameDescriptor);
Expand Down
48 changes: 47 additions & 1 deletion compat/test/browser/render.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { createElement, render } from 'preact/compat';
import React, { createElement, render, Component } from 'preact/compat';
import { setupRerender } from 'preact/test-utils';
import {
setupScratch,
teardown,
Expand All @@ -9,11 +10,15 @@ describe('compat render', () => {
/** @type {HTMLDivElement} */
let scratch;

/** @type {() => void} */
let rerender;

const ce = type => document.createElement(type);
const text = text => document.createTextNode(text);

beforeEach(() => {
scratch = setupScratch();
rerender = setupRerender();
});

afterEach(() => {
Expand Down Expand Up @@ -150,4 +155,45 @@ describe('compat render', () => {
render(<Foo fontSize="xlarge" className="new" />, scratch);
expect(scratch.firstChild.className).to.equal('new');
});

// Issue #2224
it('should not mark both class and className as enumerable', () => {
function ClassNameCheck(props) {
return (
<div>{props.propertyIsEnumerable('className') ? 'Failed' : ''}</div>
);
}

let update;
class OtherThing extends Component {
render({ children }) {
update = () => this.forceUpdate();
return (
<div>
{children}
<ClassNameCheck class="test" />
</div>
);
}
}

function App() {
return (
<OtherThing>
<ClassNameCheck class="test" />
</OtherThing>
);
}

render(<App />, scratch);

update();
rerender();

console.log(scratch.textContent);
expect(/Failed/g.test(scratch.textContent)).to.equal(
false,
'not enumerable'
);
});
});

0 comments on commit cfaa1ca

Please sign in to comment.