Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support falsy data attributes #3720

Merged
merged 2 commits into from Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should invert these two conditions now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean switch it with the remove-path? I think we would add more code in differentiating 0 and empty string

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm good with the current way.

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