Java Code Examples for com.google.gwt.dom.client.Node#getParentElement()

The following examples show how to use com.google.gwt.dom.client.Node#getParentElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Repairer.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
private void reattachImplChildren(Node parentNodelet, Node nodeletBefore, Node nodeletAfter,
    ContentNode first, ContentNode last) {
  // TODO(danilatos): Replace this hairy code with pre-order traversal
  // getters, once they exist
  while (true) {
    Node nodelet = nodeletBefore == null
        ? strippingView.getFirstChild(parentNodelet)
        : strippingView.getNextSibling(nodeletBefore);

    if (nodelet == null || nodelet == nodeletAfter) {
      break;
    }

    if (nodelet.getParentElement() != null) {
      nodelet.removeFromParent();
    }
  }

  for (ContentNode node = first; node != last; node = renderedView.getNextSibling(node)) {
    parentNodelet.insertBefore(node.getImplNodelet(), nodeletAfter);
  }
}
 
Example 2
Source File: SelectionW3CNative.java    From swellrt with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure that the rendered content view's DOM has focus
 *
 * NOTE(patcoleman): Fixes firefox bug that causes invalid selections while
 * mutating DOM that doesn't have focus - fixed by finding the next parent element directly
 * editable, and forcing this to have the focus.
 */
private static void setFocusElement(Node node) {
  if (UserAgent.isFirefox()) {
    // adjust to parent if node is a text node
    Element toFocus = null;
    if (DomHelper.isTextNode(node)) {
      toFocus = node.getParentElement();
    } else {
      toFocus = node.<Element>cast();
    }

    // traverse up until we have a concretely editable element:
    while (toFocus != null &&
        DomHelper.getContentEditability(toFocus) != ElementEditability.EDITABLE) {
      toFocus = toFocus.getParentElement();
    }
    // then focus it:
    if (toFocus != null) {
      DomHelper.focus(toFocus);
    }
  }
}
 
Example 3
Source File: SelectionW3CNative.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure that the rendered content view's DOM has focus
 *
 * NOTE(patcoleman): Fixes firefox bug that causes invalid selections while
 * mutating DOM that doesn't have focus - fixed by finding the next parent element directly
 * editable, and forcing this to have the focus.
 */
private static void setFocusElement(Node node) {
  if (UserAgent.isFirefox()) {
    // adjust to parent if node is a text node
    Element toFocus = null;
    if (DomHelper.isTextNode(node)) {
      toFocus = node.getParentElement();
    } else {
      toFocus = node.<Element>cast();
    }

    // traverse up until we have a concretely editable element:
    while (toFocus != null &&
        DomHelper.getContentEditability(toFocus) != ElementEditability.EDITABLE) {
      toFocus = toFocus.getParentElement();
    }
    // then focus it:
    if (toFocus != null) {
      DomHelper.focus(toFocus);
    }
  }
}
 
Example 4
Source File: Repairer.java    From swellrt with Apache License 2.0 6 votes vote down vote up
private void reattachImplChildren(Node parentNodelet, Node nodeletBefore, Node nodeletAfter,
    ContentNode first, ContentNode last) {
  // TODO(danilatos): Replace this hairy code with pre-order traversal
  // getters, once they exist
  while (true) {
    Node nodelet = nodeletBefore == null
        ? strippingView.getFirstChild(parentNodelet)
        : strippingView.getNextSibling(nodeletBefore);

    if (nodelet == null || nodelet == nodeletAfter) {
      break;
    }

    if (nodelet.getParentElement() != null) {
      nodelet.removeFromParent();
    }
  }

  for (ContentNode node = first; node != last; node = renderedView.getNextSibling(node)) {
    parentNodelet.insertBefore(node.getImplNodelet(), nodeletAfter);
  }
}
 
Example 5
Source File: DomHelper.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures the given container contains exactly one child, the given one.
 * Provides the important property that if the container is already the parent
 * of the given child, then the child is not removed and re-added, it is left
 * there; any siblings, if present, are removed.
 *
 * @param container
 * @param child
 */
