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

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

Introduction

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

Prototype

public static Text as(Node node) 

Source Link

Document

Assert that the given Node is of type Node#TEXT_NODE and automatically typecast it.

Usage

From source file:com.dom_distiller.client.DomToSaxVisitor.java

License:Open Source License

@Override
public boolean visit(Node n) {
    switch (n.getNodeType()) {
    case Node.TEXT_NODE:
        handler.textNode(Text.as(n));
        return false;
    case Node.ELEMENT_NODE:
        Element e = Element.as(n);
        handler.startElement(e);/*from w w  w.j ava2  s.  c  o  m*/
        return true;
    case Node.DOCUMENT_NODE: // Don't recurse into sub-documents.
    default: // This case is for comment nodes.
        return false;
    }
}

From source file:org.chromium.distiller.webdocument.DomConverter.java

License:Open Source License

@Override
public boolean visit(Node n) {
    switch (n.getNodeType()) {
    case Node.TEXT_NODE:
        builder.textNode(Text.as(n));
        return false;
    case Node.ELEMENT_NODE:
        return visitElement(Element.as(n));
    default://from  w  ww .ja v a2s .  co m
        return false;
    }
}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.rt.dom.operation.DomDeleteText.java

License:Open Source License

@Override
public Range execute(Document document) {
    Node targetNode = getTargetNode(document);
    if (Node.TEXT_NODE == targetNode.getNodeType()) {
        // Delete 1 character.
        Text.as(targetNode).deleteData(getOperation().getPosition(), 1);
    } else if (Node.ELEMENT_NODE == targetNode.getNodeType()) {
        // There is no delete text operation generated on elements yet.
    }//w w w.jav  a 2  s.c o  m
    // Selection shifts 1 char left.
    Range caret = document.createRange();
    caret.setStart(targetNode, getOperation().getPosition());
    caret.collapse(true);
    return caret;
}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.rt.dom.operation.DomInsertText.java

License:Open Source License

@Override
public Range execute(Document document) {
    TreeInsertText insertText = getOperation();
    String text = String.valueOf(insertText.getText());
    Node targetNode = getTargetNode(document);

    if (Node.ELEMENT_NODE == targetNode.getNodeType()) {
        // Create a new text node in the target element.
        Node newTextNode = document.createTextNode(text);
        domUtils.insertAt(targetNode, newTextNode, insertText.getPosition());
    } else if (Node.TEXT_NODE == targetNode.getNodeType()) {
        // Insert a character in the target text node.
        Text.as(targetNode).insertData(insertText.getPosition(), text);
    }/*from www  . j  a v  a 2  s . co m*/

    // Selection shifts 1 char right.
    Range caret = document.createRange();
    caret.setStart(targetNode, insertText.getPosition() + 1);
    caret.collapse(true);
    return caret;
}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.rt.RealTimePlugin.java

License:Open Source License

public TreeOperation handleBackspaceOnTextNode(Range caret) {
    int pos = caret.getStartOffset();
    Node startContainer = caret.getStartContainer();
    List<Integer> path = EditorUtils.getLocator(startContainer);
    TreeOperation op = null;/*  w w  w .  j  a  va  2  s  .  c o m*/

    Node ancestorBelowContainer = DOMUtils.getInstance().getFarthestInlineAncestor(startContainer);
    Element rightParagraph = ancestorBelowContainer.getParentElement();
    Node leftParagraph = rightParagraph.getPreviousSibling(); // Go up below the parent paragraph node, because we might have span tags with text nodes

    Text textNode = Text.as(startContainer);
    if (pos == 0) {
        if (leftParagraph != null) {
            op = maybeMergeParagraphs(true, textNode, path, rightParagraph, leftParagraph);
        } else {
            log.fine("Backspace on text node: Above paragraph is null, nothing to be done.");
        }
    } else {
        pos = pos - 1;
        op = new TreeDeleteText(clientJupiter.getSiteId(), pos, EditorUtils.toIntArray(path));
    }
    return op;
}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.rt.RealTimePlugin.java

License:Open Source License

/**
 * @param caret the caret location in the editor
 * @return a new paragraph or split paragraph operation
 *///from   w  w  w. j a v a2  s  .c o  m
public TreeOperation handleEnterOnTextNode(Range caret) {
    int pos = caret.getEndOffset();
    Node container = caret.getEndContainer();
    Text textNode = Text.as(container);
    List<Integer> path = EditorUtils.getLocator(container);
    TreeOperation op;

    boolean isNewParagraph = false;

    // Go up below the parent paragraph node, because we might have span tags with text nodes
    Node ancestorBelowParagraph = DOMUtils.getInstance().getFarthestInlineAncestor(textNode);

    // Start of the line: textNode is the first child of it's ancestor (directly or indirectly)
    boolean isFirstChild = false;
    if (textNode != ancestorBelowParagraph) {
        isFirstChild = ancestorBelowParagraph == ancestorBelowParagraph.getParentElement().getFirstChild();
        isFirstChild = isFirstChild && (textNode == ancestorBelowParagraph.getFirstChild());
    } else {
        isFirstChild = textNode == ancestorBelowParagraph.getParentElement().getFirstChild();
    }
    isFirstChild = isFirstChild || (ancestorBelowParagraph.getPreviousSibling() == null);
    if (isFirstChild && 0 == pos) {
        isNewParagraph = true;
        pos = path.get(0);
    }

    // End of line: textNode is last child of it's ancestor (directly or indirectly)
    boolean isLastChild = false;
    if (textNode != ancestorBelowParagraph) {
        isLastChild = ancestorBelowParagraph == ancestorBelowParagraph.getParentElement().getLastChild();
        isLastChild = isLastChild && (textNode == ancestorBelowParagraph.getLastChild());
    } else {
        isLastChild = textNode == ancestorBelowParagraph.getParentNode().getLastChild();
    }
    isLastChild = isLastChild || (ancestorBelowParagraph.getNextSibling() == null
            || BR.equalsIgnoreCase(ancestorBelowParagraph.getNextSibling().getNodeName()));
    if (isLastChild && textNode.getLength() == pos) {
        isNewParagraph = true;
        pos = path.get(0) + 1;
    }

    if (isNewParagraph) {
        op = new TreeNewParagraph(clientJupiter.getSiteId(), pos);
        op.setPath(EditorUtils.toIntArray(path));
    } else {
        op = new TreeInsertParagraph(clientJupiter.getSiteId(), pos, EditorUtils.toIntArray(path));
    }
    return op;
}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.rt.RealTimePlugin.java

License:Open Source License

public TreeOperation handleDeleteOnTextNode(Range caret) {
    int pos = caret.getStartOffset();
    Node startContainer = caret.getStartContainer();
    Text textNode = Text.as(startContainer);
    List<Integer> path = EditorUtils.getLocator(startContainer);
    TreeOperation op = null;//from www.  j  a v  a 2 s  . c  om

    // Go up below the parent paragraph node, because we might have span tags with text nodes
    final Node ancestorParagraph = DOMUtils.getInstance().getFarthestInlineAncestor(startContainer);
    Element leftParagraph = Element.as(ancestorParagraph);
    Element rightParagraph = leftParagraph.getNextSiblingElement();

    if (pos < textNode.getLength()) {
        op = new TreeDeleteText(clientJupiter.getSiteId(), pos, EditorUtils.toIntArray(path));
    } else { // perhaps a line merge
        op = maybeMergeParagraphs(false, textNode, path, rightParagraph, leftParagraph);
    }
    return op;
}