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

The following examples show how to use com.google.gwt.dom.client.Node#getNextSibling() . 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: DomHelper.java    From swellrt 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 2
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 3
Source File: SelectionImplIE.java    From swellrt with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unused") // NOTE(user): may be used later
private Point<Node> linearSearchForRange(JsTextRangeIE target, Element parent) {
  try {
    // We'll iterate through the parent's children while moving
    // a new collapsed range, attempt, through the points before
    // each child.
    Node child = parent.getFirstChild();
    // Start attempt at beginning of parent
    JsTextRangeIE attempt = JsTextRangeIE.create().moveToElementText(parent).collapse(true);
    while (child != null) {
      // Treat text node children separately
      if (DomHelper.isTextNode(child)) {
        // Move attempt to end of the text node
        int len = child.<Text> cast().getLength();
        attempt.move(character, len);
        // Test if attempt is now at or past target
        if (attempt.compareEndPoints(StartToStart, target) >= 0) {
          // Target is in this text node. Compute the offset by creating a new
          // text range from target to attempt and measuring the length of the
          // text in that range
          JsTextRangeIE dup =
              attempt.duplicate().setEndPoint(StartToStart, target)
                  .setEndPoint(EndToEnd, attempt);
          return Point.inText(child, len - dup.getText().length());
        }
      } else {
        // Child is an element. Move attempt before child, and test
        // if attempt is at or past range
        attempt.moveToElementText(child.<Element> cast()).collapse(true);
        if (attempt.compareEndPoints(StartToStart, target) >= 0) {
          // Return the point before child
          return Point.inElement(parent, child);
        } else {
          // Move attempt past child
          // We use our inline, non-empty marker element to do this.
          // We also leave it in the dom for max reliability until it's needed
          // later, or gets taken out in the finally clause at the end of this method
          child.getParentNode().insertAfter(setter, child);
          // skip pass the setter.
          child = child.getNextSibling();

          attempt.moveToElementText(setter).collapse(false);
        }
      }
      // Move to next child
      child = child.getNextSibling();
    }

    // We didn't find target before or in children; return point at end of
    // parent
    return Point.<Node> end(parent);
    // TODO(user): look out for other corner cases
    // TODO(user, danilatos): implement danilatos' optimisation of first
    // checking inside the last text node that held a point.
    // TODO(user): consider binary rather than linear search for target
    // TODO(user): does this handle the end of a <p>ab[</p><p>]cd</p> type
    // selection?
    // TODO(danilatos): When someone selects the "newline" at the edge
    // of a <p>, e.g. <p>abc[</p><p>]def</p> (where [ ] is the sel)
    // this reports the selection still as <p>abc[]</p><p>def</p>
    // It appears to be a detectable scenario when the first line
    // is not empty, but it isn't when both lines are empty. However,
    // I did notice a difference before when I was experimenting with
    // getBookmark(), so perhaps we could resort to tricks with checking
    // bookmarks around paragraph boundaries...
    // TODO(user, danilatos): consider making attempt and other ranges
    // used here static singletons
  } finally {
    setter.removeFromParent();
  }
}
 
Example 4
Source File: HTMLPretty.java    From swellrt with Apache License 2.0 4 votes vote down vote up
/**
 * @param n Node to pretty print
 * @param selection Selection to mark in pretty print
 * @param indent Indentation level to print with.
 * @param multiLine True if output should be multi-line
 * @return A pretty-print HTML string (with '<' and '>' already escaped)
 */
