Java Code Examples for com.google.gwt.dom.client.Text#getLength()

The following examples show how to use com.google.gwt.dom.client.Text#getLength() . 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: ContentTextNode.java    From swellrt with Apache License 2.0 6 votes vote down vote up
/**
 * Splits and returns the second.
 * If split point at a node boundary, doesn't split, but returns the next nodelet.
 */
private Text implSplitText(int offset) {
  findNodeletWithOffset(offset, nodeletOffsetOutput, getRepairer());
  Text text = nodeletOffsetOutput.getNode().<Text>cast();
  if (text.getLength() == nodeletOffsetOutput.getOffset()) {
    return text.getNextSibling().cast();
  } else if (nodeletOffsetOutput.getOffset() == 0) {
    return text;
  } else {
    int nodeletOffset = nodeletOffsetOutput.getOffset();
    Text ret = text.splitText(nodeletOffset);
    // -10000 because the number should be ignored in the splitText case,
    // so some large number to trigger an error if it is not ignored.
    getExtendedContext().editing().textNodeletAffected(
        text, nodeletOffset, -10000, TextNodeChangeType.SPLIT);
    return ret;
  }
}
 
Example 2
Source File: EditorEventHandler.java    From swellrt with Apache License 2.0 6 votes vote down vote up
void checkForWebkitEndOfLinkHack(SignalEvent signal) {
  // If it's inserting text
  if (DomHelper.isTextNode(signal.getTarget()) &&
      (signal.getType().equals(BrowserEvents.DOMCharacterDataModified) ||
       signal.getType().equals(BrowserEvents.DOMNodeInserted))) {

    Text textNode = signal.getTarget().cast();
    if (textNode.getLength() > 0) {
      Node e = textNode.getPreviousSibling();
      if (e != null && !DomHelper.isTextNode(e)
          && e.<Element>cast().getTagName().toLowerCase().equals("a")) {

        FocusedPointRange<Node> selection =  editorInteractor.getHtmlSelection();
        if (selection.isCollapsed() && selection.getFocus().getTextOffset() == 0) {
          editorInteractor.noteWebkitEndOfLinkHackOccurred(textNode);
        }
      }
    }
  }
}
 
Example 3
Source File: NodeManager.java    From swellrt with Apache License 2.0 6 votes vote down vote up
/**
 * Converts the given point to a parent-nodeAfter point, splitting a
 * text node if necessary.
 *
 * @param point
 * @return a point at the same location, between node boundaries
 */
public static Point.El<Node> forceElementPoint(Point<Node> point) {
  Point.El<Node> elementPoint = point.asElementPoint();
  if (elementPoint != null) {
    return elementPoint;
  }
  Element parent;
  Node nodeAfter;
  Text text = point.getContainer().cast();
  parent = text.getParentElement();
  int offset = point.getTextOffset();
  if (offset == 0) {
    nodeAfter = text;
  } else if (offset == text.getLength()) {
    nodeAfter = text.getNextSibling();
  } else {
    nodeAfter = text.splitText(offset);
  }
  return Point.inElement(parent, nodeAfter);
}
 
Example 4
Source File: ContentTextNode.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
/**
 * Splits and returns the second.
 * If split point at a node boundary, doesn't split, but returns the next nodelet.
 */
private Text implSplitText(int offset) {
  findNodeletWithOffset(offset, nodeletOffsetOutput, getRepairer());
  Text text = nodeletOffsetOutput.getNode().<Text>cast();
  if (text.getLength() == nodeletOffsetOutput.getOffset()) {
    return text.getNextSibling().cast();
  } else if (nodeletOffsetOutput.getOffset() == 0) {
    return text;
  } else {
    int nodeletOffset = nodeletOffsetOutput.getOffset();
    Text ret = text.splitText(nodeletOffset);
    // -10000 because the number should be ignored in the splitText case,
    // so some large number to trigger an error if it is not ignored.
    getExtendedContext().editing().textNodeletAffected(
        text, nodeletOffset, -10000, TextNodeChangeType.SPLIT);
    return ret;
  }
}
 
Example 5
Source File: EditorEventHandler.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
void checkForWebkitEndOfLinkHack(SignalEvent signal) {
  // If it's inserting text
  if (DomHelper.isTextNode(signal.getTarget()) &&
      (signal.getType().equals(BrowserEvents.DOMCharacterDataModified) ||
       signal.getType().equals(BrowserEvents.DOMNodeInserted))) {

    Text textNode = signal.getTarget().cast();
    if (textNode.getLength() > 0) {
      Node e = textNode.getPreviousSibling();
      if (e != null && !DomHelper.isTextNode(e)
          && e.<Element>cast().getTagName().toLowerCase().equals("a")) {

        FocusedPointRange<Node> selection =  editorInteractor.getHtmlSelection();
        if (selection.isCollapsed() && selection.getFocus().getTextOffset() == 0) {
          editorInteractor.noteWebkitEndOfLinkHackOccurred(textNode);
        }
      }
    }
  }
}
 
