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

The following examples show how to use com.google.gwt.dom.client.Node#cast() . 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: ContentDocument.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
@Override
protected Skip getSkipLevel(Node node) {
  // TODO(danilatos): Detect and repair new elements. Currently we just ignore them.
  if (DomHelper.isTextNode(node) || NodeManager.hasBackReference(node.<Element>cast())) {
    return Skip.NONE;
  } else {
    Element element = node.<Element>cast();

    Skip level = NodeManager.getTransparency(element);
    if (level == null) {
      if (!getDocumentElement().isOrHasChild(element)) {
        return Skip.INVALID;
      }
      register(element);
    }
    // For now, we treat unknown nodes as shallow as well.
    // TODO(danilatos): Either strip them or extract them
    return level == null ? Skip.SHALLOW : level;
  }
}
 
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: ContentDocument.java    From swellrt with Apache License 2.0 6 votes vote down vote up
@Override
protected Skip getSkipLevel(Node node) {
  // TODO(danilatos): Detect and repair new elements. Currently we just ignore them.
  if (DomHelper.isTextNode(node) || NodeManager.hasBackReference(node.<Element>cast())) {
    return Skip.NONE;
  } else {
    Element element = node.<Element>cast();

    Skip level = NodeManager.getTransparency(element);
    if (level == null) {
      if (!getDocumentElement().isOrHasChild(element)) {
        return Skip.INVALID;
      }
      register(element);
    }
    // For now, we treat unknown nodes as shallow as well.
    // TODO(danilatos): Either strip them or extract them
    return level == null ? Skip.SHALLOW : level;
  }
}
 
Example 4
Source File: ParagraphHelperBr.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
/**
 * Get the spacer for the given paragraph.
 * Lazily creates & registers one if not present.
 * If there's one that the browser created, registers it as our own.
 * If the browser put a different one in to the one that we were already
 * using, replace ours with the browser's.
 * @param paragraph
 * @return The spacer
 */
protected BRElement getSpacer(Element paragraph) {
  Node last = paragraph.getLastChild();
  BRElement spacer = paragraph.getPropertyJSO(BR_REF).cast();
  if (spacer == null) {
    // Register our spacer, using one the browser put in if present
    spacer = isSpacer(last) ? last.<BRElement>cast() : Document.get().createBRElement();
    setupSpacer(paragraph, spacer);
  } else if (isSpacer(last) && last != spacer) {
    // The browser put a different one in by itself, so let's use that one
    if (spacer.hasParentElement()) {
      spacer.removeFromParent();
    }
    spacer = last.<BRElement>cast();
    setupSpacer(paragraph, spacer);
  }
  return spacer;
}
 
Example 5
Source File: ContentElement.java    From swellrt with Apache License 2.0 6 votes vote down vote up
/**
 * Also affects the container nodelet. If the current container nodelet is the
 * same as the current impl nodelet, the new container will be the same as the new
 * impl nodelet. If it is null, it will stay null. Other scenarios are not supported
 *
 * @deprecated Use {@link #setImplNodelets(Element, Element)} instead of this method.
 */
@Override
@Deprecated // Use #setImplNodelets(impl, container) instead
public void setImplNodelet(Node nodelet) {
  Preconditions.checkNotNull(nodelet,
      "Null nodelet not supported with this deprecated method, use setImplNodelets instead");
  Preconditions.checkState(containerNodelet == null || containerNodelet == getImplNodelet(),
      "Cannot set only the impl nodelet if the container nodelet is different");
  Preconditions.checkArgument(!DomHelper.isTextNode(nodelet),
      "element cannot have text implnodelet");

  Element element = nodelet.cast();

  if (this.containerNodelet != null) {
    setContainerNodelet(element);
  }
  setImplNodeletInner(element);
}
 
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: 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 8
Source File: DomHelper.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a nodelet/offset pair to a Point of Node.
 * Just a simple mapping, it is agnostic to inconsistencies, filtered views, etc.
 * @param node
 * @param offset
 * @return html node point
 */
public static Point<Node> nodeOffsetToNodeletPoint(Node node, int offset) {
  if (isTextNode(node)) {
    return Point.inText(node, offset);
  } else {
    Element container = node.<Element>cast();
    return Point.inElement(container, nodeAfterFromOffset(container, offset));
  }
}
 
