Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix replaceWith bugfix for node argument that is already in document tree #84

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 21 additions & 44 deletions lib/src/html/dom/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -386,83 +386,60 @@ abstract class Node extends EventTarget {
return sb.toString();
}

void remove() {
void _removeFromTree(Node? replaceWith) {
final parent = _parent;
if (parent == null) {
assert(_previousNode == null);
assert(_nextNode == null);
return;
}

// Mark node as dirty
_markDirty();

// Get previous and next
final previous = _previousNode;
final next = _nextNode;

if (previous == null) {
// This was the first sibling
parent._firstChild = next;
parent._firstChild = replaceWith ?? next;
} else {
// Mutate the previous sibling
previous._nextNode = next;
previous._nextNode = replaceWith ?? next;
}

if (next == null) {
// This was the last sibling
parent._lastChild = previous;
parent._lastChild = replaceWith ?? previous;
} else {
// Mutate the next sibling
next._previousNode = previous;
next._previousNode = replaceWith ?? previous;
}
if (replaceWith != null) {
// move replaceWith
replaceWith._parent = parent;
replaceWith._previousNode = previous;
replaceWith._nextNode = next;
}

// Set fields of this node
_parent = null;
_previousNode = null;
_nextNode = null;
parent._mutated();
_mutated();
}

void replaceWith(Node node) {
void remove() {
final parent = _parent;
if (parent == null) {
assert(_previousNode == null);
assert(_nextNode == null);
return;
}
// Mark node as dirty
_markDirty();
_removeFromTree(null);
parent?._mutated();
_mutated();
}

void replaceWith(Node node) {
// Mark nodes as dirty
_markDirty();
node._markDirty();

// Get previous and next
final previous = _previousNode;
final next = _nextNode;

if (previous == null) {
// This was the first sibling
parent._firstChild = node;
} else {
// Mutate the previous sibling
previous._nextNode = node;
}

if (next == null) {
// This was the last sibling
parent._lastChild = node;
} else {
// Mutate the next sibling
next._previousNode = node;
}

node._parent = parent;
node._previousNode = previous;
node._nextNode = next;
_parent = null;
_previousNode = null;
_nextNode = null;
node._removeFromTree(null);
_removeFromTree(node);
_mutated();
node._mutated();
}
Expand Down
28 changes: 24 additions & 4 deletions test/src/html/dom/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ void _testNode() {
test('replaceWith', () {
final e0 = Element.tag('e0')..appendText('e0-text');
final e1 = Element.tag('e1')..appendText('e1-text');
final e2 = Element.tag('e2')..appendText('e2-text');
final e2 = Element.tag('e2')..append(
Element.tag('e2c1')
)..append(
Element.tag('e2c2')
);
final root = Element.tag('sometag')
..setAttribute('k', 'v')
..append(e0)
Expand All @@ -48,7 +52,7 @@ void _testNode() {
expect(
root.outerHtml,
equals(
'<sometag k="v"><e0>e0-text</e0><e1>e1-text</e1><e2>e2-text</e2></sometag>'));
'<sometag k="v"><e0>e0-text</e0><e1>e1-text</e1><e2><e2c1></e2c1><e2c2></e2c2></e2></sometag>'));

// Replace child #1 of 'e1'
{
Expand All @@ -64,7 +68,7 @@ void _testNode() {
expect(
root.outerHtml,
equals(
'<sometag k="v"><e0>e0-text</e0><e1>e1-text-replaced</e1><e2>e2-text</e2></sometag>'));
'<sometag k="v"><e0>e0-text</e0><e1>e1-text-replaced</e1><e2><e2c1></e2c1><e2c2></e2c2></e2></sometag>'));
}

// Replace child #2 of root ('e1')
Expand All @@ -83,7 +87,23 @@ void _testNode() {
expect(
root.outerHtml,
equals(
'<sometag k="v"><e0>e0-text</e0>e1-replaced<e2>e2-text</e2></sometag>'));
'<sometag k="v"><e0>e0-text</e0>e1-replaced<e2><e2c1></e2c1><e2c2></e2c2></e2></sometag>'));
}
// Replace with existing node to check if it moved properly
{
final replacement = e2.children.first;
e0.replaceWith(replacement);

expect(e0.nextNode, isNull);
expect(e0.previousNode, isNull);
expect(e0.parent, isNull);
expect(replacement.parent, same(root));
expect(replacement.previousNode, isNull);
expect(replacement.nextNode, isNotNull);
expect(
root.outerHtml,
equals(
'<sometag k="v"><e2c1></e2c1>e1-replaced<e2><e2c2></e2c2></e2></sometag>'));
}
});
test('replaceWith when the node has no parent', () {
Expand Down