private static String print(Node n, PointRange<Node> selection, int indent, boolean multiLine) {

  // Inspect selection's relevance to this element
  boolean collapsed = selection != null && selection.isCollapsed();
  boolean printStartMarker =
      selection != null && selection.getFirst().getContainer().equals(n);
  boolean printEndMarker =
      selection != null && !collapsed && selection.getSecond().getContainer().equals(n);
  String startMarker =
      printStartMarker ? (collapsed ? "|" : "[") : "";
  String endMarker = printEndMarker ? "]" : "";
  if (DomHelper.isTextNode(n)) {
    // Print text node as 'value'
    String value = displayWhitespace(n.getNodeValue());
    int startOffset = printStartMarker ? selection.getFirst().getTextOffset() : 0;
    int endOffset = printEndMarker ? selection.getSecond().getTextOffset() : value.length();
    String ret = "'" + value.substring(0, startOffset)
        + startMarker
        + value.substring(startOffset, endOffset)
        + endMarker
        + value.substring(endOffset, value.length())
        + "'" ;
    return multiLine ? StringUtil.xmlEscape(ret) : ret;
  } else {
    Element e = n.cast();
    if (e.getChildCount() == 0) {
      // Print child-less element as self-closing tag
      return startTag(e, true, multiLine);
    } else {
      boolean singleLineHtml = multiLine &&
        (e.getChildCount() == 1 &&
          e.getFirstChild()
          .getChildCount() == 0);
      // Print element w/ children. One line each for start tag, child, end tag
      String pretty = startTag(e, false, multiLine);
      Node child = e.getFirstChild();
      Node startNodeAfter = selection.getFirst().getNodeAfter();
      Node endNodeAfter = selection.getSecond().getNodeAfter();
      while (child != null) {
        pretty += (multiLine && !singleLineHtml ? newLine(indent + 1) : "")
          + (printStartMarker && child.equals(startNodeAfter) ? startMarker : "")
          + (printEndMarker && child.equals(endNodeAfter) ? endMarker : "")
          + print(child, selection, indent + 1, multiLine);
        child = child.getNextSibling();
      }
      if (printEndMarker && endNodeAfter == null) {
        pretty += endMarker;
      }
      return pretty + (multiLine  && !singleLineHtml ? newLine(indent) : "")
          + endTag(e, multiLine);
    }
  }
}
 
Example 5
Source File: HtmlViewImpl.java    From swellrt with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public Node getNextSibling(Node node) {
  return node.getNextSibling();
}
 
Example 6
Source File: SelectionImplIE.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unused") // NOTE(user): may be used later
private Point<Node> linearSearchForRange(JsTextRangeIE target, Element parent) {
  try {
    // We'll iterate through the parent's children while moving
    // a new collapsed range, attempt, through the points before
    // each child.
    Node child = parent.getFirstChild();
    // Start attempt at beginning of parent
    JsTextRangeIE attempt = JsTextRangeIE.create().moveToElementText(parent).collapse(true);
    while (child != null) {
      // Treat text node children separately
      if (DomHelper.isTextNode(child)) {
        // Move attempt to end of the text node
        int len = child.<Text> cast().getLength();
        attempt.move(character, len);
        // Test if attempt is now at or past target
        if (attempt.compareEndPoints(StartToStart, target) >= 0) {
          // Target is in this text node. Compute the offset by creating a new
          // text range from target to attempt and measuring the length of the
          // text in that range
          JsTextRangeIE dup =
              attempt.duplicate().setEndPoint(StartToStart, target)
                  .setEndPoint(EndToEnd, attempt);
          return Point.inText(child, len - dup.getText().length());
        }
      } else {
        // Child is an element. Move attempt before child, and test
        // if attempt is at or past range
        attempt.moveToElementText(child.<Element> cast()).collapse(true);
        if (attempt.compareEndPoints(StartToStart, target) >= 0) {
          // Return the point before child
          return Point.inElement(parent, child);
        } else {
          // Move attempt past child
          // We use our inline, non-empty marker element to do this.
          // We also leave it in the dom for max reliability until it's needed
          // later, or gets taken out in the finally clause at the end of this method
          child.getParentNode().insertAfter(setter, child);
          // skip pass the setter.
          child = child.getNextSibling();

          attempt.moveToElementText(setter).collapse(false);
        }
      }
      // Move to next child
      child = child.getNextSibling();
    }

    // We didn't find target before or in children; return point at end of
    // parent
    return Point.<Node> end(parent);
    // TODO(user): look out for other corner cases
    // TODO(user, danilatos): implement danilatos' optimisation of first
    // checking inside the last text node that held a point.
    // TODO(user): consider binary rather than linear search for target
    // TODO(user): does this handle the end of a <p>ab[</p><p>]cd</p> type
    // selection?
    // TODO(danilatos): When someone selects the "newline" at the edge
    // of a <p>, e.g. <p>abc[</p><p>]def</p> (where [ ] is the sel)
    // this reports the selection still as <p>abc[]</p><p>def</p>
    // It appears to be a detectable scenario when the first line
    // is not empty, but it isn't when both lines are empty. However,
    // I did notice a difference before when I was experimenting with
    // getBookmark(), so perhaps we could resort to tricks with checking
    // bookmarks around paragraph boundaries...
    // TODO(user, danilatos): consider making attempt and other ranges
    // used here static singletons
  } finally {
    setter.removeFromParent();
  }
}
 
