ModifySelectionListLevel.cpp [plain text]
#include "config.h"
#include "ModifySelectionListLevel.h"
#include "Document.h"
#include "Frame.h"
#include "FrameSelection.h"
#include "HTMLElement.h"
#include "RenderObject.h"
#include "htmlediting.h"
namespace WebCore {
ModifySelectionListLevelCommand::ModifySelectionListLevelCommand(Document* document)
: CompositeEditCommand(document)
{
}
bool ModifySelectionListLevelCommand::preservesTypingStyle() const
{
return true;
}
static bool getStartEndListChildren(const VisibleSelection& selection, Node*& start, Node*& end)
{
if (selection.isNone())
return false;
Node* startListChild = enclosingListChild(selection.start().anchorNode());
if (!startListChild)
return false;
Node* endListChild = selection.isRange() ? enclosingListChild(selection.end().anchorNode()) : startListChild;
if (!endListChild)
return false;
while (startListChild->parentNode() != endListChild->parentNode()) {
endListChild = endListChild->parentNode();
if (!endListChild)
return false;
}
if (endListChild->renderer()->isListItem()) {
RenderObject* r = endListChild->renderer()->nextSibling();
if (r && isListElement(r->node()))
endListChild = r->node();
}
start = startListChild;
end = endListChild;
return true;
}
void ModifySelectionListLevelCommand::insertSiblingNodeRangeBefore(Node* startNode, Node* endNode, Node* refNode)
{
Node* node = startNode;
while (1) {
Node* next = node->nextSibling();
removeNode(node);
insertNodeBefore(node, refNode);
if (node == endNode)
break;
node = next;
}
}
void ModifySelectionListLevelCommand::insertSiblingNodeRangeAfter(Node* startNode, Node* endNode, Node* refNode)
{
Node* node = startNode;
while (1) {
Node* next = node->nextSibling();
removeNode(node);
insertNodeAfter(node, refNode);
if (node == endNode)
break;
refNode = node;
node = next;
}
}
void ModifySelectionListLevelCommand::appendSiblingNodeRange(Node* startNode, Node* endNode, Element* newParent)
{
Node* node = startNode;
while (1) {
Node* next = node->nextSibling();
removeNode(node);
appendNode(node, newParent);
if (node == endNode)
break;
node = next;
}
}
IncreaseSelectionListLevelCommand::IncreaseSelectionListLevelCommand(Document* document, Type listType)
: ModifySelectionListLevelCommand(document)
, m_listType(listType)
{
}
static bool canIncreaseListLevel(const VisibleSelection& selection, Node*& start, Node*& end)
{
if (!getStartEndListChildren(selection, start, end))
return false;
if (!start->renderer()->previousSibling())
return false;
return true;
}
void IncreaseSelectionListLevelCommand::doApply()
{
Node* startListChild;
Node* endListChild;
if (!canIncreaseListLevel(endingSelection(), startListChild, endListChild))
return;
Node* previousItem = startListChild->renderer()->previousSibling()->node();
if (isListElement(previousItem)) {
appendSiblingNodeRange(startListChild, endListChild, static_cast<Element*>(previousItem));
m_listElement = previousItem;
} else {
RefPtr<Element> newParent;
switch (m_listType) {
case InheritedListType:
newParent = startListChild->parentElement();
if (newParent)
newParent = newParent->cloneElementWithoutChildren();
break;
case OrderedList:
newParent = createOrderedListElement(document());
break;
case UnorderedList:
newParent = createUnorderedListElement(document());
break;
}
insertNodeBefore(newParent, startListChild);
appendSiblingNodeRange(startListChild, endListChild, newParent.get());
m_listElement = newParent.release();
}
}
bool IncreaseSelectionListLevelCommand::canIncreaseSelectionListLevel(Document* document)
{
Node* startListChild;
Node* endListChild;
return canIncreaseListLevel(document->frame()->selection()->selection(), startListChild, endListChild);
}
PassRefPtr<Node> IncreaseSelectionListLevelCommand::increaseSelectionListLevel(Document* document, Type type)
{
ASSERT(document);
ASSERT(document->frame());
RefPtr<IncreaseSelectionListLevelCommand> command = create(document, type);
command->apply();
return command->m_listElement.release();
}
PassRefPtr<Node> IncreaseSelectionListLevelCommand::increaseSelectionListLevel(Document* document)
{
return increaseSelectionListLevel(document, InheritedListType);
}
PassRefPtr<Node> IncreaseSelectionListLevelCommand::increaseSelectionListLevelOrdered(Document* document)
{
return increaseSelectionListLevel(document, OrderedList);
}
PassRefPtr<Node> IncreaseSelectionListLevelCommand::increaseSelectionListLevelUnordered(Document* document)
{
return increaseSelectionListLevel(document, UnorderedList);
}
DecreaseSelectionListLevelCommand::DecreaseSelectionListLevelCommand(Document* document)
: ModifySelectionListLevelCommand(document)
{
}
static bool canDecreaseListLevel(const VisibleSelection& selection, Node*& start, Node*& end)
{
if (!getStartEndListChildren(selection, start, end))
return false;
if (!isListElement(start->parentNode()->parentNode()))
return false;
return true;
}
void DecreaseSelectionListLevelCommand::doApply()
{
Node* startListChild;
Node* endListChild;
if (!canDecreaseListLevel(endingSelection(), startListChild, endListChild))
return;
Node* previousItem = startListChild->renderer()->previousSibling() ? startListChild->renderer()->previousSibling()->node() : 0;
Node* nextItem = endListChild->renderer()->nextSibling() ? endListChild->renderer()->nextSibling()->node() : 0;
Element* listNode = startListChild->parentElement();
if (!previousItem) {
insertSiblingNodeRangeBefore(startListChild, endListChild, listNode);
if (!nextItem)
removeNode(listNode);
} else if (!nextItem) {
insertSiblingNodeRangeAfter(startListChild, endListChild, listNode);
} else if (listNode) {
splitElement(listNode, startListChild);
insertSiblingNodeRangeBefore(startListChild, endListChild, listNode);
}
}
bool DecreaseSelectionListLevelCommand::canDecreaseSelectionListLevel(Document* document)
{
Node* startListChild;
Node* endListChild;
return canDecreaseListLevel(document->frame()->selection()->selection(), startListChild, endListChild);
}
void DecreaseSelectionListLevelCommand::decreaseSelectionListLevel(Document* document)
{
ASSERT(document);
ASSERT(document->frame());
applyCommand(create(document));
}
}