public static void setOnlyChild(Element container, Node child) {
  if (child.getParentElement() != container) {
    // simple case
    emptyElement(container);
    container.appendChild(child);
  } else {
    // tricky case - avoid removing then re-appending the same child
    while (child.getNextSibling() != null) {
      child.getNextSibling().removeFromParent();
    }
    while (child.getPreviousSibling() != null) {
      child.getPreviousSibling().removeFromParent();
    }
  }
}
 
Example 6
Source File: SelectionCoordinatesHelperW3C.java    From swellrt with Apache License 2.0 5 votes vote down vote up
private OffsetPosition getNearestElementPosition(Node focusNode, int focusOffset) {
  Node startContainer;
  if (focusNode == null) {
    return null;
  }

  Element e =
    DomHelper.isTextNode(focusNode) ? focusNode.getParentElement() : focusNode.<Element>cast();
  return e == null ? null
      : new OffsetPosition(e.getOffsetLeft(), e.getOffsetTop(), e.getOffsetParent());
}
 
Example 7
Source File: StrippingHtmlView.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public Node getVisibleNode(Node node) {
  Node next;
  while (true) {
    next = node.getParentElement();
    if (maybeStrip(node) == false) {
      return node;
    }
    node = next;
  }
}
 
Example 8
Source File: HtmlSelectionHelperImpl.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
private boolean isInChildEditor(Point<Node> point) {
  // The editable doc is marked by an attribute EDITABLE_DOC_MARKER, if
  // an element is found with that attribute, and is not the element of this
  // editor's doc element, then it must be a child's doc element.
  Node n = point.getContainer();
  Element e = DomHelper.isTextNode(n) ? n.getParentElement() : (Element) n;
  while (e != null && e != doc) {
    if (e.hasAttribute(EditorImpl.EDITABLE_DOC_MARKER)) {
      return true;
    }
    e = e.getParentElement();
  }
  return false;
}
 
Example 9
Source File: SelectionCoordinatesHelperW3C.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
private IntRange getBounds(Node node, int offset) {
  if (node == null || node.getParentElement() == null) {
    // Return null if cannot get selection, or selection is inside an
    // "unattached" node.
    return null;
  }

  if (DomHelper.isTextNode(node)) {
    Element parentElement = node.getParentElement();
    return new IntRange(parentElement.getAbsoluteTop(), parentElement.getAbsoluteBottom());
  } else {
    Element e = node.<Element>cast();
    return new IntRange(e.getAbsoluteTop(), e.getAbsoluteBottom());
  }
}
 
Example 10
Source File: SelectionCoordinatesHelperW3C.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
private OffsetPosition getPosition(Node focusNode, int focusOffset) {
  if (focusNode == null || focusNode.getParentElement() == null) {
    // Return null if cannot get selection, or selection is inside an
    // "unattached" node.
    return null;
  }

  if (DomHelper.isTextNode(focusNode)) {
    // We don't want to split the existing child text node, so we just add
    // duplicate the string up to the offset.
    Node txt =
        Document.get().createTextNode(
            focusNode.getNodeValue().substring(0, focusOffset));
    try {
      mutationListener.startTransientMutations();
      focusNode.getParentNode().insertBefore(txt, focusNode);
      txt.getParentNode().insertAfter(SPAN, txt);
      OffsetPosition ret =
          new OffsetPosition(SPAN.getOffsetLeft(), SPAN.getOffsetTop(), SPAN.getOffsetParent());

      return ret;
    } finally {
      SPAN.removeFromParent();
      txt.removeFromParent();
      mutationListener.endTransientMutations();
    }
  } else {
    Element e = focusNode.cast();
    return new OffsetPosition(e.getOffsetLeft(), e.getOffsetTop(), e.getOffsetParent());
  }
}
 
Example 11
Source File: SelectionCoordinatesHelperW3C.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
private OffsetPosition getNearestElementPosition(Node focusNode, int focusOffset) {
  Node startContainer;
  if (focusNode == null) {
    return null;
  }

  Element e =
    DomHelper.isTextNode(focusNode) ? focusNode.getParentElement() : focusNode.<Element>cast();
  return e == null ? null
      : new OffsetPosition(e.getOffsetLeft(), e.getOffsetTop(), e.getOffsetParent());
}
 
