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, bool useDefaultParagraphElement)
: CompositeEditCommand(document)
, m_useDefaultParagraphElement(useDefaultParagraphElement)
{
}
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 = enclosingBlock(pos.node());
Position canonicalPos = VisiblePosition(pos).deepEquivalent();
if (!block || !block->parentNode() ||
block->renderer() && block->renderer()->isTableCell() ||
canonicalPos.node()->renderer() && canonicalPos.node()->renderer()->isTable() ||
canonicalPos.node()->hasTagName(hrTag)) {
applyCommandToComposite(new InsertLineBreakCommand(document()));
return;
}
pos = pos.upstream();
if (!pos.isCandidate())
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 if (m_useDefaultParagraphElement)
blockToInsert = static_pointer_cast<Node>(createDefaultParagraphElement(document()));
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(Selection(Position(blockToInsert.get(), 0), DOWNSTREAM));
applyStyleAfterInsertion();
return;
}
if (isFirstInBlock || !inSameBlock(visiblePos, visiblePos.previous())) {
Node *refNode;
if (isFirstInBlock && !nestNewBlock)
refNode = startBlock;
else if (pos.node() == startBlock && nestNewBlock) {
refNode = startBlock->childNode(pos.offset());
ASSERT(refNode); } else
refNode = pos.node();
pos = pos.downstream();
insertNodeBefore(blockToInsert.get(), refNode);
appendBlockPlaceholder(blockToInsert.get());
setEndingSelection(Selection(Position(blockToInsert.get(), 0), DOWNSTREAM));
applyStyleAfterInsertion();
setEndingSelection(Selection(pos, DOWNSTREAM));
return;
}
if (isStartOfParagraph(visiblePos)) {
RefPtr<Element> br = createBreakElement(document());
insertNodeAt(br.get(), pos);
pos = positionAfterNode(br.get());
}
pos = pos.downstream();
startNode = pos.node();
Vector<Node*> ancestors;
if (startNode != startBlock)
for (Node* n = startNode->parentNode(); n && n != startBlock; n = n->parentNode())
ancestors.append(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);
updateLayout();
RefPtr<Node> parent = blockToInsert;
for (size_t i = ancestors.size(); i != 0; --i) {
RefPtr<Node> child = ancestors[i - 1]->cloneNode(false); appendNode(child.get(), parent.get());
parent = child.release();
}
if (isEndOfParagraph(visiblePos) && !lineBreakExistsAtPosition(visiblePos))
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;
}
}
if (!ancestors.isEmpty()) {
Node* leftParent = ancestors.first();
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(Selection(Position(blockToInsert.get(), 0), DOWNSTREAM));
applyStyleAfterInsertion();
}
}