Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: Source/core/dom/Document.cpp

Issue 779173002: Be more strict about multiple documentElements (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Do the c -> child rename Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/dom/Document.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org) 5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2011, 2012 Apple Inc. All r ights reserved. 6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2011, 2012 Apple Inc. All r ights reserved.
7 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/) 7 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/)
8 * Copyright (C) 2008, 2009, 2011, 2012 Google Inc. All rights reserved. 8 * Copyright (C) 2008, 2009, 2011, 2012 Google Inc. All rights reserved.
9 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) 9 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
10 * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved. 10 * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved.
(...skipping 3240 matching lines...) Expand 10 before | Expand all | Expand 10 after
3251 // Documents may contain no more than one of each of these. 3251 // Documents may contain no more than one of each of these.
3252 // (One Element and one DocumentType.) 3252 // (One Element and one DocumentType.)
3253 for (Node& c : NodeTraversal::childrenOf(*this)) 3253 for (Node& c : NodeTraversal::childrenOf(*this))
3254 if (c.nodeType() == type) 3254 if (c.nodeType() == type)
3255 return false; 3255 return false;
3256 return true; 3256 return true;
3257 } 3257 }
3258 return false; 3258 return false;
3259 } 3259 }
3260 3260
3261 bool Document::canReplaceChild(const Node& newChild, const Node& oldChild) const 3261 bool Document::canAcceptChild(const Node& newChild, const Node* oldChild, Except ionState& exceptionState) const
3262 { 3262 {
3263 if (oldChild.nodeType() == newChild.nodeType()) 3263 if (oldChild && oldChild->nodeType() == newChild.nodeType())
3264 return true; 3264 return true;
3265 3265
3266 int numDoctypes = 0; 3266 int numDoctypes = 0;
3267 int numElements = 0; 3267 int numElements = 0;
3268 3268
3269 // First, check how many doctypes and elements we have, not counting 3269 // First, check how many doctypes and elements we have, not counting
3270 // the child we're about to remove. 3270 // the child we're about to remove.
3271 for (Node& c : NodeTraversal::childrenOf(*this)) { 3271 for (Node& child : NodeTraversal::childrenOf(*this)) {
3272 if (c == oldChild) 3272 if (oldChild && *oldChild == child)
3273 continue; 3273 continue;
3274 3274
3275 switch (c.nodeType()) { 3275 switch (child.nodeType()) {
3276 case DOCUMENT_TYPE_NODE: 3276 case DOCUMENT_TYPE_NODE:
3277 numDoctypes++; 3277 numDoctypes++;
3278 break; 3278 break;
3279 case ELEMENT_NODE: 3279 case ELEMENT_NODE:
3280 numElements++; 3280 numElements++;
3281 break; 3281 break;
3282 default: 3282 default:
3283 break; 3283 break;
3284 } 3284 }
3285 } 3285 }
3286 3286
3287 // Then, see how many doctypes and elements might be added by the new child. 3287 // Then, see how many doctypes and elements might be added by the new child.
3288 if (newChild.isDocumentFragment()) { 3288 if (newChild.isDocumentFragment()) {
3289 for (Node& c : NodeTraversal::childrenOf(toDocumentFragment(newChild))) { 3289 for (Node& child : NodeTraversal::childrenOf(toDocumentFragment(newChild ))) {
3290 switch (c.nodeType()) { 3290 switch (child.nodeType()) {
3291 case ATTRIBUTE_NODE: 3291 case ATTRIBUTE_NODE:
3292 case CDATA_SECTION_NODE: 3292 case CDATA_SECTION_NODE:
3293 case DOCUMENT_FRAGMENT_NODE: 3293 case DOCUMENT_FRAGMENT_NODE:
3294 case DOCUMENT_NODE: 3294 case DOCUMENT_NODE:
3295 case TEXT_NODE: 3295 case TEXT_NODE:
3296 exceptionState.throwDOMException(HierarchyRequestError, "Nodes o f type '" + newChild.nodeName() +
3297 "' may not be inserted inside nodes of type '#document'.");
3296 return false; 3298 return false;
3297 case COMMENT_NODE: 3299 case COMMENT_NODE:
3298 case PROCESSING_INSTRUCTION_NODE: 3300 case PROCESSING_INSTRUCTION_NODE:
3299 break; 3301 break;
3300 case DOCUMENT_TYPE_NODE: 3302 case DOCUMENT_TYPE_NODE:
3301 numDoctypes++; 3303 numDoctypes++;
3302 break; 3304 break;
3303 case ELEMENT_NODE: 3305 case ELEMENT_NODE:
3304 numElements++; 3306 numElements++;
3305 break; 3307 break;
3306 } 3308 }
3307 } 3309 }
3308 } else { 3310 } else {
3309 switch (newChild.nodeType()) { 3311 switch (newChild.nodeType()) {
3310 case ATTRIBUTE_NODE: 3312 case ATTRIBUTE_NODE:
3311 case CDATA_SECTION_NODE: 3313 case CDATA_SECTION_NODE:
3312 case DOCUMENT_FRAGMENT_NODE: 3314 case DOCUMENT_FRAGMENT_NODE:
3313 case DOCUMENT_NODE: 3315 case DOCUMENT_NODE:
3314 case TEXT_NODE: 3316 case TEXT_NODE:
3317 exceptionState.throwDOMException(HierarchyRequestError, "Nodes of ty pe '" + newChild.nodeName() +
3318 "' may not be inserted inside nodes of type '#document'.");
3315 return false; 3319 return false;
3316 case COMMENT_NODE: 3320 case COMMENT_NODE:
3317 case PROCESSING_INSTRUCTION_NODE: 3321 case PROCESSING_INSTRUCTION_NODE:
3318 return true; 3322 return true;
3319 case DOCUMENT_TYPE_NODE: 3323 case DOCUMENT_TYPE_NODE:
3320 numDoctypes++; 3324 numDoctypes++;
3321 break; 3325 break;
3322 case ELEMENT_NODE: 3326 case ELEMENT_NODE:
3323 numElements++; 3327 numElements++;
3324 break; 3328 break;
3325 } 3329 }
3326 } 3330 }
3327 3331
3328 if (numElements > 1 || numDoctypes > 1) 3332 if (numElements > 1 || numDoctypes > 1) {
3333 exceptionState.throwDOMException(HierarchyRequestError,
3334 String::format("Only one %s on document allowed.",
3335 numElements > 1 ? "element" : "doctype"));
3329 return false; 3336 return false;
3337 }
3330 3338
3331 return true; 3339 return true;
3332 } 3340 }
3333 3341
3334 PassRefPtrWillBeRawPtr<Node> Document::cloneNode(bool deep) 3342 PassRefPtrWillBeRawPtr<Node> Document::cloneNode(bool deep)
3335 { 3343 {
3336 RefPtrWillBeRawPtr<Document> clone = cloneDocumentWithoutChildren(); 3344 RefPtrWillBeRawPtr<Document> clone = cloneDocumentWithoutChildren();
3337 clone->cloneDataFromDocument(*this); 3345 clone->cloneDataFromDocument(*this);
3338 if (deep) 3346 if (deep)
3339 cloneChildNodes(clone.get()); 3347 cloneChildNodes(clone.get());
(...skipping 2499 matching lines...) Expand 10 before | Expand all | Expand 10 after
5839 #ifndef NDEBUG 5847 #ifndef NDEBUG
5840 using namespace blink; 5848 using namespace blink;
5841 void showLiveDocumentInstances() 5849 void showLiveDocumentInstances()
5842 { 5850 {
5843 WeakDocumentSet& set = liveDocumentSet(); 5851 WeakDocumentSet& set = liveDocumentSet();
5844 fprintf(stderr, "There are %u documents currently alive:\n", set.size()); 5852 fprintf(stderr, "There are %u documents currently alive:\n", set.size());
5845 for (Document* document : set) 5853 for (Document* document : set)
5846 fprintf(stderr, "- Document %p URL: %s\n", document, document->url().str ing().utf8().data()); 5854 fprintf(stderr, "- Document %p URL: %s\n", document, document->url().str ing().utf8().data());
5847 } 5855 }
5848 #endif 5856 #endif
OLDNEW
« no previous file with comments | « Source/core/dom/Document.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698