InsertLineBreakCommand.cpp [plain text]
#include "config.h"
#include "InsertLineBreakCommand.h"
#include "CSSMutableStyleDeclaration.h"
#include "Document.h"
#include "Element.h"
#include "Frame.h"
#include "Text.h"
#include "VisiblePosition.h"
#include "Range.h"
#include "htmlediting.h"
#include "HTMLNames.h"
#include "visible_units.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)
{
Position upstream(pos.upstream());
Node *cb = pos.node()->enclosingBlockFlowElement();
if (cb == pos.node())
appendNode(node, cb);
else
insertNodeAfter(node, pos.node());
}
void InsertLineBreakCommand::insertNodeBeforePosition(Node *node, const Position &pos)
{
Position upstream(pos.upstream());
Node *cb = pos.node()->enclosingBlockFlowElement();
if (cb == pos.node())
appendNode(node, cb);
else
insertNodeBefore(node, pos.node());
}
bool InsertLineBreakCommand::shouldUseBreakElement(const Position& insertionPos)
{
Position p(rangeCompliantEquivalent(insertionPos));
return p.node()->renderer() && !p.node()->renderer()->style()->preserveNewline();
}
void InsertLineBreakCommand::doApply()
{
deleteSelection();
Selection selection = endingSelection();
if (selection.isNone())
return;
VisiblePosition caret(selection.visibleStart());
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) && !lineBreakExistsAtPosition(caret)) {
bool needExtraLineBreak = !pos.node()->hasTagName(hrTag) && !pos.node()->hasTagName(tableTag);
insertNodeAt(nodeToInsert.get(), pos);
if (needExtraLineBreak)
insertNodeBefore(nodeToInsert->cloneNode(false).get(), nodeToInsert.get());
VisiblePosition endingPosition(Position(nodeToInsert.get(), 0));
setEndingSelection(Selection(endingPosition));
} else if (pos.offset() <= caretMinOffset(pos.node())) {
insertNodeAt(nodeToInsert.get(), pos);
if (!isStartOfParagraph(VisiblePosition(Position(nodeToInsert.get(), 0))))
insertNodeBefore(nodeToInsert->cloneNode(false).get(), nodeToInsert.get());
setEndingSelection(Selection(positionAfterNode(nodeToInsert.get()), DOWNSTREAM));
} else if (pos.offset() >= caretMaxOffset(pos.node())) {
insertNodeAt(nodeToInsert.get(), pos);
setEndingSelection(Selection(positionAfterNode(nodeToInsert.get()), DOWNSTREAM));
} else {
ASSERT(pos.node()->isTextNode());
ExceptionCode ec = 0;
Text *textNode = static_cast<Text *>(pos.node());
RefPtr<Text> textBeforeNode = document()->createTextNode(textNode->substringData(0, selection.start().offset(), ec));
deleteTextFromNode(textNode, 0, pos.offset());
insertNodeBefore(textBeforeNode.get(), textNode);
insertNodeBefore(nodeToInsert.get(), textNode);
Position endingPosition = Position(textNode, 0);
updateLayout();
if (!endingPosition.isRenderedCharacter()) {
Position positionBeforeTextNode(positionBeforeNode(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 = Position(nbspNode.get(), 0);
}
}
setEndingSelection(Selection(endingPosition, DOWNSTREAM));
}
CSSMutableStyleDeclaration* typingStyle = document()->frame()->typingStyle();
if (typingStyle && typingStyle->length() > 0) {
Selection selectionBeforeStyle = endingSelection();
applyStyle(typingStyle, Position(nodeToInsert.get(), 0),
Position(nodeToInsert.get(), maxDeepOffset(nodeToInsert.get())));
setEndingSelection(selectionBeforeStyle);
}
rebalanceWhitespace();
}
}