Example 6
Source File: NodeManager.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
/**
 * Converts the given point to a parent-nodeAfter point, splitting a
 * text node if necessary.
 *
 * @param point
 * @return a point at the same location, between node boundaries
 */
public static Point.El<Node> forceElementPoint(Point<Node> point) {
  Point.El<Node> elementPoint = point.asElementPoint();
  if (elementPoint != null) {
    return elementPoint;
  }
  Element parent;
  Node nodeAfter;
  Text text = point.getContainer().cast();
  parent = text.getParentElement();
  int offset = point.getTextOffset();
  if (offset == 0) {
    nodeAfter = text;
  } else if (offset == text.getLength()) {
    nodeAfter = text.getNextSibling();
  } else {
    nodeAfter = text.splitText(offset);
  }
  return Point.inElement(parent, nodeAfter);
}
 
Example 7
Source File: ContentTextNode.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * @see RawDocument#deleteData(Object, int, int)
 */
void deleteData(int offset, int count, boolean affectImpl) {
  String data = getData();

  setContentData(
      data.substring(0, offset) +
      data.substring(offset + count, data.length()));

  if (affectImpl) {
    if (isImplAttached()) {
      findNodeletWithOffset(offset, nodeletOffsetOutput, getRepairer());
      Text nodelet = nodeletOffsetOutput.getNode().cast();
      int subOffset = nodeletOffsetOutput.getOffset();

      if (nodelet.getLength() - subOffset >= count) {
        // Handle the special case where the delete is in a single text nodelet
        // carefully, to avoid splitting it
        nodelet.deleteData(subOffset, count);
        getExtendedContext().editing().textNodeletAffected(
            nodelet, subOffset, -count, TextNodeChangeType.DATA);
      } else {
        // General case
        Node toExcl = implSplitText(offset + count);
        Node fromIncl = implSplitText(offset);

        HtmlView filteredHtml = getFilteredHtmlView();
        for (Node node = fromIncl; node != toExcl && node != null;) {
          Node next = filteredHtml.getNextSibling(node);
          node.removeFromParent();
          node = next;
        }
      }
    } else {
      // TODO(user): have these assertion failure fixed (b/2129931)
      // assert getImplNodelet().getLength() == getLength() :
      //    "text node's html impl not normalised while not attached to html dom";
      getImplNodelet().deleteData(offset, count);
    }
  }
}
 
Example 8
Source File: ContentTextNode.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * Compacts the multiple impl text nodelets into one
 * @throws HtmlMissing
 */
public void normaliseImplThrow() throws HtmlMissing {
  // TODO(danilatos): Some code in line container depends on the isImplAttached() check,
  // but sometimes it might not be attached but should, and so should throw an exception.
  if (!isContentAttached() || !isImplAttached()) {
    simpleNormaliseImpl();
  }

  Text first = getImplNodelet();
  if (first.getLength() == getLength()) {
    return;
  }

  ContentNode next = checkNodeAndNeighbour(this);
  HtmlView filteredHtml = getFilteredHtmlView();

  //String sum = "";
  Node nextImpl = (next == null) ? null : next.getImplNodelet();
  for (Text nodelet = first; nodelet != nextImpl && nodelet != null;
      nodelet = filteredHtml.getNextSibling(first).cast()) {
    //sum += nodelet.getData();
    if (nodelet != first) {
      getExtendedContext().editing().textNodeletAffected(
          nodelet, -1000, -1000, TextNodeChangeType.REMOVE);
      nodelet.removeFromParent();
    }
  }

  getExtendedContext().editing().textNodeletAffected(
      first, -1000, -1000, TextNodeChangeType.REPLACE_DATA);
  first.setData(getData());
}
 
Example 9
Source File: FakeUser.java    From swellrt with Apache License 2.0 5 votes vote down vote up
public void moveCaret(int distance) {
  Point<Node> caret = getSelectionStart();
  if (!caret.isInTextNode()) {
    Node before = Point.nodeBefore(htmlView, caret.asElementPoint());
    if (DomHelper.isTextNode(before)) {
      caret = Point.inText(before, before.<Text>cast().getLength());
    } else if (DomHelper.isTextNode(caret.getNodeAfter())) {
      caret = Point.inText(caret.getNodeAfter(), 0);
    } else {
      throw new RuntimeException("Unimplemented/Invalid");
    }
  }
  Text nodelet = caret.getContainer().cast();
  int offset = caret.getTextOffset() + distance;
  while (offset < 0) {
    nodelet = htmlView.getPreviousSibling(nodelet).cast();
    if (nodelet == null || !DomHelper.isTextNode(nodelet)) {
      throw new RuntimeException("Action ran off end of text node");
    }
    offset += nodelet.getLength();
  }
  while (offset > nodelet.getLength()) {
    offset -= nodelet.getLength();
    nodelet = htmlView.getPreviousSibling(nodelet).cast();
    if (nodelet == null || !DomHelper.isTextNode(nodelet)) {
      throw new RuntimeException("Action ran off end of text node");
    }
  }
  setCaret(Point.inText((Node)nodelet, offset));
}
 
