Detect cycles in tpl-range nesting and pick a winner.

* Because multiple templates can collectively generate a DOM subtree
  (whereas individual elements of this collection dont), the
  expanded template ranges for each member of the collection can
  end up being identical. In addition, fostering of content from
  tables can result in situations where some ranges can be flipped
  over (end marker occuring earlier than the start marker).

  Both these conditions are true with en:Mujigen_Hunter_Fandora

* Previous logic to not introduce cycles in the nesting detection
  was simplistic and assumed that at most 2 templates can participate
  in cycles, whereas in the above page, 3 templates participated in
  a cycle.  This sent the template encapsulation logic in an infinite
  loop (in an attempt to find the topmost enclosing range).

* This patch eliminates the checks and introduces a more robust cycle
  detection check that breaks cycles no matter how they were introduced.

  It arbitrarily picks the smallest template range id as the winner,
  i.e. the template range within which all the other elements of the
  cycle are considered nested in.  This works in eliminating loops
  during template encapsulation.

  However, the strategy of picking the smallest range always wont
  work well if that range happens to be a flipped range and introduces
  duplicate content during RTing. (See new wt2wt failure in parserTests)

  This failure can be handled either in the serializer or by not
  picking a flipped range as the outermost range in the parser --
  to be decided what is better.

* 1 new wt2wt test failure (see explanation above)
  1 new selser of the same test

Change-Id: I8e3042466719c98e2e6364fc756398d5ce6543ef
Merge "Detect cycles in tpl-range nesting and pick a winner."