Skip to content

Commit

Permalink
Fix getComponentForEl with nested fragments (returns owner component) (
Browse files Browse the repository at this point in the history
…#1382)

* Fix getComponentForEl with nested fragments (returns owner component)
  • Loading branch information
DylanPiercey committed Jul 8, 2019
1 parent c401b7f commit fc5fb03
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/runtime/components/util-browser.js
Expand Up @@ -11,22 +11,15 @@ var componentLookup = {};
var defaultDocument = document;
var EMPTY_OBJECT = {};

function getParentComponentForEl(node) {
while (node && !componentsByDOMNode.get(node)) {
node = node.previousSibling || node.parentNode;
node = (node && node.fragment) || node;
}
return node && componentsByDOMNode.get(node);
}

function getComponentForEl(el, doc) {
if (el) {
var node =
typeof el == "string"
? (doc || defaultDocument).getElementById(el)
: el;
if (node) {
return getParentComponentForEl(node);
var vElement = vElementsByDOMNode.get(node);
return vElement && vElement.___ownerComponent;
}
}
}
Expand Down
@@ -0,0 +1,15 @@
class {
onCreate(input) {
this.name = input.name;
}
}

<macro|input| name="fragment">
<${input}/>
</macro>

<fragment>
<fragment>
<div class=input.name/>
</fragment>
</fragment>
@@ -0,0 +1,19 @@
class {
onCreate() {
this.name = "parent";
}
}

<macro|input| name="fragment">
<${input}/>
</macro>

<div key="root">
<fragment>
<fragment>
<child name="child-a"/>
</fragment>
</fragment>
<div.child-b/>
<child name="child-c"/>
</div>
@@ -0,0 +1,14 @@
var expect = require("chai").expect;
var getComponentForEl = require("marko/components").getComponentForEl;

module.exports = function(helpers) {
var component = helpers.mount(require.resolve("./index.marko"), {});
var root = component.getEl("root");
var childA = root.querySelector(".child-a");
var childB = root.querySelector(".child-b");
var childC = root.querySelector(".child-c");

expect(getComponentForEl(childA)).has.property("name", "child-a");
expect(getComponentForEl(childB)).has.property("name", "parent");
expect(getComponentForEl(childC)).has.property("name", "child-c");
};

0 comments on commit fc5fb03

Please sign in to comment.