Example 12
Source File: NodeManager.java    From swellrt with Apache License 2.0 5 votes vote down vote up
private Point<ContentNode> elementNodeToWrapperPoint(Element container, Node nodeAfter)
    throws HtmlInserted, HtmlMissing {
  HtmlView filteredHtml = filteredHtmlView;
  // TODO(danilatos): Generalise/abstract this idiom
  if (filteredHtml.getVisibleNode(nodeAfter) != nodeAfter) {
    nodeAfter = filteredHtml.getNextSibling(nodeAfter);
    if (nodeAfter != null) {
      container = nodeAfter.getParentElement();
    }
  }
  return Point.inElement(findElementWrapper(container), findNodeWrapper(nodeAfter));
}
 
Example 13
Source File: StrippingHtmlView.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public Node getVisibleNode(Node node) {
  Node next;
  while (true) {
    next = node.getParentElement();
    if (maybeStrip(node) == false) {
      return node;
    }
    node = next;
  }
}
 
Example 14
Source File: HtmlSelectionHelperImpl.java    From swellrt with Apache License 2.0 5 votes vote down vote up
private boolean isInChildEditor(Point<Node> point) {
  // The editable doc is marked by an attribute EDITABLE_DOC_MARKER, if
  // an element is found with that attribute, and is not the element of this
  // editor's doc element, then it must be a child's doc element.
  Node n = point.getContainer();
  Element e = DomHelper.isTextNode(n) ? n.getParentElement() : (Element) n;
  while (e != null && e != doc) {
    if (e.hasAttribute(EditorImpl.EDITABLE_DOC_MARKER)) {
      return true;
    }
    e = e.getParentElement();
  }
  return false;
}
 
Example 15
Source File: SelectionCoordinatesHelperW3C.java    From swellrt with Apache License 2.0 5 votes vote down vote up
private OffsetPosition getPosition(Node focusNode, int focusOffset) {
  if (focusNode == null || focusNode.getParentElement() == null) {
    // Return null if cannot get selection, or selection is inside an
    // "unattached" node.
    return null;
  }

  if (DomHelper.isTextNode(focusNode)) {
    // We don't want to split the existing child text node, so we just add
    // duplicate the string up to the offset.
    Node txt =
        Document.get().createTextNode(
            focusNode.getNodeValue().substring(0, focusOffset));
    try {
      mutationListener.startTransientMutations();
      focusNode.getParentNode().insertBefore(txt, focusNode);
      txt.getParentNode().insertAfter(SPAN, txt);
      OffsetPosition ret =
          new OffsetPosition(SPAN.getOffsetLeft(), SPAN.getOffsetTop(), SPAN.getOffsetParent());

      return ret;
    } finally {
      SPAN.removeFromParent();
      txt.removeFromParent();
      mutationListener.endTransientMutations();
    }
  } else {
    Element e = focusNode.cast();
    return new OffsetPosition(e.getOffsetLeft(), e.getOffsetTop(), e.getOffsetParent());
  }
}
 
Example 16
Source File: NodeManager.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
private Point<ContentNode> elementNodeToWrapperPoint(Element container, Node nodeAfter)
    throws HtmlInserted, HtmlMissing {
  HtmlView filteredHtml = filteredHtmlView;
  // TODO(danilatos): Generalise/abstract this idiom
  if (filteredHtml.getVisibleNode(nodeAfter) != nodeAfter) {
    nodeAfter = filteredHtml.getNextSibling(nodeAfter);
    if (nodeAfter != null) {
      container = nodeAfter.getParentElement();
    }
  }
  return Point.inElement(findElementWrapper(container), findNodeWrapper(nodeAfter));
}
 
Example 17
Source File: HtmlViewImpl.java    From swellrt with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public Element getParentElement(Node node) {
  return node.getParentElement();
}
 
