] const ancestors = listAncestor(point.node, func.eq(root)); if (!ancestors.length) { return null; } else if (ancestors.length === 1) { return splitNode(point, options); } return ancestors.reduce(function(node, parent) { if (node === point.node) { node = splitNode(point, options); } return splitNode({ node: parent, offset: node ? position(node) : nodeLength(parent), }, options); }); } /** * split point * * @param {Point} point * @param {Boolean} isInline * @return {Object} */ function splitPoint(point, isInline) { // find splitRoot, container // - inline: splitRoot is a child of paragraph // - block: splitRoot is a child of bodyContainer const pred = isInline ? isPara : isBodyContainer; const ancestors = listAncestor(point.node, pred); const topAncestor = lists.last(ancestors) || point.node; let splitRoot, container; if (pred(topAncestor)) { splitRoot = ancestors[ancestors.length - 2]; container = topAncestor; } else { splitRoot = topAncestor; container = splitRoot.parentNode; } // if splitRoot is exists, split with splitTree let pivot = splitRoot && splitTree(splitRoot, point, { isSkipPaddingBlankHTML: isInline, isNotSplitEdgePoint: isInline, }); // if container is point.node, find pivot with point.offset if (!pivot && container === point.node) { pivot = point.node.childNodes[point.offset]; } return { rightNode: pivot, container: container, }; } function create(nodeName) { return document.createElement(nodeName); } function createText(text) { return document.createTextNode(text); } /** * @method remove * * remove node, (isRemoveChild: remove child or not) * * @param {Node} node * @param {Boolean} isRemoveChild */ function remove(node, isRemoveChild) { if (!node || !node.parentNode) { return; } if (node.removeNode) { return node.removeNode(isRemoveChild); } const parent = node.parentNode; if (!isRemoveChild) { const nodes = []; for (let i = 0, len = node.childNodes.length; i < len; i++) { nodes.push(node.childNodes[i]); } for (let i = 0, len = nodes.length; i < len; i++) { parent.insertBefore(nodes[i], node); } } parent.removeChild(node); } /** * @method removeWhile * * @param {Node} node * @param {Function} pred */ function removeWhile(node, pred) { while (node) { if (isEditable(node) || !pred(node)) { break; } const parent = node.parentNode; remove(node); node = parent; } } /** * @method replace * * replace node with provided nodeName * * @param {Node} node * @param {String} nodeName * @return {Node} - new node */ function replace(node, nodeName) { if (node.nodeName.toUpperCase() === nodeName.toUpperCase()) { return node; } const newNode = create(nodeName); if (node.style.cssText) { newNode.style.cssText = node.style.cssText; } appendChildNodes(newNode, lists.from(node.childNodes)); insertAfter(newNode, node); remove(node); return newNode; } const isTextarea = makePredByNodeName('TEXTAREA'); /** * @param {jQuery} $node * @param {Boolean} [stripLinebreaks] - default: false */ function value($node, stripLinebreaks) { const val = isTextarea($node[0]) ? $node.val() : $node.html(); if (stripLinebreaks) { return val.replace(/[\n\r]/g, ''); } return val; } /** * @method html * * get the HTML contents of node * * @param {jQuery} $node * @param {Boolean} [isNewlineOnBlock] */ function html($node, isNewlineOnBlock) { let markup = value($node); if (isNewlineOnBlock) { const regexTag = /<(\/?)(\b(?!!)[^>\s]*)(.*?)(\s*\/?>)/g; markup = markup.replace(regexTag, function(match, endSlash, name) { name = name.toUpperCase(); const isEndOfInlineContainer = /^DIV|^TD|^TH|^P|^LI|^H[1-7]/.test(name) && !!endSlash; const isBlockNode = /^BLOCKQUOTE|^TABLE|^TBODY|^TR|^HR|^UL|^OL/.test(name); return match + ((isEndOfInlineContainer || isBlockNode) ? '\n' : ''); }); markup = markup.trim(); } return markup; } function posFromPlaceholder(placeholder) { const $placeholder = $(placeholder); const pos = $placeholder.offset(); const height = $placeholder.outerHeight(true); // include margin return { left: pos.left, top: pos.top + height, }; } function attachEvents($node, events) { Object.keys(events).forEach(function(key) { $node.on(key, events[key]); }); } function detachEvents($node, events) { Object.keys(events).forEach(function(key) { $node.off(key, events[key]); }); } /** * @method isCustomStyleTag * * assert if a node contains a "note-styletag" class, * which implies that's a custom-made style tag node * * @param {Node} an HTML DOM node */ function isCustomStyleTag(node) { return node && !isText(node) && lists.contains(node.classList, 'note-styletag'); } export default { /** @property {String} NBSP_CHAR */ NBSP_CHAR, /** @property {String} ZERO_WIDTH_NBSP_CHAR */ ZERO_WIDTH_NBSP_CHAR, /** @property {String} blank */ blank: blankHTML, /** @property {String} emptyPara */ emptyPara: `
${blankHTML}
`, makePredByNodeName, isEditable, isControlSizing, isText, isElement, isVoid, isPara, isPurePara, isHeading, isInline, isBlock: func.not(isInline), isBodyInline, isBody, isParaInline, isPre, isList, isTable, isData, isCell, isBlockquote, isBodyContainer, isAnchor, isDiv: makePredByNodeName('DIV'), isLi, isBR: makePredByNodeName('BR'), isSpan: makePredByNodeName('SPAN'), isB: makePredByNodeName('B'), isU: makePredByNodeName('U'), isS: makePredByNodeName('S'), isI: makePredByNodeName('I'), isImg: makePredByNodeName('IMG'), isTextarea, deepestChildIsEmpty, isEmpty, isEmptyAnchor: func.and(isAnchor, isEmpty), isClosestSibling, withClosestSiblings, nodeLength, isLeftEdgePoint, isRightEdgePoint, isEdgePoint, isLeftEdgeOf, isRightEdgeOf, isLeftEdgePointOf, isRightEdgePointOf, prevPoint, nextPoint, nextPointWithEmptyNode, isSamePoint, isVisiblePoint, prevPointUntil, nextPointUntil, isCharPoint, isSpacePoint, walkPoint, ancestor, singleChildAncestor, listAncestor, lastAncestor, listNext, listPrev, listDescendant, commonAncestor, wrap, insertAfter, appendChildNodes, position, hasChildren, makeOffsetPath, fromOffsetPath, splitTree, splitPoint, create, createText, remove, removeWhile, replace, html, value, posFromPlaceholder, attachEvents, detachEvents, isCustomStyleTag, };