PassRefPtr<Text> Text::splitText(unsigned offset, ExceptionState& exceptionState) { // IndexSizeError: Raised if the specified offset is negative or greater than // the number of 16-bit units in data. if (offset > length()) { exceptionState.ThrowDOMException(IndexSizeError, "The offset " + String::number(offset) + " is larger than the Text node's length."); return nullptr; } String oldStr = data(); RefPtr<Text> newText = cloneWithData(oldStr.substring(offset)); setDataWithoutUpdate(oldStr.substring(0, offset)); didModifyData(oldStr); if (parentNode()) parentNode()->insertBefore(newText.get(), nextSibling(), exceptionState); if (exceptionState.had_exception()) return nullptr; if (renderer()) renderer()->setTextWithOffset(dataImpl(), 0, oldStr.length()); if (parentNode()) document().didSplitTextNode(*this); return newText.release(); }
RefPtr<Text> Text::splitText(unsigned offset, ExceptionCode& ec) { ec = 0; // INDEX_SIZE_ERR: Raised if the specified offset is negative or greater than // the number of 16-bit units in data. if (offset > length()) { ec = INDEX_SIZE_ERR; return 0; } EventQueueScope scope; String oldStr = data(); Ref<Text> newText = virtualCreate(oldStr.substring(offset)); setDataWithoutUpdate(oldStr.substring(0, offset)); dispatchModifiedEvent(oldStr); if (parentNode()) parentNode()->insertBefore(newText.ptr(), nextSibling(), ec); if (ec) return 0; if (parentNode()) document().textNodeSplit(this); if (renderer()) renderer()->setTextWithOffset(data(), 0, oldStr.length()); return WTF::move(newText); }
PassRefPtr<Text> Text::splitText(unsigned offset, ExceptionState& es) { // IndexSizeError: Raised if the specified offset is negative or greater than // the number of 16-bit units in data. if (offset > length()) { es.throwDOMException(IndexSizeError); return 0; } EventQueueScope scope; String oldStr = data(); RefPtr<Text> newText = cloneWithData(oldStr.substring(offset)); setDataWithoutUpdate(oldStr.substring(0, offset)); didModifyData(oldStr); if (parentNode()) parentNode()->insertBefore(newText.get(), nextSibling(), es); if (es.hadException()) return 0; if (parentNode()) document()->textNodeSplit(this); if (renderer()) toRenderText(renderer())->setTextWithOffset(dataImpl(), 0, oldStr.length()); return newText.release(); }
PassRefPtr<Node> Text::mergeNextSiblingNodesIfPossible() { RefPtr<Node> protect(this); // Remove empty text nodes. if (!length()) { // Care must be taken to get the next node before removing the current node. RefPtr<Node> nextNode(NodeTraversal::nextPostOrder(*this)); remove(IGNORE_EXCEPTION); return nextNode.release(); } // Merge text nodes. while (Node* nextSibling = this->nextSibling()) { if (nextSibling->nodeType() != TEXT_NODE) break; RefPtr<Text> nextText = toText(nextSibling); // Remove empty text nodes. if (!nextText->length()) { nextText->remove(IGNORE_EXCEPTION); continue; } // Both non-empty text nodes. Merge them. unsigned offset = length(); String nextTextData = nextText->data(); String oldTextData = data(); setDataWithoutUpdate(data() + nextTextData); updateTextRenderer(oldTextData.length(), 0); // Empty nextText for layout update. nextText->setDataWithoutUpdate(emptyString()); nextText->updateTextRenderer(0, nextTextData.length()); document().didMergeTextNodes(nextText.get(), offset); // Restore nextText for mutation event. nextText->setDataWithoutUpdate(nextTextData); nextText->updateTextRenderer(0, 0); document().incDOMTreeVersion(); didModifyData(oldTextData); nextText->remove(IGNORE_EXCEPTION); } return NodeTraversal::nextPostOrder(*this); }