@@ -207,40 +207,8 @@ impl<Handle, Sink> TreeBuilderActions<Handle>
207207
208208 // Insert at the "appropriate place for inserting a node".
209209 fn insert_appropriately ( & mut self , child : NodeOrText < Handle > , override_target : Option < Handle > ) {
210- declare_tag_set ! ( foster_target = table tbody tfoot thead tr) ;
211- let target = override_target. unwrap_or_else ( || self . current_node ( ) ) ;
212- if !( self . foster_parenting && self . elem_in ( target. clone ( ) , foster_target) ) {
213- // No foster parenting (the common case).
214- return self . sink . append ( target, child) ;
215- }
216-
217- // Foster parenting
218- // FIXME: <template>
219- let last_table = self . open_elems . iter ( )
220- . enumerate ( )
221- . rev ( )
222- . filter ( |& ( _, e) | self . html_elem_named ( e. clone ( ) , atom ! ( table) ) )
223- . next ( ) ;
224-
225- match last_table {
226- None => {
227- let html_elem = self . html_elem ( ) ;
228- self . sink . append ( html_elem, child) ;
229- }
230- Some ( ( idx, last_table) ) => {
231- // Try inserting "inside last table's parent node, immediately before last table"
232- match self . sink . append_before_sibling ( last_table. clone ( ) , child) {
233- Ok ( ( ) ) => ( ) ,
234-
235- // If last_table has no parent, we regain ownership of the child.
236- // Insert "inside previous element, after its last child (if any)"
237- Err ( child) => {
238- let previous_element = self . open_elems [ idx-1 ] . clone ( ) ;
239- self . sink . append ( previous_element, child) ;
240- }
241- }
242- }
243- }
210+ let insertion_point = self . appropriate_place_for_insertion ( override_target) ;
211+ self . insert_at ( insertion_point, child) ;
244212 }
245213
246214 fn adoption_agency ( & mut self , subject : Atom ) {
@@ -755,10 +723,39 @@ impl<Handle, Sink> TreeBuilderActions<Handle>
755723 // FIXME: application cache selection algorithm
756724 }
757725
726+ // https://html.spec.whatwg.org/multipage/syntax.html#create-an-element-for-the-token
758727 fn insert_element ( & mut self , push : PushFlag , ns : Namespace , name : Atom , attrs : Vec < Attribute > )
759728 -> Handle {
760- let elem = self . sink . create_element ( QualName :: new ( ns, name) , attrs) ;
761- self . insert_appropriately ( AppendNode ( elem. clone ( ) ) , None ) ;
729+ declare_tag_set ! ( form_associatable =
730+ button fieldset input keygen label
731+ object output select textarea img) ;
732+
733+ declare_tag_set ! ( reassociatable = form_associatable - img) ;
734+
735+ let qname = QualName :: new ( ns, name) ;
736+ let elem = self . sink . create_element ( qname. clone ( ) , attrs. clone ( ) ) ;
737+
738+ let insertion_point = self . appropriate_place_for_insertion ( None ) ;
739+ let tree_node = match insertion_point {
740+ LastChild ( ref p) |
741+ BeforeSibling ( ref p) => p. clone ( )
742+ } ;
743+
744+ // Step 4.
745+ // TODO: Handle template element case
746+ if form_associatable ( qname. clone ( ) )
747+ && self . form_elem . is_some ( )
748+ && !( reassociatable ( qname. clone ( ) )
749+ && attrs. iter ( ) . any ( |a| a. name == qualname ! ( "" , "form" ) ) ) {
750+
751+ let form = self . form_elem . as_ref ( ) . unwrap ( ) . clone ( ) ;
752+ if self . sink . same_home_subtree ( tree_node, form. clone ( ) ) {
753+ self . sink . associate_with_form ( elem. clone ( ) , form)
754+ }
755+ }
756+
757+ self . insert_at ( insertion_point, AppendNode ( elem. clone ( ) ) ) ;
758+
762759 match push {
763760 Push => self . push ( & elem) ,
764761 NoPush => ( ) ,
0 commit comments