diff --git a/Tests/LibWeb/Text/expected/DOM/TreeWalker-nextNode-with-detached-currentNode.txt b/Tests/LibWeb/Text/expected/DOM/TreeWalker-nextNode-with-detached-currentNode.txt
new file mode 100644
index 0000000000..1eb5d9030c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/DOM/TreeWalker-nextNode-with-detached-currentNode.txt
@@ -0,0 +1 @@
+PASS (Didn't get stuck)
diff --git a/Tests/LibWeb/Text/input/DOM/TreeWalker-nextNode-with-detached-currentNode.html b/Tests/LibWeb/Text/input/DOM/TreeWalker-nextNode-with-detached-currentNode.html
new file mode 100644
index 0000000000..4fcc83a844
--- /dev/null
+++ b/Tests/LibWeb/Text/input/DOM/TreeWalker-nextNode-with-detached-currentNode.html
@@ -0,0 +1,13 @@
+
+
diff --git a/Userland/Libraries/LibWeb/DOM/TreeWalker.cpp b/Userland/Libraries/LibWeb/DOM/TreeWalker.cpp
index 87ab5771e3..00d58b69f1 100644
--- a/Userland/Libraries/LibWeb/DOM/TreeWalker.cpp
+++ b/Userland/Libraries/LibWeb/DOM/TreeWalker.cpp
@@ -221,6 +221,13 @@ JS::ThrowCompletionOr> TreeWalker::next_node()
// 4. Set temporary to temporary’s parent.
temporary = temporary->parent();
+
+ // NON-STANDARD: If temporary is null, then return null.
+ // This prevents us from infinite looping if the current node is not connected.
+ // Spec bug: https://github.com/whatwg/dom/issues/1102
+ if (temporary == nullptr) {
+ return nullptr;
+ }
}
// 5. Set result to the result of filtering node within this.