InsertLineBreakCommand.cpp [plain text]
#include "config.h"
#include "InsertLineBreakCommand.h"
#include "Document.h"
#include "Frame.h"
#include "FrameSelection.h"
#include "HTMLBRElement.h"
#include "HTMLHRElement.h"
#include "HTMLNames.h"
#include "HTMLTableElement.h"
#include "RenderElement.h"
#include "RenderText.h"
#include "Text.h"
#include "VisibleUnits.h"
#include "htmlediting.h"
namespace WebCore {
using namespace HTMLNames;
InsertLineBreakCommand::InsertLineBreakCommand(Document& document)
: CompositeEditCommand(document)
{
}
bool InsertLineBreakCommand::preservesTypingStyle() const
{
return true;
}
void InsertLineBreakCommand::insertNodeAfterPosition(Node* node, const Position& position)
{
auto* element = deprecatedEnclosingBlockFlowElement(position.deprecatedNode());
if (element == position.deprecatedNode())
appendNode(node, element);
else
insertNodeAfter(node, position.deprecatedNode());
}
void InsertLineBreakCommand::insertNodeBeforePosition(Node* node, const Position& position)
{
auto* element = deprecatedEnclosingBlockFlowElement(position.deprecatedNode());
if (element == position.deprecatedNode())
appendNode(node, element);
else
insertNodeBefore(node, position.deprecatedNode());
}
bool InsertLineBreakCommand::shouldUseBreakElement(const Position& position)
{
auto* node = position.parentAnchoredEquivalent().deprecatedNode();
return node->renderer() && !node->renderer()->style().preserveNewline();
}
void InsertLineBreakCommand::doApply()
{
deleteSelection();
VisibleSelection selection = endingSelection();
if (selection.isNoneOrOrphaned())
return;
VisiblePosition caret(selection.visibleStart());
if (caret.isNull())
return;
Position position(caret.deepEquivalent());
position = positionAvoidingSpecialElementBoundary(position);
position = positionOutsideTabSpan(position);
RefPtr<Node> nodeToInsert;
if (shouldUseBreakElement(position))
nodeToInsert = HTMLBRElement::create(document());
else
nodeToInsert = document().createTextNode("\n");
if (isEndOfParagraph(caret) && !lineBreakExistsAtVisiblePosition(caret)) {
bool needExtraLineBreak = !is<HTMLHRElement>(*position.deprecatedNode()) && !is<HTMLTableElement>(*position.deprecatedNode());
insertNodeAt(nodeToInsert.get(), position);
if (needExtraLineBreak)
insertNodeBefore(nodeToInsert->cloneNode(false), nodeToInsert);
VisiblePosition endingPosition(positionBeforeNode(nodeToInsert.get()));
setEndingSelection(VisibleSelection(endingPosition, endingSelection().isDirectional()));
} else if (position.deprecatedEditingOffset() <= caretMinOffset(*position.deprecatedNode())) {
insertNodeAt(nodeToInsert.get(), position);
if (!isStartOfParagraph(positionBeforeNode(nodeToInsert.get())))
insertNodeBefore(nodeToInsert->cloneNode(false), nodeToInsert.get());
setEndingSelection(VisibleSelection(positionInParentAfterNode(nodeToInsert.get()), DOWNSTREAM, endingSelection().isDirectional()));
} else if (position.deprecatedEditingOffset() >= caretMaxOffset(*position.deprecatedNode()) || !is<Text>(*position.deprecatedNode())) {
insertNodeAt(nodeToInsert.get(), position);
setEndingSelection(VisibleSelection(positionInParentAfterNode(nodeToInsert.get()), DOWNSTREAM, endingSelection().isDirectional()));
} else if (is<Text>(*position.deprecatedNode())) {
Text& textNode = downcast<Text>(*position.deprecatedNode());
splitTextNode(&textNode, position.deprecatedEditingOffset());
insertNodeBefore(nodeToInsert, &textNode);
Position endingPosition = firstPositionInNode(&textNode);
document().updateLayoutIgnorePendingStylesheets();
if (!endingPosition.isRenderedCharacter()) {
Position positionBeforeTextNode(positionInParentBeforeNode(&textNode));
deleteInsignificantTextDownstream(endingPosition);
ASSERT(!textNode.renderer() || textNode.renderer()->style().collapseWhiteSpace());
if (textNode.inDocument())
insertTextIntoNode(&textNode, 0, nonBreakingSpaceString());
else {
RefPtr<Text> nbspNode = document().createTextNode(nonBreakingSpaceString());
insertNodeAt(nbspNode.get(), positionBeforeTextNode);
endingPosition = firstPositionInNode(nbspNode.get());
}
}
setEndingSelection(VisibleSelection(endingPosition, DOWNSTREAM, endingSelection().isDirectional()));
}
RefPtr<EditingStyle> typingStyle = frame().selection().typingStyle();
if (typingStyle && !typingStyle->isEmpty()) {
applyStyle(typingStyle.get(), firstPositionInOrBeforeNode(nodeToInsert.get()), lastPositionInOrAfterNode(nodeToInsert.get()));
setEndingSelection(endingSelection().visibleEnd());
}
rebalanceWhitespace();
}
}