@@ -6096,6 +6096,7 @@ private function reset_insertion_mode_appropriately(): void {
60966096 * @see https://html.spec.whatwg.org/#adoption-agency-algorithm
60976097 */
60986098 private function run_adoption_agency_algorithm (): void {
6099+ $ budget = 1000 ;
60996100 $ subject = $ this ->get_tag ();
61006101 $ current_node = $ this ->state ->stack_of_open_elements ->current_node ();
61016102
@@ -6109,88 +6110,88 @@ private function run_adoption_agency_algorithm(): void {
61096110 return ;
61106111 }
61116112
6112- /*
6113- * > Let outer loop counter be 0.
6114- * > Outer loop: If outer loop counter is greater than or equal to 8, then return.
6115- *
6116- * Only the first pass through the outer loop is supported. Every path through it
6117- * either returns or bails before reaching the steps that would start another pass,
6118- * so the loop and its counter are left out until those steps are implemented.
6119- */
6120-
6121- /*
6122- * > Let formatting element be the last element in the list of active formatting elements that:
6123- * > - is between the end of the list and the last marker in the list,
6124- * > if any, or the start of the list otherwise,
6125- * > - and has the tag name subject.
6126- */
6127- $ formatting_element = null ;
6128- foreach ( $ this ->state ->active_formatting_elements ->walk_up () as $ item ) {
6129- if ( 'marker ' === $ item ->node_name ) {
6130- break ;
6113+ $ outer_loop_counter = 0 ;
6114+ while ( $ budget -- > 0 ) {
6115+ if ( $ outer_loop_counter ++ >= 8 ) {
6116+ return ;
61316117 }
61326118
6133- if ( $ subject === $ item ->node_name ) {
6134- $ formatting_element = $ item ;
6135- break ;
6136- }
6137- }
6138-
6139- // > If there is no such element, then return and instead act as described in the "any other end tag" entry above.
6140- if ( null === $ formatting_element ) {
6141- $ this ->bail ( 'Cannot run adoption agency when "any other end tag" is required. ' );
6142- }
6119+ /*
6120+ * > Let formatting element be the last element in the list of active formatting elements that:
6121+ * > - is between the end of the list and the last marker in the list,
6122+ * > if any, or the start of the list otherwise,
6123+ * > - and has the tag name subject.
6124+ */
6125+ $ formatting_element = null ;
6126+ foreach ( $ this ->state ->active_formatting_elements ->walk_up () as $ item ) {
6127+ if ( 'marker ' === $ item ->node_name ) {
6128+ break ;
6129+ }
61436130
6144- // > If formatting element is not in the stack of open elements, then this is a parse error; remove the element from the list, and return.
6145- if ( ! $ this -> state -> stack_of_open_elements -> contains_node ( $ formatting_element ) ) {
6146- $ this -> state -> active_formatting_elements -> remove_node ( $ formatting_element ) ;
6147- return ;
6148- }
6131+ if ( $ subject === $ item -> node_name ) {
6132+ $ formatting_element = $ item ;
6133+ break ;
6134+ }
6135+ }
61496136
6150- // > If formatting element is in the stack of open elements, but the element is not in scope, then this is a parse error; return .
6151- if ( ! $ this -> state -> stack_of_open_elements -> has_element_in_scope ( $ formatting_element-> node_name ) ) {
6152- return ;
6153- }
6137+ // > If there is no such element, then return and instead act as described in the "any other end tag" entry above .
6138+ if ( null === $ formatting_element ) {
6139+ $ this -> bail ( ' Cannot run adoption agency when "any other end tag" is required. ' ) ;
6140+ }
61546141
6155- /*
6156- * > Let furthest block be the topmost node in the stack of open elements that is lower in the stack
6157- * > than formatting element, and is an element in the special category. There might not be one.
6158- */
6159- $ is_above_formatting_element = true ;
6160- $ furthest_block = null ;
6161- foreach ( $ this ->state ->stack_of_open_elements ->walk_down () as $ item ) {
6162- if ( $ is_above_formatting_element && $ formatting_element ->bookmark_name !== $ item ->bookmark_name ) {
6163- continue ;
6142+ // > If formatting element is not in the stack of open elements, then this is a parse error; remove the element from the list, and return.
6143+ if ( ! $ this ->state ->stack_of_open_elements ->contains_node ( $ formatting_element ) ) {
6144+ $ this ->state ->active_formatting_elements ->remove_node ( $ formatting_element );
6145+ return ;
61646146 }
61656147
6166- if ( $ is_above_formatting_element ) {
6167- $ is_above_formatting_element = false ;
6168- continue ;
6148+ // > If formatting element is in the stack of open elements, but the element is not in scope, then this is a parse error; return.
6149+ if ( ! $ this -> state -> stack_of_open_elements -> has_element_in_scope ( $ formatting_element -> node_name ) ) {
6150+ return ;
61696151 }
61706152
6171- if ( self ::is_special ( $ item ) ) {
6172- $ furthest_block = $ item ;
6173- break ;
6153+ /*
6154+ * > Let furthest block be the topmost node in the stack of open elements that is lower in the stack
6155+ * > than formatting element, and is an element in the special category. There might not be one.
6156+ */
6157+ $ is_above_formatting_element = true ;
6158+ $ furthest_block = null ;
6159+ foreach ( $ this ->state ->stack_of_open_elements ->walk_down () as $ item ) {
6160+ if ( $ is_above_formatting_element && $ formatting_element ->bookmark_name !== $ item ->bookmark_name ) {
6161+ continue ;
6162+ }
6163+
6164+ if ( $ is_above_formatting_element ) {
6165+ $ is_above_formatting_element = false ;
6166+ continue ;
6167+ }
6168+
6169+ if ( self ::is_special ( $ item ) ) {
6170+ $ furthest_block = $ item ;
6171+ break ;
6172+ }
61746173 }
6175- }
61766174
6177- /*
6178- * > If there is no furthest block, then the UA must first pop all the nodes from the bottom of the
6179- * > stack of open elements, from the current node up to and including formatting element, then
6180- * > remove formatting element from the list of active formatting elements, and finally return.
6181- */
6182- if ( null === $ furthest_block ) {
6183- foreach ( $ this ->state ->stack_of_open_elements ->walk_up () as $ item ) {
6184- $ this ->state ->stack_of_open_elements ->pop ();
6175+ /*
6176+ * > If there is no furthest block, then the UA must first pop all the nodes from the bottom of the
6177+ * > stack of open elements, from the current node up to and including formatting element, then
6178+ * > remove formatting element from the list of active formatting elements, and finally return.
6179+ */
6180+ if ( null === $ furthest_block ) {
6181+ foreach ( $ this ->state ->stack_of_open_elements ->walk_up () as $ item ) {
6182+ $ this ->state ->stack_of_open_elements ->pop ();
61856183
6186- if ( $ formatting_element ->bookmark_name === $ item ->bookmark_name ) {
6187- $ this ->state ->active_formatting_elements ->remove_node ( $ formatting_element );
6188- return ;
6184+ if ( $ formatting_element ->bookmark_name === $ item ->bookmark_name ) {
6185+ $ this ->state ->active_formatting_elements ->remove_node ( $ formatting_element );
6186+ return ;
6187+ }
61896188 }
61906189 }
6190+
6191+ $ this ->bail ( 'Cannot extract common ancestor in adoption agency algorithm. ' );
61916192 }
61926193
6193- $ this ->bail ( 'Cannot extract common ancestor in adoption agency algorithm . ' );
6194+ $ this ->bail ( 'Cannot run adoption agency when looping required . ' );
61946195 }
61956196
61966197 /**
0 commit comments