Ejemplo n.º 1
0
    function correctContentEditableSelectionBeforeDelete(el) {
        var selection = exports.getSelectionByElement(el),

            startNode = selection.anchorNode,
            endNode = selection.focusNode,

            startOffset = selection.anchorOffset,
            endOffset = selection.focusOffset,

            startNodeFirstNonWhitespaceSymbol = ContentEditableHelper.getFirstNonWhitespaceSymbolIndex(startNode.nodeValue),
            startNodeLastNonWhitespaceSymbol = ContentEditableHelper.getLastNonWhitespaceSymbolIndex(startNode.nodeValue),

            endNodeFirstNonWhitespaceSymbol = ContentEditableHelper.getFirstNonWhitespaceSymbolIndex(endNode.nodeValue),
            endNodeLastNonWhitespaceSymbol = ContentEditableHelper.getLastNonWhitespaceSymbolIndex(endNode.nodeValue),

            newStartOffset = null,
            newEndOffset = null;

        if (startNode.nodeType === 3) {
            if (startOffset < startNodeFirstNonWhitespaceSymbol && startOffset !== 0)
                newStartOffset = 0;
            else if (startOffset !== startNode.nodeValue.length && ((ContentEditableHelper.isInvisibleTextNode(startNode) && startOffset !== 0) ||
                (startOffset > startNodeLastNonWhitespaceSymbol)))
                newStartOffset = startNode.nodeValue.length;
        }

        if (endNode.nodeType === 3) {
            if (endOffset < endNodeFirstNonWhitespaceSymbol && endOffset !== 0)
                newEndOffset = 0;
            else if (endOffset !== endNode.nodeValue.length && ((ContentEditableHelper.isInvisibleTextNode(endNode) && endOffset !== 0) ||
                (endOffset > endNodeLastNonWhitespaceSymbol)))
                newEndOffset = endNode.nodeValue.length;
        }

        if ($.browser.webkit) {
            if (newStartOffset !== null) {
                if (newStartOffset === 0)
                    startNode.nodeValue = startNode.nodeValue.substring(startNodeFirstNonWhitespaceSymbol);
                else
                    startNode.nodeValue = startNode.nodeValue.substring(0, startNodeLastNonWhitespaceSymbol);
            }

            if (newEndOffset !== null) {
                if (newEndOffset === 0)
                    endNode.nodeValue = endNode.nodeValue.substring(endNodeFirstNonWhitespaceSymbol);
                else
                    endNode.nodeValue = endNode.nodeValue.substring(0, endNodeLastNonWhitespaceSymbol);
            }
        }

        if (newStartOffset !== null || newEndOffset !== null) {
            newStartOffset = newStartOffset !== null ? (newStartOffset === 0 ? newStartOffset : startNode.nodeValue.length) : startOffset;
            newEndOffset = newEndOffset !== null ? (newEndOffset === 0 ? newEndOffset : endNode.nodeValue.length) : endOffset;
            exports.selectByNodesAndOffsets(startNode, newStartOffset, endNode, newEndOffset);
        }
    }
Ejemplo n.º 2
0
    function selectContentEditable(el, from, to, needFocus, inverse) {
        var endPosition = null,
            firstTextNodeChild = null,
            latestTextNodeChild = null,
            startPosition = null,
            temp = null;

        if (typeof from !== 'undefined' && typeof to !== 'undefined' && from > to) {
            temp = from;
            from = to;
            to = temp;
            inverse = true;
        }

        if (typeof from === 'undefined') {
            firstTextNodeChild = ContentEditableHelper.getFirstVisibleTextNode(el);
            startPosition = {
                node: firstTextNodeChild || el,
                offset: firstTextNodeChild && firstTextNodeChild.nodeValue ?
                    ContentEditableHelper.getFirstNonWhitespaceSymbolIndex(firstTextNodeChild.nodeValue) : 0
            };
        }

        if (typeof to === 'undefined') {
            latestTextNodeChild = ContentEditableHelper.getLastVisibleTextNode(el, true);
            endPosition = {
                node: latestTextNodeChild || el,
                offset: latestTextNodeChild && latestTextNodeChild.nodeValue ?
                    ContentEditableHelper.getLastNonWhitespaceSymbolIndex(latestTextNodeChild.nodeValue) : 0
            };
        }

        startPosition = startPosition || ContentEditableHelper.calculateNodeAndOffsetByPosition(el, from);
        endPosition = endPosition || ContentEditableHelper.calculateNodeAndOffsetByPosition(el, to);

        if (!startPosition.node || !endPosition.node)
            return;

        exports.selectByNodesAndOffsets(startPosition.node, startPosition.offset, endPosition.node, endPosition.offset, needFocus, inverse);
    }