Example usage for com.google.gwt.dom.client Text getLength

List of usage examples for com.google.gwt.dom.client Text getLength

Introduction

In this page you can find the example usage for com.google.gwt.dom.client Text getLength.

Prototype

@Override
    public int getLength() 

Source Link

Usage

From source file:com.bfr.client.selection.HtmlBBox.java

License:Apache License

/**
 * Create a bounding box around the single character at the offset given
 * within a text node.  If the offset is at the end of the text, the
 * bounding box is a point.  Temporarily modifies the document as indicated
 * in getBoundingBox(textNode, offset1, offset2)
 *
 * @param textNode Text node to find character in
 * @param offset   offset of the character
 * @return a new bounding box//  ww  w  .ja v a2 s.  c o  m
 */
public static HtmlBBox getBoundingBox(Text textNode, int offset) {
    return getBoundingBox(textNode, offset, (offset == textNode.getLength()) ? offset : offset + 1);
}

From source file:com.bfr.client.selection.impl.RangeImplIE6.java

License:Apache License

/**
 * Get the IE start or end point of the given range, have to search for it
 * to find it properly.//from  ww  w  . jav a 2  s. co  m
 *
 * @param range    used to get the document
 * @param selRange the selection we are getting the point of
 * @param start    whether to get the start or end point
 * @return RangeEndPoint representing this, or null on error
 */
private RangeEndPoint getRangeEndPoint(Range range, JSRange selRange, boolean start) {
    RangeEndPoint res = null;

    // Create a cursor at either the beginning or end of the range, to
    // get that point's immediate parent
    JSRange checkRange = cloneRange(selRange);
    collapseRange(checkRange, start);
    Element parent = getCommonAncestor(checkRange);

    String compareFcn = BOUNDARY_STRINGS[start ? Range.START_TO_START : Range.END_TO_END];

    Node compNode;

    // Test element we move around the document to check relative selection
    Element testElement = getTestElement(range.getDocument());

    try {
        // Move the test element backwards past nodes until we are in front
        // of the desired range endpoint
        for (compNode = parent.getLastChild(); compNode != null; compNode = testElement.getPreviousSibling()) {
            parent.insertBefore(testElement, compNode);
            moveToElementText(checkRange, testElement);
            if (compareBoundaryPoint(checkRange, selRange, compareFcn) <= 0) {
                break;
            }
        }

        if (compNode == null) {
            // Sometimes selection at beginning of a span causes a fail
            compNode = testElement.getNextSibling();
        }

        if (compNode == null) {
        } else if (compNode.getNodeType() == Node.ELEMENT_NODE) {
            // We only represent text elements right now, so if this is not
            // then go find one.  Check if the desired selection is at the
            // beginning or end of this element, first select the entire
            // element to determine whether the endpoint is at the
            // beginning or the end of it, ie whether to look forward or
            // backward.
            testElement.removeFromParent();
            moveToElementText(checkRange, (Element) compNode);
            int cmp = compareBoundaryPoint(checkRange, selRange, compareFcn);
            boolean dir = (cmp == 0) ? !start : (cmp < 0);
            Text closest = Range.getAdjacentTextElement(compNode, parent, dir, true);
            if (closest == null) {
                dir = !dir;
                closest = Range.getAdjacentTextElement(compNode, parent, dir, true);
            }

            if (closest != null) {
                // Found a text node in one direction or the other
                res = new RangeEndPoint(closest, dir ? 0 : closest.getLength());
            }
        } else {
            // Get the proper offset, move the end of the check range to the
            // boundary of the actual range and get its length
            moveRangePoint(checkRange, selRange,
                    BOUNDARY_STRINGS[start ? Range.END_TO_START : Range.END_TO_END]);
            res = new RangeEndPoint((Text) compNode, getText(checkRange).length());
        }
    } catch (Exception ex) {
        GWT.log("Failed to find IE selection", ex);
    } finally {
        // Make sure this gets removed from the document no matter what
        testElement.removeFromParent();
    }

    return (res == null) ? new RangeEndPoint() : res;
}

From source file:com.bfr.client.selection.Range.java

License:Apache License

/**
 * Sets the range to encompass the given element.  May not work around
 * non-text containing elements.//from   ww  w. j a v  a  2s.c om
 *
 * @param element Element to surround by this range
 * @return whether a range can be placed around this element.
 */
public boolean setRange(Element element) {
    Text firstText = getAdjacentTextElement(element, element, true, false);
    Text lastText = getAdjacentTextElement(element, element, false, false);

    if ((firstText == null) || (lastText == null)) {
        return false;
    }

    setRange(new RangeEndPoint(firstText, 0), new RangeEndPoint(lastText, lastText.getLength()));

    return true;
}

From source file:com.bfr.client.selection.RangeEndPoint.java

License:Apache License

/**
 * If the offset occurs at the beginning/end of the text node, potentially
 * move to the end/beginning of the next/previous text node, to remove
 * text nodes where 0 characters are actually used.  If asStart is true then
 * move a cursor at the end of a text node to the beginning of the next;
 * vice versa for false.//from ww w  .j  a  v a 2s.c om
 *
 * @param asStart Whether to do this as a start or end range point
 */
