void InsertLineBreakCommand::doApply() { deleteSelection(); VisibleSelection selection = endingSelection(); if (!selection.isNonOrphanedCaretOrRange()) return; VisiblePosition caret(selection.visibleStart()); // FIXME: If the node is hidden, we should still be able to insert text. // For now, we return to avoid a crash. https://bugs.webkit.org/show_bug.cgi?id=40342 if (caret.isNull()) return; Position pos(caret.deepEquivalent()); pos = positionAvoidingSpecialElementBoundary(pos); pos = positionOutsideTabSpan(pos); RefPtrWillBeRawPtr<Node> nodeToInsert = nullptr; if (shouldUseBreakElement(pos)) nodeToInsert = HTMLBRElement::create(document()); else nodeToInsert = document().createTextNode("\n"); // FIXME: Need to merge text nodes when inserting just after or before text. if (isEndOfParagraph(caret) && !lineBreakExistsAtVisiblePosition(caret)) { bool needExtraLineBreak = !isHTMLHRElement(*pos.anchorNode()) && !isHTMLTableElement(*pos.anchorNode()); insertNodeAt(nodeToInsert.get(), pos); if (needExtraLineBreak) insertNodeBefore(nodeToInsert->cloneNode(false), nodeToInsert); VisiblePosition endingPosition = createVisiblePosition(positionBeforeNode(nodeToInsert.get())); setEndingSelection(VisibleSelection(endingPosition, endingSelection().isDirectional())); } else if (pos.computeEditingOffset() <= caretMinOffset(pos.anchorNode())) { insertNodeAt(nodeToInsert.get(), pos); // Insert an extra br or '\n' if the just inserted one collapsed. if (!isStartOfParagraph(createVisiblePosition(positionBeforeNode(nodeToInsert.get())))) insertNodeBefore(nodeToInsert->cloneNode(false).get(), nodeToInsert.get()); setEndingSelection(VisibleSelection(positionInParentAfterNode(*nodeToInsert), TextAffinity::Downstream, endingSelection().isDirectional())); // If we're inserting after all of the rendered text in a text node, or into a non-text node, // a simple insertion is sufficient. } else if (!pos.anchorNode()->isTextNode() || pos.computeOffsetInContainerNode() >= caretMaxOffset(pos.anchorNode())) { insertNodeAt(nodeToInsert.get(), pos); setEndingSelection(VisibleSelection(positionInParentAfterNode(*nodeToInsert), TextAffinity::Downstream, endingSelection().isDirectional())); } else if (pos.anchorNode()->isTextNode()) { // Split a text node Text* textNode = toText(pos.anchorNode()); splitTextNode(textNode, pos.computeOffsetInContainerNode()); insertNodeBefore(nodeToInsert, textNode); Position endingPosition = firstPositionInNode(textNode); // Handle whitespace that occurs after the split document().updateLayoutIgnorePendingStylesheets(); // TODO(yosin) |isRenderedCharacter()| should be removed, and we should // use |VisiblePosition::characterAfter()|. if (!isRenderedCharacter(endingPosition)) { Position positionBeforeTextNode(positionInParentBeforeNode(*textNode)); // Clear out all whitespace and insert one non-breaking space deleteInsignificantTextDownstream(endingPosition); ASSERT(!textNode->layoutObject() || textNode->layoutObject()->style()->collapseWhiteSpace()); // Deleting insignificant whitespace will remove textNode if it contains nothing but insignificant whitespace. if (textNode->inDocument()) { insertTextIntoNode(textNode, 0, nonBreakingSpaceString()); } else { RefPtrWillBeRawPtr<Text> nbspNode = document().createTextNode(nonBreakingSpaceString()); insertNodeAt(nbspNode.get(), positionBeforeTextNode); endingPosition = firstPositionInNode(nbspNode.get()); } } setEndingSelection(VisibleSelection(endingPosition, TextAffinity::Downstream, endingSelection().isDirectional())); } // Handle the case where there is a typing style. RefPtrWillBeRawPtr<EditingStyle> typingStyle = document().frame()->selection().typingStyle(); if (typingStyle && !typingStyle->isEmpty()) { // Apply the typing style to the inserted line break, so that if the selection // leaves and then comes back, new input will have the right style. // FIXME: We shouldn't always apply the typing style to the line break here, // see <rdar://problem/5794462>. applyStyle(typingStyle.get(), firstPositionInOrBeforeNode(nodeToInsert.get()), lastPositionInOrAfterNode(nodeToInsert.get())); // Even though this applyStyle operates on a Range, it still sets an endingSelection(). // It tries to set a VisibleSelection around the content it operated on. So, that VisibleSelection // will either (a) select the line break we inserted, or it will (b) be a caret just // before the line break (if the line break is at the end of a block it isn't selectable). // So, this next call sets the endingSelection() to a caret just after the line break // that we inserted, or just before it if it's at the end of a block. setEndingSelection(endingSelection().visibleEnd()); } rebalanceWhitespace(); }
void InsertLineBreakCommand::doApply(EditingState* editingState) { deleteSelection(editingState); if (editingState->isAborted()) return; document().updateStyleAndLayoutIgnorePendingStylesheets(); VisibleSelection selection = endingSelection(); if (!selection.isNonOrphanedCaretOrRange()) return; // TODO(xiaochengh): Stop storing VisiblePositions through mutations. VisiblePosition caret(selection.visibleStart()); // FIXME: If the node is hidden, we should still be able to insert text. For // now, we return to avoid a crash. // https://bugs.webkit.org/show_bug.cgi?id=40342 if (caret.isNull()) return; Position pos(caret.deepEquivalent()); pos = positionAvoidingSpecialElementBoundary(pos, editingState); if (editingState->isAborted()) return; pos = positionOutsideTabSpan(pos); Node* nodeToInsert = nullptr; if (shouldUseBreakElement(pos)) nodeToInsert = HTMLBRElement::create(document()); else nodeToInsert = document().createTextNode("\n"); document().updateStyleAndLayoutIgnorePendingStylesheets(); // FIXME: Need to merge text nodes when inserting just after or before text. if (isEndOfParagraph(createVisiblePosition(caret.toPositionWithAffinity())) && !lineBreakExistsAtVisiblePosition(caret)) { bool needExtraLineBreak = !isHTMLHRElement(*pos.anchorNode()) && !isHTMLTableElement(*pos.anchorNode()); insertNodeAt(nodeToInsert, pos, editingState); if (editingState->isAborted()) return; if (needExtraLineBreak) { Node* extraNode; // TODO(tkent): Can we remove HTMLTextFormControlElement dependency? if (HTMLTextFormControlElement* textControl = enclosingTextFormControl(nodeToInsert)) { extraNode = textControl->createPlaceholderBreakElement(); // The placeholder BR should be the last child. There might be // empty Text nodes at |pos|. appendNode(extraNode, nodeToInsert->parentNode(), editingState); } else { extraNode = nodeToInsert->cloneNode(false); insertNodeAfter(extraNode, nodeToInsert, editingState); } if (editingState->isAborted()) return; nodeToInsert = extraNode; } setEndingSelection(SelectionInDOMTree::Builder() .collapse(Position::beforeNode(nodeToInsert)) .setIsDirectional(endingSelection().isDirectional()) .build()); } else if (pos.computeEditingOffset() <= caretMinOffset(pos.anchorNode())) { insertNodeAt(nodeToInsert, pos, editingState); if (editingState->isAborted()) return; document().updateStyleAndLayoutIgnorePendingStylesheets(); // Insert an extra br or '\n' if the just inserted one collapsed. if (!isStartOfParagraph(VisiblePosition::beforeNode(nodeToInsert))) { insertNodeBefore(nodeToInsert->cloneNode(false), nodeToInsert, editingState); if (editingState->isAborted()) return; } setEndingSelection(SelectionInDOMTree::Builder() .collapse(Position::inParentAfterNode(*nodeToInsert)) .setIsDirectional(endingSelection().isDirectional()) .build()); // If we're inserting after all of the rendered text in a text node, or into // a non-text node, a simple insertion is sufficient. } else if (!pos.anchorNode()->isTextNode() || pos.computeOffsetInContainerNode() >= caretMaxOffset(pos.anchorNode())) { insertNodeAt(nodeToInsert, pos, editingState); if (editingState->isAborted()) return; setEndingSelection(SelectionInDOMTree::Builder() .collapse(Position::inParentAfterNode(*nodeToInsert)) .setIsDirectional(endingSelection().isDirectional()) .build()); } else if (pos.anchorNode()->isTextNode()) { // Split a text node Text* textNode = toText(pos.anchorNode()); splitTextNode(textNode, pos.computeOffsetInContainerNode()); insertNodeBefore(nodeToInsert, textNode, editingState); if (editingState->isAborted()) return; Position endingPosition = Position::firstPositionInNode(textNode); // Handle whitespace that occurs after the split document().updateStyleAndLayoutIgnorePendingStylesheets(); // TODO(yosin) |isRenderedCharacter()| should be removed, and we should // use |VisiblePosition::characterAfter()|. if (!isRenderedCharacter(endingPosition)) { Position positionBeforeTextNode(Position::inParentBeforeNode(*textNode)); // Clear out all whitespace and insert one non-breaking space deleteInsignificantTextDownstream(endingPosition); DCHECK(!textNode->layoutObject() || textNode->layoutObject()->style()->collapseWhiteSpace()); // Deleting insignificant whitespace will remove textNode if it contains // nothing but insignificant whitespace. if (textNode->isConnected()) { insertTextIntoNode(textNode, 0, nonBreakingSpaceString()); } else { Text* nbspNode = document().createTextNode(nonBreakingSpaceString()); insertNodeAt(nbspNode, positionBeforeTextNode, editingState); if (editingState->isAborted()) return; endingPosition = Position::firstPositionInNode(nbspNode); } } setEndingSelection(SelectionInDOMTree::Builder() .collapse(endingPosition) .setIsDirectional(endingSelection().isDirectional()) .build()); } // Handle the case where there is a typing style. EditingStyle* typingStyle = document().frame()->selection().typingStyle(); if (typingStyle && !typingStyle->isEmpty()) { // Apply the typing style to the inserted line break, so that if the // selection leaves and then comes back, new input will have the right // style. // FIXME: We shouldn't always apply the typing style to the line break here, // see <rdar://problem/5794462>. applyStyle(typingStyle, firstPositionInOrBeforeNode(nodeToInsert), lastPositionInOrAfterNode(nodeToInsert), editingState); if (editingState->isAborted()) return; // Even though this applyStyle operates on a Range, it still sets an // endingSelection(). It tries to set a VisibleSelection around the content // it operated on. So, that VisibleSelection will either // (a) select the line break we inserted, or it will // (b) be a caret just before the line break (if the line break is at the // end of a block it isn't selectable). // So, this next call sets the endingSelection() to a caret just after the // line break that we inserted, or just before it if it's at the end of a // block. setEndingSelection(SelectionInDOMTree::Builder() .collapse(endingSelection().end()) .build()); } rebalanceWhitespace(); }