Пример #1
0
 /**
  * Get the node absolute start location in its residing IStructuredModel.
  *
  * @param p
  * @return the location
  */
 public static int getIndexedRegionLocation(IDOMPosition p) {
   if (!EditValidateUtil.validPosition(p)) {
     return -1;
   }
   Node parent = p.getContainerNode();
   if (p.isText()) {
     return ((IndexedRegion) parent).getStartOffset() + p.getOffset();
   }
   int index = p.getOffset();
   if (!parent.hasChildNodes()) {
     // Element:
     if (!isDocument(parent)) {
       IStructuredDocumentRegion region = ((IDOMNode) parent).getStartStructuredDocumentRegion();
       return region.getEnd();
     }
     // Document node:
     int offset = ((IndexedRegion) parent).getStartOffset();
     return offset;
   }
   NodeList children = parent.getChildNodes();
   // After rightmost child
   if (children.getLength() == index) {
     if (!isDocument(parent)) {
       int pos = getNodeEndNameStartIndex(parent);
       return pos;
     }
     int offset = ((IndexedRegion) parent).getEndOffset();
     return offset;
   }
   Node node = children.item(index);
   return ((IndexedRegion) node).getStartOffset();
 }
Пример #2
0
 /**
  * Get node's sibling according to 'forward' direction
  *
  * @param node
  * @param forward
  * @return the node
  */
 public Node getSibling(Node node, boolean forward) {
   EditValidateUtil.validNode(node);
   if (forward) {
     return node.getNextSibling();
   }
   return node.getPreviousSibling();
 }
Пример #3
0
  /**
   * First try sibling, if it retruns null, try search neighbor.
   *
   * @param parent
   * @param childIndex
   * @param forward
   * @return the node
   */
  Node getNeighbor(Node parent, int childIndex, boolean forward) {
    if (!EditValidateUtil.validNode(parent)) {
      return null;
    }

    NodeList nodeList = parent.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
      if (nodeList.getLength() < childIndex) {
        return null;
      }
      Node childNode = null;
      if (!forward) {
        --childIndex;
      }
      childNode = nodeList.item(childIndex);
      if (childNode != null) {
        return childNode;
      }
      return getNeighbor(parent, forward);
    }
    if (parent.getNodeType() == Node.TEXT_NODE) {
      return getNeighbor(parent, forward);
    }
    return null;
  }
Пример #4
0
 /**
  * Get position's container node's editable items number. this is temp functions for future use.
  *
  * @param position
  * @return the size
  */
 int getSize(IDOMPosition position) {
   EditValidateUtil.validPosition(position);
   if (position.isText()) {
     return ((Text) position.getContainerNode()).getLength();
   }
   if (position.getContainerNode().hasChildNodes()) {
     return position.getContainerNode().getChildNodes().getLength();
   }
   return 0;
 }
Пример #5
0
 /**
  * Get lowest ancestor of a 'node' which is block type.
  *
  * @param node
  * @return the node
  */
 Node getBlockAncestor(Node node) {
   if (!EditValidateUtil.validNode(node)) {
     return null;
   }
   while (node != null && isChild(IHTMLConstants.TAG_BODY, node, true)) {
     if (isBlockNode(node)) {
       return node;
     }
     node = node.getParentNode();
   }
   return null;
 }
Пример #6
0
 /**
  * Get node index in its parent's children.
  *
  * @param node
  * @return the node index or -1 if not found
  */
 static int getNodeIndex(Node node) {
   EditValidateUtil.validNode(node);
   Node parent = node.getParentNode();
   int index = 0;
   for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
     if (child == node) {
       return index;
     }
     index++;
   }
   return -1; // error
 }
Пример #7
0
 /**
  * Get previous sibling, or if sibling is null then get previous neighbor.
  *
  * @param node
  * @return the node
  */
 public Node getPreviousNeighbor(Node node) {
   if (!EditValidateUtil.validNode(node)) {
     return null;
   }
   while (node != null
       && node.getNodeType() != Node.DOCUMENT_NODE
       && node.getPreviousSibling() == null) {
     node = node.getParentNode();
   }
   return (node != null && node.getNodeType() != Node.DOCUMENT_NODE)
       ? node.getPreviousSibling()
       : null;
 }
