knockout/knockout — pull request #1152 by pimterry (2013-10-13 20:00:29+00:00)
Title: Throw errors if virtual tree traversal finds an unmatched ko closing comment

Currently Knockout will throw an error if you use a opening virtual element with no closing element, but will otherwise completely ignore extra closing comments. It's extremely easy to accidentally use too many closing elements or incorrect nesting and end up with all sorts of problems, particularly with 'with' bindings which will then leave you with slightly wrong contexts applied in the wrong places.

This patch fixes that. It hooks into the virtual tree traversal and loudly fails if at any point you attempt to traverse to a tree node that hasn't previously been matched by a starting node. End comments that are matched are tracked by tagging with ko.domData. This gets done when starting nodes are about to be included in the traversal route; they're found via virtualElements.nextSibling, which calls getMatchingEndComment, which calls getVirtualChildren, which *now* adds a marker to correctly matching end comments as they're found.

One contentious bit that's worth thinking about is the error messages: I did this with outerHTML initially, so it gave you the relevant chunk of the page as part of the error message. That was useful, but a potentially bit long-winded, and doesn't work in FF < 4. I've pulled that out, and it now gives errors along the lines of 'Found end comment without opening comment, as first child of [object HTMLDivElement]'. Gives you the parent element type, and makes the situation fairly clear if you debug into it, but otherwise not too great.