InsertParagraphSeparatorCommand.cpp [plain text]
#include "config.h"
#include "InsertParagraphSeparatorCommand.h"
#include "Document.h"
#include "Logging.h"
#include "CSSComputedStyleDeclaration.h"
#include "CSSPropertyNames.h"
#include "Text.h"
#include "htmlediting.h"
#include "HTMLElement.h"
#include "HTMLNames.h"
#include "InsertLineBreakCommand.h"
#include "RenderObject.h"
#include "visible_units.h"
namespace WebCore {
using namespace HTMLNames;
InsertParagraphSeparatorCommand::InsertParagraphSeparatorCommand(Document *document)
: CompositeEditCommand(document)
{
}
bool InsertParagraphSeparatorCommand::preservesTypingStyle() const
{
return true;
}
void InsertParagraphSeparatorCommand::calculateStyleBeforeInsertion(const Position &pos)
{
VisiblePosition visiblePos(pos, VP_DEFAULT_AFFINITY);
if (!isStartOfParagraph(visiblePos) && !isEndOfParagraph(visiblePos))
return;
m_style = styleAtPosition(pos);
}
void InsertParagraphSeparatorCommand::applyStyleAfterInsertion()
{
if (!m_style)
return;
CSSComputedStyleDeclaration endingStyle(endingSelection().start().node());
endingStyle.diff(m_style.get());
if (m_style->length() > 0)
applyStyle(m_style.get());
}
void InsertParagraphSeparatorCommand::doApply()
{
bool splitText = false;
if (endingSelection().isNone())
return;
Position pos = endingSelection().start();
EAffinity affinity = endingSelection().affinity();
if (endingSelection().isRange()) {
calculateStyleBeforeInsertion(pos);
deleteSelection(false, true);
pos = endingSelection().start();
affinity = endingSelection().affinity();
}
Node* block = pos.node()->enclosingBlockFlowElement();
if (!block ||
!block->parentNode() ||
block->renderer() && block->renderer()->isTableCell()) {
EditCommandPtr cmd(new InsertLineBreakCommand(document()));
applyCommandToComposite(cmd);
return;
}
pos = pos.upstream();
if (!pos.inRenderedContent())
pos = pos.downstream();
pos = positionAvoidingSpecialElementBoundary(pos);
VisiblePosition visiblePos(pos, affinity);
calculateStyleBeforeInsertion(pos);
if (breakOutOfEmptyListItem())
return;
Node *startNode = pos.node();
Node *startBlock = startNode->enclosingBlockFlowElement();
bool isFirstInBlock = isStartOfBlock(visiblePos);
bool isLastInBlock = isEndOfBlock(visiblePos);
bool nestNewBlock = false;
RefPtr<Node> blockToInsert;
if (startBlock == startBlock->rootEditableElement()) {
blockToInsert = static_pointer_cast<Node>(createDefaultParagraphElement(document()));
nestNewBlock = true;
} else
blockToInsert = startBlock->cloneNode(false);
if (isLastInBlock) {
if (nestNewBlock) {
if (isFirstInBlock) {
RefPtr<Node> extraBlock = createDefaultParagraphElement(document());
appendNode(extraBlock.get(), startBlock);
appendBlockPlaceholder(extraBlock.get());
}
appendNode(blockToInsert.get(), startBlock);
} else
insertNodeAfter(blockToInsert.get(), startBlock);
appendBlockPlaceholder(blockToInsert.get());
setEndingSelection(Position(blockToInsert.get(), 0), DOWNSTREAM);
applyStyleAfterInsertion();
return;
}
if (isFirstInBlock || !inSameBlock(visiblePos, visiblePos.previous())) {
pos = pos.downstream();
Position refPos;
Node *refNode;
if (isFirstInBlock && !nestNewBlock)
refNode = startBlock;
else if (pos.node() == startBlock && nestNewBlock) {
refNode = startBlock->childNode(pos.offset());
ASSERT(refNode); } else
refNode = pos.node();
insertNodeBefore(blockToInsert.get(), refNode);
appendBlockPlaceholder(blockToInsert.get());
setEndingSelection(Position(blockToInsert.get(), 0), DOWNSTREAM);
applyStyleAfterInsertion();
setEndingSelection(pos, DOWNSTREAM);
return;
}
Position upstreamPos = pos.upstream();
if (upstreamPos.node()->hasTagName(brTag)) {
RefPtr<Element> br = createBreakElement(document());
insertNodeAfter(br.get(), upstreamPos.node());
pos = positionAfterNode(br.get());
}
pos = pos.downstream();
startNode = pos.node();
DeprecatedPtrList<Node> ancestors;
if (startNode != startBlock) {
for (Node *n = startNode->parentNode(); n && n != startBlock; n = n->parentNode())
ancestors.prepend(n);
}
Position leadingWhitespace = pos.leadingWhitespacePosition(VP_DEFAULT_AFFINITY);
if (leadingWhitespace.isNotNull()) {
Text* textNode = static_cast<Text*>(leadingWhitespace.node());
ASSERT(!textNode->renderer() || textNode->renderer()->style()->collapseWhiteSpace());
replaceTextInNode(textNode, leadingWhitespace.offset(), 1, nonBreakingSpaceString());
}
if (startNode->isTextNode()) {
Text *textNode = static_cast<Text *>(startNode);
bool atEnd = (unsigned)pos.offset() >= textNode->length();
if (pos.offset() > 0 && !atEnd) {
splitTextNode(textNode, pos.offset());
pos = Position(startNode, 0);
visiblePos = VisiblePosition(pos);
splitText = true;
}
}
if (nestNewBlock)
appendNode(blockToInsert.get(), startBlock);
else
insertNodeAfter(blockToInsert.get(), startBlock);
RefPtr<Node> parent = blockToInsert;
for (DeprecatedPtrListIterator<Node> it(ancestors); it.current(); ++it) {
RefPtr<Node> child = it.current()->cloneNode(false); appendNode(child.get(), parent.get());
parent = child.release();
}
if (isEndOfParagraph(visiblePos) && !startNode->hasTagName(brTag) &&
!(startNode->renderer()->style()->preserveNewline() && visiblePos.characterAfter() == '\n'))
appendNode(createBreakElement(document()).get(), blockToInsert.get());
if (startNode != startBlock) {
Node *n = startNode;
if (pos.offset() >= startNode->caretMaxOffset())
n = startNode->nextSibling();
while (n && n != blockToInsert) {
Node *next = n->nextSibling();
removeNode(n);
appendNode(n, parent.get());
n = next;
}
}
Node *leftParent = ancestors.last();
while (leftParent && leftParent != startBlock) {
parent = parent->parentNode();
Node *n = leftParent->nextSibling();
while (n && n != blockToInsert) {
Node *next = n->nextSibling();
removeNode(n);
appendNode(n, parent.get());
n = next;
}
leftParent = leftParent->parentNode();
}
if (splitText) {
updateLayout();
pos = Position(startNode, 0);
if (!pos.isRenderedCharacter()) {
ASSERT(startNode);
ASSERT(startNode->isTextNode());
ASSERT(!startNode->renderer() || startNode->renderer()->style()->collapseWhiteSpace());
deleteInsignificantTextDownstream(pos);
insertTextIntoNode(static_cast<Text*>(startNode), 0, nonBreakingSpaceString());
}
}
setEndingSelection(Position(blockToInsert.get(), 0), DOWNSTREAM);
applyStyleAfterInsertion();
}
}