1- /**
2- * @typedef {{
3- * tag: string;
4- * custom_element: boolean;
5- * }} Element
6- */
7-
81/**
92 * 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.
103 * Theoretically one could take advantage of it but most of the time it will just result in confusing behavior and break when SSR'd.
@@ -144,33 +137,33 @@ const disallowed_children = {
144137/**
145138 * Returns an error message if the tag is not allowed inside the ancestor tag (which is grandparent and above) such that it will result
146139 * in the browser repairing the HTML, which will likely result in an error during hydration.
147- * @param {Element } child_node
148- * @param {Element [] } ancestors All nodes starting with the parent, up until the ancestor, which means two entries minimum
140+ * @param {string } child_tag
141+ * @param {string [] } ancestors All nodes starting with the parent, up until the ancestor, which means two entries minimum
149142 * @param {string } [child_loc]
150143 * @param {string } [ancestor_loc]
151144 * @returns {string | null }
152145 */
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
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
155148
156- const ancestor_tag = ancestors [ ancestors . length - 1 ] . tag ;
149+ const ancestor_tag = ancestors [ ancestors . length - 1 ] ;
157150 const disallowed = disallowed_children [ ancestor_tag ] ;
158151 if ( ! disallowed ) return null ;
159152
160153 if ( 'reset_by' in disallowed && disallowed . reset_by ) {
161154 for ( let i = ancestors . length - 2 ; i >= 0 ; i -- ) {
162155 const ancestor = ancestors [ i ] ;
163- if ( ancestor . custom_element ) return null ; // custom elements can be anything
156+ if ( ancestor . includes ( '-' ) ) return null ; // custom elements can be anything
164157
165158 // A reset means that forbidden descendants are allowed again
166- if ( disallowed . reset_by . includes ( ancestors [ i ] . tag ) ) {
159+ if ( disallowed . reset_by . includes ( ancestors [ i ] ) ) {
167160 return null ;
168161 }
169162 }
170163 }
171164
172- if ( 'descendant' in disallowed && disallowed . descendant . includes ( child_node . tag ) ) {
173- const child = child_loc ? `\`<${ child_node . tag } >\` (${ child_loc } )` : `\`<${ child_node . tag } >\`` ;
165+ if ( 'descendant' in disallowed && disallowed . descendant . includes ( child_tag ) ) {
166+ const child = child_loc ? `\`<${ child_tag } >\` (${ child_loc } )` : `\`<${ child_tag } >\`` ;
174167 const ancestor = ancestor_loc
175168 ? `\`<${ ancestor_tag } >\` (${ ancestor_loc } )`
176169 : `\`<${ ancestor_tag } >\`` ;
@@ -184,38 +177,36 @@ export function is_tag_valid_with_ancestor(child_node, ancestors, child_loc, anc
184177/**
185178 * Returns an error message if the tag is not allowed inside the parent tag such that it will result
186179 * in the browser repairing the HTML, which will likely result in an error during hydration.
187- * @param {Element } child_node
188- * @param {Element } parent_node
180+ * @param {string } child_tag
181+ * @param {string } parent_tag
189182 * @param {string } [child_loc]
190183 * @param {string } [parent_loc]
191184 * @returns {string | null }
192185 */
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
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
195188
196- if ( parent_node . tag === 'template' ) return null ; // no errors or warning should be thrown in immediate children of template tags
189+ if ( parent_tag === 'template' ) return null ; // no errors or warning should be thrown in immediate children of template tags
197190
198- const disallowed = disallowed_children [ parent_node . tag ] ;
191+ const disallowed = disallowed_children [ parent_tag ] ;
199192
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 } >\`` ;
193+ const child = child_loc ? `\`<${ child_tag } >\` (${ child_loc } )` : `\`<${ child_tag } >\`` ;
194+ const parent = parent_loc ? `\`<${ parent_tag } >\` (${ parent_loc } )` : `\`<${ parent_tag } >\`` ;
204195
205196 if ( disallowed ) {
206- if ( 'direct' in disallowed && disallowed . direct . includes ( child_node . tag ) ) {
197+ if ( 'direct' in disallowed && disallowed . direct . includes ( child_tag ) ) {
207198 return `${ child } cannot be a direct child of ${ parent } ` ;
208199 }
209200
210- if ( 'descendant' in disallowed && disallowed . descendant . includes ( child_node . tag ) ) {
201+ if ( 'descendant' in disallowed && disallowed . descendant . includes ( child_tag ) ) {
211202 return `${ child } cannot be a child of ${ parent } ` ;
212203 }
213204
214205 if ( 'only' in disallowed && disallowed . only ) {
215- if ( disallowed . only . includes ( child_node . tag ) ) {
206+ if ( disallowed . only . includes ( child_tag ) ) {
216207 return null ;
217208 } else {
218- return `${ child } cannot be a child of ${ parent } . \`<${ parent_node . tag } >\` only allows these children: ${ disallowed . only . map ( ( d ) => `\`<${ d } >\`` ) . join ( ', ' ) } ` ;
209+ return `${ child } cannot be a child of ${ parent } . \`<${ parent_tag } >\` only allows these children: ${ disallowed . only . map ( ( d ) => `\`<${ d } >\`` ) . join ( ', ' ) } ` ;
219210 }
220211 }
221212 }
@@ -224,7 +215,7 @@ export function is_tag_valid_with_parent(child_node, parent_node, child_loc, par
224215 // parsing rules - if we're down here, then none of those matched and
225216 // so we allow it only if we don't know what the parent is, as all other
226217 // cases are invalid (and we only get into this function if we know the parent).
227- switch ( child_node . tag ) {
218+ switch ( child_tag ) {
228219 case 'body' :
229220 case 'caption' :
230221 case 'col' :
0 commit comments