@@ -111,10 +111,16 @@ const HTML_CSS_TAG_ATTRIBUTES = String.raw`(?:${HTML_CSS_ATTRIBUTE_VALUE}|[^>])*
111111const HTML_TEXT_TAG_ATTRIBUTES = String . raw `(?:${ HTML_CSS_ATTRIBUTE_VALUE } |/(?!>)|[^>/])*` ;
112112const HTML_CSS_SOURCE = new RegExp (
113113 String . raw `<!--[\s\S]*?(?:-->|$)|<script(?=[\s/>])(${ HTML_CSS_TAG_ATTRIBUTES } )(?:>|$)([\s\S]*?)(?:</script\s*>|$)|<style(?=[\s/>])[^>]*(?:>|$)([\s\S]*?)(?:</style\s*>|$)` +
114- String . raw `|<(?<textElement>textarea| title|xmp|noembed|noframes|noscript )(?=[\t\n\f\r />])( ${ HTML_TEXT_TAG_ATTRIBUTES } )(?:/>|(?:>|$)[\s\S]*?(?:</\k<textElement>(?=[\t\n\f\r />]) ${ HTML_CSS_TAG_ATTRIBUTES } (?:>|$)|$) )` +
115- String . raw `|<plaintext(?=[ \t\n\f\r />])(${ HTML_CSS_TAG_ATTRIBUTES } )(? :>|$)[\s\S]*$|<[a-z]( ${ HTML_CSS_TAG_ATTRIBUTES } )` ,
114+ String . raw `|</(svg|math| title|desc|foreignobject )(?=[\t\n\f\r />])${ HTML_CSS_TAG_ATTRIBUTES } (?:>|$)` +
115+ String . raw `|<([a-z][^ \t\n\f\r />]* )(${ HTML_TEXT_TAG_ATTRIBUTES } )(\/?)(? :>|$)` ,
116116 "gi" ,
117117) ;
118+ const HTML_TEXT_END_PATTERNS = new Map (
119+ [ "textarea" , "title" , "xmp" , "noembed" , "noframes" , "noscript" ] . map ( ( tag ) => [
120+ tag ,
121+ new RegExp ( String . raw `</${ tag } (?=[\t\n\f\r />])${ HTML_CSS_TAG_ATTRIBUTES } (?:>|$)` , "gi" ) ,
122+ ] ) ,
123+ ) ;
118124// Inside a tag, skip other attributes' quoted values, such as a title that mentions style=.
119125// A quote opens a value only right after `=`; an unquoted value such as `it's` or `a="b` runs to a space.
120126const TAG_STYLE =
@@ -155,18 +161,51 @@ const findViewportHeight = (source: string, pattern: RegExp) => {
155161} ;
156162
157163const rejectViewportHeight = ( html : string ) => {
158- for ( const [ , scriptTag , script , sheet , , textTag , plaintextTag , tag ] of html . matchAll (
159- HTML_CSS_SOURCE ,
160- ) ) {
161- const attributes = textTag ?? plaintextTag ?? tag ;
164+ const pattern = new RegExp ( HTML_CSS_SOURCE . source , HTML_CSS_SOURCE . flags ) ;
165+ const foreignStack : Array < { readonly tag : string ; readonly foreign : boolean } > = [ ] ;
166+ for ( let match = pattern . exec ( html ) ; match !== null ; match = pattern . exec ( html ) ) {
167+ const [ , scriptTag , script , sheet , closeTag , tagName , tagAttributes , selfClosing ] = match ;
168+ if ( closeTag !== undefined ) {
169+ const closed = closeTag . toLowerCase ( ) ;
170+ for ( let index = foreignStack . length - 1 ; index >= 0 ; index -= 1 ) {
171+ if ( foreignStack [ index ] ?. tag === closed ) {
172+ foreignStack . length = index ;
173+ break ;
174+ }
175+ }
176+ continue ;
177+ }
162178 const found =
163179 script !== undefined
164180 ? ( findViewportHeight ( scriptTag ?? "" , TAG_STYLE ) ??
165181 findViewportHeight ( script , SCRIPT_STYLE ) )
166- : attributes !== undefined
167- ? findViewportHeight ( attributes , TAG_STYLE )
182+ : tagAttributes !== undefined
183+ ? findViewportHeight ( tagAttributes , TAG_STYLE )
168184 : findInCss ( sheet ?? "" ) ;
169185 if ( found !== undefined ) return Effect . fail ( new HtmlRenderViewportHeightError ( { found } ) ) ;
186+ if ( tagName === undefined ) continue ;
187+ const tag = tagName . toLowerCase ( ) ;
188+ const inForeign = foreignStack . at ( - 1 ) ?. foreign ?? false ;
189+ if ( ! inForeign ) {
190+ if ( tag === "plaintext" ) break ;
191+ const endPattern = HTML_TEXT_END_PATTERNS . get ( tag ) ;
192+ if ( endPattern !== undefined ) {
193+ endPattern . lastIndex = pattern . lastIndex ;
194+ if ( endPattern . exec ( html ) === null ) break ;
195+ pattern . lastIndex = endPattern . lastIndex ;
196+ continue ;
197+ }
198+ }
199+ if ( ! selfClosing ) {
200+ if ( tag === "svg" || tag === "math" ) {
201+ foreignStack . push ( { tag, foreign : true } ) ;
202+ } else if (
203+ foreignStack . at ( - 1 ) ?. tag === "svg" &&
204+ ( tag === "title" || tag === "desc" || tag === "foreignobject" )
205+ ) {
206+ foreignStack . push ( { tag, foreign : false } ) ;
207+ }
208+ }
170209 }
171210 return Effect . void ;
172211} ;
0 commit comments