Skip to content

Commit 4c3470e

Browse files
authored
Refactor DOM attribute code (take two) (#11815)
* Harden tests around init/addition/update/removal of aliased attributes I noticed some patterns weren't being tested. * Call setValueForProperty() for null and undefined The branching before the call is unnecessary because setValueForProperty() already has an internal branch that delegates to deleteValueForProperty() for null and undefined through the shouldIgnoreValue() check. The goal is to start unifying these methods because their separation doesn't reflect the current behavior (e.g. for unknown properties) anymore, and obscures what actually happens with different inputs. * Inline deleteValueForProperty() into setValueForProperty() Now we don't read propertyInfo twice in this case. I also dropped a few early returns. I added them a while ago when we had Stack-only tracking of DOM operations, and some operations were being counted twice because of how this code is structured. This isn't a problem anymore (both because we don't track operations, and because I've just inlined this method call). * Inline deleteValueForAttribute() into setValueForAttribute() The special cases for null and undefined already exist in setValueForAttribute(). * Delete some dead code * Make setValueForAttribute() a branch of setValueForProperty() Their naming is pretty confusing by now. For example setValueForProperty() calls setValueForAttribute() when shouldSetAttribute() is false (!). I want to refactor (as in, inline and then maybe factor it out differently) the relation between them. For now, I'm consolidating the callers to use setValueForProperty(). * Make it more obvious where we skip and when we reset attributes The naming of these methods is still very vague and conflicting in some cases. Will need further work. * Rewrite setValueForProperty() with early exits This makes the flow clearer in my opinion. * Move shouldIgnoreValue() into DOMProperty It was previously duplicated. It's also suspiciously similar in purpose to shouldTreatAttributeValueAsNull() so I want to see if there is a way to unify them. * Use more specific methods for testing validity * Unify shouldTreatAttributeValueAsNull() and shouldIgnoreValue() * Remove shouldSetAttribute() Its naming was confusing and it was used all over the place instead of more specific checks. Now that we only have one call site, we might as well inline and get rid of it. * Remove unnecessary condition * Remove another unnecessary condition * Add Flow coverage * Oops * Fix lint (ESLint complains about Flow suppression) * Fix treatment of Symbol/Function values on boolean attributes They weren't being properly skipped because of the early return. I added tests for this case. * Avoid getPropertyInfo() calls I think this PR looks worse on benchmarks because we have to read propertyInfo in different places. Originally I tried to get rid of propertyInfo, but looks like it's important for performance after all. So now I'm going into the opposite direction, and precompute propertyInfo as early as possible, and then just pass it around. This way we can avoid extra lookups but keep functions nice and modular. * Pass propertyInfo as argument to getValueForProperty() It always exists because this function is only called for known properties. * Make it clearer this branch is boolean-specific I wrote this and then got confused myself. * Memoize whether propertyInfo accepts boolean value Since we run these checks for all booleans, might as well remember it. * Fix a crash when numeric property is given a Symbol * Record attribute table The changes reflect that SSR doesn't crash with symbols anymore (and just warns, consistently with the client). * Refactor attribute initialization Instead of using flags, explicitly group similar attributes/properties. * Optimization: we know built-in attributes are never invalid * Use strict comparison * Rename methods for clarity * Lint nit * Minor tweaks * Document all the different attribute types
1 parent abe0faf commit 4c3470e

File tree

9 files changed

+913
-737
lines changed

9 files changed

+913
-737
lines changed

fixtures/attribute-behavior/AttributeTableSnapshot.md

Lines changed: 243 additions & 243 deletions
Large diffs are not rendered by default.

packages/react-dom/src/__tests__/ReactDOMComponent-test.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,97 @@ describe('ReactDOMComponent', () => {
442442
expect(container.firstChild.className).toEqual('');
443443
});
444444

445+
it('should not set null/undefined attributes', () => {
446+
var container = document.createElement('div');
447+
// Initial render.
448+
ReactDOM.render(<img src={null} data-foo={undefined} />, container);
449+
var node = container.firstChild;
450+
expect(node.hasAttribute('src')).toBe(false);
451+
expect(node.hasAttribute('data-foo')).toBe(false);
452+
// Update in one direction.
453+
ReactDOM.render(<img src={undefined} data-foo={null} />, container);
454+
expect(node.hasAttribute('src')).toBe(false);
455+
expect(node.hasAttribute('data-foo')).toBe(false);
456+
// Update in another direction.
457+
ReactDOM.render(<img src={null} data-foo={undefined} />, container);
458+
expect(node.hasAttribute('src')).toBe(false);
459+
expect(node.hasAttribute('data-foo')).toBe(false);
460+
// Removal.
461+
ReactDOM.render(<img />, container);
462+
expect(node.hasAttribute('src')).toBe(false);
463+
expect(node.hasAttribute('data-foo')).toBe(false);
464+
// Addition.
465+
ReactDOM.render(<img src={undefined} data-foo={null} />, container);
466+
expect(node.hasAttribute('src')).toBe(false);
467+
expect(node.hasAttribute('data-foo')).toBe(false);
468+
});
469+
470+
it('should apply React-specific aliases to HTML elements', () => {
471+
var container = document.createElement('div');
472+
ReactDOM.render(<form acceptCharset="foo" />, container);
473+
var node = container.firstChild;
474+
// Test attribute initialization.
475+
expect(node.getAttribute('accept-charset')).toBe('foo');
476+
expect(node.hasAttribute('acceptCharset')).toBe(false);
477+
// Test attribute update.
478+
ReactDOM.render(<form acceptCharset="boo" />, container);
479+
expect(node.getAttribute('accept-charset')).toBe('boo');
480+
expect(node.hasAttribute('acceptCharset')).toBe(false);
481+
// Test attribute removal by setting to null.
482+
ReactDOM.render(<form acceptCharset={null} />, container);
483+
expect(node.hasAttribute('accept-charset')).toBe(false);
484+
expect(node.hasAttribute('acceptCharset')).toBe(false);
485+
// Restore.
486+
ReactDOM.render(<form acceptCharset="foo" />, container);
487+
expect(node.getAttribute('accept-charset')).toBe('foo');
488+
expect(node.hasAttribute('acceptCharset')).toBe(false);
489+
// Test attribute removal by setting to undefined.
490+
ReactDOM.render(<form acceptCharset={undefined} />, container);
491+
expect(node.hasAttribute('accept-charset')).toBe(false);
492+
expect(node.hasAttribute('acceptCharset')).toBe(false);
493+
// Restore.
494+
ReactDOM.render(<form acceptCharset="foo" />, container);
495+
expect(node.getAttribute('accept-charset')).toBe('foo');
496+
expect(node.hasAttribute('acceptCharset')).toBe(false);
497+
// Test attribute removal.
498+
ReactDOM.render(<form />, container);
499+
expect(node.hasAttribute('accept-charset')).toBe(false);
500+
expect(node.hasAttribute('acceptCharset')).toBe(false);
501+
});
502+
503+
it('should apply React-specific aliases to SVG elements', () => {
504+
var container = document.createElement('div');
505+
ReactDOM.render(<svg arabicForm="foo" />, container);
506+
var node = container.firstChild;
507+
// Test attribute initialization.
508+
expect(node.getAttribute('arabic-form')).toBe('foo');
509+
expect(node.hasAttribute('arabicForm')).toBe(false);
510+
// Test attribute update.
511+
ReactDOM.render(<svg arabicForm="boo" />, container);
512+
expect(node.getAttribute('arabic-form')).toBe('boo');
513+
expect(node.hasAttribute('arabicForm')).toBe(false);
514+
// Test attribute removal by setting to null.
515+
ReactDOM.render(<svg arabicForm={null} />, container);
516+
expect(node.hasAttribute('arabic-form')).toBe(false);
517+
expect(node.hasAttribute('arabicForm')).toBe(false);
518+
// Restore.
519+
ReactDOM.render(<svg arabicForm="foo" />, container);
520+
expect(node.getAttribute('arabic-form')).toBe('foo');
521+
expect(node.hasAttribute('arabicForm')).toBe(false);
522+
// Test attribute removal by setting to undefined.
523+
ReactDOM.render(<svg arabicForm={undefined} />, container);
524+
expect(node.hasAttribute('arabic-form')).toBe(false);
525+
expect(node.hasAttribute('arabicForm')).toBe(false);
526+
// Restore.
527+
ReactDOM.render(<svg arabicForm="foo" />, container);
528+
expect(node.getAttribute('arabic-form')).toBe('foo');
529+
expect(node.hasAttribute('arabicForm')).toBe(false);
530+
// Test attribute removal.
531+
ReactDOM.render(<svg />, container);
532+
expect(node.hasAttribute('arabic-form')).toBe(false);
533+
expect(node.hasAttribute('arabicForm')).toBe(false);
534+
});
535+
445536
it('should properly update custom attributes on custom elements', () => {
446537
const container = document.createElement('div');
447538
ReactDOM.render(<some-custom-element foo="bar" />, container);
@@ -451,6 +542,25 @@ describe('ReactDOMComponent', () => {
451542
expect(node.getAttribute('bar')).toBe('buzz');
452543
});
453544

545+
it('should not apply React-specific aliases to custom elements', () => {
546+
var container = document.createElement('div');
547+
ReactDOM.render(<some-custom-element arabicForm="foo" />, container);
548+
var node = container.firstChild;
549+
// Should not get transformed to arabic-form as SVG would be.
550+
expect(node.getAttribute('arabicForm')).toBe('foo');
551+
expect(node.hasAttribute('arabic-form')).toBe(false);
552+
// Test attribute update.
553+
ReactDOM.render(<some-custom-element arabicForm="boo" />, container);
554+
expect(node.getAttribute('arabicForm')).toBe('boo');
555+
// Test attribute removal and addition.
556+
ReactDOM.render(<some-custom-element acceptCharset="buzz" />, container);
557+
// Verify the previous attribute was removed.
558+
expect(node.hasAttribute('arabicForm')).toBe(false);
559+
// Should not get transformed to accept-charset as HTML would be.
560+
expect(node.getAttribute('acceptCharset')).toBe('buzz');
561+
expect(node.hasAttribute('accept-charset')).toBe(false);
562+
});
563+
454564
it('should clear a single style prop when changing `style`', () => {
455565
let styles = {display: 'none', color: 'red'};
456566
const container = document.createElement('div');

packages/react-dom/src/__tests__/ReactDOMServerIntegrationAttributes-test.js

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ describe('ReactDOMServerIntegration', () => {
6262
const e = await render(<div width={null} />);
6363
expect(e.hasAttribute('width')).toBe(false);
6464
});
65+
66+
itRenders('no string prop with function value', async render => {
67+
const e = await render(<div width={function() {}} />, 1);
68+
expect(e.hasAttribute('width')).toBe(false);
69+
});
70+
71+
itRenders('no string prop with symbol value', async render => {
72+
const e = await render(<div width={Symbol('foo')} />, 1);
73+
expect(e.hasAttribute('width')).toBe(false);
74+
});
6575
});
6676

6777
describe('boolean properties', function() {
@@ -122,6 +132,16 @@ describe('ReactDOMServerIntegration', () => {
122132
const e = await render(<div hidden={null} />);
123133
expect(e.hasAttribute('hidden')).toBe(false);
124134
});
135+
136+
itRenders('no boolean prop with function value', async render => {
137+
const e = await render(<div hidden={function() {}} />, 1);
138+
expect(e.hasAttribute('hidden')).toBe(false);
139+
});
140+
141+
itRenders('no boolean prop with symbol value', async render => {
142+
const e = await render(<div hidden={Symbol('foo')} />, 1);
143+
expect(e.hasAttribute('hidden')).toBe(false);
144+
});
125145
});
126146

127147
describe('download property (combined boolean/string attribute)', function() {
@@ -164,6 +184,16 @@ describe('ReactDOMServerIntegration', () => {
164184
const e = await render(<div download={undefined} />);
165185
expect(e.hasAttribute('download')).toBe(false);
166186
});
187+
188+
itRenders('no download prop with function value', async render => {
189+
const e = await render(<div download={function() {}} />, 1);
190+
expect(e.hasAttribute('download')).toBe(false);
191+
});
192+
193+
itRenders('no download prop with symbol value', async render => {
194+
const e = await render(<div download={Symbol('foo')} />, 1);
195+
expect(e.hasAttribute('download')).toBe(false);
196+
});
167197
});
168198

169199
describe('className property', function() {
@@ -257,6 +287,11 @@ describe('ReactDOMServerIntegration', () => {
257287
},
258288
);
259289

290+
itRenders('numeric property with zero value', async render => {
291+
const e = await render(<ol start={0} />);
292+
expect(e.getAttribute('start')).toBe('0');
293+
});
294+
260295
itRenders(
261296
'no positive numeric property with zero value',
262297
async render => {
@@ -265,9 +300,27 @@ describe('ReactDOMServerIntegration', () => {
265300
},
266301
);
267302

268-
itRenders('numeric property with zero value', async render => {
269-
const e = await render(<ol start={0} />);
270-
expect(e.getAttribute('start')).toBe('0');
303+
itRenders('no numeric prop with function value', async render => {
304+
const e = await render(<ol start={function() {}} />, 1);
305+
expect(e.hasAttribute('start')).toBe(false);
306+
});
307+
308+
itRenders('no numeric prop with symbol value', async render => {
309+
const e = await render(<ol start={Symbol('foo')} />, 1);
310+
expect(e.hasAttribute('start')).toBe(false);
311+
});
312+
313+
itRenders(
314+
'no positive numeric prop with function value',
315+
async render => {
316+
const e = await render(<input size={function() {}} />, 1);
317+
expect(e.hasAttribute('size')).toBe(false);
318+
},
319+
);
320+
321+
itRenders('no positive numeric prop with symbol value', async render => {
322+
const e = await render(<input size={Symbol('foo')} />, 1);
323+
expect(e.hasAttribute('size')).toBe(false);
271324
});
272325
});
273326

0 commit comments

Comments
 (0)