Java Code Examples for com.google.gwt.dom.client.Element#getFirstChild()

The following examples show how to use com.google.gwt.dom.client.Element#getFirstChild() . 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: ContentElement.java    From swellrt with Apache License 2.0 6 votes vote down vote up
/**
  * Add back in the impl nodelets of the children.
  *
  * Override this to provide specific functionality as needed. For example,
  * ensuring certain children live in certain specific locations of the
  * doodad's dom.
  */
protected void reattachImplChildren() {
  ContentView renderedContent = getRenderedContentView();
  Element container = getContainerNodelet();
  if (container != null) {
    while (container.getFirstChild() != null) {
      container.getFirstChild().removeFromParent();
    }
    for (ContentNode node = renderedContent.getFirstChild(this); node != null;
        node = renderedContent.getNextSibling(node)) {
      container.appendChild(node.getImplNodelet());
    }
  } else {
    EditorStaticDeps.logger.error().log(
        "You need to override this method for your doodad: " + tagName);
  }
}
 
Example 2
Source File: DiffHighlightingFilter.java    From swellrt with Apache License 2.0 6 votes vote down vote up
private void deletify(Element element) {
  if (element == null) {
    // NOTE(danilatos): Not handling the case where the content element
    // is transparent w.r.t. the rendered view, but has visible children.
    return;
  }

  DiffManager.styleElement(element, DiffType.DELETE,
      operationCtx.getCreator() != null ? operationCtx.getCreator().getAddress() : "",
      operationCtx.getHashedVersion().getVersion(), operationCtx.getTimestamp());
  DomHelper.makeUnselectable(element);

  for (Node n = element.getFirstChild(); n != null; n = n.getNextSibling()) {
    if (!DomHelper.isTextNode(n)) {
      deletify(n.<Element> cast());
    }
  }
}
 
Example 3
Source File: DiffHighlightingFilter.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
private void deletify(Element element) {
  if (element == null) {
    // NOTE(danilatos): Not handling the case where the content element
    // is transparent w.r.t. the rendered view, but has visible children.
    return;
  }

  DiffManager.styleElement(element, DiffType.DELETE);
  DomHelper.makeUnselectable(element);

  for (Node n = element.getFirstChild(); n != null; n = n.getNextSibling()) {
    if (!DomHelper.isTextNode(n)) {
      deletify(n.<Element> cast());
    }
  }
}
 
Example 4
Source File: ContentElement.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
/**
  * Add back in the impl nodelets of the children.
  *
  * Override this to provide specific functionality as needed. For example,
  * ensuring certain children live in certain specific locations of the
  * doodad's dom.
  */
protected void reattachImplChildren() {
  ContentView renderedContent = getRenderedContentView();
  Element container = getContainerNodelet();
  if (container != null) {
    while (container.getFirstChild() != null) {
      container.getFirstChild().removeFromParent();
    }
    for (ContentNode node = renderedContent.getFirstChild(this); node != null;
        node = renderedContent.getNextSibling(node)) {
      container.appendChild(node.getImplNodelet());
    }
  } else {
    EditorStaticDeps.logger.error().log(
        "You need to override this method for your doodad: " + tagName);
  }
}
 
Example 5
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 6
Source File: ParagraphHelperEmptyLineBr.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void onRemovingChild(Node child, Element paragraph) {
  Node first = paragraph.getFirstChild();
  BRElement spacer = getSpacer(paragraph);
  if (first == null) {
    appendSpacer(paragraph);
  } else if (first != spacer) {
    spacer.removeFromParent();
  }
}
 
Example 7
Source File: ContentElement.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * Mark implementation elements that aren't transparent as part of a
 * a complex implementation structure.
 *
 * @param element
 */
public static void walkImpl(Element element) {
  for (Node n = element.getFirstChild(); n != null;) {
    if (DomHelper.isTextNode(n)) {
      n = n.getNextSibling();
    } else {
      Element e = n.cast();
      if (!NodeManager.isTransparent(e)) {
        e.setPropertyBoolean(COMPLEX_IMPLEMENTATION_MARKER, true);
      }
      walkImpl(e);
      n = n.getNextSibling();
    }
  }
}
 
Example 8
Source File: StrippingHtmlView.java    From swellrt 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 9
Source File: ContentElement.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Mark implementation elements that aren't transparent as part of a
 * a complex implementation structure.
 *
 * @param element
 */
public static void walkImpl(Element element) {
  for (Node n = element.getFirstChild(); n != null;) {
    if (DomHelper.isTextNode(n)) {
      n = n.getNextSibling();
    } else {
      Element e = n.cast();
      if (!NodeManager.isTransparent(e)) {
        e.setPropertyBoolean(COMPLEX_IMPLEMENTATION_MARKER, true);
      }
      walkImpl(e);
      n = n.getNextSibling();
    }
  }
}
 
Example 10
Source File: ParagraphHelperEmptyLineBr.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void onRemovingChild(Node child, Element paragraph) {
  Node first = paragraph.getFirstChild();
  BRElement spacer = getSpacer(paragraph);
  if (first == null) {
    appendSpacer(paragraph);
  } else if (first != spacer) {
    spacer.removeFromParent();
  }
}
 
Example 11
Source File: CheckBox.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
private void updateCheckboxDom(ContentElement checkbox, boolean isChecked) {
  Element implNodelet = checkbox.getImplNodelet();
  InputElement checkboxElem = (InputElement) implNodelet.getFirstChild();
  checkboxElem.setChecked(isChecked);
}
 
Example 12
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 13
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 14
Source File: DomHelper.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
/**
 * Remove all children from an element
 * @param element
 */
public static void emptyElement(Element element) {
  while (element.getFirstChild() != null) {
    element.removeChild(element.getFirstChild());
  }
}
 
Example 15
Source File: CheckBox.java    From swellrt with Apache License 2.0 4 votes vote down vote up
private void updateCheckboxDom(ContentElement checkbox, boolean isChecked) {
  Element implNodelet = checkbox.getImplNodelet();
  InputElement checkboxElem = (InputElement) implNodelet.getFirstChild();
  checkboxElem.setChecked(isChecked);
}
 
Example 16
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 17
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 18
Source File: DomHelper.java    From swellrt with Apache License 2.0 4 votes vote down vote up
/**
 * Remove all children from an element
 * @param element
 */
public static void emptyElement(Element element) {
  while (element.getFirstChild() != null) {
    element.removeChild(element.getFirstChild());
  }
}