SimplifyMarkupCommand.cpp [plain text]
#include "config.h"
#include "SimplifyMarkupCommand.h"
#include "NodeRenderStyle.h"
#include "NodeTraversal.h"
#include "RenderInline.h"
#include "RenderObject.h"
#include "RenderStyle.h"
namespace WebCore {
SimplifyMarkupCommand::SimplifyMarkupCommand(Document& document, Node* firstNode, Node* nodeAfterLast)
: CompositeEditCommand(document)
, m_firstNode(firstNode)
, m_nodeAfterLast(nodeAfterLast)
{
}
void SimplifyMarkupCommand::doApply()
{
Node* rootNode = m_firstNode->parentNode();
Vector<Ref<Node>> nodesToRemove;
document().updateLayoutIgnorePendingStylesheets();
for (Node* node = m_firstNode.get(); node && node != m_nodeAfterLast; node = NodeTraversal::next(*node)) {
if (node->firstChild() || (node->isTextNode() && node->nextSibling()))
continue;
Node* startingNode = node->parentNode();
auto* startingStyle = startingNode->renderStyle();
if (!startingStyle)
continue;
Node* currentNode = startingNode;
Node* topNodeWithStartingStyle = nullptr;
while (currentNode != rootNode) {
if (currentNode->parentNode() != rootNode && isRemovableBlock(currentNode))
nodesToRemove.append(*currentNode);
currentNode = currentNode->parentNode();
if (!currentNode)
break;
auto* renderer = currentNode->renderer();
if (!is<RenderInline>(renderer) || downcast<RenderInline>(*renderer).alwaysCreateLineBoxes())
continue;
if (currentNode->firstChild() != currentNode->lastChild()) {
topNodeWithStartingStyle = nullptr;
break;
}
unsigned context;
if (currentNode->renderStyle()->diff(*startingStyle, context) == StyleDifferenceEqual)
topNodeWithStartingStyle = currentNode;
}
if (topNodeWithStartingStyle) {
for (Node* node = startingNode; node && node != topNodeWithStartingStyle; node = node->parentNode())
nodesToRemove.append(*node);
}
}
for (size_t i = 0; i < nodesToRemove.size(); ++i) {
int numPrunedAncestors = pruneSubsequentAncestorsToRemove(nodesToRemove, i);
if (numPrunedAncestors < 0)
continue;
removeNodePreservingChildren(nodesToRemove[i], AssumeContentIsAlwaysEditable);
i += numPrunedAncestors;
}
}
int SimplifyMarkupCommand::pruneSubsequentAncestorsToRemove(Vector<Ref<Node>>& nodesToRemove, size_t startNodeIndex)
{
size_t pastLastNodeToRemove = startNodeIndex + 1;
for (; pastLastNodeToRemove < nodesToRemove.size(); ++pastLastNodeToRemove) {
if (nodesToRemove[pastLastNodeToRemove - 1]->parentNode() != nodesToRemove[pastLastNodeToRemove].ptr())
break;
if (nodesToRemove[pastLastNodeToRemove]->firstChild() != nodesToRemove[pastLastNodeToRemove]->lastChild())
break;
}
Node* highestAncestorToRemove = nodesToRemove[pastLastNodeToRemove - 1].ptr();
RefPtr<ContainerNode> parent = highestAncestorToRemove->parentNode();
if (!parent) return -1;
if (pastLastNodeToRemove == startNodeIndex + 1)
return 0;
removeNode(nodesToRemove[startNodeIndex], AssumeContentIsAlwaysEditable);
insertNodeBefore(nodesToRemove[startNodeIndex].copyRef(), *highestAncestorToRemove, AssumeContentIsAlwaysEditable);
removeNode(*highestAncestorToRemove, AssumeContentIsAlwaysEditable);
return pastLastNodeToRemove - startNodeIndex - 1;
}
}