2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Peter Kelly (pmk@post.com)
5 * (C) 2001 Dirk Mueller (mueller@kde.org)
6 * Copyright (C) 2003-2011, 2013, 2014 Apple Inc. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
28 #include "core/CSSPropertyNames.h"
29 #include "core/HTMLNames.h"
30 #include "core/css/CSSPrimitiveValue.h"
31 #include "core/css/CSSSelector.h"
32 #include "core/dom/Attribute.h"
33 #include "core/dom/ContainerNode.h"
34 #include "core/dom/ElementData.h"
35 #include "core/dom/SpaceSplitString.h"
36 #include "core/html/CollectionType.h"
37 #include "core/page/FocusType.h"
38 #include "platform/heap/Handle.h"
42 class ActiveAnimations;
45 class CSSStyleDeclaration;
48 class CustomElementDefinition;
52 class ElementRareData;
56 class InputMethodContext;
59 class MutableStylePropertySet;
60 class PropertySetCSSStyleDeclaration;
63 class StylePropertySet;
65 enum SpellcheckAttributeState {
66 SpellcheckAttributeTrue,
67 SpellcheckAttributeFalse,
68 SpellcheckAttributeDefault
72 TabIndexWasSetExplicitly = 1 << 0,
73 StyleAffectedByEmpty = 1 << 1,
74 IsInCanvasSubtree = 1 << 2,
75 ContainsFullScreenElement = 1 << 3,
76 IsInTopLayer = 1 << 4,
77 HasPendingResources = 1 << 5,
79 NumberOfElementFlags = 6, // Required size of bitfield used to store the flags.
82 class Element : public ContainerNode {
83 DEFINE_WRAPPERTYPEINFO();
85 static PassRefPtrWillBeRawPtr<Element> create(const QualifiedName&, Document*);
88 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecopy);
89 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecut);
90 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforepaste);
91 DEFINE_ATTRIBUTE_EVENT_LISTENER(copy);
92 DEFINE_ATTRIBUTE_EVENT_LISTENER(cut);
93 DEFINE_ATTRIBUTE_EVENT_LISTENER(paste);
94 DEFINE_ATTRIBUTE_EVENT_LISTENER(search);
95 DEFINE_ATTRIBUTE_EVENT_LISTENER(selectstart);
96 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchcancel);
97 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchend);
98 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchmove);
99 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchstart);
100 DEFINE_ATTRIBUTE_EVENT_LISTENER(wheel);
102 bool hasAttribute(const QualifiedName&) const;
103 const AtomicString& getAttribute(const QualifiedName&) const;
105 // Passing nullAtom as the second parameter removes the attribute when calling either of these set methods.
106 void setAttribute(const QualifiedName&, const AtomicString& value);
107 void setSynchronizedLazyAttribute(const QualifiedName&, const AtomicString& value);
109 void removeAttribute(const QualifiedName&);
111 // Typed getters and setters for language bindings.
112 int getIntegralAttribute(const QualifiedName& attributeName) const;
113 void setIntegralAttribute(const QualifiedName& attributeName, int value);
114 void setUnsignedIntegralAttribute(const QualifiedName& attributeName, unsigned value);
115 double getFloatingPointAttribute(const QualifiedName& attributeName, double fallbackValue = std::numeric_limits<double>::quiet_NaN()) const;
116 void setFloatingPointAttribute(const QualifiedName& attributeName, double value);
118 // Call this to get the value of an attribute that is known not to be the style
119 // attribute or one of the SVG animatable attributes.
120 bool fastHasAttribute(const QualifiedName&) const;
121 const AtomicString& fastGetAttribute(const QualifiedName&) const;
123 bool fastAttributeLookupAllowed(const QualifiedName&) const;
126 #ifdef DUMP_NODE_STATISTICS
127 bool hasNamedNodeMap() const;
129 bool hasAttributes() const;
131 bool hasAttribute(const AtomicString& name) const;
132 bool hasAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
134 const AtomicString& getAttribute(const AtomicString& name) const;
135 const AtomicString& getAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
137 void setAttribute(const AtomicString& name, const AtomicString& value, ExceptionState&);
138 static bool parseAttributeName(QualifiedName&, const AtomicString& namespaceURI, const AtomicString& qualifiedName, ExceptionState&);
139 void setAttributeNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, const AtomicString& value, ExceptionState&);
141 bool isIdAttributeName(const QualifiedName&) const;
142 const AtomicString& getIdAttribute() const;
143 void setIdAttribute(const AtomicString&);
145 const AtomicString& getNameAttribute() const;
146 const AtomicString& getClassAttribute() const;
148 bool shouldIgnoreAttributeCase() const;
150 // Call this to get the value of the id attribute for style resolution purposes.
151 // The value will already be lowercased if the document is in compatibility mode,
152 // so this function is not suitable for non-style uses.
153 const AtomicString& idForStyleResolution() const;
155 // This getter takes care of synchronizing all attributes before returning the
156 // AttributeCollection. If the Element has no attributes, an empty AttributeCollection
157 // will be returned. This is not a trivial getter and its return value should be cached
159 AttributeCollection attributes() const;
160 // This variant will not update the potentially invalid attributes. To be used when not interested
161 // in style attribute or one of the SVG animation attributes.
162 AttributeCollection attributesWithoutUpdate() const;
164 void scrollIntoView(bool alignToTop = true);
165 void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
172 // FIXME: Replace uses of offsetParent in the platform with calls
173 // to the render layer and merge offsetParentForBindings and offsetParent.
174 Element* offsetParentForBindings();
176 Element* offsetParent();
181 virtual double scrollLeft();
182 virtual double scrollTop();
183 virtual void setScrollLeft(double);
184 virtual void setScrollLeft(const Dictionary& scrollOptionsHorizontal, ExceptionState&);
185 virtual void setScrollTop(double);
186 virtual void setScrollTop(const Dictionary& scrollOptionsVertical, ExceptionState&);
187 virtual int scrollWidth();
188 virtual int scrollHeight();
190 IntRect boundsInRootViewSpace();
192 PassRefPtrWillBeRawPtr<ClientRectList> getClientRects();
193 PassRefPtrWillBeRawPtr<ClientRect> getBoundingClientRect();
195 // Returns the absolute bounding box translated into screen coordinates:
196 IntRect screenRect() const;
198 virtual void didMoveToNewDocument(Document&) override;
200 void removeAttribute(const AtomicString& name);
201 void removeAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName);
203 PassRefPtrWillBeRawPtr<Attr> detachAttribute(size_t index);
205 PassRefPtrWillBeRawPtr<Attr> getAttributeNode(const AtomicString& name);
206 PassRefPtrWillBeRawPtr<Attr> getAttributeNodeNS(const AtomicString& namespaceURI, const AtomicString& localName);
207 PassRefPtrWillBeRawPtr<Attr> setAttributeNode(Attr*, ExceptionState&);
208 PassRefPtrWillBeRawPtr<Attr> setAttributeNodeNS(Attr*, ExceptionState&);
209 PassRefPtrWillBeRawPtr<Attr> removeAttributeNode(Attr*, ExceptionState&);
211 PassRefPtrWillBeRawPtr<Attr> attrIfExists(const QualifiedName&);
212 PassRefPtrWillBeRawPtr<Attr> ensureAttr(const QualifiedName&);
214 WillBeHeapVector<RefPtrWillBeMember<Attr>>* attrNodeList();
216 CSSStyleDeclaration* style();
218 const QualifiedName& tagQName() const { return m_tagName; }
219 String tagName() const { return nodeName(); }
221 bool hasTagName(const QualifiedName& tagName) const { return m_tagName.matches(tagName); }
222 bool hasTagName(const HTMLQualifiedName& tagName) const { return ContainerNode::hasTagName(tagName); }
223 bool hasTagName(const SVGQualifiedName& tagName) const { return ContainerNode::hasTagName(tagName); }
225 // Should be called only by Document::createElementNS to fix up m_tagName immediately after construction.
226 void setTagNameForCreateElementNS(const QualifiedName&);
228 // A fast function for checking the local name against another atomic string.
229 bool hasLocalName(const AtomicString& other) const { return m_tagName.localName() == other; }
231 virtual const AtomicString& localName() const override final { return m_tagName.localName(); }
232 const AtomicString& prefix() const { return m_tagName.prefix(); }
233 virtual const AtomicString& namespaceURI() const override final { return m_tagName.namespaceURI(); }
235 const AtomicString& locateNamespacePrefix(const AtomicString& namespaceURI) const;
237 virtual KURL baseURI() const override final;
239 virtual String nodeName() const override;
241 PassRefPtrWillBeRawPtr<Element> cloneElementWithChildren();
242 PassRefPtrWillBeRawPtr<Element> cloneElementWithoutChildren();
244 void scheduleSVGFilterLayerUpdateHack();
246 void normalizeAttributes();
248 void setBooleanAttribute(const QualifiedName& name, bool);
250 virtual const StylePropertySet* additionalPresentationAttributeStyle() { return nullptr; }
251 void invalidateStyleAttribute();
253 const StylePropertySet* inlineStyle() const { return elementData() ? elementData()->m_inlineStyle.get() : nullptr; }
255 bool setInlineStyleProperty(CSSPropertyID, CSSValueID identifier, bool important = false);
256 bool setInlineStyleProperty(CSSPropertyID, double value, CSSPrimitiveValue::UnitType, bool important = false);
257 bool setInlineStyleProperty(CSSPropertyID, const String& value, bool important = false);
258 bool removeInlineStyleProperty(CSSPropertyID);
259 void removeAllInlineStyleProperties();
261 void synchronizeStyleAttributeInternal() const;
263 const StylePropertySet* presentationAttributeStyle();
264 virtual bool isPresentationAttribute(const QualifiedName&) const { return false; }
265 virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) { }
267 // For exposing to DOM only.
268 NamedNodeMap* attributesForBindings() const;
270 enum AttributeModificationReason {
275 // This method is called whenever an attribute is added, changed or removed.
276 virtual void attributeWillChange(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue) { }
277 virtual void attributeChanged(const QualifiedName&, const AtomicString&, AttributeModificationReason = ModifiedDirectly);
278 virtual void parseAttribute(const QualifiedName&, const AtomicString&);
280 virtual bool hasLegalLinkAttribute(const QualifiedName&) const;
281 virtual const QualifiedName& subResourceAttributeName() const;
283 // Only called by the parser immediately after element construction.
284 void parserSetAttributes(const Vector<Attribute>&);
286 // Remove attributes that might introduce scripting from the vector leaving the element unchanged.
287 void stripScriptingAttributes(Vector<Attribute>&) const;
289 bool sharesSameElementData(const Element& other) const { return elementData() == other.elementData(); }
291 // Clones attributes only.
292 void cloneAttributesFromElement(const Element&);
294 // Clones all attribute-derived data, including subclass specifics (through copyNonAttributeProperties.)
295 void cloneDataFromElement(const Element&);
297 bool hasEquivalentAttributes(const Element* other) const;
299 virtual void copyNonAttributePropertiesFromElement(const Element&) { }
301 virtual void attach(const AttachContext& = AttachContext()) override;
302 virtual void detach(const AttachContext& = AttachContext()) override;
303 virtual RenderObject* createRenderer(RenderStyle*);
304 virtual bool rendererIsNeeded(const RenderStyle&);
305 void recalcStyle(StyleRecalcChange, Text* nextTextSibling = nullptr);
306 void pseudoStateChanged(CSSSelector::PseudoType);
307 void setAnimationStyleChange(bool);
308 void setNeedsAnimationStyleRecalc();
310 void setNeedsCompositingUpdate();
312 bool supportsStyleSharing() const;
314 ElementShadow* shadow() const;
315 ElementShadow& ensureShadow();
316 PassRefPtrWillBeRawPtr<ShadowRoot> createShadowRoot(ExceptionState&);
317 ShadowRoot* shadowRoot() const;
318 ShadowRoot* youngestShadowRoot() const;
320 bool hasAuthorShadowRoot() const { return shadowRoot(); }
321 ShadowRoot* userAgentShadowRoot() const;
322 ShadowRoot& ensureUserAgentShadowRoot();
323 virtual void willAddFirstAuthorShadowRoot() { }
325 bool isInDescendantTreeOf(const Element* shadowHost) const;
327 RenderStyle* computedStyle(PseudoId = NOPSEUDO);
329 // Methods for indicating the style is affected by dynamic updates (e.g., children changing, our position changing in our sibling list, etc.)
330 bool styleAffectedByEmpty() const { return hasElementFlag(StyleAffectedByEmpty); }
331 void setStyleAffectedByEmpty() { setElementFlag(StyleAffectedByEmpty); }
333 void setIsInCanvasSubtree(bool value) { setElementFlag(IsInCanvasSubtree, value); }
334 bool isInCanvasSubtree() const { return hasElementFlag(IsInCanvasSubtree); }
336 bool isUpgradedCustomElement() { return customElementState() == Upgraded; }
337 bool isUnresolvedCustomElement() { return customElementState() == WaitingForUpgrade; }
339 AtomicString computeInheritedLanguage() const;
340 Locale& locale() const;
342 virtual void accessKeyAction(bool /*sendToAnyEvent*/) { }
344 virtual bool isURLAttribute(const Attribute&) const { return false; }
345 virtual bool isHTMLContentAttribute(const Attribute&) const { return false; }
347 virtual bool isLiveLink() const { return false; }
348 KURL hrefURL() const;
350 KURL getURLAttribute(const QualifiedName&) const;
351 KURL getNonEmptyURLAttribute(const QualifiedName&) const;
353 virtual const AtomicString imageSourceURL() const;
354 virtual Image* imageContents() { return nullptr; }
356 virtual void focus(bool restorePreviousSelection = true, FocusType = FocusTypeNone);
357 virtual void updateFocusAppearance(bool restorePreviousSelection);
359 // Whether this element can receive focus at all. Most elements are not
360 // focusable but some elements, such as form controls and links, are. Unlike
361 // rendererIsFocusable(), this method may be called when layout is not up to
362 // date, so it must not use the renderer to determine focusability.
363 virtual bool supportsFocus() const;
364 // Whether the node can actually be focused.
365 bool isFocusable() const;
366 virtual bool isKeyboardFocusable() const;
367 virtual bool isMouseFocusable() const;
368 virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType);
369 virtual void dispatchBlurEvent(Element* newFocusedElement);
370 virtual void dispatchFocusInEvent(const AtomicString& eventType, Element* oldFocusedElement, FocusType);
371 void dispatchFocusOutEvent(const AtomicString& eventType, Element* newFocusedElement);
375 String innerHTML() const;
376 String outerHTML() const;
377 void setInnerHTML(const String&, ExceptionState&);
378 void setOuterHTML(const String&, ExceptionState&);
380 Element* insertAdjacentElement(const String& where, Element* newChild, ExceptionState&);
381 void insertAdjacentText(const String& where, const String& text, ExceptionState&);
382 void insertAdjacentHTML(const String& where, const String& html, ExceptionState&);
384 String textFromChildren();
386 virtual String title() const { return String(); }
388 virtual const AtomicString& shadowPseudoId() const;
389 void setShadowPseudoId(const AtomicString&);
391 LayoutSize minimumSizeForResizing() const;
392 void setMinimumSizeForResizing(const LayoutSize&);
394 virtual void didBecomeFullscreenElement() { }
395 virtual void willStopBeingFullscreenElement() { }
397 // Called by the parser when this element's close tag is reached,
398 // signaling that all child tags have been parsed and added.
399 // This is needed for <applet> and <object> elements, which can't lay themselves out
400 // until they know all of their nested <param>s. [Radar 3603191, 4040848].
401 // Also used for script elements and some SVG elements for similar purposes,
402 // but making parsing a special case in this respect should be avoided if possible.
403 virtual void finishParsingChildren();
405 void beginParsingChildren() { setIsFinishedParsingChildren(false); }
407 PseudoElement* pseudoElement(PseudoId) const;
408 RenderObject* pseudoElementRenderer(PseudoId) const;
410 virtual bool matchesReadOnlyPseudoClass() const { return false; }
411 virtual bool matchesReadWritePseudoClass() const { return false; }
412 virtual bool matchesValidityPseudoClasses() const { return false; }
413 bool matches(const String& selectors, ExceptionState&);
414 virtual bool shouldAppearIndeterminate() const { return false; }
416 DOMTokenList& classList();
418 DOMStringMap& dataset();
420 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
421 virtual bool isDateTimeEditElement() const { return false; }
422 virtual bool isDateTimeFieldElement() const { return false; }
423 virtual bool isPickerIndicatorElement() const { return false; }
426 virtual bool isFormControlElement() const { return false; }
427 virtual bool isSpinButtonElement() const { return false; }
428 virtual bool isTextFormControl() const { return false; }
429 virtual bool isOptionalFormControl() const { return false; }
430 virtual bool isRequiredFormControl() const { return false; }
431 virtual bool isDefaultButtonForForm() const { return false; }
432 virtual bool willValidate() const { return false; }
433 virtual bool isValidElement() { return false; }
434 virtual bool isInRange() const { return false; }
435 virtual bool isOutOfRange() const { return false; }
436 virtual bool isClearButtonElement() const { return false; }
438 virtual bool canContainRangeEndPoint() const override { return true; }
440 // Used for disabled form elements; if true, prevents mouse events from being dispatched
441 // to event listeners, and prevents DOMActivate events from being sent at all.
442 virtual bool isDisabledFormControl() const { return false; }
444 bool hasPendingResources() const { return hasElementFlag(HasPendingResources); }
445 void setHasPendingResources() { setElementFlag(HasPendingResources); }
446 void clearHasPendingResources() { clearElementFlag(HasPendingResources); }
447 virtual void buildPendingResource() { };
449 void setCustomElementDefinition(PassRefPtr<CustomElementDefinition>);
450 CustomElementDefinition* customElementDefinition() const;
452 bool containsFullScreenElement() const { return hasElementFlag(ContainsFullScreenElement); }
453 void setContainsFullScreenElement(bool);
454 void setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(bool);
456 bool isInTopLayer() const { return hasElementFlag(IsInTopLayer); }
457 void setIsInTopLayer(bool);
459 void requestPointerLock();
461 bool isSpellCheckingEnabled() const;
463 // FIXME: public for RenderTreeBuilder, we shouldn't expose this though.
464 PassRefPtr<RenderStyle> styleForRenderer();
467 bool hasClass() const;
468 const SpaceSplitString& classNames() const;
470 IntSize savedLayerScrollOffset() const;
471 void setSavedLayerScrollOffset(const IntSize&);
473 ActiveAnimations* activeAnimations() const;
474 ActiveAnimations& ensureActiveAnimations();
475 bool hasActiveAnimations() const;
477 InputMethodContext& inputMethodContext();
478 bool hasInputMethodContext() const;
480 void setPrefix(const AtomicString&, ExceptionState&);
482 void synchronizeAttribute(const AtomicString& localName) const;
484 MutableStylePropertySet& ensureMutableInlineStyle();
485 void clearMutableInlineStyleIfEmpty();
487 void setTabIndex(int);
488 virtual short tabIndex() const override;
490 virtual void trace(Visitor*) override;
492 virtual v8::Handle<v8::Object> wrap(v8::Handle<v8::Object> creationContext, v8::Isolate*) override;
495 Element(const QualifiedName& tagName, Document*, ConstructionType);
497 const ElementData* elementData() const { return m_elementData.get(); }
498 UniqueElementData& ensureUniqueElementData();
500 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, CSSValueID identifier);
501 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, double value, CSSPrimitiveValue::UnitType);
502 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, const String& value);
504 virtual InsertionNotificationRequest insertedInto(ContainerNode*) override;
505 virtual void removedFrom(ContainerNode*) override;
506 virtual void childrenChanged(const ChildrenChange&) override;
508 virtual void willRecalcStyle(StyleRecalcChange);
509 virtual void didRecalcStyle(StyleRecalcChange);
510 virtual PassRefPtr<RenderStyle> customStyleForRenderer();
512 virtual bool shouldRegisterAsNamedItem() const { return false; }
513 virtual bool shouldRegisterAsExtraNamedItem() const { return false; }
515 bool supportsSpatialNavigationFocus() const;
517 void clearTabIndexExplicitlyIfNeeded();
518 void setTabIndexExplicitly(short);
519 // Subclasses may override this method to affect focusability. Unlike
520 // supportsFocus, this method must be called on an up-to-date layout, so it
521 // may use the renderer to reason about focusability. This method cannot be
522 // moved to RenderObject because some focusable nodes don't have renderers,
523 // e.g., HTMLOptionElement.
524 virtual bool rendererIsFocusable() const;
526 // classAttributeChanged() exists to share code between
527 // parseAttribute (called via setAttribute()) and
528 // svgAttributeChanged (called when element.className.baseValue is set)
529 void classAttributeChanged(const AtomicString& newClassString);
531 PassRefPtr<RenderStyle> originalStyleForRenderer();
533 Node* insertAdjacent(const String& where, Node* newChild, ExceptionState&);
536 bool hasElementFlag(ElementFlags mask) const { return hasRareData() && hasElementFlagInternal(mask); }
537 void setElementFlag(ElementFlags, bool value = true);
538 void clearElementFlag(ElementFlags);
539 bool hasElementFlagInternal(ElementFlags) const;
541 bool isElementNode() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
542 bool isDocumentFragment() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
543 bool isDocumentNode() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
545 void styleAttributeChanged(const AtomicString& newStyleString, AttributeModificationReason);
547 void updatePresentationAttributeStyle();
549 void inlineStyleChanged();
550 PropertySetCSSStyleDeclaration* inlineStyleCSSOMWrapper();
551 void setInlineStyleFromString(const AtomicString&);
553 StyleRecalcChange recalcOwnStyle(StyleRecalcChange);
555 inline void checkForEmptyStyleChange();
557 void updatePseudoElement(PseudoId, StyleRecalcChange);
559 inline void createPseudoElementIfNeeded(PseudoId);
561 // FIXME: Everyone should allow author shadows.
562 virtual bool areAuthorShadowsAllowed() const { return true; }
563 virtual void didAddUserAgentShadowRoot(ShadowRoot&) { }
564 virtual bool alwaysCreateUserAgentShadowRoot() const { return false; }
566 // FIXME: Remove the need for Attr to call willModifyAttribute/didModifyAttribute.
569 enum SynchronizationOfLazyAttribute { NotInSynchronizationOfLazyAttribute = 0, InSynchronizationOfLazyAttribute };
571 void didAddAttribute(const QualifiedName&, const AtomicString&);
572 void willModifyAttribute(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue);
573 void didModifyAttribute(const QualifiedName&, const AtomicString&);
574 void didRemoveAttribute(const QualifiedName&);
576 void synchronizeAllAttributes() const;
577 void synchronizeAttribute(const QualifiedName&) const;
579 void updateId(const AtomicString& oldId, const AtomicString& newId);
580 void updateId(TreeScope&, const AtomicString& oldId, const AtomicString& newId);
581 void updateName(const AtomicString& oldName, const AtomicString& newName);
583 virtual NodeType nodeType() const override final;
584 virtual bool childTypeAllowed(NodeType) const override final;
586 void setAttributeInternal(size_t index, const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
587 void appendAttributeInternal(const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
588 void removeAttributeInternal(size_t index, SynchronizationOfLazyAttribute);
589 void attributeChangedFromParserOrByCloning(const QualifiedName&, const AtomicString&, AttributeModificationReason);
592 virtual void formatForDebugger(char* buffer, unsigned length) const override;
595 bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
597 void cancelFocusAppearanceUpdate();
599 virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) override { return computedStyle(pseudoElementSpecifier); }
601 inline void updateCallbackSelectors(RenderStyle* oldStyle, RenderStyle* newStyle);
602 inline void removeCallbackSelectors();
603 inline void addCallbackSelectors();
605 // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
607 virtual PassRefPtrWillBeRawPtr<Node> cloneNode(bool deep) override;
608 virtual PassRefPtrWillBeRawPtr<Element> cloneElementWithoutAttributesAndChildren();
610 QualifiedName m_tagName;
612 SpellcheckAttributeState spellcheckAttributeState() const;
614 void updateNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
615 void updateExtraNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
617 void createUniqueElementData();
619 bool shouldInvalidateDistributionWhenAttributeChanged(ElementShadow*, const QualifiedName&, const AtomicString&);
621 ElementRareData* elementRareData() const;
622 ElementRareData& ensureElementRareData();
624 WillBeHeapVector<RefPtrWillBeMember<Attr>>& ensureAttrNodeList();
625 void removeAttrNodeList();
626 void detachAllAttrNodesFromElement();
627 void detachAttrNodeFromElementWithValue(Attr*, const AtomicString& value);
628 void detachAttrNodeAtIndex(Attr*, size_t index);
630 bool isJavaScriptURLAttribute(const Attribute&) const;
632 v8::Handle<v8::Object> wrapCustomElement(v8::Handle<v8::Object> creationContext, v8::Isolate*);
634 RefPtrWillBeMember<ElementData> m_elementData;
637 DEFINE_NODE_TYPE_CASTS(Element, isElementNode());
638 template <typename T> bool isElementOfType(const Node&);
639 template <> inline bool isElementOfType<const Element>(const Node& node) { return node.isElementNode(); }
640 template <typename T> inline bool isElementOfType(const Element& element) { return isElementOfType<T>(static_cast<const Node&>(element)); }
641 template <> inline bool isElementOfType<const Element>(const Element&) { return true; }
644 template<typename T> inline T& toElement(Node& node)
646 ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
647 return static_cast<T&>(node);
649 template<typename T> inline T* toElement(Node* node)
651 ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
652 return static_cast<T*>(node);
654 template<typename T> inline const T& toElement(const Node& node)
656 ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
657 return static_cast<const T&>(node);
659 template<typename T> inline const T* toElement(const Node* node)
661 ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
662 return static_cast<const T*>(node);
664 template<typename T, typename U> inline T* toElement(const RefPtr<U>& node) { return toElement<T>(node.get()); }
666 inline bool isDisabledFormControl(const Node* node)
668 return node->isElementNode() && toElement(node)->isDisabledFormControl();
671 inline Element* Node::parentElement() const
673 ContainerNode* parent = parentNode();
674 return parent && parent->isElementNode() ? toElement(parent) : nullptr;
677 inline bool Element::fastHasAttribute(const QualifiedName& name) const
679 ASSERT(fastAttributeLookupAllowed(name));
680 return elementData() && elementData()->attributes().findIndex(name) != kNotFound;
683 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
685 ASSERT(fastAttributeLookupAllowed(name));
687 if (const Attribute* attribute = elementData()->attributes().find(name))
688 return attribute->value();
693 inline AttributeCollection Element::attributes() const
696 return AttributeCollection();
697 synchronizeAllAttributes();
698 return elementData()->attributes();
701 inline AttributeCollection Element::attributesWithoutUpdate() const
704 return AttributeCollection();
705 return elementData()->attributes();
708 inline bool Element::hasAttributes() const
710 return !attributes().isEmpty();
713 inline const AtomicString& Element::idForStyleResolution() const
716 return elementData()->idForStyleResolution();
719 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
721 // FIXME: This check is probably not correct for the case where the document has an id attribute
722 // with a non-null namespace, because it will return false, a false negative, if the prefixes
723 // don't match but the local name and namespace both do. However, since this has been like this
724 // for a while and the code paths may be hot, we'll have to measure performance if we fix it.
725 return attributeName == HTMLNames::idAttr;
728 inline const AtomicString& Element::getIdAttribute() const
730 return hasID() ? fastGetAttribute(HTMLNames::idAttr) : nullAtom;
733 inline const AtomicString& Element::getNameAttribute() const
735 return hasName() ? fastGetAttribute(HTMLNames::nameAttr) : nullAtom;
738 inline const AtomicString& Element::getClassAttribute() const
743 return getAttribute(HTMLNames::classAttr);
744 return fastGetAttribute(HTMLNames::classAttr);
747 inline void Element::setIdAttribute(const AtomicString& value)
749 setAttribute(HTMLNames::idAttr, value);
752 inline const SpaceSplitString& Element::classNames() const
755 ASSERT(elementData());
756 return elementData()->classNames();
759 inline bool Element::hasID() const
761 return elementData() && elementData()->hasID();
764 inline bool Element::hasClass() const
766 return elementData() && elementData()->hasClass();
769 inline UniqueElementData& Element::ensureUniqueElementData()
771 if (!elementData() || !elementData()->isUnique())
772 createUniqueElementData();
773 return toUniqueElementData(*m_elementData);
776 inline Node::InsertionNotificationRequest Node::insertedInto(ContainerNode* insertionPoint)
778 ASSERT(!childNeedsStyleInvalidation());
779 ASSERT(!needsStyleInvalidation());
780 ASSERT(insertionPoint->inDocument() || isContainerNode());
781 if (insertionPoint->inDocument())
782 setFlag(InDocumentFlag);
783 if (parentOrShadowHostNode()->isInShadowTree())
784 setFlag(IsInShadowTreeFlag);
785 if (childNeedsDistributionRecalc() && !insertionPoint->childNeedsDistributionRecalc())
786 insertionPoint->markAncestorsWithChildNeedsDistributionRecalc();
787 return InsertionDone;
790 inline void Node::removedFrom(ContainerNode* insertionPoint)
792 ASSERT(insertionPoint->inDocument() || isContainerNode());
793 if (insertionPoint->inDocument())
794 clearFlag(InDocumentFlag);
795 if (isInShadowTree() && !treeScope().rootNode().isShadowRoot())
796 clearFlag(IsInShadowTreeFlag);
799 inline void Element::invalidateStyleAttribute()
801 ASSERT(elementData());
802 elementData()->m_styleAttributeIsDirty = true;
805 inline const StylePropertySet* Element::presentationAttributeStyle()
809 if (elementData()->m_presentationAttributeStyleIsDirty)
810 updatePresentationAttributeStyle();
811 // Need to call elementData() again since updatePresentationAttributeStyle()
812 // might swap it with a UniqueElementData.
813 return elementData()->presentationAttributeStyle();
816 inline void Element::setTagNameForCreateElementNS(const QualifiedName& tagName)
818 // We expect this method to be called only to reset the prefix.
819 ASSERT(tagName.localName() == m_tagName.localName());
820 ASSERT(tagName.namespaceURI() == m_tagName.namespaceURI());
824 inline bool isShadowHost(const Node* node)
826 return node && node->isElementNode() && toElement(node)->shadow();
829 inline bool isShadowHost(const Node& node)
831 return node.isElementNode() && toElement(node).shadow();
834 inline bool isShadowHost(const Element* element)
836 return element && element->shadow();
839 inline bool isShadowHost(const Element& element)
841 return element.shadow();
844 inline bool isAtShadowBoundary(const Element* element)
848 ContainerNode* parentNode = element->parentNode();
849 return parentNode && parentNode->isShadowRoot();
852 // These macros do the same as their NODE equivalents but additionally provide a template specialization
853 // for isElementOfType<>() so that the Traversal<> API works for these Element types.
854 #define DEFINE_ELEMENT_TYPE_CASTS(thisType, predicate) \
855 template <> inline bool isElementOfType<const thisType>(const Node& node) { return node.predicate; } \
856 DEFINE_NODE_TYPE_CASTS(thisType, predicate)
858 #define DEFINE_ELEMENT_TYPE_CASTS_WITH_FUNCTION(thisType) \
859 template <> inline bool isElementOfType<const thisType>(const Node& node) { return is##thisType(node); } \
860 DEFINE_NODE_TYPE_CASTS_WITH_FUNCTION(thisType)
862 #define DECLARE_ELEMENT_FACTORY_WITH_TAGNAME(T) \
863 static PassRefPtrWillBeRawPtr<T> create(const QualifiedName&, Document&)
864 #define DEFINE_ELEMENT_FACTORY_WITH_TAGNAME(T) \
865 PassRefPtrWillBeRawPtr<T> T::create(const QualifiedName& tagName, Document& document) \
867 return adoptRefWillBeNoop(new T(tagName, document)); \