Example 9
Source File: PasteBufferImplSafariAndNewFirefox.java    From swellrt with Apache License 2.0 5 votes vote down vote up
private void maybeStripMarker(Node node, Element parent, boolean leading) {
  if (node == null) {
    logEndNotFound("node is null");
    return;
  }
  if (DomHelper.isTextNode(node)) {
    Text textNode = node.cast();
    String text = textNode.getData();
    if (!text.isEmpty()) {
      if (leading) {
        if (text.charAt(0) == MARKER_CHAR) {
          textNode.setData(text.substring(1));
        }
      } else {
        if (text.charAt(text.length() - 1) == MARKER_CHAR) {
          textNode.setData(text.substring(0, text.length() - 1));
        } else {
          logEndNotFound("last character is not marker");
        }
      }
    } else {
      logEndNotFound("text node is empty");
    }
    if (textNode.getData().isEmpty()) {
      parent.removeChild(textNode);
    }
  } else {
    // In some cases, Safari will put the marker inside of a div, so this
    // traverses down the left or right side of the tree to find it.
    // For example: x<div><span>pasted</span>x</div>
    maybeStripMarker(leading ? node.getFirstChild() : node.getLastChild(), node.<Element> cast(),
        leading);
  }
}
 
Example 10
Source File: StrippingHtmlView.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Remove the given node (leaving its children in the dom) if
 * it does not correspond to a wrapper ContentNode
 * @param node
 * @return true if the node was removed, false if not.
 */
private boolean maybeStrip(Node node) {
  if (node == null || DomHelper.isTextNode(node)) return false;

  Element element = node.cast();
  if (!NodeManager.hasBackReference(element)) {
    Node n;
    while ((n = element.getFirstChild()) != null) {
      element.getParentNode().insertBefore(n, element);
    }
    element.removeFromParent();
    return true;
  }
  return false;
}
 
Example 11
Source File: ParagraphHelperBr.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * @param n Node to test, or null
 * @return true if n is a spacer <br/>, WHETHER OR NOT we created it
 *   or the browser's native editor created it
 */
protected boolean isSpacer(Node n) {
  if (n == null) {
    return false;
  }
  if (DomHelper.isTextNode(n)) {
    return false;
  }
  Element e = n.cast();
  return e.getTagName().toLowerCase().equals("br") && !NodeManager.hasBackReference(e);
}
 
Example 12
Source File: SelectionCoordinatesHelperW3C.java    From swellrt 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 13
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 14
Source File: DomHelper.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a nodelet/offset pair to a Point of Node.
 * Just a simple mapping, it is agnostic to inconsistencies, filtered views, etc.
 * @param node
 * @param offset
 * @return html node point
 */
public static Point<Node> nodeOffsetToNodeletPoint(Node node, int offset) {
  if (isTextNode(node)) {
    return Point.inText(node, offset);
  } else {
    Element container = node.<Element>cast();
    return Point.inElement(container, nodeAfterFromOffset(container, offset));
  }
}
 
Example 15
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 16
Source File: TypingExtractor.java    From swellrt with Apache License 2.0 4 votes vote down vote up
/**
 *
 * TODO: use isSameRange. currently, it'll just start a new typing sequence
 * @param previousSelectionStart Where the selection start is,
 *    before the result of typing
 * @throws HtmlMissing
 * @throws HtmlInserted
 */
public void somethingHappened(Point<Node> previousSelectionStart)
    throws HtmlMissing, HtmlInserted {
  Preconditions.checkNotNull(previousSelectionStart,
      "Typing extractor notified with null selection");
  Text node = previousSelectionStart.isInTextNode()
    ? previousSelectionStart.getContainer().<Text>cast()
    : null;

  // Attempt to associate our location with a node
  // This should be a last resort, ideally we should be given selections
  // in the correct text node, when the selection is at a text node boundary
  if (node == null) {
    HtmlView filteredHtml = filteredHtmlView;
    Node nodeBefore = Point.nodeBefore(filteredHtml, previousSelectionStart.asElementPoint());
    Node nodeAfter = previousSelectionStart.getNodeAfter();
    //TODO(danilatos): Usually we would want nodeBefore as a preference, but
    // not always...
    if (nodeBefore != null && DomHelper.isTextNode(nodeBefore)) {
      node = nodeBefore.cast();
      previousSelectionStart = Point.<Node>inText(node, 0);
    } else if (nodeAfter != null && DomHelper.isTextNode(nodeAfter)) {
      node = nodeAfter.cast();
      previousSelectionStart = Point.<Node>inText(node, node.getLength());
    }
  }

  TypingState t = findTypingState(previousSelectionStart);

  if (t == null) {
    t = getFreshTypingState();
    if (node != null) {
      // check the selection is in a text point, and start the sequence
      Preconditions.checkNotNull(previousSelectionStart.asTextPoint(),
          "previousSelectionStart must be a text point");
      t.startTypingSequence(previousSelectionStart.asTextPoint());
    } else {
      // otherwise make sure we're not in a text point, and start the sequence
      Preconditions.checkState(!previousSelectionStart.isInTextNode(),
          "previousSelectionStart must not be a text point");
      t.startTypingSequence(previousSelectionStart.asElementPoint());
    }
  } else {
    t.continueTypingSequence(previousSelectionStart);
  }

  t.setLastTextNode(node);
  timerService.schedule(flushCmd);
}
 
