(Bug 64017) Cleanup nested range and cycle detection.

* Always record outermost enclosing range.

* Prevent cycles while recording nesting of ranges.

* Together, this ensures that nesting cycles are short-circuited
  while recording template range nesting graphs and lets us find
  the outermost enclosing range in all scenarios.

* Enables template encapsulation on enwiki:Indology which has
  a missing {{bottom}} transclusion to close a table. Reduced
  test case is:
---------
{{top}}
{{div col|2}}
{{div col end}}
---------

  Before this patch, template encapsulation would fail on this
  snippet.

* Added parser test that captures this behavior. Renamed another
  test to more accurately reflect what is being tested.

* Tested on:
  - enwiki/Mujigen_Hunter_Fandora from 8c22025b
  - enwiki/Dogma_(studio) from bc7b574d

History
-------
It looks like this took several tries before I/we fully understood
the problem. The history of work on this so far indicates all the
bad assumptions I made about templates, what kind of output might
be produced, about ordering of tpls, ids, fostering, nesting, etc.
Many assumptions were implicit assumptions rather than explicit
assumptions.

Ordered chronologically (reverse of git log order):

 ecf0376c: Fix template wrapping to merge overlapping templates
           (old assumption: never considered merging of ranges)
 606c7b57: Fix template merges in the presence of foster-parenting
           (old assumption: never considered fostering)
 1554f4da: Fix template merging code to get rid of weird hash conflicts
           (old assumption: n^2 checks to detect merging + dom nodes
            can be used as hash keys!)
 861d5993: Fixed merging of nested template ranges
           (old assumption: templates don't nest)
 95f50b42: Sort tpl-ranges by aboutId before adding encap. info to DOM
           (old assumption: tpl-ranges are detected in WT source order)
 683a485a: Improved detection of tpl-content fostered out of a table
           (old assumption: something to do with outer/innermost tpls)
 efe2f968: Dont encapsulate tpls that are nested in other tpls
 92ec4761: Reworked detection of nested and overlapping tpl-ranges
           (old assumption: about-ids are assigned in WT source order
            <ref> tags break this assumption)
           First introduction of algo currently in use
 b979895c: Code cleanup + bug fix in detection of template range nesting
 bc7b574d: Handle flipped tpl-ranges in findTopLevelNonOverlappingRanges
           (old assumption: no fostering of partial tpl content)
 8c22025b: Detect cycles in tpl-range nesting and pick a winner
           (old assumption: two tpl ranges cannot be identical)
 605b3a07: Improvement to detection of nested template ranges
 3f8b7b6c: (Bug 64017): Fixed longstanding tpl-range overlap detection bug
           (old assumption: no true overlap. Start-range of one template
           is identical to end-range of overlapping template)
 <THIS>  : (Bug 64017) Cleanup nested range and cycle detection.

           This is a continuation of 605b3a07 and is more robust.
           Now, the (implicit) Nesting graph is constructed after
           breaking a cycle rather than breaking cycles after constructing
           the graph. This matters because the graph only adds one edge
           per node => the cycle effectively leaves it disconnected from
           the rest of the graph.

Change-Id: Iecce8a2e3e07068be5717bfa60c5390a72157611
Merge "(Bug 64017) Cleanup nested range and cycle detection."