1+ /**
2+ * @typedef {{
3+ * tag: string;
4+ * custom_element: boolean;
5+ * }} Element
6+ */
7+
18/**
29 * Map of elements that have certain elements that are not allowed inside them, in the sense that they will auto-close the parent/ancestor element.
310 * Theoretically one could take advantage of it but most of the time it will just result in confusing behavior and break when SSR'd.
@@ -137,33 +144,33 @@ const disallowed_children = {
137144/**
138145 * Returns an error message if the tag is not allowed inside the ancestor tag (which is grandparent and above) such that it will result
139146 * in the browser repairing the HTML, which will likely result in an error during hydration.
140- * @param {string } child_tag
141- * @param {string [] } ancestors All nodes starting with the parent, up until the ancestor, which means two entries minimum
147+ * @param {Element } child_node
148+ * @param {Element [] } ancestors All nodes starting with the parent, up until the ancestor, which means two entries minimum
142149 * @param {string } [child_loc]
143150 * @param {string } [ancestor_loc]
144151 * @returns {string | null }
145152 */
146- export function is_tag_valid_with_ancestor ( child_tag , ancestors , child_loc , ancestor_loc ) {
147- if ( child_tag . includes ( '-' ) ) return null ; // custom elements can be anything
153+ export function is_tag_valid_with_ancestor ( child_node , ancestors , child_loc , ancestor_loc ) {
154+ if ( child_node . custom_element ) return null ; // custom elements can be anything
148155
149- const ancestor_tag = ancestors [ ancestors . length - 1 ] ;
156+ const ancestor_tag = ancestors [ ancestors . length - 1 ] . tag ;
150157 const disallowed = disallowed_children [ ancestor_tag ] ;
151158 if ( ! disallowed ) return null ;
152159
153160 if ( 'reset_by' in disallowed && disallowed . reset_by ) {
154161 for ( let i = ancestors . length - 2 ; i >= 0 ; i -- ) {
155162 const ancestor = ancestors [ i ] ;
156- if ( ancestor . includes ( '-' ) ) return null ; // custom elements can be anything
163+ if ( ancestor . custom_element ) return null ; // custom elements can be anything
157164
158165 // A reset means that forbidden descendants are allowed again
159- if ( disallowed . reset_by . includes ( ancestors [ i ] ) ) {
166+ if ( disallowed . reset_by . includes ( ancestors [ i ] . tag ) ) {
160167 return null ;
161168 }
162169 }
163170 }
164171
165- if ( 'descendant' in disallowed && disallowed . descendant . includes ( child_tag ) ) {
166- const child = child_loc ? `\`<${ child_tag } >\` (${ child_loc } )` : `\`<${ child_tag } >\`` ;
172+ if ( 'descendant' in disallowed && disallowed . descendant . includes ( child_node . tag ) ) {
173+ const child = child_loc ? `\`<${ child_node . tag } >\` (${ child_loc } )` : `\`<${ child_node . tag } >\`` ;
167174 const ancestor = ancestor_loc
168175 ? `\`<${ ancestor_tag } >\` (${ ancestor_loc } )`
169176 : `\`<${ ancestor_tag } >\`` ;
@@ -177,36 +184,38 @@ export function is_tag_valid_with_ancestor(child_tag, ancestors, child_loc, ance
177184/**
178185 * Returns an error message if the tag is not allowed inside the parent tag such that it will result
179186 * in the browser repairing the HTML, which will likely result in an error during hydration.
180- * @param {string } child_tag
181- * @param {string } parent_tag
187+ * @param {Element } child_node
188+ * @param {Element } parent_node
182189 * @param {string } [child_loc]
183190 * @param {string } [parent_loc]
184191 * @returns {string | null }
185192 */
186- export function is_tag_valid_with_parent ( child_tag , parent_tag , child_loc , parent_loc ) {
187- if ( child_tag . includes ( '-' ) || parent_tag ?. includes ( '-' ) ) return null ; // custom elements can be anything
193+ export function is_tag_valid_with_parent ( child_node , parent_node , child_loc , parent_loc ) {
194+ if ( child_node . custom_element || parent_node ?. custom_element ) return null ; // custom elements can be anything
188195
189- if ( parent_tag === 'template' ) return null ; // no errors or warning should be thrown in immediate children of template tags
196+ if ( parent_node . tag === 'template' ) return null ; // no errors or warning should be thrown in immediate children of template tags
190197
191- const disallowed = disallowed_children [ parent_tag ] ;
198+ const disallowed = disallowed_children [ parent_node . tag ] ;
192199
193- const child = child_loc ? `\`<${ child_tag } >\` (${ child_loc } )` : `\`<${ child_tag } >\`` ;
194- const parent = parent_loc ? `\`<${ parent_tag } >\` (${ parent_loc } )` : `\`<${ parent_tag } >\`` ;
200+ const child = child_loc ? `\`<${ child_node . tag } >\` (${ child_loc } )` : `\`<${ child_node . tag } >\`` ;
201+ const parent = parent_loc
202+ ? `\`<${ parent_node . tag } >\` (${ parent_loc } )`
203+ : `\`<${ parent_node . tag } >\`` ;
195204
196205 if ( disallowed ) {
197- if ( 'direct' in disallowed && disallowed . direct . includes ( child_tag ) ) {
206+ if ( 'direct' in disallowed && disallowed . direct . includes ( child_node . tag ) ) {
198207 return `${ child } cannot be a direct child of ${ parent } ` ;
199208 }
200209
201- if ( 'descendant' in disallowed && disallowed . descendant . includes ( child_tag ) ) {
210+ if ( 'descendant' in disallowed && disallowed . descendant . includes ( child_node . tag ) ) {
202211 return `${ child } cannot be a child of ${ parent } ` ;
203212 }
204213
205214 if ( 'only' in disallowed && disallowed . only ) {
206- if ( disallowed . only . includes ( child_tag ) ) {
215+ if ( disallowed . only . includes ( child_node . tag ) ) {
207216 return null ;
208217 } else {
209- return `${ child } cannot be a child of ${ parent } . \`<${ parent_tag } >\` only allows these children: ${ disallowed . only . map ( ( d ) => `\`<${ d } >\`` ) . join ( ', ' ) } ` ;
218+ return `${ child } cannot be a child of ${ parent } . \`<${ parent_node . tag } >\` only allows these children: ${ disallowed . only . map ( ( d ) => `\`<${ d } >\`` ) . join ( ', ' ) } ` ;
210219 }
211220 }
212221 }
@@ -215,7 +224,7 @@ export function is_tag_valid_with_parent(child_tag, parent_tag, child_loc, paren
215224 // parsing rules - if we're down here, then none of those matched and
216225 // so we allow it only if we don't know what the parent is, as all other
217226 // cases are invalid (and we only get into this function if we know the parent).
218- switch ( child_tag ) {
227+ switch ( child_node . tag ) {
219228 case 'body' :
220229 case 'caption' :
221230 case 'col' :
0 commit comments