Example 18
Source File: ContentDocument.java    From swellrt with Apache License 2.0 4 votes vote down vote up
@Override
protected Node getNextOrPrevNodeDepthFirst(ReadableDocument<Node, Element, Text> doc,
    Node start, Node stopAt, boolean enter, boolean rightwards) {

  if (!DomHelper.isTextNode(start) && start.<Element>cast().getPropertyBoolean(
      ContentElement.COMPLEX_IMPLEMENTATION_MARKER)) {

    // If the nodelet is marked as part of a complex implementation structure
    // (e.g. a part of an image thumbnail's implementation), then we find the
    // next html node by instead popping up into the filtered content view,
    // getting the next node from there, and popping back down into html land.
    // This should be both faster and more accurate than doing a depth first
    // search through the impl dom of an arbitrarily complex doodad.

    // Go upwards to find the backreference to the doodad wrapper
    Element e = start.cast();
    while (!NodeManager.hasBackReference(e)) {
      e = e.getParentElement();
    }

    // This must be true, otherwise we could get into an infinite loop
    assert (start == stopAt || !start.isOrHasChild(stopAt));

    // Try to get a wrapper for stopAt as well.
    // TODO(danilatos): How robust is this?
    // What are the chances that it would ever fail in practice anyway?
    ContentNode stopAtWrapper = null;
    for (int tries = 1; tries <= 3; tries++) {
      try {
        stopAtWrapper = nodeManager.findNodeWrapper(stopAt);
        break;
      } catch (InconsistencyException ex) {
        stopAt = stopAt.getParentElement();
      }
    }

    // Do a depth first next-node-find in the content view
    ContentNode next = DocHelper.getNextOrPrevNodeDepthFirst(renderedContentView,
        NodeManager.getBackReference(e),
        stopAtWrapper, enter, rightwards);

    // return the impl nodelet
    return next != null ? next.getImplNodelet() : null;
  } else {

    // In other cases, just do the default.
    return super.getNextOrPrevNodeDepthFirst(doc, start, stopAt, enter, rightwards);
  }
}
 
Example 19
Source File: ContentDocument.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
@Override
protected Node getNextOrPrevNodeDepthFirst(ReadableDocument<Node, Element, Text> doc,
    Node start, Node stopAt, boolean enter, boolean rightwards) {

  if (!DomHelper.isTextNode(start) && start.<Element>cast().getPropertyBoolean(
      ContentElement.COMPLEX_IMPLEMENTATION_MARKER)) {

    // If the nodelet is marked as part of a complex implementation structure
    // (e.g. a part of an image thumbnail's implementation), then we find the
    // next html node by instead popping up into the filtered content view,
    // getting the next node from there, and popping back down into html land.
    // This should be both faster and more accurate than doing a depth first
    // search through the impl dom of an arbitrarily complex doodad.

    // Go upwards to find the backreference to the doodad wrapper
    Element e = start.cast();
    while (!NodeManager.hasBackReference(e)) {
      e = e.getParentElement();
    }

    // This must be true, otherwise we could get into an infinite loop
    assert (start == stopAt || !start.isOrHasChild(stopAt));

    // Try to get a wrapper for stopAt as well.
    // TODO(danilatos): How robust is this?
    // What are the chances that it would ever fail in practice anyway?
    ContentNode stopAtWrapper = null;
    for (int tries = 1; tries <= 3; tries++) {
      try {
        stopAtWrapper = nodeManager.findNodeWrapper(stopAt);
        break;
      } catch (InconsistencyException ex) {
        stopAt = stopAt.getParentElement();
      }
    }

    // Do a depth first next-node-find in the content view
    ContentNode next = DocHelper.getNextOrPrevNodeDepthFirst(renderedContentView,
        NodeManager.getBackReference(e),
        stopAtWrapper, enter, rightwards);

    // return the impl nodelet
    return next != null ? next.getImplNodelet() : null;
  } else {

    // In other cases, just do the default.
    return super.getNextOrPrevNodeDepthFirst(doc, start, stopAt, enter, rightwards);
  }
}
 
Example 20
Source File: HtmlViewImpl.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public Element getParentElement(Node node) {
  return node.getParentElement();
}