Skip to content

Commit

Permalink
Merge pull request #3720 from preactjs/support-falsy-data
Browse files Browse the repository at this point in the history
support falsy data attributes
  • Loading branch information
marvinhagemeister committed Sep 13, 2022
2 parents a8e4c99 + e159f84 commit d7a433e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
10 changes: 10 additions & 0 deletions compat/test/browser/render.test.js
Expand Up @@ -475,6 +475,16 @@ describe('compat render', () => {
expect(updateSpy).to.not.be.calledOnce;
});

it('should support false aria-* attributes', () => {
render(<div aria-checked={false} />, scratch);
expect(scratch.firstChild.getAttribute('aria-checked')).to.equal('false');
});

it('should support false data-* attributes', () => {
render(<div data-checked={false} />, scratch);
expect(scratch.firstChild.getAttribute('data-checked')).to.equal('false');
});

it("should support react-relay's usage of __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED", () => {
const Ctx = createContext('foo');

Expand Down
5 changes: 1 addition & 4 deletions src/diff/props.js
Expand Up @@ -133,10 +133,7 @@ export function setProperty(dom, name, value, oldValue, isSvg) {

if (typeof value === 'function') {
// never serialize functions as attribute values
} else if (
value != null &&
(value !== false || (name[0] === 'a' && name[1] === 'r'))
) {
} else if (value != null && (value !== false || name.indexOf('-') != -1)) {
dom.setAttribute(name, value);
} else {
dom.removeAttribute(name);
Expand Down
12 changes: 11 additions & 1 deletion test/browser/render.test.js
Expand Up @@ -462,11 +462,21 @@ describe('render()', () => {
expect(scratch.childNodes[0]).to.have.property('className', 'bar');
});

it('should support false aria-* attributes', () => {
it('should support false string aria-* attributes', () => {
render(<div aria-checked="false" />, scratch);
expect(scratch.firstChild.getAttribute('aria-checked')).to.equal('false');
});

it('should support false aria-* attributes', () => {
render(<div aria-checked={false} />, scratch);
expect(scratch.firstChild.getAttribute('aria-checked')).to.equal('false');
});

it('should support false data-* attributes', () => {
render(<div data-checked={false} />, scratch);
expect(scratch.firstChild.getAttribute('data-checked')).to.equal('false');
});

it('should set checked attribute on custom elements without checked property', () => {
render(<o-checkbox checked />, scratch);
expect(scratch.innerHTML).to.equal(
Expand Down

0 comments on commit d7a433e

Please sign in to comment.