@@ -240,32 +240,44 @@ def elementInScope(self, target, tableVariant=False):
240240 assert False # We should never reach this point
241241
242242 def reconstructActiveFormattingElements (self ):
243- # This covers the ", if any" case mentioned often in the drafts, before
244- # you have to reconstruct the active formatting elements.
243+ # Within this algorithm the order of steps described in the
244+ # specification is not quite the same as the order of steps in the
245+ # code. It should still do the same though.
246+
247+ # Step 1: stop the algorithm when there's nothing to do.
245248 if not self .activeFormattingElements :
246249 return
247250
248- # We start with the last element. So i is -1.
251+ # Step 2 and step 3: we start with the last element. So i is -1.
249252 i = - 1
250253 entry = self .activeFormattingElements [i ]
251-
252- # Step 1
253254 if entry == Marker or entry in self .openElements :
254255 return
255256
256- # Step 2 is covered above. When we assign entry to the last element.
257- # Step 3 and 4.
258- # XXX The specification says "and entry _not_ in" but that doesn't
259- # work. This seems to give correct results too...
260- while entry != Marker and entry in self .openElements :
257+ # Step 6
258+ while entry != Marker and entry not in self .openElements :
259+ # Step 5: let entry be one earlier in the list.
261260 i -= 1
262- entry = self .activeFormattingElements [i ]
261+ try :
262+ entry = self .activeFormattingElements [i ]
263+ except :
264+ # Step 4: at this point we need to jump to step 8. By not doing
265+ # i += 1 which is also done in step 7 we achieve that.
266+ break
263267 while True :
264- # XXX why clone?
268+ # Step 7
265269 i += 1
270+
271+ # Step 8
266272 clone = self .activeFormattingElements [i ].cloneNode ()
273+
274+ # Step 9
267275 element = self .insertElement (clone .name , clone .attributes )
276+
277+ # Step 10
268278 self .activeFormattingElements [i ] = element
279+
280+ # Step 11
269281 if element == self .activeFormattingElements [- 1 ]:
270282 break
271283
0 commit comments