2020-03-08 14:32:38 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-04-28 19:20:07 +00:00
|
|
|
/**
|
|
|
|
* Return a native Range object corresponding to our comment's range.
|
|
|
|
*
|
|
|
|
* @param {Object} comment
|
|
|
|
* @return {Range}
|
|
|
|
*/
|
|
|
|
function getNativeRange( comment ) {
|
|
|
|
var
|
|
|
|
doc = comment.range.startContainer.ownerDocument,
|
|
|
|
nativeRange = doc.createRange();
|
|
|
|
nativeRange.setStart( comment.range.startContainer, comment.range.startOffset );
|
|
|
|
nativeRange.setEnd( comment.range.endContainer, comment.range.endOffset );
|
|
|
|
return nativeRange;
|
|
|
|
}
|
|
|
|
|
2020-05-22 17:09:21 +00:00
|
|
|
/**
|
|
|
|
* Get the index of a node in its parentNode's childNode list
|
|
|
|
*
|
|
|
|
* @param {Node} child
|
|
|
|
* @return {number} Index in parentNode's childNode list
|
|
|
|
*/
|
|
|
|
function childIndexOf( child ) {
|
|
|
|
var i = 0;
|
|
|
|
while ( ( child = child.previousSibling ) ) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2020-03-08 14:32:38 +00:00
|
|
|
/**
|
|
|
|
* Find closest ancestor element using one of the given tag names.
|
|
|
|
*
|
2020-05-11 20:07:14 +00:00
|
|
|
* @param {Node} node
|
2020-03-08 14:32:38 +00:00
|
|
|
* @param {string[]} tagNames
|
|
|
|
* @return {HTMLElement|null}
|
|
|
|
*/
|
2020-05-11 20:07:14 +00:00
|
|
|
function closestElement( node, tagNames ) {
|
2020-03-08 14:32:38 +00:00
|
|
|
do {
|
2020-05-11 20:07:14 +00:00
|
|
|
if (
|
|
|
|
node.nodeType === Node.ELEMENT_NODE &&
|
|
|
|
tagNames.indexOf( node.tagName.toLowerCase() ) !== -1
|
|
|
|
) {
|
|
|
|
return node;
|
2020-03-08 14:32:38 +00:00
|
|
|
}
|
2020-05-11 20:07:14 +00:00
|
|
|
node = node.parentNode;
|
|
|
|
} while ( node );
|
2020-03-08 14:32:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:07:14 +00:00
|
|
|
/**
|
|
|
|
* Trim ASCII whitespace, as defined in the HTML spec.
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function htmlTrim( str ) {
|
|
|
|
// https://infra.spec.whatwg.org/#ascii-whitespace
|
|
|
|
return str.replace( /^[\t\n\f\r ]+/, '' ).replace( /[\t\n\f\r ]+$/, '' );
|
|
|
|
}
|
|
|
|
|
2020-03-08 14:32:38 +00:00
|
|
|
module.exports = {
|
2020-04-28 19:20:07 +00:00
|
|
|
getNativeRange: getNativeRange,
|
2020-05-22 17:09:21 +00:00
|
|
|
childIndexOf: childIndexOf,
|
2020-05-11 20:07:14 +00:00
|
|
|
closestElement: closestElement,
|
|
|
|
htmlTrim: htmlTrim
|
2020-03-08 14:32:38 +00:00
|
|
|
};
|