public void minimizeBoundaryTextNodes(boolean asStart) {
    Text text = getTextNode();
    if ((text != null) && (m_offset == (asStart ? text.getLength() : 0))) {
        Text next = Range.getAdjacentTextElement(text, asStart);
        if (next != null) {
            setTextNode(next);
            m_offset = asStart ? 0 : next.getLength();
        }
    }
}

From source file:com.bfr.client.selection.RangeEndPoint.java

License:Apache License

/**
 * TODO IMPLEMENTED ONLY FOR CHARACTER/*from w w  w .  j  av  a2  s . c om*/
 * Move the end point forwards or backwards by one unit of type, such as
 * by a word.
 *
 * @param forward     true if moving forward
 * @param topMostNode top node to not move past, or null
 * @param limit       an endpoint not to move past, or null
 * @param type        what unit to move by, ie MOVE_CHARACTER or MOVE_WORD
 * @param count       how many of these to move by
 * @return how far this actually moved
 */
public int move(boolean forward, Node topMostNode, RangeEndPoint limit, short type, int count) {
    int res = 0;

    Text limitText = (limit == null) ? null : limit.getTextNode();
    Text curr = getTextNode();
    if (curr != null) {
        Text last = curr;
        int offset = getOffset();

        switch (type) {
        case MOVE_CHARACTER:
            while (curr != null) {
                last = curr;
                int len = forward ? curr.getLength() - offset : offset;
                if (curr == limitText) {
                    // If there is a limiting endpoint, may not be able to
                    // go as far
                    len = forward ? (limit.getOffset() - offset) : (offset - limit.getOffset());
                }

                if ((len + res) < count) {
                    res += len;

                    if (curr == limitText) {
                        break;
                    }
                } else {
                    // Finis
                    len = count - res;
                    offset = forward ? (offset + len) : offset - len;
                    res = count;
                    break;
                }

                do {
                    // Next node, skipping any 0-length texts
                    curr = Range.getAdjacentTextElement(curr, topMostNode, forward, false);
                } while ((curr != null) && (curr.getLength() == 0));

                if (curr != null) {
                    offset = forward ? 0 : curr.getLength();
                }

            }
            break;
        /*
        case MOVE_WORDSTART:
        case MOVE_WORDEND:
        if (c_wsRexp == null)
        {
          setWhitespaceRexp(DEFAULT_WS_REXP);
        }
                
        while (curr != null)
        {
                  
          do
          {
         // Next node, skipping any 0-length texts
         curr = Range.getAdjacentTextElement(curr, topMostNode, 
                                     forward, false);
          } while  ((curr != null) && (curr.getLength() == 0));
                  
          if (curr != null)
          {
         offset = forward ? 0 : curr.getLength();
          }
        }
        break;
        */
        default:
            assert (false);
        }
        setTextNode(last);
        setOffset(offset);
    }
    return res;
}

From source file:com.bfr.client.selection.RangeEndPoint.java

License:Apache License

/**
 * Set this range end point at the start or end of a text node
 *
 * @param text  text node this end point starts/end in
 * @param start whether to make the end point at the start or the end
 *//*  ww  w  .ja  v a2 s  .  c  o m*/
public void setTextNode(Text textNode, boolean start) {
    setTextNode(textNode);
    setOffset((start || (textNode == null)) ? 0 : textNode.getLength());
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.view.annotater.TextAnnotater.java

License:Apache License

private Container getCustomContainer(Node node, int currentOffset) {
    int offset = 0;
    Node n = node.getPreviousSibling();
    while (n != null) {
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text text = (Text) n;
            offset += text.getLength();
        } else if (n.getNodeType() == Node.ELEMENT_NODE) {
            Element ele = (Element) n;
            offset += ele.getInnerText().length();
        }//from  w ww.j av  a  2s. c  o m
        n = n.getPreviousSibling();
    }
    node = node.getParentNode();
    currentOffset += offset;
    return new Container(xpathUtil.getXPath(node), currentOffset);
}

From source file:org.rstudio.core.client.dom.impl.NodeRelativePosition.java

License:Open Source License

private static NodeRelativePosition toPositionHelper(Node here, int[] counter) {
    switch (here.getNodeType()) {
    case Node.TEXT_NODE:
        Text text = (Text) here;
        if (counter[0] <= text.getLength())
            return new NodeRelativePosition(here, counter[0]);
        counter[0] -= text.getLength();/* www  .j a va  2  s.  c  o m*/
        return null;
    case Node.ELEMENT_NODE:
        Element el = (Element) here;
        String tagName = el.getTagName().toLowerCase();
        if (tagName.equals("br")) {
            if (counter[0] <= 0)
                return new NodeRelativePosition(here, 0);
            counter[0] -= 1;
            return null;
        } else if (tagName.equals("script") || tagName.equals("style"))
            return null;
        break;
    }

    NodeList<Node> children = here.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        NodeRelativePosition result = toPositionHelper(children.getItem(i), counter);
        if (result != null)
            return result;
    }

    return null;
}

From source file:org.waveprotocol.wave.client.editor.content.ContentTextNode.java

License:Apache License

/**
 * @see RawDocument#deleteData(Object, int, int)
 *//*from  w  w  w.  j  a v a  2  s.co  m*/
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);
        }
    }
}

From source file:org.waveprotocol.wave.client.editor.content.ContentTextNode.java

License:Apache License

/**
 * Splits and returns the second.//  w  w w  . j  av  a2  s . c  o m
 * 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;
    }
}