Skip to content

Commit

Permalink
Use the Polymer getById functionality to find element. (#1617)
Browse files Browse the repository at this point in the history
* Use the Polymer getById functionality to find element.

* Update gwt tests for polymerElement.$ usage
  • Loading branch information
caalador authored May 18, 2017
1 parent 9d9f03e commit d915ebf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 70 deletions.
10 changes: 1 addition & 9 deletions flow-client/src/main/java/com/vaadin/client/ElementUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,7 @@ public static void attachExistingElementById(StateNode parent,

private static native Element getDomElementById(Element shadowRootParent,
String id) /*-{
var elementById = null;
var shadowRoot = shadowRootParent.shadowRoot;
if (shadowRoot && typeof(shadowRoot.getElementById) === 'function') {
elementById = shadowRoot.getElementById(id);
}
if(elementById == null) {
elementById = $doc.getElementById(id);
}
return elementById;
return shadowRootParent.$[id];
}-*/;

private static Integer getExistingIdOrUpdate(StateNode parent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ public void testAttachExistingElement_noRequestedElement() {
}

public void testAttachExistingElementById_elementExistsInDom() {
Element shadowRoot = setupShadowRoot();
setupShadowRoot();

String id = "identifier";

Element child = Browser.getDocument().createElement("div");
child.setAttribute("id", id);
shadowRoot.appendChild(child);
addChildElement(element, child);

ElementUtils.attachExistingElementById(node,
"div", requestedId, id);
Expand All @@ -189,13 +189,12 @@ public void testAttachExistingElementById_elementMissingInDom() {
}

public void testAttachExistingElementById_elementIsAlreadyAssociated() {
Element shadowRoot = setupShadowRoot();

setupShadowRoot();
String id = "identifier";

Element child = Browser.getDocument().createElement("div");
child.setAttribute("id", id);
shadowRoot.appendChild(child);
addChildElement(element, child);


NodeMap map = node.getMap(NodeFeatures.SHADOW_ROOT_DATA);
Expand All @@ -215,70 +214,21 @@ public void testAttachExistingElementById_elementIsAlreadyAssociated() {
assertRpcToServerArguments(99, child.getTagName(), id);
}

// This test emulates FireFox that doesn't hide element children under shadowRoot
public void testAttachExistingElementById_elementOutsideShadowRoot() {
Browser.getDocument().getBody().appendChild(element);
setupShadowRoot();

String id = "identifier";

Element child = Browser.getDocument().createElement("div");
child.setAttribute("id", id);
element.appendChild(child);

ElementUtils.attachExistingElementById(node,
"div", requestedId, id);

assertRpcToServerArguments(requestedId, child.getTagName(), id);
}

// This test emulates Edge that doesn't support requesting getElementById from shadowRoot
public void testAttachExistingElementById_noByIdMethodInShadowRoot() {
Browser.getDocument().getBody().appendChild(element);
Element shadowRoot = addShadowRootWithoutGetElementById(element);

NodeMap map = node.getMap(NodeFeatures.SHADOW_ROOT_DATA);
map.getProperty(NodeFeatures.SHADOW_ROOT).setValue(new StateNode(34, tree));

String id = "identifier";

Element child = Browser.getDocument().createElement("div");
child.setAttribute("id", id);
element.appendChild(child);

ElementUtils.attachExistingElementById(node,
"div", requestedId, id);

assertRpcToServerArguments(requestedId, child.getTagName(), id);
}

private Element setupShadowRoot() {
Element shadowRoot = addShadowRoot(element);
private void setupShadowRoot() {
setupParent(element);

NodeMap map = node.getMap(NodeFeatures.SHADOW_ROOT_DATA);
map.getProperty(NodeFeatures.SHADOW_ROOT).setValue(new StateNode(34, tree));

return shadowRoot;
}

private native Element addShadowRoot(Element element) /*-{
element.shadowRoot = $doc.createElement("div");
element.shadowRoot.getElementById = function(id) {
var children = element.shadowRoot.children;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if(child.getAttribute("id") === id) {
return child;
}
}
private native void setupParent(Element element) /*-{
element.$ = function() {
return element;
}
return element.shadowRoot;
}-*/;

private native Element addShadowRootWithoutGetElementById(Element element) /*-{
element.shadowRoot = $doc.createElement("div");
return element.shadowRoot;
private native void addChildElement(Element parent, Element child) /*-{
parent.$[child.getAttribute("id")] = child;
}-*/;

private void assertRpcToServerArguments(int associatedId, String tagName,
Expand Down

0 comments on commit d915ebf

Please sign in to comment.