This source file includes following definitions.
- enclosingListChild
- fixOrphanedListChild
- mergeWithNeighboringLists
- selectionHasListOfType
- m_type
- doApply
- doApplyForSingleParagraph
- unlistifyParagraph
- adjacentEnclosingList
- listifyParagraph
#include "config.h"
#include "core/editing/InsertListCommand.h"
#include "HTMLNames.h"
#include "bindings/v8/ExceptionStatePlaceholder.h"
#include "core/dom/Element.h"
#include "core/dom/ElementTraversal.h"
#include "core/editing/TextIterator.h"
#include "core/editing/VisibleUnits.h"
#include "core/editing/htmlediting.h"
#include "core/html/HTMLElement.h"
namespace WebCore {
using namespace HTMLNames;
static Node* enclosingListChild(Node* node, Node* listNode)
{
Node* listChild = enclosingListChild(node);
while (listChild && enclosingList(listChild) != listNode)
listChild = enclosingListChild(listChild->parentNode());
return listChild;
}
HTMLElement* InsertListCommand::fixOrphanedListChild(Node* node)
{
RefPtr<HTMLElement> listElement = createUnorderedListElement(document());
insertNodeBefore(listElement, node);
removeNode(node);
appendNode(node, listElement);
m_listElement = listElement;
return listElement.get();
}
PassRefPtr<HTMLElement> InsertListCommand::mergeWithNeighboringLists(PassRefPtr<HTMLElement> passedList)
{
RefPtr<HTMLElement> list = passedList;
Element* previousList = ElementTraversal::previousSibling(*list);
if (canMergeLists(previousList, list.get()))
mergeIdenticalElements(previousList, list);
if (!list)
return nullptr;
Element* nextSibling = ElementTraversal::nextSibling(*list);
if (!nextSibling || !nextSibling->isHTMLElement())
return list.release();
RefPtr<HTMLElement> nextList = toHTMLElement(nextSibling);
if (canMergeLists(list.get(), nextList.get())) {
mergeIdenticalElements(list, nextList);
return nextList.release();
}
return list.release();
}
bool InsertListCommand::selectionHasListOfType(const VisibleSelection& selection, const QualifiedName& listTag)
{
VisiblePosition start = selection.visibleStart();
if (!enclosingList(start.deepEquivalent().deprecatedNode()))
return false;
VisiblePosition end = startOfParagraph(selection.visibleEnd());
while (start.isNotNull() && start != end) {
Element* listNode = enclosingList(start.deepEquivalent().deprecatedNode());
if (!listNode || !listNode->hasTagName(listTag))
return false;
start = startOfNextParagraph(start);
}
return true;
}
InsertListCommand::InsertListCommand(Document& document, Type type)
: CompositeEditCommand(document), m_type(type)
{
}
void InsertListCommand::doApply()
{
if (!endingSelection().isNonOrphanedCaretOrRange())
return;
if (!endingSelection().rootEditableElement())
return;
VisiblePosition visibleEnd = endingSelection().visibleEnd();
VisiblePosition visibleStart = endingSelection().visibleStart();
if (visibleEnd != visibleStart && isStartOfParagraph(visibleEnd, CanSkipOverEditingBoundary)) {
setEndingSelection(VisibleSelection(visibleStart, visibleEnd.previous(CannotCrossEditingBoundary), endingSelection().isDirectional()));
if (!endingSelection().rootEditableElement())
return;
}
const QualifiedName& listTag = (m_type == OrderedList) ? olTag : ulTag;
if (endingSelection().isRange()) {
VisibleSelection selection = selectionForParagraphIteration(endingSelection());
ASSERT(selection.isRange());
VisiblePosition startOfSelection = selection.visibleStart();
VisiblePosition endOfSelection = selection.visibleEnd();
VisiblePosition startOfLastParagraph = startOfParagraph(endOfSelection, CanSkipOverEditingBoundary);
if (startOfParagraph(startOfSelection, CanSkipOverEditingBoundary) != startOfLastParagraph) {
RefPtr<ContainerNode> scope;
int indexForEndOfSelection = indexForVisiblePosition(endOfSelection, scope);
bool forceCreateList = !selectionHasListOfType(selection, listTag);
RefPtrWillBeRawPtr<Range> currentSelection = endingSelection().firstRange();
VisiblePosition startOfCurrentParagraph = startOfSelection;
while (!inSameParagraph(startOfCurrentParagraph, startOfLastParagraph, CanCrossEditingBoundary)) {
if (!startOfLastParagraph.deepEquivalent().inDocument())
return;
setEndingSelection(startOfCurrentParagraph);
doApplyForSingleParagraph(forceCreateList, listTag, *currentSelection);
if (endOfSelection.isNull() || endOfSelection.isOrphan() || startOfLastParagraph.isNull() || startOfLastParagraph.isOrphan()) {
endOfSelection = visiblePositionForIndex(indexForEndOfSelection, scope.get());
ASSERT(endOfSelection.isNotNull());
if (endOfSelection.isNull())
return;
startOfLastParagraph = startOfParagraph(endOfSelection, CanSkipOverEditingBoundary);
}
if (startOfCurrentParagraph == startOfSelection)
startOfSelection = endingSelection().visibleStart();
startOfCurrentParagraph = startOfNextParagraph(endingSelection().visibleStart());
}
setEndingSelection(endOfSelection);
doApplyForSingleParagraph(forceCreateList, listTag, *currentSelection);
if (endOfSelection.isNull() || endOfSelection.isOrphan()) {
endOfSelection = visiblePositionForIndex(indexForEndOfSelection, scope.get());
if (endOfSelection.isNull())
return;
}
setEndingSelection(VisibleSelection(startOfSelection, endOfSelection, endingSelection().isDirectional()));
return;
}
}
ASSERT(endingSelection().firstRange());
doApplyForSingleParagraph(false, listTag, *endingSelection().firstRange());
}
void InsertListCommand::doApplyForSingleParagraph(bool forceCreateList, const QualifiedName& listTag, Range& currentSelection)
{
Node* selectionNode = endingSelection().start().deprecatedNode();
Node* listChildNode = enclosingListChild(selectionNode);
bool switchListType = false;
if (listChildNode) {
RefPtr<HTMLElement> listNode = enclosingList(listChildNode);
if (!listNode) {
listNode = fixOrphanedListChild(listChildNode);
listNode = mergeWithNeighboringLists(listNode);
}
if (!listNode->hasTagName(listTag))
switchListType = true;
if (!switchListType && forceCreateList)
return;
if (switchListType && isNodeVisiblyContainedWithin(*listNode, currentSelection)) {
bool rangeStartIsInList = visiblePositionBeforeNode(*listNode) == VisiblePosition(currentSelection.startPosition());
bool rangeEndIsInList = visiblePositionAfterNode(*listNode) == VisiblePosition(currentSelection.endPosition());
RefPtr<HTMLElement> newList = createHTMLElement(document(), listTag);
insertNodeBefore(newList, listNode);
Node* firstChildInList = enclosingListChild(VisiblePosition(firstPositionInNode(listNode.get())).deepEquivalent().deprecatedNode(), listNode.get());
Node* outerBlock = firstChildInList && firstChildInList->isBlockFlowElement() ? firstChildInList : listNode.get();
moveParagraphWithClones(VisiblePosition(firstPositionInNode(listNode.get())), VisiblePosition(lastPositionInNode(listNode.get())), newList.get(), outerBlock);
if (listNode && listNode->inDocument())
removeNode(listNode);
newList = mergeWithNeighboringLists(newList);
if (rangeStartIsInList && newList)
currentSelection.setStart(newList, 0, IGNORE_EXCEPTION);
if (rangeEndIsInList && newList)
currentSelection.setEnd(newList, lastOffsetInNode(newList.get()), IGNORE_EXCEPTION);
setEndingSelection(VisiblePosition(firstPositionInNode(newList.get())));
return;
}
unlistifyParagraph(endingSelection().visibleStart(), listNode.get(), listChildNode);
}
if (!listChildNode || switchListType || forceCreateList)
m_listElement = listifyParagraph(endingSelection().visibleStart(), listTag);
}
void InsertListCommand::unlistifyParagraph(const VisiblePosition& originalStart, HTMLElement* listNode, Node* listChildNode)
{
Node* nextListChild;
Node* previousListChild;
VisiblePosition start;
VisiblePosition end;
ASSERT(listChildNode);
if (isHTMLLIElement(*listChildNode)) {
start = VisiblePosition(firstPositionInNode(listChildNode));
end = VisiblePosition(lastPositionInNode(listChildNode));
nextListChild = listChildNode->nextSibling();
previousListChild = listChildNode->previousSibling();
} else {
start = startOfParagraph(originalStart, CanSkipOverEditingBoundary);
end = endOfParagraph(start, CanSkipOverEditingBoundary);
nextListChild = enclosingListChild(end.next().deepEquivalent().deprecatedNode(), listNode);
ASSERT(nextListChild != listChildNode);
previousListChild = enclosingListChild(start.previous().deepEquivalent().deprecatedNode(), listNode);
ASSERT(previousListChild != listChildNode);
}
RefPtr<Element> placeholder = createBreakElement(document());
RefPtr<Element> nodeToInsert = placeholder;
if (enclosingList(listNode)) {
nodeToInsert = createListItemElement(document());
appendNode(placeholder, nodeToInsert);
}
if (nextListChild && previousListChild) {
splitElement(listNode, splitTreeToNode(nextListChild, listNode));
insertNodeBefore(nodeToInsert, listNode);
} else if (nextListChild || listChildNode->parentNode() != listNode) {
if (listChildNode->parentNode() != listNode)
splitElement(listNode, splitTreeToNode(listChildNode, listNode).get());
insertNodeBefore(nodeToInsert, listNode);
} else
insertNodeAfter(nodeToInsert, listNode);
VisiblePosition insertionPoint = VisiblePosition(positionBeforeNode(placeholder.get()));
moveParagraphs(start, end, insertionPoint, true, true, listChildNode);
}
static Element* adjacentEnclosingList(const VisiblePosition& pos, const VisiblePosition& adjacentPos, const QualifiedName& listTag)
{
Element* listNode = outermostEnclosingList(adjacentPos.deepEquivalent().deprecatedNode());
if (!listNode)
return 0;
Node* previousCell = enclosingTableCell(pos.deepEquivalent());
Node* currentCell = enclosingTableCell(adjacentPos.deepEquivalent());
if (!listNode->hasTagName(listTag)
|| listNode->contains(pos.deepEquivalent().deprecatedNode())
|| previousCell != currentCell
|| enclosingList(listNode) != enclosingList(pos.deepEquivalent().deprecatedNode()))
return 0;
return listNode;
}
PassRefPtr<HTMLElement> InsertListCommand::listifyParagraph(const VisiblePosition& originalStart, const QualifiedName& listTag)
{
VisiblePosition start = startOfParagraph(originalStart, CanSkipOverEditingBoundary);
VisiblePosition end = endOfParagraph(start, CanSkipOverEditingBoundary);
if (start.isNull() || end.isNull())
return nullptr;
RefPtr<HTMLElement> listItemElement = createListItemElement(document());
RefPtr<HTMLElement> placeholder = createBreakElement(document());
appendNode(placeholder, listItemElement);
Element* previousList = adjacentEnclosingList(start, start.previous(CannotCrossEditingBoundary), listTag);
Element* nextList = adjacentEnclosingList(start, end.next(CannotCrossEditingBoundary), listTag);
RefPtr<HTMLElement> listElement;
if (previousList)
appendNode(listItemElement, previousList);
else if (nextList)
insertNodeAt(listItemElement, positionBeforeNode(nextList));
else {
listElement = createHTMLElement(document(), listTag);
appendNode(listItemElement, listElement);
if (start == end && isBlock(start.deepEquivalent().deprecatedNode())) {
RefPtr<Node> placeholder = insertBlockPlaceholder(start.deepEquivalent());
start = VisiblePosition(positionBeforeNode(placeholder.get()));
end = start;
}
Position insertionPos(start.deepEquivalent().upstream());
Node* listChild = enclosingListChild(insertionPos.deprecatedNode());
if (isHTMLLIElement(listChild))
insertionPos = positionInParentBeforeNode(*listChild);
insertNodeAt(listElement, insertionPos);
if (insertionPos == start.deepEquivalent()) {
listElement->document().updateLayoutIgnorePendingStylesheets();
start = startOfParagraph(originalStart, CanSkipOverEditingBoundary);
end = endOfParagraph(start, CanSkipOverEditingBoundary);
}
}
moveParagraph(start, end, VisiblePosition(positionBeforeNode(placeholder.get())), true);
if (listElement)
return mergeWithNeighboringLists(listElement);
if (canMergeLists(previousList, nextList))
mergeIdenticalElements(previousList, nextList);
return listElement;
}
}