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 mustUseDefaultParagraphElement)
: CompositeEditCommand(document)
, m_mustUseDefaultParagraphElement(mustUseDefaultParagraphElement)
{
}
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(Node* originalEnclosingBlock)
{
if (originalEnclosingBlock->hasTagName(h1Tag) ||
originalEnclosingBlock->hasTagName(h2Tag) ||
originalEnclosingBlock->hasTagName(h3Tag) ||
originalEnclosingBlock->hasTagName(h4Tag) ||
originalEnclosingBlock->hasTagName(h5Tag))
return;
if (!m_style)
return;
computedStyle(endingSelection().start().node())->diff(m_style.get());
if (m_style->length() > 0)
applyStyle(m_style.get());
}
bool InsertParagraphSeparatorCommand::shouldUseDefaultParagraphElement(Node* enclosingBlock) const
{
if (m_mustUseDefaultParagraphElement)
return true;
if (!isEndOfBlock(endingSelection().visibleStart()))
return false;
return enclosingBlock->hasTagName(h1Tag) ||
enclosingBlock->hasTagName(h2Tag) ||
enclosingBlock->hasTagName(h3Tag) ||
enclosingBlock->hasTagName(h4Tag) ||
enclosingBlock->hasTagName(h5Tag);
}
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* startBlockNode = enclosingBlock(rangeCompliantEquivalent(pos).node());
Position canonicalPos = VisiblePosition(pos).deepEquivalent();
Element* startBlock = static_cast<Element*>(startBlockNode);
if (!startBlockNode
|| !startBlockNode->isElementNode()
|| !startBlock->parentNode()
|| isTableCell(startBlock)
|| startBlock->hasTagName(formTag)
|| canonicalPos.node()->renderer() && canonicalPos.node()->renderer()->isTable()
|| canonicalPos.node()->hasTagName(hrTag)) {
applyCommandToComposite(InsertLineBreakCommand::create(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();
bool isFirstInBlock = isStartOfBlock(visiblePos);
bool isLastInBlock = isEndOfBlock(visiblePos);
bool nestNewBlock = false;
RefPtr<Element> blockToInsert;
if (startBlock == startBlock->rootEditableElement()) {
blockToInsert = createDefaultParagraphElement(document());
nestNewBlock = true;
} else if (shouldUseDefaultParagraphElement(startBlock))
blockToInsert = createDefaultParagraphElement(document());
else
blockToInsert = startBlock->cloneElement();
if (isLastInBlock) {
if (nestNewBlock) {
if (isFirstInBlock && !lineBreakExistsAtVisiblePosition(visiblePos)) {
RefPtr<Element> extraBlock = createDefaultParagraphElement(document());
appendNode(extraBlock, startBlock);
appendBlockPlaceholder(extraBlock);
}
appendNode(blockToInsert, startBlock);
} else
insertNodeAfter(blockToInsert, startBlock);
appendBlockPlaceholder(blockToInsert);
setEndingSelection(Selection(Position(blockToInsert.get(), 0), DOWNSTREAM));
applyStyleAfterInsertion(startBlock);
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, refNode);
appendBlockPlaceholder(blockToInsert.get());
setEndingSelection(Selection(Position(blockToInsert.get(), 0), DOWNSTREAM));
applyStyleAfterInsertion(startBlock);
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<Element*> ancestors;
if (startNode != startBlock) {
for (Element* n = startNode->parentElement(); n && n != startBlock; n = n->parentElement())
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<Element> parent = blockToInsert;
for (size_t i = ancestors.size(); i != 0; --i) {
RefPtr<Element> child = ancestors[i - 1]->cloneElement(); appendNode(child, parent);
parent = child.release();
}
if (isEndOfParagraph(visiblePos) && !lineBreakExistsAtVisiblePosition(visiblePos))
appendNode(createBreakElement(document()).get(), blockToInsert.get());
if (startNode != startBlock) {
Node *n = startNode;
if (pos.offset() >= caretMaxOffset(startNode))
n = startNode->nextSibling();
while (n && n != blockToInsert) {
Node *next = n->nextSibling();
removeNode(n);
appendNode(n, parent.get());
n = next;
}
}
if (!ancestors.isEmpty()) {
Element* leftParent = ancestors.first();
while (leftParent && leftParent != startBlock) {
parent = parent->parentElement();
if (!parent)
break;
Node* n = leftParent->nextSibling();
while (n && n != blockToInsert) {
Node* next = n->nextSibling();
removeNode(n);
appendNode(n, parent.get());
n = next;
}
leftParent = leftParent->parentElement();
}
}
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(startBlock);
}
}