Пример #8
0
 /**
  * If parent has more than one children of which each node's localName is the same as of 'node',
  * return the index of 'node' in the same type children list.
  *
  * @param node
  * @return the index
  */
 public int getSameTypeNodeIndex(Node node) {
   EditValidateUtil.validNode(node);
   int i = 0;
   while (node != null) {
     Node sibling = node.getPreviousSibling();
     if (sibling != null
         && sibling.getLocalName() != null
         && sibling.getLocalName().equals(node.getLocalName())) {
       i++;
     }
     node = sibling;
   }
   return i; // error
 }
Пример #9
0
  /**
   * Get neighbor which is descendent of root.
   *
   * @param node
   * @param root
   * @return the node
   */
  Node getNextNeighbor(Node node, Node root) {
    if (!EditValidateUtil.validNode(node)) {
      return null;
    }

    while (node != null
        && node != root
        && node.getNodeType() != Node.DOCUMENT_NODE
        && node.getNextSibling() == null) {
      node = node.getParentNode();
    }
    return (node != null && node != root && node.getNodeType() != Node.DOCUMENT_NODE)
        ? node.getNextSibling()
        : null;
  }
Пример #10
0
 /**
  * Return 'node' indexed end name' start position Example: <a>aaa| </a>, the position is indicated
  * by '|' If node is <a /> style or there is no </a> to pair with <a>, the function return -1.
  *
  * @param node
  * @return the start index
  */
 public static int getNodeEndNameStartIndex(Node node) {
   if (isText(node)) {
     return getNodeEndIndex(node);
   }
   if (EditValidateUtil.validNode(node) && node instanceof IDOMNode) {
     IStructuredDocumentRegion region = ((IDOMNode) node).getEndStructuredDocumentRegion();
     if (region != null) {
       return region.getStartOffset();
     }
     // else
     // {
     // if (node.hasChildNodes())
     // {
     // return getNodeEndIndex(node);
     // }
     // }
   }
   return getNodeEndIndex(node);
 }
Пример #11
0
  /**
   * If text only contains chars '\r' or '\n', it is considered to be transparent.
   *
   * @param node
   * @return true if transparent text
   */
  public static boolean isTransparentText(Node node) {
    // should valid non null?
    Assert.isTrue(node != null);
    if (node == null || !isText(node)) {
      return false;
    }
    if (!EditValidateUtil.validText(node)) {
      return false;
    }

    Text text = (Text) node;
    if (text.getLength() == 0) {
      return true;
    }
    String value = text.getNodeValue();
    int i = 0;
    while (i < value.length() && HTMLUtil.isHTMLWhitespace(value.charAt(i))) {
      i++;
    }
    return i == value.length();
  }
Пример #12
0
 /**
  * Return 'node' indexed start name's end position Example: <a>|aaa </a>, the position is
  * indicated by '|'
  *
  * @param node
  * @return the index
  */
 public static int getNodeStartNameEndIndex(Node node) {
   if (isText(node)) {
     return getNodeStartIndex(node);
   }
   if (EditValidateUtil.validNode(node) && node instanceof IDOMNode) {
     IStructuredDocumentRegion region = ((IDOMNode) node).getStartStructuredDocumentRegion();
     if (region != null) {
       return region.getEndOffset();
     }
     // else
     // {
     // // if (node.hasChildNodes())
     // // {
     // // // Node should always have start name, so this part should
     // never reach,
     // // // the assert is for inner debug.
     // // Assert.isTrue(false);
     // // return getNodeStartIndex(node);
     // // }
     // }
   }
   // This should never happen.
   return getNodeStartIndex(node);
 }
Пример #13
0
 /**
  * To see whether the range's start and end position are pointed to a same location.
  *
  * @param range
  * @return true if is same
  */
 public static boolean isSame(DOMRange range) {
   EditValidateUtil.validRange(range);
   return isSame(range.getStartPosition(), range.getEndPosition());
 }
Пример #14
0
 /**
  * Return 'node' indexed end position Example: <a></a>|, the position is indicated by '|'
  *
  * @param node
  * @return the end index
  */
 public static int getNodeEndIndex(Node node) {
   if (EditValidateUtil.validNode(node) && node instanceof IndexedRegion) {
     return ((IndexedRegion) node).getEndOffset();
   }
   return -1;
 }
Пример #15
0
 /**
  * Calculate node's Indexed length in model.
  *
  * @param node
  * @return the node length
  */
 public static int getNodeLenth(Node node) {
   if (node != null && EditValidateUtil.validNode(node)) {
     return ((IndexedRegion) node).getEndOffset() - ((IndexedRegion) node).getStartOffset();
   }
   return 0;
 }