| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Apple Computer, Inc. All rights reserved. | 2 * Copyright (C) 2012 Apple Computer, Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 namespace blink { | 35 namespace blink { |
| 36 | 36 |
| 37 SimplifyMarkupCommand::SimplifyMarkupCommand(Document& document, Node* firstNode
, Node* nodeAfterLast) | 37 SimplifyMarkupCommand::SimplifyMarkupCommand(Document& document, Node* firstNode
, Node* nodeAfterLast) |
| 38 : CompositeEditCommand(document), m_firstNode(firstNode), m_nodeAfterLast(no
deAfterLast) | 38 : CompositeEditCommand(document), m_firstNode(firstNode), m_nodeAfterLast(no
deAfterLast) |
| 39 { | 39 { |
| 40 } | 40 } |
| 41 | 41 |
| 42 void SimplifyMarkupCommand::doApply() | 42 void SimplifyMarkupCommand::doApply() |
| 43 { | 43 { |
| 44 Node* rootNode = m_firstNode->parentNode(); | 44 ContainerNode* rootNode = m_firstNode->parentNode(); |
| 45 WillBeHeapVector<RefPtrWillBeMember<Node> > nodesToRemove; | 45 WillBeHeapVector<RefPtrWillBeMember<ContainerNode> > nodesToRemove; |
| 46 | 46 |
| 47 // Walk through the inserted nodes, to see if there are elements that could
be removed | 47 // Walk through the inserted nodes, to see if there are elements that could
be removed |
| 48 // without affecting the style. The goal is to produce leaner markup even wh
en starting | 48 // without affecting the style. The goal is to produce leaner markup even wh
en starting |
| 49 // from a verbose fragment. | 49 // from a verbose fragment. |
| 50 // We look at inline elements as well as non top level divs that don't have
attributes. | 50 // We look at inline elements as well as non top level divs that don't have
attributes. |
| 51 for (Node* node = m_firstNode.get(); node && node != m_nodeAfterLast; node =
NodeTraversal::next(*node)) { | 51 for (Node* node = m_firstNode.get(); node && node != m_nodeAfterLast; node =
NodeTraversal::next(*node)) { |
| 52 if (node->hasChildren() || (node->isTextNode() && node->nextSibling())) | 52 if (node->hasChildren() || (node->isTextNode() && node->nextSibling())) |
| 53 continue; | 53 continue; |
| 54 | 54 |
| 55 Node* startingNode = node->parentNode(); | 55 ContainerNode* startingNode = node->parentNode(); |
| 56 if (!startingNode) | 56 if (!startingNode) |
| 57 continue; | 57 continue; |
| 58 RenderStyle* startingStyle = startingNode->renderStyle(); | 58 RenderStyle* startingStyle = startingNode->renderStyle(); |
| 59 if (!startingStyle) | 59 if (!startingStyle) |
| 60 continue; | 60 continue; |
| 61 Node* currentNode = startingNode; | 61 ContainerNode* currentNode = startingNode; |
| 62 Node* topNodeWithStartingStyle = 0; | 62 ContainerNode* topNodeWithStartingStyle = 0; |
| 63 while (currentNode != rootNode) { | 63 while (currentNode != rootNode) { |
| 64 if (currentNode->parentNode() != rootNode && isRemovableBlock(curren
tNode)) | 64 if (currentNode->parentNode() != rootNode && isRemovableBlock(curren
tNode)) |
| 65 nodesToRemove.append(currentNode); | 65 nodesToRemove.append(currentNode); |
| 66 | 66 |
| 67 currentNode = currentNode->parentNode(); | 67 currentNode = currentNode->parentNode(); |
| 68 if (!currentNode) | 68 if (!currentNode) |
| 69 break; | 69 break; |
| 70 | 70 |
| 71 if (!currentNode->renderer() || !currentNode->renderer()->isRenderIn
line() || toRenderInline(currentNode->renderer())->alwaysCreateLineBoxes()) | 71 if (!currentNode->renderer() || !currentNode->renderer()->isRenderIn
line() || toRenderInline(currentNode->renderer())->alwaysCreateLineBoxes()) |
| 72 continue; | 72 continue; |
| 73 | 73 |
| 74 if (currentNode->firstChild() != currentNode->lastChild()) { | 74 if (currentNode->firstChild() != currentNode->lastChild()) { |
| 75 topNodeWithStartingStyle = 0; | 75 topNodeWithStartingStyle = 0; |
| 76 break; | 76 break; |
| 77 } | 77 } |
| 78 | 78 |
| 79 if (!currentNode->renderStyle()->visualInvalidationDiff(*startingSty
le).hasDifference()) | 79 if (!currentNode->renderStyle()->visualInvalidationDiff(*startingSty
le).hasDifference()) |
| 80 topNodeWithStartingStyle = currentNode; | 80 topNodeWithStartingStyle = currentNode; |
| 81 | 81 |
| 82 } | 82 } |
| 83 if (topNodeWithStartingStyle) { | 83 if (topNodeWithStartingStyle) { |
| 84 for (Node* node = startingNode; node != topNodeWithStartingStyle; no
de = node->parentNode()) | 84 for (ContainerNode* node = startingNode; node != topNodeWithStarting
Style; node = node->parentNode()) |
| 85 nodesToRemove.append(node); | 85 nodesToRemove.append(node); |
| 86 } | 86 } |
| 87 } | 87 } |
| 88 | 88 |
| 89 // we perform all the DOM mutations at once. | 89 // we perform all the DOM mutations at once. |
| 90 for (size_t i = 0; i < nodesToRemove.size(); ++i) { | 90 for (size_t i = 0; i < nodesToRemove.size(); ++i) { |
| 91 // FIXME: We can do better by directly moving children from nodesToRemov
e[i]. | 91 // FIXME: We can do better by directly moving children from nodesToRemov
e[i]. |
| 92 int numPrunedAncestors = pruneSubsequentAncestorsToRemove(nodesToRemove,
i); | 92 int numPrunedAncestors = pruneSubsequentAncestorsToRemove(nodesToRemove,
i); |
| 93 if (numPrunedAncestors < 0) | 93 if (numPrunedAncestors < 0) |
| 94 continue; | 94 continue; |
| 95 removeNodePreservingChildren(nodesToRemove[i], AssumeContentIsAlwaysEdit
able); | 95 removeNodePreservingChildren(nodesToRemove[i], AssumeContentIsAlwaysEdit
able); |
| 96 i += numPrunedAncestors; | 96 i += numPrunedAncestors; |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 | 99 |
| 100 int SimplifyMarkupCommand::pruneSubsequentAncestorsToRemove(WillBeHeapVector<Ref
PtrWillBeMember<Node> >& nodesToRemove, size_t startNodeIndex) | 100 int SimplifyMarkupCommand::pruneSubsequentAncestorsToRemove(WillBeHeapVector<Ref
PtrWillBeMember<ContainerNode> >& nodesToRemove, size_t startNodeIndex) |
| 101 { | 101 { |
| 102 size_t pastLastNodeToRemove = startNodeIndex + 1; | 102 size_t pastLastNodeToRemove = startNodeIndex + 1; |
| 103 for (; pastLastNodeToRemove < nodesToRemove.size(); ++pastLastNodeToRemove)
{ | 103 for (; pastLastNodeToRemove < nodesToRemove.size(); ++pastLastNodeToRemove)
{ |
| 104 if (nodesToRemove[pastLastNodeToRemove - 1]->parentNode() != nodesToRemo
ve[pastLastNodeToRemove]) | 104 if (nodesToRemove[pastLastNodeToRemove - 1]->parentNode() != nodesToRemo
ve[pastLastNodeToRemove]) |
| 105 break; | 105 break; |
| 106 ASSERT(nodesToRemove[pastLastNodeToRemove]->firstChild() == nodesToRemov
e[pastLastNodeToRemove]->lastChild()); | 106 ASSERT(nodesToRemove[pastLastNodeToRemove]->firstChild() == nodesToRemov
e[pastLastNodeToRemove]->lastChild()); |
| 107 } | 107 } |
| 108 | 108 |
| 109 Node* highestAncestorToRemove = nodesToRemove[pastLastNodeToRemove - 1].get(
); | 109 ContainerNode* highestAncestorToRemove = nodesToRemove[pastLastNodeToRemove
- 1].get(); |
| 110 RefPtrWillBeRawPtr<ContainerNode> parent = highestAncestorToRemove->parentNo
de(); | 110 RefPtrWillBeRawPtr<ContainerNode> parent = highestAncestorToRemove->parentNo
de(); |
| 111 if (!parent) // Parent has already been removed. | 111 if (!parent) // Parent has already been removed. |
| 112 return -1; | 112 return -1; |
| 113 | 113 |
| 114 if (pastLastNodeToRemove == startNodeIndex + 1) | 114 if (pastLastNodeToRemove == startNodeIndex + 1) |
| 115 return 0; | 115 return 0; |
| 116 | 116 |
| 117 removeNode(nodesToRemove[startNodeIndex], AssumeContentIsAlwaysEditable); | 117 removeNode(nodesToRemove[startNodeIndex], AssumeContentIsAlwaysEditable); |
| 118 insertNodeBefore(nodesToRemove[startNodeIndex], highestAncestorToRemove, Ass
umeContentIsAlwaysEditable); | 118 insertNodeBefore(nodesToRemove[startNodeIndex], highestAncestorToRemove, Ass
umeContentIsAlwaysEditable); |
| 119 removeNode(highestAncestorToRemove, AssumeContentIsAlwaysEditable); | 119 removeNode(highestAncestorToRemove, AssumeContentIsAlwaysEditable); |
| 120 | 120 |
| 121 return pastLastNodeToRemove - startNodeIndex - 1; | 121 return pastLastNodeToRemove - startNodeIndex - 1; |
| 122 } | 122 } |
| 123 | 123 |
| 124 void SimplifyMarkupCommand::trace(Visitor* visitor) | 124 void SimplifyMarkupCommand::trace(Visitor* visitor) |
| 125 { | 125 { |
| 126 visitor->trace(m_firstNode); | 126 visitor->trace(m_firstNode); |
| 127 visitor->trace(m_nodeAfterLast); | 127 visitor->trace(m_nodeAfterLast); |
| 128 CompositeEditCommand::trace(visitor); | 128 CompositeEditCommand::trace(visitor); |
| 129 } | 129 } |
| 130 | 130 |
| 131 } // namespace blink | 131 } // namespace blink |
| OLD | NEW |