InsertLineBreakCommand.cpp [plain text]
#include "config.h"
#include "InsertLineBreakCommand.h"
#include "Document.h"
#include "Frame.h"
#include "FrameSelection.h"
#include "HTMLElement.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& pos)
{
Element* cb = deprecatedEnclosingBlockFlowElement(pos.deprecatedNode());
if (cb == pos.deprecatedNode())
appendNode(node, cb);
else
insertNodeAfter(node, pos.deprecatedNode());
}
void InsertLineBreakCommand::insertNodeBeforePosition(Node* node, const Position& pos)
{
Element* cb = deprecatedEnclosingBlockFlowElement(pos.deprecatedNode());
if (cb == pos.deprecatedNode())
appendNode(node, cb);
else
insertNodeBefore(node, pos.deprecatedNode());
}
bool InsertLineBreakCommand::shouldUseBreakElement(const Position& insertionPos)
{
Position p(insertionPos.parentAnchoredEquivalent());
return p.deprecatedNode()->renderer() && !p.deprecatedNode()->renderer()->style().preserveNewline();
}
void InsertLineBreakCommand::doApply()
{
deleteSelection();
VisibleSelection selection = endingSelection();
if (!selection.isNonOrphanedCaretOrRange())
return;
VisiblePosition caret(selection.visibleStart());
if (caret.isNull())
return;
Position pos(caret.deepEquivalent());
pos = positionAvoidingSpecialElementBoundary(pos);
pos = positionOutsideTabSpan(pos);
RefPtr<Node> nodeToInsert;
if (shouldUseBreakElement(pos))
nodeToInsert = createBreakElement(document());
else
nodeToInsert = document().createTextNode("\n");
if (isEndOfParagraph(caret) && !lineBreakExistsAtVisiblePosition(caret)) {
bool needExtraLineBreak = !pos.deprecatedNode()->hasTagName(hrTag) && !isHTMLTableElement(pos.deprecatedNode());
insertNodeAt(nodeToInsert.get(), pos);
if (needExtraLineBreak)
insertNodeBefore(nodeToInsert->cloneNode(false), nodeToInsert);
VisiblePosition endingPosition(positionBeforeNode(nodeToInsert.get()));
setEndingSelection(VisibleSelection(endingPosition, endingSelection().isDirectional()));
} else if (pos.deprecatedEditingOffset() <= caretMinOffset(pos.deprecatedNode())) {
insertNodeAt(nodeToInsert.get(), pos);
if (!isStartOfParagraph(positionBeforeNode(nodeToInsert.get())))
insertNodeBefore(nodeToInsert->cloneNode(false).get(), nodeToInsert.get());
setEndingSelection(VisibleSelection(positionInParentAfterNode(nodeToInsert.get()), DOWNSTREAM, endingSelection().isDirectional()));
} else if (pos.deprecatedEditingOffset() >= caretMaxOffset(pos.deprecatedNode()) || !pos.deprecatedNode()->isTextNode()) {
insertNodeAt(nodeToInsert.get(), pos);
setEndingSelection(VisibleSelection(positionInParentAfterNode(nodeToInsert.get()), DOWNSTREAM, endingSelection().isDirectional()));
} else if (pos.deprecatedNode()->isTextNode()) {
Text* textNode = toText(pos.deprecatedNode());
splitTextNode(textNode, pos.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();
}
}