Example 17
Source File: TypingExtractor.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
/**
 *
 * TODO: use isSameRange. currently, it'll just start a new typing sequence
 * @param previousSelectionStart Where the selection start is,
 *    before the result of typing
 * @throws HtmlMissing
 * @throws HtmlInserted
 */
public void somethingHappened(Point<Node> previousSelectionStart)
    throws HtmlMissing, HtmlInserted {
  Preconditions.checkNotNull(previousSelectionStart,
      "Typing extractor notified with null selection");
  Text node = previousSelectionStart.isInTextNode()
    ? previousSelectionStart.getContainer().<Text>cast()
    : null;

  // Attempt to associate our location with a node
  // This should be a last resort, ideally we should be given selections
  // in the correct text node, when the selection is at a text node boundary
  if (node == null) {
    HtmlView filteredHtml = filteredHtmlView;
    Node nodeBefore = Point.nodeBefore(filteredHtml, previousSelectionStart.asElementPoint());
    Node nodeAfter = previousSelectionStart.getNodeAfter();
    //TODO(danilatos): Usually we would want nodeBefore as a preference, but
    // not always...
    if (nodeBefore != null && DomHelper.isTextNode(nodeBefore)) {
      node = nodeBefore.cast();
      previousSelectionStart = Point.<Node>inText(node, 0);
    } else if (nodeAfter != null && DomHelper.isTextNode(nodeAfter)) {
      node = nodeAfter.cast();
      previousSelectionStart = Point.<Node>inText(node, node.getLength());
    }
  }

  TypingState t = findTypingState(previousSelectionStart);

  if (t == null) {
    t = getFreshTypingState();
    if (node != null) {
      // check the selection is in a text point, and start the sequence
      Preconditions.checkNotNull(previousSelectionStart.asTextPoint(),
          "previousSelectionStart must be a text point");
      t.startTypingSequence(previousSelectionStart.asTextPoint());
    } else {
      // otherwise make sure we're not in a text point, and start the sequence
      Preconditions.checkState(!previousSelectionStart.isInTextNode(),
          "previousSelectionStart must not be a text point");
      t.startTypingSequence(previousSelectionStart.asElementPoint());
    }
  } else {
    t.continueTypingSequence(previousSelectionStart);
  }

  t.setLastTextNode(node);
  timerService.schedule(flushCmd);
}
 
Example 18
Source File: HtmlViewImpl.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public Text asText(Node node) {
  return DomHelper.isTextNode(node) ? node.<Text>cast() : null;
}
 
Example 19
Source File: NodeManager.java    From swellrt with Apache License 2.0 3 votes vote down vote up
/**
 * Converts a nodelet/offset pair to a Point of ContentNode.
 * Does its best in the face of adversity to yield reasonably accurate
 * results, but may throw inconsistency exceptions.
 *
 * @param node
 * @param offset
 * @return content node point
 * @throws HtmlInserted
 * @throws HtmlMissing
 */
public Point<ContentNode> nodeOffsetToWrapperPoint(Node node, int offset) throws HtmlInserted,
    HtmlMissing {
  if (DomHelper.isTextNode(node)) {
    return textNodeToWrapperPoint(node.<Text> cast(), offset);
  } else {
    Element container = node.<Element> cast();
    return elementNodeToWrapperPoint(container, DomHelper.nodeAfterFromOffset(container, offset));
  }
}
 
Example 20
Source File: NodeManager.java    From incubator-retired-wave with Apache License 2.0 3 votes vote down vote up
/**
 * Converts a nodelet/offset pair to a Point of ContentNode.
 * Does its best in the face of adversity to yield reasonably accurate
 * results, but may throw inconsistency exceptions.
 *
 * @param node
 * @param offset
 * @return content node point
 * @throws HtmlInserted
 * @throws HtmlMissing
 */
public Point<ContentNode> nodeOffsetToWrapperPoint(Node node, int offset) throws HtmlInserted,
    HtmlMissing {
  if (DomHelper.isTextNode(node)) {
    return textNodeToWrapperPoint(node.<Text> cast(), offset);
  } else {
    Element container = node.<Element> cast();
    return elementNodeToWrapperPoint(container, DomHelper.nodeAfterFromOffset(container, offset));
  }
}