src/highlight-page.js
function refineRangeBoundaries(range) {
    let startContainer = range.startContainer,
        endContainer = range.endContainer,
        ancestor = range.commonAncestorContainer,
        goDeeper = true;

    if (range.endOffset === 0) {
        while (!endContainer.previousSibling && endContainer.parentNode !== ancestor) {
            endContainer = endContainer.parentNode;
        }
        endContainer = endContainer.previousSibling;
    } else if (endContainer.nodeType === NODE_TYPE.TEXT_NODE) {
        if (range.endOffset < endContainer.nodeValue.length) {
            endContainer.splitText(range.endOffset);
        }
    } else if (range.endOffset > 0) {
        endContainer = endContainer.childNodes.item(range.endOffset - 1);
    }

    if (startContainer.nodeType === NODE_TYPE.TEXT_NODE) {
        if (range.startOffset === startContainer.nodeValue.length) {
            goDeeper = false;
        } else if (range.startOffset > 0) {
            startContainer = startContainer.splitText(range.startOffset);
            if (endContainer === startContainer.previousSibling) {
                endContainer = startContainer;
            }
        }
    } else if (range.startOffset < startContainer.childNodes.length) {
        startContainer = startContainer.childNodes.item(range.startOffset);
    } else {
        startContainer = startContainer.nextSibling;
    }

    return {
        startContainer: startContainer,
        endContainer: endContainer,
        goDeeper: goDeeper
    };
}
function refineRangeBoundaries(range) {
    let startContainer = range.startContainer,
        endContainer = range.endContainer,
        ancestor = range.commonAncestorContainer,
        goDeeper = true;

    if (range.endOffset === 0) {
        while (!endContainer.previousSibling && endContainer.parentNode !== ancestor) {
            endContainer = endContainer.parentNode;
        }
        endContainer = endContainer.previousSibling;
    } else if (endContainer.nodeType === NODE_TYPE.TEXT_NODE) {
        if (range.endOffset < endContainer.nodeValue.length) {
            endContainer.splitText(range.endOffset);
        }
    } else if (range.endOffset > 0) {
        endContainer = endContainer.childNodes.item(range.endOffset - 1);
    }

    if (startContainer.nodeType === NODE_TYPE.TEXT_NODE) {
        if (range.startOffset === startContainer.nodeValue.length) {
            goDeeper = false;
        } else if (range.startOffset > 0) {
            startContainer = startContainer.splitText(range.startOffset);
            if (endContainer === startContainer.previousSibling) {
                endContainer = startContainer;
            }
        }
    } else if (range.startOffset < startContainer.childNodes.length) {
        startContainer = startContainer.childNodes.item(range.startOffset);
    } else {
        startContainer = startContainer.nextSibling;
    }

    return {
        startContainer: startContainer,
        endContainer: endContainer,
        goDeeper: goDeeper
    };
}
function refineRangeBoundaries ( range ) { let startContainer = range . startContainer , endContainer = range . endContainer , ancestor = range . commonAncestorContainer , goDeeper = true ; if ( range . endOffset === 0 ) { while ( ! endContainer . previousSibling && endContainer . parentNode !== ancestor ) { endContainer = endContainer . parentNode ; } endContainer = endContainer . previousSibling ; } else if ( endContainer . nodeType === NODE_TYPE . TEXT_NODE ) { if ( range . endOffset < endContainer . nodeValue . length ) { endContainer . splitText ( range . endOffset ) ; } } else if ( range . endOffset > 0 ) { endContainer = endContainer . childNodes . item ( range . endOffset - 1 ) ; } if ( startContainer . nodeType === NODE_TYPE . TEXT_NODE ) { if ( range . startOffset === startContainer . nodeValue . length ) { goDeeper = false ; } else if ( range . startOffset > 0 ) { startContainer = startContainer . splitText ( range . startOffset ) ; if ( endContainer === startContainer . previousSibling ) { endContainer = startContainer ; } } } else if ( range . startOffset < startContainer . childNodes . length ) { startContainer = startContainer . childNodes . item ( range . startOffset ) ; } else { startContainer = startContainer . nextSibling ; } return { startContainer : startContainer , endContainer : endContainer , goDeeper : goDeeper } ; }
Takes range object as parameter and refines it boundaries
@param range
@returns {object} refined boundaries and initial state of highlighting algorithm.
Takes range object as parameter and refines it boundaries