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/dom/Attribute.h"
32 #include "core/dom/ContainerNode.h"
33 #include "core/dom/ElementData.h"
34 #include "core/dom/SpaceSplitString.h"
35 #include "core/html/CollectionType.h"
36 #include "core/page/FocusType.h"
37 #include "platform/heap/Handle.h"
41 class ActiveAnimations;
44 class CSSStyleDeclaration;
47 class CustomElementDefinition;
51 class ElementRareData;
55 class InputMethodContext;
58 class MutableStylePropertySet;
59 class PropertySetCSSStyleDeclaration;
62 class StylePropertySet;
64 enum AffectedSelectorType {
65 AffectedSelectorChecked = 1,
66 AffectedSelectorEnabled = 1 << 1,
67 AffectedSelectorDisabled = 1 << 2,
68 AffectedSelectorIndeterminate = 1 << 3,
69 AffectedSelectorLink = 1 << 4,
70 AffectedSelectorTarget = 1 << 5,
71 AffectedSelectorVisited = 1 << 6
73 typedef int AffectedSelectorMask;
75 enum SpellcheckAttributeState {
76 SpellcheckAttributeTrue,
77 SpellcheckAttributeFalse,
78 SpellcheckAttributeDefault
82 TabIndexWasSetExplicitly = 1 << 0,
83 StyleAffectedByEmpty = 1 << 1,
84 IsInCanvasSubtree = 1 << 2,
85 ContainsFullScreenElement = 1 << 3,
86 IsInTopLayer = 1 << 4,
87 HasPendingResources = 1 << 5,
89 NumberOfElementFlags = 6, // Required size of bitfield used to store the flags.
92 class Element : public ContainerNode {
94 static PassRefPtrWillBeRawPtr<Element> create(const QualifiedName&, Document*);
97 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecopy);
98 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecut);
99 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforepaste);
100 DEFINE_ATTRIBUTE_EVENT_LISTENER(copy);
101 DEFINE_ATTRIBUTE_EVENT_LISTENER(cut);
102 DEFINE_ATTRIBUTE_EVENT_LISTENER(paste);
103 DEFINE_ATTRIBUTE_EVENT_LISTENER(search);
104 DEFINE_ATTRIBUTE_EVENT_LISTENER(selectstart);
105 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchcancel);
106 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchend);
107 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchmove);
108 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchstart);
109 DEFINE_ATTRIBUTE_EVENT_LISTENER(wheel);
111 bool hasAttribute(const QualifiedName&) const;
112 const AtomicString& getAttribute(const QualifiedName&) const;
114 // Passing nullAtom as the second parameter removes the attribute when calling either of these set methods.
115 void setAttribute(const QualifiedName&, const AtomicString& value);
116 void setSynchronizedLazyAttribute(const QualifiedName&, const AtomicString& value);
118 void removeAttribute(const QualifiedName&);
120 // Typed getters and setters for language bindings.
121 int getIntegralAttribute(const QualifiedName& attributeName) const;
122 void setIntegralAttribute(const QualifiedName& attributeName, int value);
123 unsigned getUnsignedIntegralAttribute(const QualifiedName& attributeName) const;
124 void setUnsignedIntegralAttribute(const QualifiedName& attributeName, unsigned value);
125 double getFloatingPointAttribute(const QualifiedName& attributeName, double fallbackValue = std::numeric_limits<double>::quiet_NaN()) const;
126 void setFloatingPointAttribute(const QualifiedName& attributeName, double value);
128 // Call this to get the value of an attribute that is known not to be the style
129 // attribute or one of the SVG animatable attributes.
130 bool fastHasAttribute(const QualifiedName&) const;
131 const AtomicString& fastGetAttribute(const QualifiedName&) const;
133 bool fastAttributeLookupAllowed(const QualifiedName&) const;
136 #ifdef DUMP_NODE_STATISTICS
137 bool hasNamedNodeMap() const;
139 bool hasAttributes() const;
141 bool hasAttribute(const AtomicString& name) const;
142 bool hasAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
144 const AtomicString& getAttribute(const AtomicString& name) const;
145 const AtomicString& getAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
147 void setAttribute(const AtomicString& name, const AtomicString& value, ExceptionState&);
148 static bool parseAttributeName(QualifiedName&, const AtomicString& namespaceURI, const AtomicString& qualifiedName, ExceptionState&);
149 void setAttributeNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, const AtomicString& value, ExceptionState&);
151 bool isIdAttributeName(const QualifiedName&) const;
152 const AtomicString& getIdAttribute() const;
153 void setIdAttribute(const AtomicString&);
155 const AtomicString& getNameAttribute() const;
156 const AtomicString& getClassAttribute() const;
158 bool shouldIgnoreAttributeCase() const;
160 // Call this to get the value of the id attribute for style resolution purposes.
161 // The value will already be lowercased if the document is in compatibility mode,
162 // so this function is not suitable for non-style uses.
163 const AtomicString& idForStyleResolution() const;
165 // This getter takes care of synchronizing all attributes before returning the
166 // AttributeCollection. If the Element has no attributes, an empty AttributeCollection
167 // will be returned. This is not a trivial getter and its return value should be cached
169 AttributeCollection attributes() const;
170 // This variant will not update the potentially invalid attributes. To be used when not interested
171 // in style attribute or one of the SVG animation attributes.
172 AttributeCollection attributesWithoutUpdate() const;
174 void scrollIntoView(bool alignToTop = true);
175 void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
182 // FIXME: Replace uses of offsetParent in the platform with calls
183 // to the render layer and merge offsetParentForBindings and offsetParent.
184 Element* offsetParentForBindings();
186 Element* offsetParent();
191 virtual int scrollLeft();
192 virtual int scrollTop();
193 virtual void setScrollLeft(int);
194 virtual void setScrollLeft(const Dictionary& scrollOptionsHorizontal, ExceptionState&);
195 virtual void setScrollTop(int);
196 virtual void setScrollTop(const Dictionary& scrollOptionsVertical, ExceptionState&);
197 virtual int scrollWidth();
198 virtual int scrollHeight();
200 IntRect boundsInRootViewSpace();
202 PassRefPtrWillBeRawPtr<ClientRectList> getClientRects();
203 PassRefPtrWillBeRawPtr<ClientRect> getBoundingClientRect();
205 // Returns the absolute bounding box translated into screen coordinates:
206 IntRect screenRect() const;
208 virtual void didMoveToNewDocument(Document&) OVERRIDE;
210 void removeAttribute(const AtomicString& name);
211 void removeAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName);
213 PassRefPtrWillBeRawPtr<Attr> detachAttribute(size_t index);
215 PassRefPtrWillBeRawPtr<Attr> getAttributeNode(const AtomicString& name);
216 PassRefPtrWillBeRawPtr<Attr> getAttributeNodeNS(const AtomicString& namespaceURI, const AtomicString& localName);
217 PassRefPtrWillBeRawPtr<Attr> setAttributeNode(Attr*, ExceptionState&);
218 PassRefPtrWillBeRawPtr<Attr> setAttributeNodeNS(Attr*, ExceptionState&);
219 PassRefPtrWillBeRawPtr<Attr> removeAttributeNode(Attr*, ExceptionState&);
221 PassRefPtrWillBeRawPtr<Attr> attrIfExists(const QualifiedName&);
222 PassRefPtrWillBeRawPtr<Attr> ensureAttr(const QualifiedName&);
224 WillBeHeapVector<RefPtrWillBeMember<Attr> >* attrNodeList();
226 CSSStyleDeclaration* style();
228 const QualifiedName& tagQName() const { return m_tagName; }
229 String tagName() const { return nodeName(); }
231 bool hasTagName(const QualifiedName& tagName) const { return m_tagName.matches(tagName); }
232 bool hasTagName(const HTMLQualifiedName& tagName) const { return ContainerNode::hasTagName(tagName); }
233 bool hasTagName(const SVGQualifiedName& tagName) const { return ContainerNode::hasTagName(tagName); }
235 // Should be called only by Document::createElementNS to fix up m_tagName immediately after construction.
236 void setTagNameForCreateElementNS(const QualifiedName&);
238 // A fast function for checking the local name against another atomic string.
239 bool hasLocalName(const AtomicString& other) const { return m_tagName.localName() == other; }
241 virtual const AtomicString& localName() const OVERRIDE FINAL { return m_tagName.localName(); }
242 const AtomicString& prefix() const { return m_tagName.prefix(); }
243 virtual const AtomicString& namespaceURI() const OVERRIDE FINAL { return m_tagName.namespaceURI(); }
245 const AtomicString& locateNamespacePrefix(const AtomicString& namespaceURI) const;
247 virtual KURL baseURI() const OVERRIDE FINAL;
249 virtual String nodeName() const OVERRIDE;
251 PassRefPtrWillBeRawPtr<Element> cloneElementWithChildren();
252 PassRefPtrWillBeRawPtr<Element> cloneElementWithoutChildren();
254 void scheduleSVGFilterLayerUpdateHack();
256 void normalizeAttributes();
258 void setBooleanAttribute(const QualifiedName& name, bool);
260 virtual const StylePropertySet* additionalPresentationAttributeStyle() { return 0; }
261 void invalidateStyleAttribute();
263 const StylePropertySet* inlineStyle() const { return elementData() ? elementData()->m_inlineStyle.get() : 0; }
265 bool setInlineStyleProperty(CSSPropertyID, CSSValueID identifier, bool important = false);
266 bool setInlineStyleProperty(CSSPropertyID, double value, CSSPrimitiveValue::UnitType, bool important = false);
267 bool setInlineStyleProperty(CSSPropertyID, const String& value, bool important = false);
268 bool removeInlineStyleProperty(CSSPropertyID);
269 void removeAllInlineStyleProperties();
271 void synchronizeStyleAttributeInternal() const;
273 const StylePropertySet* presentationAttributeStyle();
274 virtual bool isPresentationAttribute(const QualifiedName&) const { return false; }
275 virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) { }
277 // For exposing to DOM only.
278 NamedNodeMap* attributesForBindings() const;
280 enum AttributeModificationReason {
285 // This method is called whenever an attribute is added, changed or removed.
286 virtual void attributeWillChange(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue) { }
287 virtual void attributeChanged(const QualifiedName&, const AtomicString&, AttributeModificationReason = ModifiedDirectly);
288 virtual void parseAttribute(const QualifiedName&, const AtomicString&);
290 virtual bool hasLegalLinkAttribute(const QualifiedName&) const;
291 virtual const QualifiedName& subResourceAttributeName() const;
293 // Only called by the parser immediately after element construction.
294 void parserSetAttributes(const Vector<Attribute>&);
296 // Remove attributes that might introduce scripting from the vector leaving the element unchanged.
297 void stripScriptingAttributes(Vector<Attribute>&) const;
299 bool sharesSameElementData(const Element& other) const { return elementData() == other.elementData(); }
301 // Clones attributes only.
302 void cloneAttributesFromElement(const Element&);
304 // Clones all attribute-derived data, including subclass specifics (through copyNonAttributeProperties.)
305 void cloneDataFromElement(const Element&);
307 bool hasEquivalentAttributes(const Element* other) const;
309 virtual void copyNonAttributePropertiesFromElement(const Element&) { }
311 virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
312 virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
313 virtual RenderObject* createRenderer(RenderStyle*);
314 virtual bool rendererIsNeeded(const RenderStyle&);
315 void recalcStyle(StyleRecalcChange, Text* nextTextSibling = 0);
316 void didAffectSelector(AffectedSelectorMask);
317 void setAnimationStyleChange(bool);
318 void setNeedsAnimationStyleRecalc();
320 void setNeedsCompositingUpdate();
322 bool supportsStyleSharing() const;
324 ElementShadow* shadow() const;
325 ElementShadow& ensureShadow();
326 PassRefPtrWillBeRawPtr<ShadowRoot> createShadowRoot(ExceptionState&);
327 ShadowRoot* shadowRoot() const;
328 ShadowRoot* youngestShadowRoot() const;
330 bool hasAuthorShadowRoot() const { return shadowRoot(); }
331 ShadowRoot* userAgentShadowRoot() const;
332 ShadowRoot& ensureUserAgentShadowRoot();
333 virtual void willAddFirstAuthorShadowRoot() { }
335 bool isInDescendantTreeOf(const Element* shadowHost) const;
337 RenderStyle* computedStyle(PseudoId = NOPSEUDO);
339 // Methods for indicating the style is affected by dynamic updates (e.g., children changing, our position changing in our sibling list, etc.)
340 bool styleAffectedByEmpty() const { return hasElementFlag(StyleAffectedByEmpty); }
341 void setStyleAffectedByEmpty() { setElementFlag(StyleAffectedByEmpty); }
343 void setIsInCanvasSubtree(bool value) { setElementFlag(IsInCanvasSubtree, value); }
344 bool isInCanvasSubtree() const { return hasElementFlag(IsInCanvasSubtree); }
346 bool isUpgradedCustomElement() { return customElementState() == Upgraded; }
347 bool isUnresolvedCustomElement() { return customElementState() == WaitingForUpgrade; }
349 AtomicString computeInheritedLanguage() const;
350 Locale& locale() const;
352 virtual void accessKeyAction(bool /*sendToAnyEvent*/) { }
354 virtual bool isURLAttribute(const Attribute&) const { return false; }
355 virtual bool isHTMLContentAttribute(const Attribute&) const { return false; }
357 virtual bool isLiveLink() const { return false; }
358 KURL hrefURL() const;
360 KURL getURLAttribute(const QualifiedName&) const;
361 KURL getNonEmptyURLAttribute(const QualifiedName&) const;
363 virtual const AtomicString imageSourceURL() const;
364 virtual Image* imageContents() { return 0; }
366 virtual void focus(bool restorePreviousSelection = true, FocusType = FocusTypeNone);
367 virtual void updateFocusAppearance(bool restorePreviousSelection);
369 // Whether this element can receive focus at all. Most elements are not
370 // focusable but some elements, such as form controls and links, are. Unlike
371 // rendererIsFocusable(), this method may be called when layout is not up to
372 // date, so it must not use the renderer to determine focusability.
373 virtual bool supportsFocus() const;
374 // Whether the node can actually be focused.
375 bool isFocusable() const;
376 virtual bool isKeyboardFocusable() const;
377 virtual bool isMouseFocusable() const;
378 virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType);
379 virtual void dispatchBlurEvent(Element* newFocusedElement);
380 void dispatchFocusInEvent(const AtomicString& eventType, Element* oldFocusedElement);
381 void dispatchFocusOutEvent(const AtomicString& eventType, Element* newFocusedElement);
385 String innerHTML() const;
386 String outerHTML() const;
387 void setInnerHTML(const String&, ExceptionState&);
388 void setOuterHTML(const String&, ExceptionState&);
390 Element* insertAdjacentElement(const String& where, Element* newChild, ExceptionState&);
391 void insertAdjacentText(const String& where, const String& text, ExceptionState&);
392 void insertAdjacentHTML(const String& where, const String& html, ExceptionState&);
394 String textFromChildren();
396 virtual String title() const { return String(); }
398 virtual const AtomicString& shadowPseudoId() const;
399 void setShadowPseudoId(const AtomicString&);
401 LayoutSize minimumSizeForResizing() const;
402 void setMinimumSizeForResizing(const LayoutSize&);
404 virtual void didBecomeFullscreenElement() { }
405 virtual void willStopBeingFullscreenElement() { }
407 // Called by the parser when this element's close tag is reached,
408 // signaling that all child tags have been parsed and added.
409 // This is needed for <applet> and <object> elements, which can't lay themselves out
410 // until they know all of their nested <param>s. [Radar 3603191, 4040848].
411 // Also used for script elements and some SVG elements for similar purposes,
412 // but making parsing a special case in this respect should be avoided if possible.
413 virtual void finishParsingChildren();
415 void beginParsingChildren() { setIsFinishedParsingChildren(false); }
417 PseudoElement* pseudoElement(PseudoId) const;
418 RenderObject* pseudoElementRenderer(PseudoId) const;
420 virtual bool matchesReadOnlyPseudoClass() const { return false; }
421 virtual bool matchesReadWritePseudoClass() const { return false; }
422 bool matches(const String& selectors, ExceptionState&);
423 virtual bool shouldAppearIndeterminate() const { return false; }
425 DOMTokenList& classList();
427 DOMStringMap& dataset();
429 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
430 virtual bool isDateTimeEditElement() const { return false; }
431 virtual bool isDateTimeFieldElement() const { return false; }
432 virtual bool isPickerIndicatorElement() const { return false; }
435 virtual bool isFormControlElement() const { return false; }
436 virtual bool isSpinButtonElement() const { return false; }
437 virtual bool isTextFormControl() const { return false; }
438 virtual bool isOptionalFormControl() const { return false; }
439 virtual bool isRequiredFormControl() const { return false; }
440 virtual bool isDefaultButtonForForm() const { return false; }
441 virtual bool willValidate() const { return false; }
442 virtual bool isValidFormControlElement() { return false; }
443 virtual bool isInRange() const { return false; }
444 virtual bool isOutOfRange() const { return false; }
445 virtual bool isClearButtonElement() const { return false; }
447 virtual bool canContainRangeEndPoint() const OVERRIDE { return true; }
449 // Used for disabled form elements; if true, prevents mouse events from being dispatched
450 // to event listeners, and prevents DOMActivate events from being sent at all.
451 virtual bool isDisabledFormControl() const { return false; }
453 bool hasPendingResources() const { return hasElementFlag(HasPendingResources); }
454 void setHasPendingResources() { setElementFlag(HasPendingResources); }
455 void clearHasPendingResources() { clearElementFlag(HasPendingResources); }
456 virtual void buildPendingResource() { };
458 void setCustomElementDefinition(PassRefPtr<CustomElementDefinition>);
459 CustomElementDefinition* customElementDefinition() const;
461 bool containsFullScreenElement() const { return hasElementFlag(ContainsFullScreenElement); }
462 void setContainsFullScreenElement(bool);
463 void setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(bool);
465 bool isInTopLayer() const { return hasElementFlag(IsInTopLayer); }
466 void setIsInTopLayer(bool);
468 void requestPointerLock();
470 bool isSpellCheckingEnabled() const;
472 // FIXME: public for RenderTreeBuilder, we shouldn't expose this though.
473 PassRefPtr<RenderStyle> styleForRenderer();
476 bool hasClass() const;
477 const SpaceSplitString& classNames() const;
479 IntSize savedLayerScrollOffset() const;
480 void setSavedLayerScrollOffset(const IntSize&);
482 ActiveAnimations* activeAnimations() const;
483 ActiveAnimations& ensureActiveAnimations();
484 bool hasActiveAnimations() const;
486 InputMethodContext& inputMethodContext();
487 bool hasInputMethodContext() const;
489 void setPrefix(const AtomicString&, ExceptionState&);
491 void synchronizeAttribute(const AtomicString& localName) const;
493 MutableStylePropertySet& ensureMutableInlineStyle();
494 void clearMutableInlineStyleIfEmpty();
496 void setTabIndex(int);
497 virtual short tabIndex() const OVERRIDE;
499 virtual void trace(Visitor*) OVERRIDE;
502 Element(const QualifiedName& tagName, Document*, ConstructionType);
504 const ElementData* elementData() const { return m_elementData.get(); }
505 UniqueElementData& ensureUniqueElementData();
507 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, CSSValueID identifier);
508 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, double value, CSSPrimitiveValue::UnitType);
509 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, const String& value);
511 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
512 virtual void removedFrom(ContainerNode*) OVERRIDE;
513 virtual void childrenChanged(const ChildrenChange&) OVERRIDE;
515 virtual void willRecalcStyle(StyleRecalcChange);
516 virtual void didRecalcStyle(StyleRecalcChange);
517 virtual PassRefPtr<RenderStyle> customStyleForRenderer();
519 virtual bool shouldRegisterAsNamedItem() const { return false; }
520 virtual bool shouldRegisterAsExtraNamedItem() const { return false; }
522 bool supportsSpatialNavigationFocus() const;
524 void clearTabIndexExplicitlyIfNeeded();
525 void setTabIndexExplicitly(short);
526 // Subclasses may override this method to affect focusability. Unlike
527 // supportsFocus, this method must be called on an up-to-date layout, so it
528 // may use the renderer to reason about focusability. This method cannot be
529 // moved to RenderObject because some focusable nodes don't have renderers,
530 // e.g., HTMLOptionElement.
531 virtual bool rendererIsFocusable() const;
533 // classAttributeChanged() exists to share code between
534 // parseAttribute (called via setAttribute()) and
535 // svgAttributeChanged (called when element.className.baseValue is set)
536 void classAttributeChanged(const AtomicString& newClassString);
538 PassRefPtr<RenderStyle> originalStyleForRenderer();
540 Node* insertAdjacent(const String& where, Node* newChild, ExceptionState&);
543 bool hasElementFlag(ElementFlags mask) const { return hasRareData() && hasElementFlagInternal(mask); }
544 void setElementFlag(ElementFlags, bool value = true);
545 void clearElementFlag(ElementFlags);
546 bool hasElementFlagInternal(ElementFlags) const;
548 bool isElementNode() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
549 bool isDocumentFragment() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
550 bool isDocumentNode() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
552 void styleAttributeChanged(const AtomicString& newStyleString, AttributeModificationReason);
554 void updatePresentationAttributeStyle();
556 void inlineStyleChanged();
557 PropertySetCSSStyleDeclaration* inlineStyleCSSOMWrapper();
558 void setInlineStyleFromString(const AtomicString&);
560 StyleRecalcChange recalcOwnStyle(StyleRecalcChange);
561 void recalcChildStyle(StyleRecalcChange);
563 inline void checkForEmptyStyleChange();
565 void updatePseudoElement(PseudoId, StyleRecalcChange);
567 inline void createPseudoElementIfNeeded(PseudoId);
569 // FIXME: Everyone should allow author shadows.
570 virtual bool areAuthorShadowsAllowed() const { return true; }
571 virtual void didAddUserAgentShadowRoot(ShadowRoot&) { }
572 virtual bool alwaysCreateUserAgentShadowRoot() const { return false; }
574 // FIXME: Remove the need for Attr to call willModifyAttribute/didModifyAttribute.
577 enum SynchronizationOfLazyAttribute { NotInSynchronizationOfLazyAttribute = 0, InSynchronizationOfLazyAttribute };
579 void didAddAttribute(const QualifiedName&, const AtomicString&);
580 void willModifyAttribute(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue);
581 void didModifyAttribute(const QualifiedName&, const AtomicString&);
582 void didRemoveAttribute(const QualifiedName&);
584 void synchronizeAllAttributes() const;
585 void synchronizeAttribute(const QualifiedName&) const;
587 void updateId(const AtomicString& oldId, const AtomicString& newId);
588 void updateId(TreeScope&, const AtomicString& oldId, const AtomicString& newId);
589 void updateName(const AtomicString& oldName, const AtomicString& newName);
591 virtual NodeType nodeType() const OVERRIDE FINAL;
592 virtual bool childTypeAllowed(NodeType) const OVERRIDE FINAL;
594 void setAttributeInternal(size_t index, const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
595 void appendAttributeInternal(const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
596 void removeAttributeInternal(size_t index, SynchronizationOfLazyAttribute);
597 void attributeChangedFromParserOrByCloning(const QualifiedName&, const AtomicString&, AttributeModificationReason);
600 virtual void formatForDebugger(char* buffer, unsigned length) const OVERRIDE;
603 bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
605 void cancelFocusAppearanceUpdate();
607 virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) OVERRIDE { return computedStyle(pseudoElementSpecifier); }
609 inline void updateCallbackSelectors(RenderStyle* oldStyle, RenderStyle* newStyle);
610 inline void removeCallbackSelectors();
611 inline void addCallbackSelectors();
613 // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
615 virtual PassRefPtrWillBeRawPtr<Node> cloneNode(bool deep) OVERRIDE;
616 virtual PassRefPtrWillBeRawPtr<Element> cloneElementWithoutAttributesAndChildren();
618 QualifiedName m_tagName;
620 SpellcheckAttributeState spellcheckAttributeState() const;
622 void updateNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
623 void updateExtraNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
625 void createUniqueElementData();
627 bool shouldInvalidateDistributionWhenAttributeChanged(ElementShadow*, const QualifiedName&, const AtomicString&);
629 ElementRareData* elementRareData() const;
630 ElementRareData& ensureElementRareData();
632 WillBeHeapVector<RefPtrWillBeMember<Attr> >& ensureAttrNodeList();
633 void removeAttrNodeList();
634 void detachAllAttrNodesFromElement();
635 void detachAttrNodeFromElementWithValue(Attr*, const AtomicString& value);
636 void detachAttrNodeAtIndex(Attr*, size_t index);
638 bool isJavaScriptURLAttribute(const Attribute&) const;
640 RefPtrWillBeMember<ElementData> m_elementData;
643 DEFINE_NODE_TYPE_CASTS(Element, isElementNode());
644 template <typename T> bool isElementOfType(const Node&);
645 template <> inline bool isElementOfType<const Element>(const Node& node) { return node.isElementNode(); }
646 template <typename T> inline bool isElementOfType(const Element& element) { return isElementOfType<T>(static_cast<const Node&>(element)); }
647 template <> inline bool isElementOfType<const Element>(const Element&) { return true; }
650 template<typename T> inline T& toElement(Node& node)
652 ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
653 return static_cast<T&>(node);
655 template<typename T> inline T* toElement(Node* node)
657 ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
658 return static_cast<T*>(node);
660 template<typename T> inline const T& toElement(const Node& node)
662 ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
663 return static_cast<const T&>(node);
665 template<typename T> inline const T* toElement(const Node* node)
667 ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
668 return static_cast<const T*>(node);
670 template<typename T, typename U> inline T* toElement(const RefPtr<U>& node) { return toElement<T>(node.get()); }
672 inline bool isDisabledFormControl(const Node* node)
674 return node->isElementNode() && toElement(node)->isDisabledFormControl();
677 inline Element* Node::parentElement() const
679 ContainerNode* parent = parentNode();
680 return parent && parent->isElementNode() ? toElement(parent) : 0;
683 inline bool Element::fastHasAttribute(const QualifiedName& name) const
685 ASSERT(fastAttributeLookupAllowed(name));
686 return elementData() && elementData()->attributes().findIndex(name) != kNotFound;
689 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
691 ASSERT(fastAttributeLookupAllowed(name));
693 if (const Attribute* attribute = elementData()->attributes().find(name))
694 return attribute->value();
699 inline AttributeCollection Element::attributes() const
702 return AttributeCollection();
703 synchronizeAllAttributes();
704 return elementData()->attributes();
707 inline AttributeCollection Element::attributesWithoutUpdate() const
710 return AttributeCollection();
711 return elementData()->attributes();
714 inline bool Element::hasAttributes() const
716 return !attributes().isEmpty();
719 inline const AtomicString& Element::idForStyleResolution() const
722 return elementData()->idForStyleResolution();
725 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
727 // FIXME: This check is probably not correct for the case where the document has an id attribute
728 // with a non-null namespace, because it will return false, a false negative, if the prefixes
729 // don't match but the local name and namespace both do. However, since this has been like this
730 // for a while and the code paths may be hot, we'll have to measure performance if we fix it.
731 return attributeName == HTMLNames::idAttr;
734 inline const AtomicString& Element::getIdAttribute() const
736 return hasID() ? fastGetAttribute(HTMLNames::idAttr) : nullAtom;
739 inline const AtomicString& Element::getNameAttribute() const
741 return hasName() ? fastGetAttribute(HTMLNames::nameAttr) : nullAtom;
744 inline const AtomicString& Element::getClassAttribute() const
749 return getAttribute(HTMLNames::classAttr);
750 return fastGetAttribute(HTMLNames::classAttr);
753 inline void Element::setIdAttribute(const AtomicString& value)
755 setAttribute(HTMLNames::idAttr, value);
758 inline const SpaceSplitString& Element::classNames() const
761 ASSERT(elementData());
762 return elementData()->classNames();
765 inline bool Element::hasID() const
767 return elementData() && elementData()->hasID();
770 inline bool Element::hasClass() const
772 return elementData() && elementData()->hasClass();
775 inline UniqueElementData& Element::ensureUniqueElementData()
777 if (!elementData() || !elementData()->isUnique())
778 createUniqueElementData();
779 return toUniqueElementData(*m_elementData);
782 inline Node::InsertionNotificationRequest Node::insertedInto(ContainerNode* insertionPoint)
784 ASSERT(!childNeedsStyleInvalidation());
785 ASSERT(!needsStyleInvalidation());
786 ASSERT(insertionPoint->inDocument() || isContainerNode());
787 if (insertionPoint->inDocument())
788 setFlag(InDocumentFlag);
789 if (parentOrShadowHostNode()->isInShadowTree())
790 setFlag(IsInShadowTreeFlag);
791 if (childNeedsDistributionRecalc() && !insertionPoint->childNeedsDistributionRecalc())
792 insertionPoint->markAncestorsWithChildNeedsDistributionRecalc();
793 return InsertionDone;
796 inline void Node::removedFrom(ContainerNode* insertionPoint)
798 ASSERT(insertionPoint->inDocument() || isContainerNode());
799 if (insertionPoint->inDocument())
800 clearFlag(InDocumentFlag);
801 if (isInShadowTree() && !treeScope().rootNode().isShadowRoot())
802 clearFlag(IsInShadowTreeFlag);
805 inline void Element::invalidateStyleAttribute()
807 ASSERT(elementData());
808 elementData()->m_styleAttributeIsDirty = true;
811 inline const StylePropertySet* Element::presentationAttributeStyle()
815 if (elementData()->m_presentationAttributeStyleIsDirty)
816 updatePresentationAttributeStyle();
817 // Need to call elementData() again since updatePresentationAttributeStyle()
818 // might swap it with a UniqueElementData.
819 return elementData()->presentationAttributeStyle();
822 inline void Element::setTagNameForCreateElementNS(const QualifiedName& tagName)
824 // We expect this method to be called only to reset the prefix.
825 ASSERT(tagName.localName() == m_tagName.localName());
826 ASSERT(tagName.namespaceURI() == m_tagName.namespaceURI());
830 inline bool isShadowHost(const Node* node)
832 return node && node->isElementNode() && toElement(node)->shadow();
835 inline bool isShadowHost(const Element* element)
837 return element && element->shadow();
840 // These macros do the same as their NODE equivalents but additionally provide a template specialization
841 // for isElementOfType<>() so that the Traversal<> API works for these Element types.
842 #define DEFINE_ELEMENT_TYPE_CASTS(thisType, predicate) \
843 template <> inline bool isElementOfType<const thisType>(const Node& node) { return node.predicate; } \
844 DEFINE_NODE_TYPE_CASTS(thisType, predicate)
846 #define DEFINE_ELEMENT_TYPE_CASTS_WITH_FUNCTION(thisType) \
847 template <> inline bool isElementOfType<const thisType>(const Node& node) { return is##thisType(node); } \
848 DEFINE_NODE_TYPE_CASTS_WITH_FUNCTION(thisType)
850 #define DECLARE_ELEMENT_FACTORY_WITH_TAGNAME(T) \
851 static PassRefPtrWillBeRawPtr<T> create(const QualifiedName&, Document&)
852 #define DEFINE_ELEMENT_FACTORY_WITH_TAGNAME(T) \
853 PassRefPtrWillBeRawPtr<T> T::create(const QualifiedName& tagName, Document& document) \
855 return adoptRefWillBeNoop(new T(tagName, document)); \