Example 7
Source File: HTMLPretty.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
/**
 * @param n Node to pretty print
 * @param selection Selection to mark in pretty print
 * @param indent Indentation level to print with.
 * @param multiLine True if output should be multi-line
 * @return A pretty-print HTML string (with '<' and '>' already escaped)
 */
private static String print(Node n, PointRange<Node> selection, int indent, boolean multiLine) {

  // Inspect selection's relevance to this element
  boolean collapsed = selection != null && selection.isCollapsed();
  boolean printStartMarker =
      selection != null && selection.getFirst().getContainer().equals(n);
  boolean printEndMarker =
      selection != null && !collapsed && selection.getSecond().getContainer().equals(n);
  String startMarker =
      printStartMarker ? (collapsed ? "|" : "[") : "";
  String endMarker = printEndMarker ? "]" : "";
  if (DomHelper.isTextNode(n)) {
    // Print text node as 'value'
    String value = displayWhitespace(n.getNodeValue());
    int startOffset = printStartMarker ? selection.getFirst().getTextOffset() : 0;
    int endOffset = printEndMarker ? selection.getSecond().getTextOffset() : value.length();
    String ret = "'" + value.substring(0, startOffset)
        + startMarker
        + value.substring(startOffset, endOffset)
        + endMarker
        + value.substring(endOffset, value.length())
        + "'" ;
    return multiLine ? StringUtil.xmlEscape(ret) : ret;
  } else {
    Element e = n.cast();
    if (e.getChildCount() == 0) {
      // Print child-less element as self-closing tag
      return startTag(e, true, multiLine);
    } else {
      boolean singleLineHtml = multiLine &&
        (e.getChildCount() == 1 &&
          e.getFirstChild()
          .getChildCount() == 0);
      // Print element w/ children. One line each for start tag, child, end tag
      String pretty = startTag(e, false, multiLine);
      Node child = e.getFirstChild();
      Node startNodeAfter = selection.getFirst().getNodeAfter();
      Node endNodeAfter = selection.getSecond().getNodeAfter();
      while (child != null) {
        pretty += (multiLine && !singleLineHtml ? newLine(indent + 1) : "")
          + (printStartMarker && child.equals(startNodeAfter) ? startMarker : "")
          + (printEndMarker && child.equals(endNodeAfter) ? endMarker : "")
          + print(child, selection, indent + 1, multiLine);
        child = child.getNextSibling();
      }
      if (printEndMarker && endNodeAfter == null) {
        pretty += endMarker;
      }
      return pretty + (multiLine  && !singleLineHtml ? newLine(indent) : "")
          + endTag(e, multiLine);
    }
  }
}
 
Example 8
Source File: HtmlViewImpl.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public Node getNextSibling(Node node) {
  return node.getNextSibling();
}