SimplifyMarkupCommand.cpp [plain text]
#include "config.h"
#include "SimplifyMarkupCommand.h"
#include "NodeRenderStyle.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<Node*> nodesToRemove;
for (Node* node = m_firstNode.get(); node && node != m_nodeAfterLast; node = node->traverseNextNode()) {
if (node->firstChild() || (node->isTextNode() && node->nextSibling()))
continue;
Node* startingNode = node->parentNode();
RenderStyle* startingStyle = startingNode->renderStyle();
if (!startingStyle)
continue;
Node* currentNode = startingNode;
Node* topNodeWithStartingStyle = 0;
while (currentNode != rootNode) {
if (currentNode->parentNode() != rootNode && isRemovableBlock(currentNode))
nodesToRemove.append(currentNode);
currentNode = currentNode->parentNode();
if (!currentNode)
break;
if (!currentNode->renderer() || !currentNode->renderer()->isRenderInline() || toRenderInline(currentNode->renderer())->alwaysCreateLineBoxes())
continue;
if (currentNode->firstChild() != currentNode->lastChild()) {
topNodeWithStartingStyle = 0;
break;
}
unsigned context;
if (currentNode->renderStyle()->diff(startingStyle, context) == StyleDifferenceEqual)
topNodeWithStartingStyle = currentNode;
}
if (topNodeWithStartingStyle) {
for (Node* node = startingNode; node != topNodeWithStartingStyle; node = node->parentNode())
nodesToRemove.append(node);
}
}
for (size_t i = 0; i < nodesToRemove.size(); ++i)
removeNodePreservingChildren(nodesToRemove[i]);
}
}