FormatBlockCommand.cpp [plain text]
#include "config.h"
#include "Element.h"
#include "FormatBlockCommand.h"
#include "Document.h"
#include "htmlediting.h"
#include "HTMLElement.h"
#include "HTMLNames.h"
#include "visible_units.h"
namespace WebCore {
using namespace HTMLNames;
FormatBlockCommand::FormatBlockCommand(Document* document, const AtomicString& tagName)
: CompositeEditCommand(document), m_tagName(tagName)
{
}
bool FormatBlockCommand::modifyRange()
{
ASSERT(endingSelection().isRange());
VisiblePosition visibleStart = endingSelection().visibleStart();
VisiblePosition visibleEnd = endingSelection().visibleEnd();
VisiblePosition startOfLastParagraph = startOfParagraph(visibleEnd);
if (startOfParagraph(visibleStart) == startOfLastParagraph)
return false;
setEndingSelection(visibleStart);
doApply();
visibleStart = endingSelection().visibleStart();
VisiblePosition nextParagraph = endOfParagraph(visibleStart).next();
while (nextParagraph.isNotNull() && nextParagraph != startOfLastParagraph) {
setEndingSelection(nextParagraph);
doApply();
nextParagraph = endOfParagraph(endingSelection().visibleStart()).next();
}
setEndingSelection(visibleEnd);
doApply();
visibleEnd = endingSelection().visibleEnd();
setEndingSelection(VisibleSelection(visibleStart.deepEquivalent(), visibleEnd.deepEquivalent(), DOWNSTREAM));
return true;
}
void FormatBlockCommand::doApply()
{
if (endingSelection().isNone())
return;
if (!endingSelection().rootEditableElement())
return;
VisiblePosition visibleEnd = endingSelection().visibleEnd();
VisiblePosition visibleStart = endingSelection().visibleStart();
if (visibleEnd != visibleStart && isStartOfParagraph(visibleEnd))
setEndingSelection(VisibleSelection(visibleStart, visibleEnd.previous(true)));
if (endingSelection().isRange() && modifyRange())
return;
ExceptionCode ec;
String localName, prefix;
if (!Document::parseQualifiedName(m_tagName, prefix, localName, ec))
return;
QualifiedName qTypeOfBlock(prefix, localName, xhtmlNamespaceURI);
Node* refNode = enclosingBlockFlowElement(endingSelection().visibleStart());
if (refNode->hasTagName(qTypeOfBlock))
return;
VisiblePosition paragraphStart = startOfParagraph(endingSelection().visibleStart());
VisiblePosition paragraphEnd = endOfParagraph(endingSelection().visibleStart());
VisiblePosition blockStart = startOfBlock(endingSelection().visibleStart());
VisiblePosition blockEnd = endOfBlock(endingSelection().visibleStart());
RefPtr<Element> blockNode = createHTMLElement(document(), m_tagName);
RefPtr<Element> placeholder = createBreakElement(document());
Node* root = endingSelection().start().node()->rootEditableElement();
if (validBlockTag(refNode->nodeName().lower()) &&
paragraphStart == blockStart && paragraphEnd == blockEnd &&
refNode != root && !root->isDescendantOf(refNode))
insertNodeBefore(blockNode, refNode);
else {
insertNodeAt(blockNode, paragraphStart.deepEquivalent().upstream());
}
appendNode(placeholder, blockNode);
VisiblePosition destination(Position(placeholder.get(), 0));
if (paragraphStart == paragraphEnd && !lineBreakExistsAtVisiblePosition(paragraphStart)) {
setEndingSelection(destination);
return;
}
moveParagraph(paragraphStart, paragraphEnd, destination, true, false);
}
}