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

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

Issue 22508006: Refactor adding and removing named and IDed elements (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 4 months 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 | Annotate | Revision Log
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 Peter Kelly (pmk@post.com) 4 * (C) 2001 Peter Kelly (pmk@post.com)
5 * (C) 2001 Dirk Mueller (mueller@kde.org) 5 * (C) 2001 Dirk Mueller (mueller@kde.org)
6 * (C) 2007 David Smith (catfish.man@gmail.com) 6 * (C) 2007 David Smith (catfish.man@gmail.com)
7 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013 Apple Inc. All rights reserved. 7 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013 Apple Inc. All rights reserved.
8 * (C) 2007 Eric Seidel (eric@webkit.org) 8 * (C) 2007 Eric Seidel (eric@webkit.org)
9 * 9 *
10 * This library is free software; you can redistribute it and/or 10 * This library is free software; you can redistribute it and/or
(...skipping 1198 matching lines...) Expand 10 before | Expand all | Expand 10 after
1209 bool Element::isInert() const 1209 bool Element::isInert() const
1210 { 1210 {
1211 const Element* dialog = document()->activeModalDialog(); 1211 const Element* dialog = document()->activeModalDialog();
1212 if (dialog && !containsIncludingShadowDOM(dialog) && !dialog->containsInclud ingShadowDOM(this)) 1212 if (dialog && !containsIncludingShadowDOM(dialog) && !dialog->containsInclud ingShadowDOM(this))
1213 return true; 1213 return true;
1214 return document()->ownerElement() && document()->ownerElement()->isInert(); 1214 return document()->ownerElement() && document()->ownerElement()->isInert();
1215 } 1215 }
1216 1216
1217 Node::InsertionNotificationRequest Element::insertedInto(ContainerNode* insertio nPoint) 1217 Node::InsertionNotificationRequest Element::insertedInto(ContainerNode* insertio nPoint)
1218 { 1218 {
1219 bool wasInDocument = inDocument();
1219 // need to do superclass processing first so inDocument() is true 1220 // need to do superclass processing first so inDocument() is true
1220 // by the time we reach updateId 1221 // by the time we reach updateId
1221 ContainerNode::insertedInto(insertionPoint); 1222 ContainerNode::insertedInto(insertionPoint);
1223 ASSERT(!wasInDocument || inDocument());
adamk 2013/08/09 00:59:11 How useful is this ASSERT?
1222 1224
1223 if (containsFullScreenElement() && parentElement() && !parentElement()->cont ainsFullScreenElement()) 1225 if (containsFullScreenElement() && parentElement() && !parentElement()->cont ainsFullScreenElement())
1224 setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(true); 1226 setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(true);
1225 1227
1226 if (Element* before = pseudoElement(BEFORE)) 1228 if (Element* before = pseudoElement(BEFORE))
1227 before->insertedInto(insertionPoint); 1229 before->insertedInto(insertionPoint);
1228 1230
1229 if (Element* after = pseudoElement(AFTER)) 1231 if (Element* after = pseudoElement(AFTER))
1230 after->insertedInto(insertionPoint); 1232 after->insertedInto(insertionPoint);
1231 1233
1232 if (!insertionPoint->isInTreeScope()) 1234 if (!insertionPoint->isInTreeScope())
1233 return InsertionDone; 1235 return InsertionDone;
1234 1236
1235 if (hasRareData()) 1237 if (hasRareData())
1236 elementRareData()->clearClassListValueForQuirksMode(); 1238 elementRareData()->clearClassListValueForQuirksMode();
1237 1239
1238 if (isUpgradedCustomElement() && inDocument()) 1240 if (isUpgradedCustomElement() && inDocument())
1239 CustomElement::didEnterDocument(this, document()); 1241 CustomElement::didEnterDocument(this, document());
1240 1242
1241 TreeScope* scope = insertionPoint->treeScope(); 1243 TreeScope* newScope = insertionPoint->treeScope();
1242 if (scope != treeScope()) 1244 if (newScope != treeScope())
1243 return InsertionDone; 1245 return InsertionDone;
1244 1246
1247 HTMLDocument* newDocument = !wasInDocument && inDocument() && !isInShadowTre e() && newScope->documentScope()->isHTMLDocument() ? toHTMLDocument(newScope->do cumentScope()) : 0;
adamk 2013/08/09 00:59:11 Gah, this looks weird to me. Isn't newScope->docum
1248
1245 const AtomicString& idValue = getIdAttribute(); 1249 const AtomicString& idValue = getIdAttribute();
1246 if (!idValue.isNull()) 1250 if (!idValue.isNull()) {
1247 updateId(scope, nullAtom, idValue); 1251 updateIdForTreeScope(newScope, nullAtom, idValue);
1252 if (newDocument)
1253 updateIdForDocument(newDocument, nullAtom, idValue);
1254 }
1248 1255
1249 const AtomicString& nameValue = getNameAttribute(); 1256 const AtomicString& nameValue = getNameAttribute();
1250 if (!nameValue.isNull()) 1257 if (!nameValue.isNull()) {
1251 updateName(nullAtom, nameValue); 1258 updateNameForTreeScope(newScope, nullAtom, nameValue);
1259 if (newDocument)
1260 updateNameForDocument(newDocument, nullAtom, nameValue);
1261 }
1252 1262
1253 if (hasTagName(labelTag)) { 1263 if (hasTagName(labelTag)) {
1254 if (scope->shouldCacheLabelsByForAttribute()) 1264 if (newScope->shouldCacheLabelsByForAttribute())
1255 updateLabel(scope, nullAtom, fastGetAttribute(forAttr)); 1265 updateLabel(newScope, nullAtom, fastGetAttribute(forAttr));
1256 } 1266 }
1257 1267
1258 if (parentElement() && parentElement()->isInCanvasSubtree()) 1268 if (parentElement() && parentElement()->isInCanvasSubtree())
1259 setIsInCanvasSubtree(true); 1269 setIsInCanvasSubtree(true);
1260 1270
1261 return InsertionDone; 1271 return InsertionDone;
1262 } 1272 }
1263 1273
1264 void Element::removedFrom(ContainerNode* insertionPoint) 1274 void Element::removedFrom(ContainerNode* insertionPoint)
1265 { 1275 {
(...skipping 11 matching lines...) Expand all
1277 1287
1278 if (containsFullScreenElement()) 1288 if (containsFullScreenElement())
1279 setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(false); 1289 setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(false);
1280 1290
1281 if (document()->page()) 1291 if (document()->page())
1282 document()->page()->pointerLockController()->elementRemoved(this); 1292 document()->page()->pointerLockController()->elementRemoved(this);
1283 1293
1284 setSavedLayerScrollOffset(IntSize()); 1294 setSavedLayerScrollOffset(IntSize());
1285 1295
1286 if (insertionPoint->isInTreeScope() && treeScope() == document()) { 1296 if (insertionPoint->isInTreeScope() && treeScope() == document()) {
1297 TreeScope* oldScope = insertionPoint->treeScope();
1298 HTMLDocument* oldDocument = inDocument() && !isInShadowTree() && oldScop e->documentScope()->isHTMLDocument() ? toHTMLDocument(oldScope->documentScope()) : 0;
adamk 2013/08/09 00:59:11 Eww, this is one of those places where inDocument(
1299
1287 const AtomicString& idValue = getIdAttribute(); 1300 const AtomicString& idValue = getIdAttribute();
1288 if (!idValue.isNull()) 1301 if (!idValue.isNull()) {
1289 updateId(insertionPoint->treeScope(), idValue, nullAtom); 1302 updateIdForTreeScope(oldScope, idValue, nullAtom);
1303 if (oldDocument)
1304 updateIdForDocument(oldDocument, idValue, nullAtom);
1305 }
1290 1306
1291 const AtomicString& nameValue = getNameAttribute(); 1307 const AtomicString& nameValue = getNameAttribute();
1292 if (!nameValue.isNull()) 1308 if (!nameValue.isNull()) {
1293 updateName(nameValue, nullAtom); 1309 updateNameForTreeScope(oldScope, nameValue, nullAtom);
1310 if (oldDocument)
1311 updateNameForDocument(oldDocument, nameValue, nullAtom);
1312 }
1294 1313
1295 if (hasTagName(labelTag)) { 1314 if (hasTagName(labelTag)) {
1296 TreeScope* treeScope = insertionPoint->treeScope(); 1315 if (oldScope->shouldCacheLabelsByForAttribute())
1297 if (treeScope->shouldCacheLabelsByForAttribute()) 1316 updateLabel(oldScope, fastGetAttribute(forAttr), nullAtom);
1298 updateLabel(treeScope, fastGetAttribute(forAttr), nullAtom);
1299 } 1317 }
1300 } 1318 }
1301 1319
1302 ContainerNode::removedFrom(insertionPoint); 1320 ContainerNode::removedFrom(insertionPoint);
1321
1303 if (wasInDocument) { 1322 if (wasInDocument) {
1304 if (hasPendingResources()) 1323 if (hasPendingResources())
1305 document()->accessSVGExtensions()->removeElementFromPendingResources (this); 1324 document()->accessSVGExtensions()->removeElementFromPendingResources (this);
1306 1325
1307 if (isUpgradedCustomElement()) 1326 if (isUpgradedCustomElement())
1308 CustomElement::didLeaveDocument(this, insertionPoint->document()); 1327 CustomElement::didLeaveDocument(this, insertionPoint->document());
1309 } 1328 }
1310 1329
1311 if (hasRareData()) 1330 if (hasRareData())
1312 elementRareData()->setIsInCanvasSubtree(false); 1331 elementRareData()->setIsInCanvasSubtree(false);
(...skipping 1440 matching lines...) Expand 10 before | Expand all | Expand 10 after
2753 } 2772 }
2754 #endif 2773 #endif
2755 2774
2756 #ifdef DUMP_NODE_STATISTICS 2775 #ifdef DUMP_NODE_STATISTICS
2757 bool Element::hasNamedNodeMap() const 2776 bool Element::hasNamedNodeMap() const
2758 { 2777 {
2759 return hasRareData() && elementRareData()->attributeMap(); 2778 return hasRareData() && elementRareData()->attributeMap();
2760 } 2779 }
2761 #endif 2780 #endif
2762 2781
2763 inline void Element::updateName(const AtomicString& oldName, const AtomicString& newName) 2782 void Element::updateName(const AtomicString& oldName, const AtomicString& newNam e)
2764 { 2783 {
2765 if (!inDocument() || isInShadowTree()) 2784 if (!isInTreeScope())
2766 return; 2785 return;
2767 2786
2768 if (oldName == newName) 2787 if (oldName == newName)
2769 return; 2788 return;
2770 2789
2790 updateNameForTreeScope(treeScope(), oldName, newName);
2791
2792 if (!inDocument() || isInShadowTree())
2793 return;
2794
2795 Document* htmlDocument = document();
2796 if (!htmlDocument->isHTMLDocument())
2797 return;
2798
2799 updateNameForDocument(toHTMLDocument(htmlDocument), oldName, newName);
2800 }
2801
2802 void Element::updateNameForTreeScope(TreeScope* scope, const AtomicString& oldNa me, const AtomicString& newName)
2803 {
2804 ASSERT(isInTreeScope());
2805 ASSERT(oldName != newName);
2806
2807 if (!oldName.isEmpty())
2808 scope->removeElementByName(oldName, this);
2809 if (!newName.isEmpty())
2810 scope->addElementByName(newName, this);
2811 }
2812
2813 void Element::updateNameForDocument(HTMLDocument* document, const AtomicString& oldName, const AtomicString& newName)
2814 {
2815 ASSERT(inDocument() && !isInShadowTree());
2816 ASSERT(oldName != newName);
2817
2771 if (shouldRegisterAsNamedItem()) 2818 if (shouldRegisterAsNamedItem())
2772 updateNamedItemRegistration(oldName, newName); 2819 updateNamedItemRegistration(oldName, newName);
2773 } 2820 }
2774 2821
2775 inline void Element::updateId(const AtomicString& oldId, const AtomicString& new Id) 2822 void Element::updateId(const AtomicString& oldId, const AtomicString& newId)
2776 { 2823 {
2777 if (!isInTreeScope()) 2824 if (!isInTreeScope())
2778 return; 2825 return;
2779 2826
2780 if (oldId == newId) 2827 if (oldId == newId)
2781 return; 2828 return;
2782 2829
2783 updateId(treeScope(), oldId, newId); 2830 updateIdForTreeScope(treeScope(), oldId, newId);
2831
2832 if (!inDocument() || isInShadowTree())
2833 return;
2834
2835 Document* htmlDocument = document();
2836 if (!htmlDocument->isHTMLDocument())
2837 return;
2838
2839 updateIdForDocument(toHTMLDocument(htmlDocument), oldId, newId);
2784 } 2840 }
2785 2841
2786 inline void Element::updateId(TreeScope* scope, const AtomicString& oldId, const AtomicString& newId) 2842 void Element::updateIdForTreeScope(TreeScope* scope, const AtomicString& oldId, const AtomicString& newId)
2787 { 2843 {
2788 ASSERT(isInTreeScope()); 2844 ASSERT(isInTreeScope());
2789 ASSERT(oldId != newId); 2845 ASSERT(oldId != newId);
2790 2846
2791 if (!oldId.isEmpty()) 2847 if (!oldId.isEmpty())
2792 scope->removeElementById(oldId, this); 2848 scope->removeElementById(oldId, this);
2793 if (!newId.isEmpty()) 2849 if (!newId.isEmpty())
2794 scope->addElementById(newId, this); 2850 scope->addElementById(newId, this);
2851 }
2852
2853 void Element::updateIdForDocument(HTMLDocument* document, const AtomicString& ol dId, const AtomicString& newId)
2854 {
2855 ASSERT(inDocument() && !isInShadowTree());
2856 ASSERT(oldId != newId);
2795 2857
2796 if (shouldRegisterAsExtraNamedItem()) 2858 if (shouldRegisterAsExtraNamedItem())
2797 updateExtraNamedItemRegistration(oldId, newId); 2859 updateExtraNamedItemRegistration(oldId, newId);
2798 } 2860 }
2799 2861
2800 void Element::updateLabel(TreeScope* scope, const AtomicString& oldForAttributeV alue, const AtomicString& newForAttributeValue) 2862 void Element::updateLabel(TreeScope* scope, const AtomicString& oldForAttributeV alue, const AtomicString& newForAttributeValue)
2801 { 2863 {
2802 ASSERT(hasTagName(labelTag)); 2864 ASSERT(hasTagName(labelTag));
2803 2865
2804 if (!inDocument()) 2866 if (!inDocument())
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
2875 if (oldDocument->inQuirksMode() != document()->inQuirksMode()) { 2937 if (oldDocument->inQuirksMode() != document()->inQuirksMode()) {
2876 if (hasID()) 2938 if (hasID())
2877 setIdAttribute(getIdAttribute()); 2939 setIdAttribute(getIdAttribute());
2878 if (hasClass()) 2940 if (hasClass())
2879 setAttribute(HTMLNames::classAttr, getClassAttribute()); 2941 setAttribute(HTMLNames::classAttr, getClassAttribute());
2880 } 2942 }
2881 } 2943 }
2882 2944
2883 void Element::updateNamedItemRegistration(const AtomicString& oldName, const Ato micString& newName) 2945 void Element::updateNamedItemRegistration(const AtomicString& oldName, const Ato micString& newName)
2884 { 2946 {
2885 if (!document()->isHTMLDocument()) 2947 ASSERT(document()->isHTMLDocument());
2886 return;
2887 2948
2888 if (!oldName.isEmpty()) 2949 if (!oldName.isEmpty())
2889 toHTMLDocument(document())->removeNamedItem(oldName); 2950 toHTMLDocument(document())->removeNamedItem(oldName);
2890 2951
2891 if (!newName.isEmpty()) 2952 if (!newName.isEmpty())
2892 toHTMLDocument(document())->addNamedItem(newName); 2953 toHTMLDocument(document())->addNamedItem(newName);
2893 } 2954 }
2894 2955
2895 void Element::updateExtraNamedItemRegistration(const AtomicString& oldId, const AtomicString& newId) 2956 void Element::updateExtraNamedItemRegistration(const AtomicString& oldId, const AtomicString& newId)
2896 { 2957 {
2897 if (!document()->isHTMLDocument()) 2958 ASSERT(document()->isHTMLDocument());
2898 return;
2899 2959
2900 if (!oldId.isEmpty()) 2960 if (!oldId.isEmpty())
2901 toHTMLDocument(document())->removeExtraNamedItem(oldId); 2961 toHTMLDocument(document())->removeExtraNamedItem(oldId);
2902 2962
2903 if (!newId.isEmpty()) 2963 if (!newId.isEmpty())
2904 toHTMLDocument(document())->addExtraNamedItem(newId); 2964 toHTMLDocument(document())->addExtraNamedItem(newId);
2905 } 2965 }
2906 2966
2907 PassRefPtr<HTMLCollection> Element::ensureCachedHTMLCollection(CollectionType ty pe) 2967 PassRefPtr<HTMLCollection> Element::ensureCachedHTMLCollection(CollectionType ty pe)
2908 { 2968 {
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
3026 { 3086 {
3027 if (hasSyntheticAttrChildNodes()) 3087 if (hasSyntheticAttrChildNodes())
3028 detachAllAttrNodesFromElement(); 3088 detachAllAttrNodesFromElement();
3029 3089
3030 other.synchronizeAllAttributes(); 3090 other.synchronizeAllAttributes();
3031 if (!other.m_elementData) { 3091 if (!other.m_elementData) {
3032 m_elementData.clear(); 3092 m_elementData.clear();
3033 return; 3093 return;
3034 } 3094 }
3035 3095
3096 // We can't update window and document's named item maps since the presence of image and object elements depend on other attributes and children.
3097 // Fortunately, those named item maps are only updated when this element is in the document, which should never be the case.
3098 ASSERT(!inDocument());
3099
3036 const AtomicString& oldID = getIdAttribute(); 3100 const AtomicString& oldID = getIdAttribute();
3037 const AtomicString& newID = other.getIdAttribute(); 3101 const AtomicString& newID = other.getIdAttribute();
3038 3102
3039 if (!oldID.isNull() || !newID.isNull()) 3103 if (!oldID.isNull() || !newID.isNull())
3040 updateId(oldID, newID); 3104 updateId(oldID, newID);
3041 3105
3042 const AtomicString& oldName = getNameAttribute(); 3106 const AtomicString& oldName = getNameAttribute();
3043 const AtomicString& newName = other.getNameAttribute(); 3107 const AtomicString& newName = other.getNameAttribute();
3044 3108
3045 if (!oldName.isNull() || !newName.isNull()) 3109 if (!oldName.isNull() || !newName.isNull())
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after
3642 return 0; 3706 return 0;
3643 } 3707 }
3644 3708
3645 Attribute* UniqueElementData::attributeItem(unsigned index) 3709 Attribute* UniqueElementData::attributeItem(unsigned index)
3646 { 3710 {
3647 ASSERT_WITH_SECURITY_IMPLICATION(index < length()); 3711 ASSERT_WITH_SECURITY_IMPLICATION(index < length());
3648 return &m_attributeVector.at(index); 3712 return &m_attributeVector.at(index);
3649 } 3713 }
3650 3714
3651 } // namespace WebCore 3715 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698