Skip to content

Commit ff7445e

Browse files
josepharhareps1lon
andauthored
[DOM] Update HTML parser rules for new select parser (#34804)
Co-authored-by: Sebastian "Sebbie" Silbermann <[email protected]>
1 parent 2dc7da7 commit ff7445e

3 files changed

Lines changed: 17 additions & 39 deletions

File tree

‎packages/react-dom-bindings/src/client/validateDOMNesting.js‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ const inScopeTags = [
180180
'th',
181181
'marquee',
182182
'object',
183+
'select',
183184
'template',
184185

185186
// https://html.spec.whatwg.org/multipage/syntax.html#html-integration-point
@@ -306,22 +307,6 @@ function isTagValidWithParent(
306307
): boolean {
307308
// First, let's check if we're in an unusual parsing mode...
308309
switch (parentTag) {
309-
// https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-inselect
310-
case 'select':
311-
return (
312-
tag === 'hr' ||
313-
tag === 'option' ||
314-
tag === 'optgroup' ||
315-
tag === 'script' ||
316-
tag === 'template' ||
317-
tag === '#text'
318-
);
319-
case 'optgroup':
320-
return tag === 'option' || tag === '#text';
321-
// Strictly speaking, seeing an <option> doesn't mean we're in a <select>
322-
// but
323-
case 'option':
324-
return tag === '#text';
325310
// https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-intd
326311
// https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-incaption
327312
// No special behavior since these rules fall back to "in body" mode for
@@ -424,6 +409,9 @@ function isTagValidWithParent(
424409
case 'caption':
425410
case 'col':
426411
case 'colgroup':
412+
case 'input':
413+
// <input> causes open <select> tags to close.
414+
return parentTag !== 'select';
427415
case 'frameset':
428416
case 'frame':
429417
case 'tbody':

‎packages/react-dom/src/__tests__/ReactDOMOption-test.js‎

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,13 @@ describe('ReactDOMOption', () => {
4444
expect(container.firstChild.innerHTML).toBe('1 foo');
4545
});
4646

47-
it('should warn for invalid child tags', async () => {
47+
it('should not warn for a child div tag', async () => {
4848
const el = (
4949
<option value="12">
5050
{1} <div /> {2}
5151
</option>
5252
);
5353
const container = await renderIntoDocument(el);
54-
assertConsoleErrorDev([
55-
'In HTML, <div> cannot be a child of <option>.\n' +
56-
'This will cause a hydration error.\n' +
57-
'\n' +
58-
'> <option value="12">\n' +
59-
'> <div>\n' +
60-
' ...\n' +
61-
'\n' +
62-
' in div (at **)',
63-
]);
6454
expect(container.firstChild.innerHTML).toBe('1 <div></div> 2');
6555
await renderIntoDocument(el);
6656
});
@@ -238,7 +228,7 @@ describe('ReactDOMOption', () => {
238228
expect(node.selectedIndex).toEqual(2);
239229
});
240230

241-
it('generates a hydration error when an invalid nested tag is used as a child', async () => {
231+
it('does not generate a hydration error when a div tag is used as a child', async () => {
242232
const ref = React.createRef();
243233
const children = (
244234
<select readOnly={true} value="bar">
@@ -266,17 +256,6 @@ describe('ReactDOMOption', () => {
266256
onRecoverableError: () => {},
267257
});
268258
});
269-
assertConsoleErrorDev([
270-
'In HTML, <div> cannot be a child of <option>.\n' +
271-
'This will cause a hydration error.\n' +
272-
'\n' +
273-
' <select readOnly={true} value="bar">\n' +
274-
'> <option value="bar">\n' +
275-
'> <div ref={{current:null}}>\n' +
276-
' ...\n' +
277-
'\n' +
278-
' in div (at **)',
279-
]);
280259
option = container.firstChild.firstChild;
281260

282261
expect(option.textContent).toBe('BarFooBaz');

‎packages/react-dom/src/__tests__/validateDOMNesting-test.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ function expectWarnings(tags, warnings = []) {
5151
const Tag = tags.pop();
5252
if (Tag === '#text') {
5353
element = 'text';
54+
} else if (Tag === 'option') {
55+
element = <Tag value={'value'}>{element}</Tag>;
5456
} else {
5557
element = <Tag>{element}</Tag>;
5658
}
@@ -74,6 +76,7 @@ describe('validateDOMNesting', () => {
7476
expectWarnings(['div', 'p', 'button', 'p']);
7577
expectWarnings(['p', 'svg', 'foreignObject', 'p']);
7678
expectWarnings(['html', 'body', 'div']);
79+
expectWarnings(['select', 'div', 'optgroup', 'div', 'option', 'span']);
7780

7881
// Invalid, but not changed by browser parsing so we allow them
7982
expectWarnings(['div', 'ul', 'ul', 'li']);
@@ -202,6 +205,14 @@ describe('validateDOMNesting', () => {
202205
' in body (at **)',
203206
],
204207
);
208+
expectWarnings(
209+
['select', 'input'],
210+
[
211+
'In HTML, <input> cannot be a child of <select>.\n' +
212+
'This will cause a hydration error.\n' +
213+
' in input (at **)',
214+
],
215+
);
205216
});
206217

207218
it('relaxes the nesting rules at the root when the container is a singleton', () => {

0 commit comments

Comments
 (0)