| 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 namespace blink { | 34 namespace blink { |
| 35 | 35 |
| 36 SimplifyMarkupCommand::SimplifyMarkupCommand(Document& document, Node* firstNode
, Node* nodeAfterLast) | 36 SimplifyMarkupCommand::SimplifyMarkupCommand(Document& document, Node* firstNode
, Node* nodeAfterLast) |
| 37 : CompositeEditCommand(document), m_firstNode(firstNode), m_nodeAfterLast(no
deAfterLast) | 37 : CompositeEditCommand(document), m_firstNode(firstNode), m_nodeAfterLast(no
deAfterLast) |
| 38 { | 38 { |
| 39 } | 39 } |
| 40 | 40 |
| 41 void SimplifyMarkupCommand::doApply() | 41 void SimplifyMarkupCommand::doApply() |
| 42 { | 42 { |
| 43 ContainerNode* rootNode = m_firstNode->parentNode(); | 43 ContainerNode* rootNode = m_firstNode->parentNode(); |
| 44 WillBeHeapVector<RefPtrWillBeMember<ContainerNode>> nodesToRemove; | 44 HeapVector<Member<ContainerNode>> nodesToRemove; |
| 45 | 45 |
| 46 // Walk through the inserted nodes, to see if there are elements that could
be removed | 46 // Walk through the inserted nodes, to see if there are elements that could
be removed |
| 47 // without affecting the style. The goal is to produce leaner markup even wh
en starting | 47 // without affecting the style. The goal is to produce leaner markup even wh
en starting |
| 48 // from a verbose fragment. | 48 // from a verbose fragment. |
| 49 // We look at inline elements as well as non top level divs that don't have
attributes. | 49 // We look at inline elements as well as non top level divs that don't have
attributes. |
| 50 for (Node* node = m_firstNode.get(); node && node != m_nodeAfterLast; node =
NodeTraversal::next(*node)) { | 50 for (Node* node = m_firstNode.get(); node && node != m_nodeAfterLast; node =
NodeTraversal::next(*node)) { |
| 51 if (node->hasChildren() || (node->isTextNode() && node->nextSibling())) | 51 if (node->hasChildren() || (node->isTextNode() && node->nextSibling())) |
| 52 continue; | 52 continue; |
| 53 | 53 |
| 54 ContainerNode* startingNode = node->parentNode(); | 54 ContainerNode* startingNode = node->parentNode(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 for (size_t i = 0; i < nodesToRemove.size(); ++i) { | 89 for (size_t i = 0; i < nodesToRemove.size(); ++i) { |
| 90 // FIXME: We can do better by directly moving children from nodesToRemov
e[i]. | 90 // FIXME: We can do better by directly moving children from nodesToRemov
e[i]. |
| 91 int numPrunedAncestors = pruneSubsequentAncestorsToRemove(nodesToRemove,
i); | 91 int numPrunedAncestors = pruneSubsequentAncestorsToRemove(nodesToRemove,
i); |
| 92 if (numPrunedAncestors < 0) | 92 if (numPrunedAncestors < 0) |
| 93 continue; | 93 continue; |
| 94 removeNodePreservingChildren(nodesToRemove[i], AssumeContentIsAlwaysEdit
able); | 94 removeNodePreservingChildren(nodesToRemove[i], AssumeContentIsAlwaysEdit
able); |
| 95 i += numPrunedAncestors; | 95 i += numPrunedAncestors; |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 | 98 |
| 99 int SimplifyMarkupCommand::pruneSubsequentAncestorsToRemove(WillBeHeapVector<Ref
PtrWillBeMember<ContainerNode>>& nodesToRemove, size_t startNodeIndex) | 99 int SimplifyMarkupCommand::pruneSubsequentAncestorsToRemove(HeapVector<Member<Co
ntainerNode>>& nodesToRemove, size_t startNodeIndex) |
| 100 { | 100 { |
| 101 size_t pastLastNodeToRemove = startNodeIndex + 1; | 101 size_t pastLastNodeToRemove = startNodeIndex + 1; |
| 102 for (; pastLastNodeToRemove < nodesToRemove.size(); ++pastLastNodeToRemove)
{ | 102 for (; pastLastNodeToRemove < nodesToRemove.size(); ++pastLastNodeToRemove)
{ |
| 103 if (nodesToRemove[pastLastNodeToRemove - 1]->parentNode() != nodesToRemo
ve[pastLastNodeToRemove]) | 103 if (nodesToRemove[pastLastNodeToRemove - 1]->parentNode() != nodesToRemo
ve[pastLastNodeToRemove]) |
| 104 break; | 104 break; |
| 105 ASSERT(nodesToRemove[pastLastNodeToRemove]->firstChild() == nodesToRemov
e[pastLastNodeToRemove]->lastChild()); | 105 ASSERT(nodesToRemove[pastLastNodeToRemove]->firstChild() == nodesToRemov
e[pastLastNodeToRemove]->lastChild()); |
| 106 } | 106 } |
| 107 | 107 |
| 108 ContainerNode* highestAncestorToRemove = nodesToRemove[pastLastNodeToRemove
- 1].get(); | 108 ContainerNode* highestAncestorToRemove = nodesToRemove[pastLastNodeToRemove
- 1].get(); |
| 109 RefPtrWillBeRawPtr<ContainerNode> parent = highestAncestorToRemove->parentNo
de(); | 109 RawPtr<ContainerNode> parent = highestAncestorToRemove->parentNode(); |
| 110 if (!parent) // Parent has already been removed. | 110 if (!parent) // Parent has already been removed. |
| 111 return -1; | 111 return -1; |
| 112 | 112 |
| 113 if (pastLastNodeToRemove == startNodeIndex + 1) | 113 if (pastLastNodeToRemove == startNodeIndex + 1) |
| 114 return 0; | 114 return 0; |
| 115 | 115 |
| 116 removeNode(nodesToRemove[startNodeIndex], AssumeContentIsAlwaysEditable); | 116 removeNode(nodesToRemove[startNodeIndex], AssumeContentIsAlwaysEditable); |
| 117 insertNodeBefore(nodesToRemove[startNodeIndex], highestAncestorToRemove, Ass
umeContentIsAlwaysEditable); | 117 insertNodeBefore(nodesToRemove[startNodeIndex], highestAncestorToRemove, Ass
umeContentIsAlwaysEditable); |
| 118 removeNode(highestAncestorToRemove, AssumeContentIsAlwaysEditable); | 118 removeNode(highestAncestorToRemove, AssumeContentIsAlwaysEditable); |
| 119 | 119 |
| 120 return pastLastNodeToRemove - startNodeIndex - 1; | 120 return pastLastNodeToRemove - startNodeIndex - 1; |
| 121 } | 121 } |
| 122 | 122 |
| 123 DEFINE_TRACE(SimplifyMarkupCommand) | 123 DEFINE_TRACE(SimplifyMarkupCommand) |
| 124 { | 124 { |
| 125 visitor->trace(m_firstNode); | 125 visitor->trace(m_firstNode); |
| 126 visitor->trace(m_nodeAfterLast); | 126 visitor->trace(m_nodeAfterLast); |
| 127 CompositeEditCommand::trace(visitor); | 127 CompositeEditCommand::trace(visitor); |
| 128 } | 128 } |
| 129 | 129 |
| 130 } // namespace blink | 130 } // namespace blink |
| OLD | NEW |