Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix #413 (tolerate certain kinds of manual DOM edits while using setDomN... #424

Merged
merged 1 commit into from

2 participants

@SteveSanderson

...odeChildrenFromArrayMapping). Thanks to mbest for the solution.

unknown Fix #413 (tolerate certain kinds of manual DOM edits while using setD…
…omNodeChildrenFromArrayMapping). Thanks to mbest for the solution.
b88a69a
@mbest mbest merged commit b768098 into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 4, 2012
  1. Fix #413 (tolerate certain kinds of manual DOM edits while using setD…

    unknown authored
    …omNodeChildrenFromArrayMapping). Thanks to mbest for the solution.
This page is out of date. Refresh to see the latest.
View
24 spec/editDetectionBehaviors.js
@@ -158,6 +158,30 @@ describe('Array to DOM node children mapping', {
value_of(mappingInvocations).should_be([]);
},
+ 'Should tolerate DOM nodes being removed manually, before the corresponding array entry is removed': function() {
+ // Represents https://github.com/SteveSanderson/knockout/issues/413
+ // Ideally, people wouldn't be mutating the generated DOM manually. But this didn't error in v2.0, so we should try to avoid introducing a break.
+ var mappingInvocations = [];
+ var mapping = function (arrayItem) {
+ mappingInvocations.push(arrayItem);
+ var output = document.createElement("DIV");
+ output.innerHTML = arrayItem;
+ return [output];
+ };
+
+ ko.utils.setDomNodeChildrenFromArrayMapping(testNode, ["A", "B", "C"], mapping);
+ value_of(testNode).should_contain_text("ABC");
+
+ // Now kill the middle DIV manually, even though people shouldn't really do this
+ var elemToRemove = testNode.childNodes[1];
+ value_of(elemToRemove.innerHTML).should_be("B"); // Be sure it's the right one
+ elemToRemove.parentNode.removeChild(elemToRemove);
+
+ // Now remove the corresponding array entry. This shouldn't cause an exception.
+ ko.utils.setDomNodeChildrenFromArrayMapping(testNode, ["A", "C"], mapping);
+ value_of(testNode).should_contain_text("AC");
+ },
+
'Should handle sequences of mixed insertions and deletions': function () {
var mappingInvocations = [], countCallbackInvocations = 0;
var mapping = function (arrayItem) {
View
6 src/binding/editDetection/arrayToDomNodeChildren.js
@@ -152,8 +152,10 @@
}
if (!invokedBeforeRemoveCallback && nodesToDelete.length) {
var commonParent = nodesToDelete[0].element.parentNode;
- for (var i = 0; i < nodesToDelete.length; i++)
- commonParent.removeChild(nodesToDelete[i].element);
+ if (commonParent) {
+ for (var i = 0; i < nodesToDelete.length; i++)
+ commonParent.removeChild(nodesToDelete[i].element);
+ }
}
// Store a copy of the array items we just considered so we can difference it next time
Something went wrong with that request. Please try again.