This source file includes following definitions.
- highestVisuallyEquivalentDivBelowRoot
- m_pasteBlockqutoeIntoUnquotedArea
- preservesTypingStyle
- calculateStyleBeforeInsertion
- applyStyleAfterInsertion
- shouldUseDefaultParagraphElement
- getAncestorsInsideBlock
- cloneHierarchyUnderNewBlock
- doApply
#include "config.h"
#include "core/editing/InsertParagraphSeparatorCommand.h"
#include "HTMLNames.h"
#include "core/dom/Document.h"
#include "core/dom/NodeTraversal.h"
#include "core/dom/Text.h"
#include "core/editing/EditingStyle.h"
#include "core/editing/InsertLineBreakCommand.h"
#include "core/editing/VisibleUnits.h"
#include "core/editing/htmlediting.h"
#include "core/html/HTMLElement.h"
#include "core/rendering/RenderObject.h"
namespace WebCore {
using namespace HTMLNames;
static Element* highestVisuallyEquivalentDivBelowRoot(Element* startBlock)
{
Element* curBlock = startBlock;
while (!curBlock->nextSibling() && isHTMLDivElement(*curBlock->parentElement()) && curBlock->parentElement()->parentElement()) {
if (curBlock->parentElement()->hasAttributes())
break;
curBlock = curBlock->parentElement();
}
return curBlock;
}
InsertParagraphSeparatorCommand::InsertParagraphSeparatorCommand(Document& document, bool mustUseDefaultParagraphElement, bool pasteBlockqutoeIntoUnquotedArea)
: CompositeEditCommand(document)
, m_mustUseDefaultParagraphElement(mustUseDefaultParagraphElement)
, m_pasteBlockqutoeIntoUnquotedArea(pasteBlockqutoeIntoUnquotedArea)
{
}
bool InsertParagraphSeparatorCommand::preservesTypingStyle() const
{
return true;
}
void InsertParagraphSeparatorCommand::calculateStyleBeforeInsertion(const Position &pos)
{
VisiblePosition visiblePos(pos, VP_DEFAULT_AFFINITY);
if (!isStartOfParagraph(visiblePos) && !isEndOfParagraph(visiblePos))
return;
ASSERT(pos.isNotNull());
m_style = EditingStyle::create(pos);
m_style->mergeTypingStyle(pos.document());
}
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;
m_style->prepareToApplyAt(endingSelection().start());
if (!m_style->isEmpty())
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::getAncestorsInsideBlock(const Node* insertionNode, Element* outerBlock, Vector<RefPtr<Element> >& ancestors)
{
ancestors.clear();
if (insertionNode != outerBlock) {
for (Element* n = insertionNode->parentElement(); n && n != outerBlock; n = n->parentElement())
ancestors.append(n);
}
}
PassRefPtr<Element> InsertParagraphSeparatorCommand::cloneHierarchyUnderNewBlock(const Vector<RefPtr<Element> >& ancestors, PassRefPtr<Element> blockToInsert)
{
RefPtr<Element> parent = blockToInsert;
for (size_t i = ancestors.size(); i != 0; --i) {
RefPtr<Element> child = ancestors[i - 1]->cloneElementWithoutChildren();
child->removeAttribute(idAttr);
appendNode(child, parent);
parent = child.release();
}
return parent.release();
}
void InsertParagraphSeparatorCommand::doApply()
{
if (!endingSelection().isNonOrphanedCaretOrRange())
return;
Position insertionPosition = endingSelection().start();
EAffinity affinity = endingSelection().affinity();
if (endingSelection().isRange()) {
calculateStyleBeforeInsertion(insertionPosition);
deleteSelection(false, true);
insertionPosition = endingSelection().start();
affinity = endingSelection().affinity();
}
RefPtr<Element> startBlock = enclosingBlock(insertionPosition.parentAnchoredEquivalent().containerNode());
Node* listChildNode = enclosingListChild(insertionPosition.parentAnchoredEquivalent().containerNode());
RefPtr<Element> listChild = listChildNode && listChildNode->isHTMLElement() ? toHTMLElement(listChildNode) : 0;
Position canonicalPos = VisiblePosition(insertionPosition).deepEquivalent();
if (!startBlock
|| !startBlock->nonShadowBoundaryParentNode()
|| isTableCell(startBlock.get())
|| isHTMLFormElement(*startBlock)
|| (!canonicalPos.isNull() && isRenderedTable(canonicalPos.deprecatedNode()))
|| (!canonicalPos.isNull() && isHTMLHRElement(*canonicalPos.deprecatedNode()))) {
applyCommandToComposite(InsertLineBreakCommand::create(document()));
return;
}
insertionPosition = insertionPosition.upstream();
if (!insertionPosition.isCandidate())
insertionPosition = insertionPosition.downstream();
insertionPosition = positionAvoidingSpecialElementBoundary(insertionPosition);
VisiblePosition visiblePos(insertionPosition, affinity);
calculateStyleBeforeInsertion(insertionPosition);
if (breakOutOfEmptyListItem())
return;
bool isFirstInBlock = isStartOfBlock(visiblePos);
bool isLastInBlock = isEndOfBlock(visiblePos);
bool nestNewBlock = false;
RefPtr<Element> blockToInsert;
if (startBlock->isRootEditableElement()) {
blockToInsert = createDefaultParagraphElement(document());
nestNewBlock = true;
} else if (shouldUseDefaultParagraphElement(startBlock.get())) {
blockToInsert = createDefaultParagraphElement(document());
} else {
blockToInsert = startBlock->cloneElementWithoutChildren();
}
if (isLastInBlock) {
if (nestNewBlock) {
if (isFirstInBlock && !lineBreakExistsAtVisiblePosition(visiblePos)) {
RefPtr<Element> extraBlock = createDefaultParagraphElement(document());
appendNode(extraBlock, startBlock);
appendBlockPlaceholder(extraBlock);
}
appendNode(blockToInsert, startBlock);
} else {
if (m_pasteBlockqutoeIntoUnquotedArea) {
if (Node* highestBlockquote = highestEnclosingNodeOfType(canonicalPos, &isMailBlockquote))
startBlock = toElement(highestBlockquote);
}
if (listChild && listChild != startBlock) {
RefPtr<Element> listChildToInsert = listChild->cloneElementWithoutChildren();
appendNode(blockToInsert, listChildToInsert.get());
insertNodeAfter(listChildToInsert.get(), listChild);
} else {
Element* siblingNode = startBlock.get();
if (isHTMLDivElement(*blockToInsert))
siblingNode = highestVisuallyEquivalentDivBelowRoot(startBlock.get());
insertNodeAfter(blockToInsert, siblingNode);
}
}
Vector<RefPtr<Element> > ancestors;
getAncestorsInsideBlock(positionOutsideTabSpan(insertionPosition).deprecatedNode(), startBlock.get(), ancestors);
RefPtr<Element> parent = cloneHierarchyUnderNewBlock(ancestors, blockToInsert);
appendBlockPlaceholder(parent);
setEndingSelection(VisibleSelection(firstPositionInNode(parent.get()), DOWNSTREAM, endingSelection().isDirectional()));
return;
}
if (isFirstInBlock || !inSameBlock(visiblePos, visiblePos.previous())) {
Node* refNode = 0;
insertionPosition = positionOutsideTabSpan(insertionPosition);
if (isFirstInBlock && !nestNewBlock) {
if (listChild && listChild != startBlock) {
RefPtr<Element> listChildToInsert = listChild->cloneElementWithoutChildren();
appendNode(blockToInsert, listChildToInsert.get());
insertNodeBefore(listChildToInsert.get(), listChild);
} else {
refNode = startBlock.get();
}
} else if (isFirstInBlock && nestNewBlock) {
ASSERT(startBlock->firstChild());
refNode = startBlock->firstChild();
}
else if (insertionPosition.deprecatedNode() == startBlock && nestNewBlock) {
refNode = startBlock->traverseToChildAt(insertionPosition.deprecatedEditingOffset());
ASSERT(refNode);
} else
refNode = insertionPosition.deprecatedNode();
insertionPosition = insertionPosition.downstream();
if (refNode)
insertNodeBefore(blockToInsert, refNode);
Vector<RefPtr<Element> > ancestors;
getAncestorsInsideBlock(positionAvoidingSpecialElementBoundary(positionOutsideTabSpan(insertionPosition)).deprecatedNode(), startBlock.get(), ancestors);
appendBlockPlaceholder(cloneHierarchyUnderNewBlock(ancestors, blockToInsert));
setEndingSelection(VisibleSelection(insertionPosition, DOWNSTREAM, endingSelection().isDirectional()));
return;
}
if (isStartOfParagraph(visiblePos)) {
RefPtr<Element> br = createBreakElement(document());
insertNodeAt(br.get(), insertionPosition);
insertionPosition = positionInParentAfterNode(*br);
if (visiblePos.deepEquivalent().anchorNode()->renderer()->isBR()) {
setEndingSelection(VisibleSelection(insertionPosition, DOWNSTREAM, endingSelection().isDirectional()));
return;
}
}
insertionPosition = insertionPosition.downstream();
insertionPosition = positionOutsideTabSpan(VisiblePosition(insertionPosition).deepEquivalent());
if (editingIgnoresContent(insertionPosition.deprecatedNode())) {
if (insertionPosition.atLastEditingPositionForNode())
insertionPosition = insertionPosition.downstream();
else if (insertionPosition.atFirstEditingPositionForNode())
insertionPosition = insertionPosition.upstream();
}
Position leadingWhitespace = insertionPosition.leadingWhitespacePosition(VP_DEFAULT_AFFINITY);
if (leadingWhitespace.isNotNull() && leadingWhitespace.deprecatedNode()->isTextNode()) {
Text* textNode = toText(leadingWhitespace.deprecatedNode());
ASSERT(!textNode->renderer() || textNode->renderer()->style()->collapseWhiteSpace());
replaceTextInNodePreservingMarkers(textNode, leadingWhitespace.deprecatedEditingOffset(), 1, nonBreakingSpaceString());
}
Position positionAfterSplit;
if (insertionPosition.anchorType() == Position::PositionIsOffsetInAnchor && insertionPosition.containerNode()->isTextNode()) {
RefPtr<Text> textNode = toText(insertionPosition.containerNode());
bool atEnd = static_cast<unsigned>(insertionPosition.offsetInContainerNode()) >= textNode->length();
if (insertionPosition.deprecatedEditingOffset() > 0 && !atEnd) {
splitTextNode(textNode, insertionPosition.offsetInContainerNode());
positionAfterSplit = firstPositionInNode(textNode.get());
insertionPosition.moveToPosition(textNode->previousSibling(), insertionPosition.offsetInContainerNode());
visiblePos = VisiblePosition(insertionPosition);
}
}
if (!startBlock->parentNode())
return;
if (nestNewBlock) {
appendNode(blockToInsert.get(), startBlock);
} else if (listChild && listChild != startBlock) {
RefPtr<Element> listChildToInsert = listChild->cloneElementWithoutChildren();
appendNode(blockToInsert.get(), listChildToInsert.get());
insertNodeAfter(listChildToInsert.get(), listChild);
} else {
insertNodeAfter(blockToInsert.get(), startBlock);
}
document().updateLayoutIgnorePendingStylesheets();
if (isEndOfParagraph(visiblePos) && !lineBreakExistsAtVisiblePosition(visiblePos))
appendNode(createBreakElement(document()).get(), blockToInsert.get());
if (VisiblePosition(insertionPosition) != VisiblePosition(positionBeforeNode(blockToInsert.get()))) {
Node* n;
if (insertionPosition.containerNode() == startBlock)
n = insertionPosition.computeNodeAfterPosition();
else {
Node* splitTo = insertionPosition.containerNode();
if (splitTo->isTextNode() && insertionPosition.offsetInContainerNode() >= caretMaxOffset(splitTo))
splitTo = NodeTraversal::next(*splitTo, startBlock.get());
ASSERT(splitTo);
splitTreeToNode(splitTo, startBlock.get());
for (n = startBlock->firstChild(); n; n = n->nextSibling()) {
VisiblePosition beforeNodePosition(positionBeforeNode(n));
if (!beforeNodePosition.isNull() && comparePositions(VisiblePosition(insertionPosition), beforeNodePosition) <= 0)
break;
}
}
moveRemainingSiblingsToNewParent(n, blockToInsert.get(), blockToInsert);
}
if (positionAfterSplit.isNotNull()) {
document().updateLayoutIgnorePendingStylesheets();
if (!positionAfterSplit.isRenderedCharacter()) {
ASSERT(!positionAfterSplit.containerNode()->renderer() || positionAfterSplit.containerNode()->renderer()->style()->collapseWhiteSpace());
deleteInsignificantTextDownstream(positionAfterSplit);
if (positionAfterSplit.deprecatedNode()->isTextNode())
insertTextIntoNode(toText(positionAfterSplit.containerNode()), 0, nonBreakingSpaceString());
}
}
setEndingSelection(VisibleSelection(firstPositionInNode(blockToInsert.get()), DOWNSTREAM, endingSelection().isDirectional()));
applyStyleAfterInsertion(startBlock.get());
}
}