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 24 matching lines...) Expand all Loading... |
35 namespace WebCore { | 35 namespace WebCore { |
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 Node* rootNode = m_firstNode->parentNode(); |
45 Vector<RefPtr<Node> > nodesToRemove; | 45 WillBeHeapVector<RefPtrWillBeMember<Node> > 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->firstChild() || (node->isTextNode() && node->nextSibling())) | 52 if (node->firstChild() || (node->isTextNode() && node->nextSibling())) |
53 continue; | 53 continue; |
54 | 54 |
55 Node* startingNode = node->parentNode(); | 55 Node* startingNode = node->parentNode(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 for (size_t i = 0; i < nodesToRemove.size(); ++i) { | 91 for (size_t i = 0; i < nodesToRemove.size(); ++i) { |
92 // FIXME: We can do better by directly moving children from nodesToRemov
e[i]. | 92 // FIXME: We can do better by directly moving children from nodesToRemov
e[i]. |
93 int numPrunedAncestors = pruneSubsequentAncestorsToRemove(nodesToRemove,
i); | 93 int numPrunedAncestors = pruneSubsequentAncestorsToRemove(nodesToRemove,
i); |
94 if (numPrunedAncestors < 0) | 94 if (numPrunedAncestors < 0) |
95 continue; | 95 continue; |
96 removeNodePreservingChildren(nodesToRemove[i], AssumeContentIsAlwaysEdit
able); | 96 removeNodePreservingChildren(nodesToRemove[i], AssumeContentIsAlwaysEdit
able); |
97 i += numPrunedAncestors; | 97 i += numPrunedAncestors; |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 int SimplifyMarkupCommand::pruneSubsequentAncestorsToRemove(Vector<RefPtr<Node>
>& nodesToRemove, size_t startNodeIndex) | 101 int SimplifyMarkupCommand::pruneSubsequentAncestorsToRemove(WillBeHeapVector<Ref
PtrWillBeMember<Node> >& nodesToRemove, size_t startNodeIndex) |
102 { | 102 { |
103 size_t pastLastNodeToRemove = startNodeIndex + 1; | 103 size_t pastLastNodeToRemove = startNodeIndex + 1; |
104 for (; pastLastNodeToRemove < nodesToRemove.size(); ++pastLastNodeToRemove)
{ | 104 for (; pastLastNodeToRemove < nodesToRemove.size(); ++pastLastNodeToRemove)
{ |
105 if (nodesToRemove[pastLastNodeToRemove - 1]->parentNode() != nodesToRemo
ve[pastLastNodeToRemove]) | 105 if (nodesToRemove[pastLastNodeToRemove - 1]->parentNode() != nodesToRemo
ve[pastLastNodeToRemove]) |
106 break; | 106 break; |
107 ASSERT(nodesToRemove[pastLastNodeToRemove]->firstChild() == nodesToRemov
e[pastLastNodeToRemove]->lastChild()); | 107 ASSERT(nodesToRemove[pastLastNodeToRemove]->firstChild() == nodesToRemov
e[pastLastNodeToRemove]->lastChild()); |
108 } | 108 } |
109 | 109 |
110 Node* highestAncestorToRemove = nodesToRemove[pastLastNodeToRemove - 1].get(
); | 110 Node* highestAncestorToRemove = nodesToRemove[pastLastNodeToRemove - 1].get(
); |
111 RefPtr<ContainerNode> parent = highestAncestorToRemove->parentNode(); | 111 RefPtrWillBeRawPtr<ContainerNode> parent = highestAncestorToRemove->parentNo
de(); |
112 if (!parent) // Parent has already been removed. | 112 if (!parent) // Parent has already been removed. |
113 return -1; | 113 return -1; |
114 | 114 |
115 if (pastLastNodeToRemove == startNodeIndex + 1) | 115 if (pastLastNodeToRemove == startNodeIndex + 1) |
116 return 0; | 116 return 0; |
117 | 117 |
118 removeNode(nodesToRemove[startNodeIndex], AssumeContentIsAlwaysEditable); | 118 removeNode(nodesToRemove[startNodeIndex], AssumeContentIsAlwaysEditable); |
119 insertNodeBefore(nodesToRemove[startNodeIndex], highestAncestorToRemove, Ass
umeContentIsAlwaysEditable); | 119 insertNodeBefore(nodesToRemove[startNodeIndex], highestAncestorToRemove, Ass
umeContentIsAlwaysEditable); |
120 removeNode(highestAncestorToRemove, AssumeContentIsAlwaysEditable); | 120 removeNode(highestAncestorToRemove, AssumeContentIsAlwaysEditable); |
121 | 121 |
122 return pastLastNodeToRemove - startNodeIndex - 1; | 122 return pastLastNodeToRemove - startNodeIndex - 1; |
123 } | 123 } |
124 | 124 |
| 125 void SimplifyMarkupCommand::trace(Visitor* visitor) |
| 126 { |
| 127 visitor->trace(m_firstNode); |
| 128 visitor->trace(m_nodeAfterLast); |
| 129 CompositeEditCommand::trace(visitor); |
| 130 } |
| 131 |
125 } // namespace WebCore | 132 } // namespace WebCore |
OLD | NEW |