|
Line 0
a/Source/WebCore/dom/ComposedShadowTreeWalker.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are |
| 6 |
* met: |
| 7 |
* |
| 8 |
* * Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* * Neither the name of Google Inc. nor the names of its |
| 11 |
* contributors may be used to endorse or promote products derived from |
| 12 |
* this software without specific prior written permission. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 15 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 16 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 17 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 18 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 20 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "config.h" |
| 28 |
#include "ComposedShadowTreeWalker.h" |
| 29 |
|
| 30 |
#include "Element.h" |
| 31 |
#include "HTMLContentSelector.h" |
| 32 |
#include "InsertionPoint.h" |
| 33 |
#include "ShadowTree.h" |
| 34 |
|
| 35 |
#ifndef NDEBUG |
| 36 |
#define ASSERT_PRECONDITION() assertPrecondition() |
| 37 |
#else |
| 38 |
#define ASSERT_PRECONDITION() |
| 39 |
#endif |
| 40 |
|
| 41 |
#ifndef NDEBUG |
| 42 |
#define ASSERT_POSTCONDITION() assertPostcondition() |
| 43 |
#else |
| 44 |
#define ASSERT_POSTCONDITION() |
| 45 |
#endif |
| 46 |
|
| 47 |
namespace WebCore { |
| 48 |
|
| 49 |
static inline bool isShadowHost(const Node* node) |
| 50 |
{ |
| 51 |
return node && node->isElementNode() && toElement(node)->hasShadowRoot(); |
| 52 |
} |
| 53 |
|
| 54 |
static inline ShadowTree* shadowTreeFor(const Node* node) |
| 55 |
{ |
| 56 |
if (node && node->isElementNode()) |
| 57 |
return toElement(node)->shadowTree(); |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
static inline ShadowTree* shadowTreeOfParent(const Node* node) |
| 62 |
{ |
| 63 |
if (node && node->parentNode()) |
| 64 |
return shadowTreeFor(node->parentNode()); |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
ComposedShadowTreeWalker::ComposedShadowTreeWalker(const Node* node, Policy policy) |
| 69 |
: m_node(node) |
| 70 |
, m_policy(policy) |
| 71 |
{ |
| 72 |
#ifndef NDEBUG |
| 73 |
assertPrecondition(); |
| 74 |
#endif |
| 75 |
} |
| 76 |
|
| 77 |
ComposedShadowTreeWalker ComposedShadowTreeWalker::startedWithFirstChild(const Node* node, Policy policy) |
| 78 |
{ |
| 79 |
ComposedShadowTreeWalker walker(node, policy); |
| 80 |
walker.firstChild(); |
| 81 |
return walker; |
| 82 |
} |
| 83 |
|
| 84 |
#ifndef NDEBUG |
| 85 |
void ComposedShadowTreeWalker::assertPrecondition() const |
| 86 |
{ |
| 87 |
ASSERT(m_node); |
| 88 |
if (canCrossUpperBoundary()) |
| 89 |
ASSERT(!m_node->isShadowRoot()); |
| 90 |
else |
| 91 |
ASSERT(!m_node->isShadowRoot() || toShadowRoot(m_node)->isYoungest()); |
| 92 |
// FIXME: Add an assertion once InsertionPoint have isActive() function. |
| 93 |
// https://bugs.webkit.org/show_bug.cgi?id=82010 |
| 94 |
// ASSERT(!isInsertionPoint(m_node) || !toInsertionPoint(node)->isActive()); |
| 95 |
} |
| 96 |
|
| 97 |
void ComposedShadowTreeWalker::assertPostcondition() const |
| 98 |
{ |
| 99 |
if (m_node) |
| 100 |
assertPrecondition(); |
| 101 |
} |
| 102 |
#endif |
| 103 |
|
| 104 |
void ComposedShadowTreeWalker::firstChild() |
| 105 |
{ |
| 106 |
ASSERT_PRECONDITION(); |
| 107 |
m_node = traverseChild(m_node, TraversalDirectionForward); |
| 108 |
ASSERT_POSTCONDITION(); |
| 109 |
} |
| 110 |
|
| 111 |
Node* ComposedShadowTreeWalker::traverseFirstChild(const Node* node) const |
| 112 |
{ |
| 113 |
ASSERT(node); |
| 114 |
return traverseChild(node, TraversalDirectionForward); |
| 115 |
} |
| 116 |
|
| 117 |
void ComposedShadowTreeWalker::lastChild() |
| 118 |
{ |
| 119 |
ASSERT_PRECONDITION(); |
| 120 |
m_node = traverseLastChild(m_node); |
| 121 |
ASSERT_POSTCONDITION(); |
| 122 |
} |
| 123 |
|
| 124 |
Node* ComposedShadowTreeWalker::traverseLastChild(const Node* node) const |
| 125 |
{ |
| 126 |
ASSERT(node); |
| 127 |
return traverseChild(node, TraversalDirectionBackward); |
| 128 |
} |
| 129 |
|
| 130 |
Node* ComposedShadowTreeWalker::traverseChild(const Node* node, TraversalDirection direction) const |
| 131 |
{ |
| 132 |
ASSERT(node); |
| 133 |
if (canCrossUpperBoundary()) { |
| 134 |
ShadowTree* shadowTree = shadowTreeFor(node); |
| 135 |
return (shadowTree && shadowTree->hasShadowRoot()) ? traverseLightChildren(shadowTree->youngestShadowRoot(), direction) |
| 136 |
: traverseLightChildren(node, direction); |
| 137 |
} |
| 138 |
if (isShadowHost(node)) |
| 139 |
return 0; |
| 140 |
return traverseLightChildren(node, direction); |
| 141 |
} |
| 142 |
|
| 143 |
Node* ComposedShadowTreeWalker::traverseLightChildren(const Node* node, TraversalDirection direction) |
| 144 |
{ |
| 145 |
ASSERT(node); |
| 146 |
if (Node* child = (direction == TraversalDirectionForward ? node->firstChild() : node->lastChild())) |
| 147 |
return traverseNode(child, direction); |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
Node* ComposedShadowTreeWalker::traverseNode(const Node* node, TraversalDirection direction) |
| 152 |
{ |
| 153 |
ASSERT(node); |
| 154 |
if (isInsertionPoint(node)) { |
| 155 |
const HTMLContentSelectionList* selectionList = toInsertionPoint(node)->selections(); |
| 156 |
if (HTMLContentSelection* selection = (direction == TraversalDirectionForward ? selectionList->first() : selectionList->last())) |
| 157 |
return traverseNode(selection->node(), direction); |
| 158 |
return traverseLightChildren(node, direction); |
| 159 |
} |
| 160 |
return const_cast<Node*>(node); |
| 161 |
} |
| 162 |
|
| 163 |
void ComposedShadowTreeWalker::nextSibling() |
| 164 |
{ |
| 165 |
ASSERT_PRECONDITION(); |
| 166 |
m_node = traverseSiblingOrBackToInsertionPoint(m_node, TraversalDirectionForward); |
| 167 |
ASSERT_POSTCONDITION(); |
| 168 |
} |
| 169 |
|
| 170 |
void ComposedShadowTreeWalker::previousSibling() |
| 171 |
{ |
| 172 |
ASSERT_PRECONDITION(); |
| 173 |
m_node = traverseSiblingOrBackToInsertionPoint(m_node, TraversalDirectionBackward); |
| 174 |
ASSERT_POSTCONDITION(); |
| 175 |
} |
| 176 |
|
| 177 |
Node* ComposedShadowTreeWalker::traverseSiblingOrBackToInsertionPoint(const Node* node, TraversalDirection direction) |
| 178 |
{ |
| 179 |
ASSERT(node); |
| 180 |
ShadowTree* shadowTree = shadowTreeOfParent(node); |
| 181 |
if (!shadowTree) |
| 182 |
return traverseSiblingInCurrentTree(node, direction); |
| 183 |
HTMLContentSelection* selection = shadowTree->selectionFor(node); |
| 184 |
if (!selection) |
| 185 |
return traverseSiblingInCurrentTree(node, direction); |
| 186 |
if (HTMLContentSelection* nextSelection = (direction == TraversalDirectionForward ? selection->next() : selection->previous())) |
| 187 |
return traverseNode(nextSelection->node(), direction); |
| 188 |
return traverseSiblingOrBackToInsertionPoint(selection->insertionPoint(), direction); |
| 189 |
} |
| 190 |
|
| 191 |
Node* ComposedShadowTreeWalker::traverseSiblingInCurrentTree(const Node* node, TraversalDirection direction) |
| 192 |
{ |
| 193 |
ASSERT(node); |
| 194 |
if (Node* next = (direction == TraversalDirectionForward ? node->nextSibling() : node->previousSibling())) |
| 195 |
return traverseNode(next, direction); |
| 196 |
if (Node* next = traverseSiblingOrBackToYoungerShadowRoot(node, direction)) |
| 197 |
return next; |
| 198 |
return escapeFallbackContentElement(node, direction); |
| 199 |
} |
| 200 |
|
| 201 |
Node* ComposedShadowTreeWalker::traverseSiblingOrBackToYoungerShadowRoot(const Node* node, TraversalDirection direction) |
| 202 |
{ |
| 203 |
ASSERT(node); |
| 204 |
if (node->parentNode() && node->parentNode()->isShadowRoot()) { |
| 205 |
ShadowRoot* parentShadowRoot = toShadowRoot(node->parentNode()); |
| 206 |
if (!parentShadowRoot->isYoungest()) { |
| 207 |
InsertionPoint* assignedInsertionPoint = parentShadowRoot->assignedTo(); |
| 208 |
ASSERT(assignedInsertionPoint); |
| 209 |
return traverseSiblingInCurrentTree(assignedInsertionPoint, direction); |
| 210 |
} |
| 211 |
} |
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
Node* ComposedShadowTreeWalker::escapeFallbackContentElement(const Node* node, TraversalDirection direction) |
| 216 |
{ |
| 217 |
ASSERT(node); |
| 218 |
if (node->parentNode() && isInsertionPoint(node->parentNode())) |
| 219 |
return traverseSiblingOrBackToInsertionPoint(node->parentNode(), direction); |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
Node* ComposedShadowTreeWalker::traverseNodeEscapingFallbackContents(const Node* node) const |
| 224 |
{ |
| 225 |
ASSERT(node); |
| 226 |
if (isInsertionPoint(node)) |
| 227 |
return traverseParentNode(node); |
| 228 |
return const_cast<Node*>(node); |
| 229 |
} |
| 230 |
|
| 231 |
void ComposedShadowTreeWalker::parentNode() |
| 232 |
{ |
| 233 |
ASSERT_PRECONDITION(); |
| 234 |
m_node = traverseParentNode(m_node); |
| 235 |
ASSERT_POSTCONDITION(); |
| 236 |
} |
| 237 |
|
| 238 |
Node* ComposedShadowTreeWalker::traverseParentNode(const Node* node) const |
| 239 |
{ |
| 240 |
if (!canCrossUpperBoundary() && node->isShadowRoot()) { |
| 241 |
ASSERT(toShadowRoot(node)->isYoungest()); |
| 242 |
return 0; |
| 243 |
} |
| 244 |
if (ShadowTree* shadowTree = shadowTreeOfParent(node)) { |
| 245 |
if (HTMLContentSelection* selection = shadowTree->selectionFor(node)) |
| 246 |
return traverseParentNode(selection->insertionPoint()); |
| 247 |
} |
| 248 |
return traverseParentNodeInCurrentTree(node); |
| 249 |
} |
| 250 |
|
| 251 |
Node* ComposedShadowTreeWalker::traverseParentNodeInCurrentTree(const Node* node) const |
| 252 |
{ |
| 253 |
if (Node* parent = node->parentNode()) |
| 254 |
return parent->isShadowRoot() ? traverseParentNodeBackToYoungerShadowRootOrHost(toShadowRoot(parent)) : traverseNodeEscapingFallbackContents(parent); |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
Node* ComposedShadowTreeWalker::traverseParentNodeBackToYoungerShadowRootOrHost(const ShadowRoot* shadowRoot) const |
| 259 |
{ |
| 260 |
ASSERT(shadowRoot); |
| 261 |
if (shadowRoot->isYoungest()) { |
| 262 |
if (canCrossUpperBoundary()) |
| 263 |
return shadowRoot->host(); |
| 264 |
return const_cast<ShadowRoot*>(shadowRoot); |
| 265 |
} |
| 266 |
InsertionPoint* assignedInsertionPoint = shadowRoot->assignedTo(); |
| 267 |
ASSERT(assignedInsertionPoint); |
| 268 |
return traverseParentNode(assignedInsertionPoint); |
| 269 |
} |
| 270 |
|
| 271 |
// FIXME: Remove from the first version. |
| 272 |
void ComposedShadowTreeWalker::adjustedParentNode() |
| 273 |
{ |
| 274 |
ASSERT(m_node); |
| 275 |
if (ShadowTree* shadowTree = shadowTreeOfParent(m_node)) { |
| 276 |
if (HTMLContentSelection* selection = shadowTree->selectionFor(m_node)) { |
| 277 |
m_node = selection->insertionPoint(); |
| 278 |
return; |
| 279 |
} |
| 280 |
} |
| 281 |
if (!m_node->isShadowRoot()) { |
| 282 |
m_node = m_node->parentNode(); |
| 283 |
return; |
| 284 |
} |
| 285 |
const ShadowRoot* shadowRoot = toShadowRoot(m_node); |
| 286 |
if (!shadowRoot->isYoungest()) { |
| 287 |
ASSERT(shadowRoot->assignedTo()); |
| 288 |
m_node = shadowRoot->assignedTo(); |
| 289 |
return; |
| 290 |
} |
| 291 |
if (canCrossUpperBoundary()) |
| 292 |
m_node = shadowRoot->host(); |
| 293 |
else |
| 294 |
m_node = 0; |
| 295 |
} |
| 296 |
|
| 297 |
Node* ComposedShadowTreeWalker::traverseNextSibling(const Node* node) |
| 298 |
{ |
| 299 |
ASSERT(node); |
| 300 |
return traverseSiblingOrBackToInsertionPoint(node, TraversalDirectionForward); |
| 301 |
} |
| 302 |
|
| 303 |
Node* ComposedShadowTreeWalker::traversePreviousSibling(const Node* node) |
| 304 |
{ |
| 305 |
ASSERT(node); |
| 306 |
return traverseSiblingOrBackToInsertionPoint(node, TraversalDirectionBackward); |
| 307 |
} |
| 308 |
|
| 309 |
void ComposedShadowTreeWalker::nextNode() |
| 310 |
{ |
| 311 |
ASSERT_PRECONDITION(); |
| 312 |
if (Node* next = traverseFirstChild(m_node)) |
| 313 |
m_node = next; |
| 314 |
else if (Node* next = traverseNextSibling(m_node)) |
| 315 |
m_node = next; |
| 316 |
else { |
| 317 |
const Node* n = m_node; |
| 318 |
while (n && !traverseNextSibling(n)) |
| 319 |
n = traverseParentNode(n); |
| 320 |
m_node = n ? traverseNextSibling(n) : 0; |
| 321 |
} |
| 322 |
ASSERT_POSTCONDITION(); |
| 323 |
} |
| 324 |
|
| 325 |
void ComposedShadowTreeWalker::previousNode() |
| 326 |
{ |
| 327 |
ASSERT_PRECONDITION(); |
| 328 |
if (Node* n = traversePreviousSibling(m_node)) { |
| 329 |
while (Node* child = traverseLastChild(n)) |
| 330 |
n = child; |
| 331 |
m_node = n; |
| 332 |
} else |
| 333 |
parentNode(); |
| 334 |
ASSERT_POSTCONDITION(); |
| 335 |
} |
| 336 |
|
| 337 |
} // namespace |