Example 10
Source File: ContentTextNode.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * @see RawDocument#deleteData(Object, int, int)
 */
void deleteData(int offset, int count, boolean affectImpl) {
  String data = getData();

  setContentData(
      data.substring(0, offset) +
      data.substring(offset + count, data.length()));

  if (affectImpl) {
    if (isImplAttached()) {
      findNodeletWithOffset(offset, nodeletOffsetOutput, getRepairer());
      Text nodelet = nodeletOffsetOutput.getNode().cast();
      int subOffset = nodeletOffsetOutput.getOffset();

      if (nodelet.getLength() - subOffset >= count) {
        // Handle the special case where the delete is in a single text nodelet
        // carefully, to avoid splitting it
        nodelet.deleteData(subOffset, count);
        getExtendedContext().editing().textNodeletAffected(
            nodelet, subOffset, -count, TextNodeChangeType.DATA);
      } else {
        // General case
        Node toExcl = implSplitText(offset + count);
        Node fromIncl = implSplitText(offset);

        HtmlView filteredHtml = getFilteredHtmlView();
        for (Node node = fromIncl; node != toExcl && node != null;) {
          Node next = filteredHtml.getNextSibling(node);
          node.removeFromParent();
          node = next;
        }
      }
    } else {
      // TODO(user): have these assertion failure fixed (b/2129931)
      // assert getImplNodelet().getLength() == getLength() :
      //    "text node's html impl not normalised while not attached to html dom";
      getImplNodelet().deleteData(offset, count);
    }
  }
}
 
Example 11
Source File: ContentTextNode.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Compacts the multiple impl text nodelets into one
 * @throws HtmlMissing
 */
public void normaliseImplThrow() throws HtmlMissing {
  // TODO(danilatos): Some code in line container depends on the isImplAttached() check,
  // but sometimes it might not be attached but should, and so should throw an exception.
  if (!isContentAttached() || !isImplAttached()) {
    simpleNormaliseImpl();
  }

  Text first = getImplNodelet();
  if (first.getLength() == getLength()) {
    return;
  }

  ContentNode next = checkNodeAndNeighbour(this);
  HtmlView filteredHtml = getFilteredHtmlView();

  //String sum = "";
  Node nextImpl = (next == null) ? null : next.getImplNodelet();
  for (Text nodelet = first; nodelet != nextImpl && nodelet != null;
      nodelet = filteredHtml.getNextSibling(first).cast()) {
    //sum += nodelet.getData();
    if (nodelet != first) {
      getExtendedContext().editing().textNodeletAffected(
          nodelet, -1000, -1000, TextNodeChangeType.REMOVE);
      nodelet.removeFromParent();
    }
  }

  getExtendedContext().editing().textNodeletAffected(
      first, -1000, -1000, TextNodeChangeType.REPLACE_DATA);
  first.setData(getData());
}
 
Example 12
Source File: FakeUser.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
public void moveCaret(int distance) {
  Point<Node> caret = getSelectionStart();
  if (!caret.isInTextNode()) {
    Node before = Point.nodeBefore(htmlView, caret.asElementPoint());
    if (DomHelper.isTextNode(before)) {
      caret = Point.inText(before, before.<Text>cast().getLength());
    } else if (DomHelper.isTextNode(caret.getNodeAfter())) {
      caret = Point.inText(caret.getNodeAfter(), 0);
    } else {
      throw new RuntimeException("Unimplemented/Invalid");
    }
  }
  Text nodelet = caret.getContainer().cast();
  int offset = caret.getTextOffset() + distance;
  while (offset < 0) {
    nodelet = htmlView.getPreviousSibling(nodelet).cast();
    if (nodelet == null || !DomHelper.isTextNode(nodelet)) {
      throw new RuntimeException("Action ran off end of text node");
    }
    offset += nodelet.getLength();
  }
  while (offset > nodelet.getLength()) {
    offset -= nodelet.getLength();
    nodelet = htmlView.getPreviousSibling(nodelet).cast();
    if (nodelet == null || !DomHelper.isTextNode(nodelet)) {
      throw new RuntimeException("Action ran off end of text node");
    }
  }
  setCaret(Point.inText((Node)nodelet, offset));
}
 
Example 13
Source File: HtmlViewImpl.java    From swellrt with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public int getLength(Text textNode) {
  return textNode.getLength();
}
 
Example 14
Source File: HtmlViewImpl.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public int getLength(Text textNode) {
  return textNode.getLength();
}