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: add devmode warning for removing fragment markers #1541

Merged
merged 2 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/marko/src/runtime/components/util-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,33 @@ function addComponentRootToKeyedElements(
}
}

// eslint-disable-next-line no-constant-condition
if ("MARKO_DEBUG") {
var warnNodeRemoved = function(event) {
var fragment = event.target.fragment;
if (fragment) {
var baseError = new Error(
"Fragment boundary marker removed. This will cause an error when the fragment is updated."
);
fragment.___markersRemovedError = function(message) {
var error = new Error(message + " Boundary markers missing.");

baseError.stack = baseError.stack.replace(/.*warnNodeRemoved.*\n/, "");

// eslint-disable-next-line no-console
console.warn(baseError);
return error;
};
}
};
exports.___startDOMManipulationWarning = function() {
document.addEventListener("DOMNodeRemoved", warnNodeRemoved);
};
exports.___stopDOMManipulationWarning = function() {
document.removeEventListener("DOMNodeRemoved", warnNodeRemoved);
};
}

exports.___runtimeId = runtimeId;
exports.___componentLookup = componentLookup;
exports.___getComponentForEl = getComponentForEl;
Expand Down
6 changes: 6 additions & 0 deletions packages/marko/src/runtime/components/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@ exports.___isServer = true;
exports.___attachBubblingEvent = attachBubblingEvent;
exports.___destroyComponentForNode = function noop() {};
exports.___destroyNodeRecursive = function noop() {};

// eslint-disable-next-line no-constant-condition
if ("MARKO_DEBUG") {
exports.___startDOMManipulationWarning = function noop() {};
exports.___stopDOMManipulationWarning = function noop() {};
}
6 changes: 6 additions & 0 deletions packages/marko/src/runtime/vdom/morphdom/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ var fragmentPrototype = {
return this.endNode.nextSibling;
},
get nodes() {
// eslint-disable-next-line no-constant-condition
if ("MARKO_DEBUG") {
if (this.___markersRemovedError) {
throw this.___markersRemovedError("Cannot get fragment nodes.");
}
}
var nodes = [];
var current = this.startNode;
while (current !== this.endNode) {
Expand Down
10 changes: 10 additions & 0 deletions packages/marko/src/runtime/vdom/morphdom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,11 @@ function morphdom(fromNode, toNode, doc, componentsContext) {
}
} // END: morphEl(...)

// eslint-disable-next-line no-constant-condition
if ("MARKO_DEBUG") {
componentsUtil.___stopDOMManipulationWarning();
}

morphChildren(fromNode, toNode, toNode.___component);

detachedNodes.forEach(function(node) {
Expand All @@ -693,6 +698,11 @@ function morphdom(fromNode, toNode, doc, componentsContext) {
}
}
});

// eslint-disable-next-line no-constant-condition
if ("MARKO_DEBUG") {
componentsUtil.___startDOMManipulationWarning();
}
}

module.exports = morphdom;