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, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013 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"
38 #include "platform/scroll/ScrollTypes.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 AffectedSelectorType {
66 AffectedSelectorChecked = 1,
67 AffectedSelectorEnabled = 1 << 1,
68 AffectedSelectorDisabled = 1 << 2,
69 AffectedSelectorIndeterminate = 1 << 3,
70 AffectedSelectorLink = 1 << 4,
71 AffectedSelectorTarget = 1 << 5,
72 AffectedSelectorVisited = 1 << 6
74 typedef int AffectedSelectorMask;
76 enum SpellcheckAttributeState {
77 SpellcheckAttributeTrue,
78 SpellcheckAttributeFalse,
79 SpellcheckAttributeDefault
83 TabIndexWasSetExplicitly = 1 << 0,
84 StyleAffectedByEmpty = 1 << 1,
85 IsInCanvasSubtree = 1 << 2,
86 ContainsFullScreenElement = 1 << 3,
87 IsInTopLayer = 1 << 4,
88 HasPendingResources = 1 << 5,
90 NumberOfElementFlags = 6, // Required size of bitfield used to store the flags.
93 class Element : public ContainerNode {
95 static PassRefPtrWillBeRawPtr<Element> create(const QualifiedName&, Document*);
98 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecopy);
99 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecut);
100 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforepaste);
101 DEFINE_ATTRIBUTE_EVENT_LISTENER(copy);
102 DEFINE_ATTRIBUTE_EVENT_LISTENER(cut);
103 DEFINE_ATTRIBUTE_EVENT_LISTENER(paste);
104 DEFINE_ATTRIBUTE_EVENT_LISTENER(search);
105 DEFINE_ATTRIBUTE_EVENT_LISTENER(selectstart);
106 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchcancel);
107 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchend);
108 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchmove);
109 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchstart);
110 DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitfullscreenchange);
111 DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitfullscreenerror);
112 DEFINE_ATTRIBUTE_EVENT_LISTENER(wheel);
114 bool hasAttribute(const QualifiedName&) const;
115 const AtomicString& getAttribute(const QualifiedName&) const;
117 // Passing nullAtom as the second parameter removes the attribute when calling either of these set methods.
118 void setAttribute(const QualifiedName&, const AtomicString& value);
119 void setSynchronizedLazyAttribute(const QualifiedName&, const AtomicString& value);
121 void removeAttribute(const QualifiedName&);
123 // Typed getters and setters for language bindings.
124 int getIntegralAttribute(const QualifiedName& attributeName) const;
125 void setIntegralAttribute(const QualifiedName& attributeName, int value);
126 unsigned getUnsignedIntegralAttribute(const QualifiedName& attributeName) const;
127 void setUnsignedIntegralAttribute(const QualifiedName& attributeName, unsigned value);
128 double getFloatingPointAttribute(const QualifiedName& attributeName, double fallbackValue = std::numeric_limits<double>::quiet_NaN()) const;
129 void setFloatingPointAttribute(const QualifiedName& attributeName, double value);
131 // Call this to get the value of an attribute that is known not to be the style
132 // attribute or one of the SVG animatable attributes.
133 bool fastHasAttribute(const QualifiedName&) const;
134 const AtomicString& fastGetAttribute(const QualifiedName&) const;
136 bool fastAttributeLookupAllowed(const QualifiedName&) const;
139 #ifdef DUMP_NODE_STATISTICS
140 bool hasNamedNodeMap() const;
142 bool hasAttributes() const;
143 // This variant will not update the potentially invalid attributes. To be used when not interested
144 // in style attribute or one of the SVG animation attributes.
145 bool hasAttributesWithoutUpdate() const;
147 bool hasAttribute(const AtomicString& name) const;
148 bool hasAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
150 const AtomicString& getAttribute(const AtomicString& name) const;
151 const AtomicString& getAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
153 void setAttribute(const AtomicString& name, const AtomicString& value, ExceptionState&);
154 static bool parseAttributeName(QualifiedName&, const AtomicString& namespaceURI, const AtomicString& qualifiedName, ExceptionState&);
155 void setAttributeNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, const AtomicString& value, ExceptionState&);
157 bool isIdAttributeName(const QualifiedName&) const;
158 const AtomicString& getIdAttribute() const;
159 void setIdAttribute(const AtomicString&);
161 const AtomicString& getNameAttribute() const;
162 const AtomicString& getClassAttribute() const;
164 bool shouldIgnoreAttributeCase() const;
166 // Call this to get the value of the id attribute for style resolution purposes.
167 // The value will already be lowercased if the document is in compatibility mode,
168 // so this function is not suitable for non-style uses.
169 const AtomicString& idForStyleResolution() const;
171 // Internal methods that assume the existence of attribute storage, one should use hasAttributes()
172 // before calling them. This is not a trivial getter and its return value should be cached for
174 AttributeCollection attributes() const { return elementData()->attributes(); }
175 size_t attributeCount() const;
176 const Attribute& attributeAt(unsigned index) const;
177 const Attribute* findAttributeByName(const QualifiedName&) const;
178 size_t findAttributeIndexByName(const QualifiedName& name) const { return elementData()->findAttributeIndexByName(name); }
179 size_t findAttributeIndexByName(const AtomicString& name, bool shouldIgnoreAttributeCase) const { return elementData()->findAttributeIndexByName(name, shouldIgnoreAttributeCase); }
181 void scrollIntoView(bool alignToTop = true);
182 void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
184 void scrollByLines(int lines);
185 void scrollByPages(int pages);
192 // FIXME: Replace uses of offsetParent in the platform with calls
193 // to the render layer and merge offsetParentForBindings and offsetParent.
194 Element* offsetParentForBindings();
196 Element* offsetParent();
201 virtual int scrollLeft();
202 virtual int scrollTop();
203 virtual void setScrollLeft(int);
204 virtual void setScrollLeft(const Dictionary& scrollOptionsHorizontal, ExceptionState&);
205 virtual void setScrollTop(int);
206 virtual void setScrollTop(const Dictionary& scrollOptionsVertical, ExceptionState&);
207 virtual int scrollWidth();
208 virtual int scrollHeight();
210 IntRect boundsInRootViewSpace();
212 PassRefPtrWillBeRawPtr<ClientRectList> getClientRects();
213 PassRefPtrWillBeRawPtr<ClientRect> getBoundingClientRect();
215 // Returns the absolute bounding box translated into screen coordinates:
216 IntRect screenRect() const;
218 virtual void didMoveToNewDocument(Document&) OVERRIDE;
220 void removeAttribute(const AtomicString& name);
221 void removeAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName);
223 PassRefPtrWillBeRawPtr<Attr> detachAttribute(size_t index);
225 PassRefPtrWillBeRawPtr<Attr> getAttributeNode(const AtomicString& name);
226 PassRefPtrWillBeRawPtr<Attr> getAttributeNodeNS(const AtomicString& namespaceURI, const AtomicString& localName);
227 PassRefPtrWillBeRawPtr<Attr> setAttributeNode(Attr*, ExceptionState&);
228 PassRefPtrWillBeRawPtr<Attr> setAttributeNodeNS(Attr*, ExceptionState&);
229 PassRefPtrWillBeRawPtr<Attr> removeAttributeNode(Attr*, ExceptionState&);
231 PassRefPtrWillBeRawPtr<Attr> attrIfExists(const QualifiedName&);
232 PassRefPtrWillBeRawPtr<Attr> ensureAttr(const QualifiedName&);
234 WillBeHeapVector<RefPtrWillBeMember<Attr> >* attrNodeList();
236 CSSStyleDeclaration* style();
238 const QualifiedName& tagQName() const { return m_tagName; }
239 String tagName() const { return nodeName(); }
240 bool hasTagName(const QualifiedName& tagName) const { return m_tagName.matches(tagName); }
242 // Should be called only by Document::createElementNS to fix up m_tagName immediately after construction.
243 void setTagNameForCreateElementNS(const QualifiedName&);
245 // A fast function for checking the local name against another atomic string.
246 bool hasLocalName(const AtomicString& other) const { return m_tagName.localName() == other; }
247 bool hasLocalName(const QualifiedName& other) const { return m_tagName.localName() == other.localName(); }
249 virtual const AtomicString& localName() const OVERRIDE FINAL { return m_tagName.localName(); }
250 const AtomicString& prefix() const { return m_tagName.prefix(); }
251 virtual const AtomicString& namespaceURI() const OVERRIDE FINAL { return m_tagName.namespaceURI(); }
253 const AtomicString& locateNamespacePrefix(const AtomicString& namespaceURI) const;
255 virtual KURL baseURI() const OVERRIDE FINAL;
257 virtual String nodeName() const OVERRIDE;
259 PassRefPtrWillBeRawPtr<Element> cloneElementWithChildren();
260 PassRefPtrWillBeRawPtr<Element> cloneElementWithoutChildren();
262 void scheduleSVGFilterLayerUpdateHack();
264 void normalizeAttributes();
266 void setBooleanAttribute(const QualifiedName& name, bool);
268 virtual const StylePropertySet* additionalPresentationAttributeStyle() { return 0; }
269 void invalidateStyleAttribute();
271 const StylePropertySet* inlineStyle() const { return elementData() ? elementData()->m_inlineStyle.get() : 0; }
273 bool setInlineStyleProperty(CSSPropertyID, CSSValueID identifier, bool important = false);
274 bool setInlineStyleProperty(CSSPropertyID, CSSPropertyID identifier, bool important = false);
275 bool setInlineStyleProperty(CSSPropertyID, double value, CSSPrimitiveValue::UnitType, bool important = false);
276 bool setInlineStyleProperty(CSSPropertyID, const String& value, bool important = false);
277 bool removeInlineStyleProperty(CSSPropertyID);
278 void removeAllInlineStyleProperties();
280 void synchronizeStyleAttributeInternal() const;
282 const StylePropertySet* presentationAttributeStyle();
283 virtual bool isPresentationAttribute(const QualifiedName&) const { return false; }
284 virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) { }
286 // For exposing to DOM only.
287 NamedNodeMap* attributesForBindings() const;
289 enum AttributeModificationReason {
294 // This method is called whenever an attribute is added, changed or removed.
295 virtual void attributeChanged(const QualifiedName&, const AtomicString&, AttributeModificationReason = ModifiedDirectly);
296 virtual void parseAttribute(const QualifiedName&, const AtomicString&);
298 virtual bool hasLegalLinkAttribute(const QualifiedName&) const;
299 virtual const QualifiedName& subResourceAttributeName() const;
301 // Only called by the parser immediately after element construction.
302 void parserSetAttributes(const Vector<Attribute>&);
304 // Remove attributes that might introduce scripting from the vector leaving the element unchanged.
305 void stripScriptingAttributes(Vector<Attribute>&) const;
307 const ElementData* elementData() const { return m_elementData.get(); }
308 UniqueElementData& ensureUniqueElementData();
310 void synchronizeAllAttributes() const;
312 // Clones attributes only.
313 void cloneAttributesFromElement(const Element&);
315 // Clones all attribute-derived data, including subclass specifics (through copyNonAttributeProperties.)
316 void cloneDataFromElement(const Element&);
318 bool hasEquivalentAttributes(const Element* other) const;
320 virtual void copyNonAttributePropertiesFromElement(const Element&) { }
322 virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
323 virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
324 virtual RenderObject* createRenderer(RenderStyle*);
325 virtual bool rendererIsNeeded(const RenderStyle&);
326 void recalcStyle(StyleRecalcChange, Text* nextTextSibling = 0);
327 void didAffectSelector(AffectedSelectorMask);
328 void setAnimationStyleChange(bool);
329 void setNeedsAnimationStyleRecalc();
331 void setNeedsCompositingUpdate();
333 bool supportsStyleSharing() const;
335 ElementShadow* shadow() const;
336 ElementShadow& ensureShadow();
337 PassRefPtrWillBeRawPtr<ShadowRoot> createShadowRoot(ExceptionState&);
338 ShadowRoot* shadowRoot() const;
339 ShadowRoot* youngestShadowRoot() const;
341 bool hasAuthorShadowRoot() const { return shadowRoot(); }
342 ShadowRoot* userAgentShadowRoot() const;
343 ShadowRoot& ensureUserAgentShadowRoot();
344 virtual void willAddFirstAuthorShadowRoot() { }
346 bool isInDescendantTreeOf(const Element* shadowHost) const;
348 RenderStyle* computedStyle(PseudoId = NOPSEUDO);
350 // Methods for indicating the style is affected by dynamic updates (e.g., children changing, our position changing in our sibling list, etc.)
351 bool styleAffectedByEmpty() const { return hasElementFlag(StyleAffectedByEmpty); }
352 void setStyleAffectedByEmpty() { setElementFlag(StyleAffectedByEmpty); }
354 void setIsInCanvasSubtree(bool value) { setElementFlag(IsInCanvasSubtree, value); }
355 bool isInCanvasSubtree() const { return hasElementFlag(IsInCanvasSubtree); }
357 bool isUpgradedCustomElement() { return customElementState() == Upgraded; }
358 bool isUnresolvedCustomElement() { return customElementState() == WaitingForUpgrade; }
360 AtomicString computeInheritedLanguage() const;
361 Locale& locale() const;
363 virtual void accessKeyAction(bool /*sendToAnyEvent*/) { }
365 virtual bool isURLAttribute(const Attribute&) const { return false; }
366 virtual bool isHTMLContentAttribute(const Attribute&) const { return false; }
368 KURL getURLAttribute(const QualifiedName&) const;
369 KURL getNonEmptyURLAttribute(const QualifiedName&) const;
371 virtual const AtomicString imageSourceURL() const;
372 virtual Image* imageContents() { return 0; }
374 virtual void focus(bool restorePreviousSelection = true, FocusType = FocusTypeNone);
375 virtual void updateFocusAppearance(bool restorePreviousSelection);
377 // Whether this element can receive focus at all. Most elements are not
378 // focusable but some elements, such as form controls and links, are. Unlike
379 // rendererIsFocusable(), this method may be called when layout is not up to
380 // date, so it must not use the renderer to determine focusability.
381 virtual bool supportsFocus() const;
382 // Whether the node can actually be focused.
383 bool isFocusable() const;
384 virtual bool isKeyboardFocusable() const;
385 virtual bool isMouseFocusable() const;
386 virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType);
387 virtual void dispatchBlurEvent(Element* newFocusedElement);
388 void dispatchFocusInEvent(const AtomicString& eventType, Element* oldFocusedElement);
389 void dispatchFocusOutEvent(const AtomicString& eventType, Element* newFocusedElement);
393 String innerHTML() const;
394 String outerHTML() const;
395 void setInnerHTML(const String&, ExceptionState&);
396 void setOuterHTML(const String&, ExceptionState&);
398 Element* insertAdjacentElement(const String& where, Element* newChild, ExceptionState&);
399 void insertAdjacentText(const String& where, const String& text, ExceptionState&);
400 void insertAdjacentHTML(const String& where, const String& html, ExceptionState&);
402 String textFromChildren();
404 virtual String title() const { return String(); }
406 virtual const AtomicString& shadowPseudoId() const;
407 void setShadowPseudoId(const AtomicString&);
409 LayoutSize minimumSizeForResizing() const;
410 void setMinimumSizeForResizing(const LayoutSize&);
412 virtual void didBecomeFullscreenElement() { }
413 virtual void willStopBeingFullscreenElement() { }
415 // Called by the parser when this element's close tag is reached,
416 // signaling that all child tags have been parsed and added.
417 // This is needed for <applet> and <object> elements, which can't lay themselves out
418 // until they know all of their nested <param>s. [Radar 3603191, 4040848].
419 // Also used for script elements and some SVG elements for similar purposes,
420 // but making parsing a special case in this respect should be avoided if possible.
421 virtual void finishParsingChildren();
423 void beginParsingChildren() { setIsFinishedParsingChildren(false); }
425 PseudoElement* pseudoElement(PseudoId) const;
426 RenderObject* pseudoElementRenderer(PseudoId) const;
428 virtual bool matchesReadOnlyPseudoClass() const { return false; }
429 virtual bool matchesReadWritePseudoClass() const { return false; }
430 bool matches(const String& selectors, ExceptionState&);
431 virtual bool shouldAppearIndeterminate() const { return false; }
433 DOMTokenList& classList();
435 DOMStringMap& dataset();
437 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
438 virtual bool isDateTimeEditElement() const { return false; }
439 virtual bool isDateTimeFieldElement() const { return false; }
440 virtual bool isPickerIndicatorElement() const { return false; }
443 virtual bool isFormControlElement() const { return false; }
444 virtual bool isSpinButtonElement() const { return false; }
445 virtual bool isTextFormControl() const { return false; }
446 virtual bool isOptionalFormControl() const { return false; }
447 virtual bool isRequiredFormControl() const { return false; }
448 virtual bool isDefaultButtonForForm() const { return false; }
449 virtual bool willValidate() const { return false; }
450 virtual bool isValidFormControlElement() { return false; }
451 virtual bool isInRange() const { return false; }
452 virtual bool isOutOfRange() const { return false; }
453 virtual bool isClearButtonElement() const { return false; }
455 virtual bool canContainRangeEndPoint() const OVERRIDE { return true; }
457 // Used for disabled form elements; if true, prevents mouse events from being dispatched
458 // to event listeners, and prevents DOMActivate events from being sent at all.
459 virtual bool isDisabledFormControl() const { return false; }
461 bool hasPendingResources() const { return hasElementFlag(HasPendingResources); }
462 void setHasPendingResources() { setElementFlag(HasPendingResources); }
463 void clearHasPendingResources() { clearElementFlag(HasPendingResources); }
464 virtual void buildPendingResource() { };
466 void setCustomElementDefinition(PassRefPtr<CustomElementDefinition>);
467 CustomElementDefinition* customElementDefinition() const;
470 ALLOW_KEYBOARD_INPUT = 1 << 0,
471 LEGACY_MOZILLA_REQUEST = 1 << 1,
474 void webkitRequestFullScreen(unsigned short flags);
475 bool containsFullScreenElement() const { return hasElementFlag(ContainsFullScreenElement); }
476 void setContainsFullScreenElement(bool);
477 void setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(bool);
480 void webkitRequestFullscreen();
482 bool isInTopLayer() const { return hasElementFlag(IsInTopLayer); }
483 void setIsInTopLayer(bool);
485 void webkitRequestPointerLock();
486 void requestPointerLock();
488 bool isSpellCheckingEnabled() const;
490 // FIXME: public for RenderTreeBuilder, we shouldn't expose this though.
491 PassRefPtr<RenderStyle> styleForRenderer();
494 bool hasClass() const;
495 const SpaceSplitString& classNames() const;
497 IntSize savedLayerScrollOffset() const;
498 void setSavedLayerScrollOffset(const IntSize&);
500 ActiveAnimations* activeAnimations() const;
501 ActiveAnimations& ensureActiveAnimations();
502 bool hasActiveAnimations() const;
504 InputMethodContext& inputMethodContext();
505 bool hasInputMethodContext() const;
507 void setPrefix(const AtomicString&, ExceptionState&);
509 void synchronizeAttribute(const AtomicString& localName) const;
511 MutableStylePropertySet& ensureMutableInlineStyle();
512 void clearMutableInlineStyleIfEmpty();
514 void setTabIndex(int);
515 virtual short tabIndex() const OVERRIDE;
517 virtual void trace(Visitor*) OVERRIDE;
520 Element(const QualifiedName& tagName, Document*, ConstructionType);
522 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, CSSValueID identifier);
523 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, double value, CSSPrimitiveValue::UnitType);
524 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, const String& value);
526 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
527 virtual void removedFrom(ContainerNode*) OVERRIDE;
528 virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0) OVERRIDE;
530 virtual void willRecalcStyle(StyleRecalcChange);
531 virtual void didRecalcStyle(StyleRecalcChange);
532 virtual PassRefPtr<RenderStyle> customStyleForRenderer();
534 virtual bool shouldRegisterAsNamedItem() const { return false; }
535 virtual bool shouldRegisterAsExtraNamedItem() const { return false; }
537 virtual bool supportsSpatialNavigationFocus() const;
539 void clearTabIndexExplicitlyIfNeeded();
540 void setTabIndexExplicitly(short);
541 // Subclasses may override this method to affect focusability. Unlike
542 // supportsFocus, this method must be called on an up-to-date layout, so it
543 // may use the renderer to reason about focusability. This method cannot be
544 // moved to RenderObject because some focusable nodes don't have renderers,
545 // e.g., HTMLOptionElement.
546 virtual bool rendererIsFocusable() const;
547 PassRefPtrWillBeRawPtr<HTMLCollection> ensureCachedHTMLCollection(CollectionType);
548 HTMLCollection* cachedHTMLCollection(CollectionType);
550 // classAttributeChanged() exists to share code between
551 // parseAttribute (called via setAttribute()) and
552 // svgAttributeChanged (called when element.className.baseValue is set)
553 void classAttributeChanged(const AtomicString& newClassString);
555 PassRefPtr<RenderStyle> originalStyleForRenderer();
557 Node* insertAdjacent(const String& where, Node* newChild, ExceptionState&);
560 bool hasElementFlag(ElementFlags mask) const { return hasRareData() && hasElementFlagInternal(mask); }
561 void setElementFlag(ElementFlags, bool value = true);
562 void clearElementFlag(ElementFlags);
563 bool hasElementFlagInternal(ElementFlags) const;
565 void styleAttributeChanged(const AtomicString& newStyleString, AttributeModificationReason);
567 void updatePresentationAttributeStyle();
569 void inlineStyleChanged();
570 PropertySetCSSStyleDeclaration* inlineStyleCSSOMWrapper();
571 void setInlineStyleFromString(const AtomicString&);
573 StyleRecalcChange recalcOwnStyle(StyleRecalcChange);
574 void recalcChildStyle(StyleRecalcChange);
576 inline void checkForEmptyStyleChange();
578 void updatePseudoElement(PseudoId, StyleRecalcChange);
580 inline void createPseudoElementIfNeeded(PseudoId);
582 // FIXME: Everyone should allow author shadows.
583 virtual bool areAuthorShadowsAllowed() const { return true; }
584 virtual void didAddUserAgentShadowRoot(ShadowRoot&) { }
585 virtual bool alwaysCreateUserAgentShadowRoot() const { return false; }
587 // FIXME: Remove the need for Attr to call willModifyAttribute/didModifyAttribute.
590 enum SynchronizationOfLazyAttribute { NotInSynchronizationOfLazyAttribute = 0, InSynchronizationOfLazyAttribute };
592 void didAddAttribute(const QualifiedName&, const AtomicString&);
593 void willModifyAttribute(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue);
594 void didModifyAttribute(const QualifiedName&, const AtomicString&);
595 void didRemoveAttribute(const QualifiedName&);
597 void synchronizeAttribute(const QualifiedName&) const;
599 void updateId(const AtomicString& oldId, const AtomicString& newId);
600 void updateId(TreeScope&, const AtomicString& oldId, const AtomicString& newId);
601 void updateName(const AtomicString& oldName, const AtomicString& newName);
602 void updateLabel(TreeScope&, const AtomicString& oldForAttributeValue, const AtomicString& newForAttributeValue);
604 void scrollByUnits(int units, ScrollGranularity);
606 virtual NodeType nodeType() const OVERRIDE FINAL;
607 virtual bool childTypeAllowed(NodeType) const OVERRIDE FINAL;
609 void setAttributeInternal(size_t index, const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
610 void appendAttributeInternal(const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
611 void removeAttributeInternal(size_t index, SynchronizationOfLazyAttribute);
612 void attributeChangedFromParserOrByCloning(const QualifiedName&, const AtomicString&, AttributeModificationReason);
615 virtual void formatForDebugger(char* buffer, unsigned length) const OVERRIDE;
618 bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
620 void cancelFocusAppearanceUpdate();
622 virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) OVERRIDE { return computedStyle(pseudoElementSpecifier); }
624 inline void updateCallbackSelectors(RenderStyle* oldStyle, RenderStyle* newStyle);
625 inline void removeCallbackSelectors();
626 inline void addCallbackSelectors();
628 // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
630 virtual PassRefPtrWillBeRawPtr<Node> cloneNode(bool deep) OVERRIDE;
631 virtual PassRefPtrWillBeRawPtr<Element> cloneElementWithoutAttributesAndChildren();
633 QualifiedName m_tagName;
635 SpellcheckAttributeState spellcheckAttributeState() const;
637 void updateNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
638 void updateExtraNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
640 void createUniqueElementData();
642 bool shouldInvalidateDistributionWhenAttributeChanged(ElementShadow*, const QualifiedName&, const AtomicString&);
644 ElementRareData* elementRareData() const;
645 ElementRareData& ensureElementRareData();
647 WillBeHeapVector<RefPtrWillBeMember<Attr> >& ensureAttrNodeList();
648 void removeAttrNodeList();
649 void detachAllAttrNodesFromElement();
650 void detachAttrNodeFromElementWithValue(Attr*, const AtomicString& value);
651 void detachAttrNodeAtIndex(Attr*, size_t index);
653 bool isJavaScriptURLAttribute(const Attribute&) const;
655 RefPtr<ElementData> m_elementData;
658 DEFINE_NODE_TYPE_CASTS(Element, isElementNode());
659 template <typename T> bool isElementOfType(const Element&);
660 template <typename T> inline bool isElementOfType(const Node& node) { return node.isElementNode() && isElementOfType<const T>(toElement(node)); }
661 template <> inline bool isElementOfType<const Element>(const Element&) { return true; }
664 template<typename T> inline T& toElement(Node& node)
666 ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
667 return static_cast<T&>(node);
669 template<typename T> inline T* toElement(Node* node)
671 ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
672 return static_cast<T*>(node);
674 template<typename T> inline const T& toElement(const Node& node)
676 ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
677 return static_cast<const T&>(node);
679 template<typename T> inline const T* toElement(const Node* node)
681 ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
682 return static_cast<const T*>(node);
684 template<typename T, typename U> inline T* toElement(const RefPtr<U>& node) { return toElement<T>(node.get()); }
686 inline bool isDisabledFormControl(const Node* node)
688 return node->isElementNode() && toElement(node)->isDisabledFormControl();
691 inline bool Node::hasTagName(const QualifiedName& name) const
693 return isElementNode() && toElement(this)->hasTagName(name);
696 inline Element* Node::parentElement() const
698 ContainerNode* parent = parentNode();
699 return parent && parent->isElementNode() ? toElement(parent) : 0;
702 inline bool Element::fastHasAttribute(const QualifiedName& name) const
704 ASSERT(fastAttributeLookupAllowed(name));
705 return elementData() && findAttributeByName(name);
708 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
710 ASSERT(fastAttributeLookupAllowed(name));
712 if (const Attribute* attribute = findAttributeByName(name))
713 return attribute->value();
718 inline bool Element::hasAttributesWithoutUpdate() const
720 return elementData() && elementData()->hasAttributes();
723 inline const AtomicString& Element::idForStyleResolution() const
726 return elementData()->idForStyleResolution();
729 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
731 // FIXME: This check is probably not correct for the case where the document has an id attribute
732 // with a non-null namespace, because it will return false, a false negative, if the prefixes
733 // don't match but the local name and namespace both do. However, since this has been like this
734 // for a while and the code paths may be hot, we'll have to measure performance if we fix it.
735 return attributeName == HTMLNames::idAttr;
738 inline const AtomicString& Element::getIdAttribute() const
740 return hasID() ? fastGetAttribute(HTMLNames::idAttr) : nullAtom;
743 inline const AtomicString& Element::getNameAttribute() const
745 return hasName() ? fastGetAttribute(HTMLNames::nameAttr) : nullAtom;
748 inline const AtomicString& Element::getClassAttribute() const
753 return getAttribute(HTMLNames::classAttr);
754 return fastGetAttribute(HTMLNames::classAttr);
757 inline void Element::setIdAttribute(const AtomicString& value)
759 setAttribute(HTMLNames::idAttr, value);
762 inline const SpaceSplitString& Element::classNames() const
765 ASSERT(elementData());
766 return elementData()->classNames();
769 inline size_t Element::attributeCount() const
771 ASSERT(elementData());
772 return elementData()->attributeCount();
775 inline const Attribute& Element::attributeAt(unsigned index) const
777 ASSERT(elementData());
778 return elementData()->attributeAt(index);
781 inline const Attribute* Element::findAttributeByName(const QualifiedName& name) const
783 ASSERT(elementData());
784 return elementData()->findAttributeByName(name);
787 inline bool Element::hasID() const
789 return elementData() && elementData()->hasID();
792 inline bool Element::hasClass() const
794 return elementData() && elementData()->hasClass();
797 inline UniqueElementData& Element::ensureUniqueElementData()
799 if (!elementData() || !elementData()->isUnique())
800 createUniqueElementData();
801 return static_cast<UniqueElementData&>(*m_elementData);
804 // Put here to make them inline.
805 inline bool Node::hasID() const
807 return isElementNode() && toElement(this)->hasID();
810 inline bool Node::hasClass() const
812 return isElementNode() && toElement(this)->hasClass();
815 inline Node::InsertionNotificationRequest Node::insertedInto(ContainerNode* insertionPoint)
817 ASSERT(!childNeedsStyleInvalidation());
818 ASSERT(!needsStyleInvalidation());
819 ASSERT(insertionPoint->inDocument() || isContainerNode());
820 if (insertionPoint->inDocument())
821 setFlag(InDocumentFlag);
822 if (parentOrShadowHostNode()->isInShadowTree())
823 setFlag(IsInShadowTreeFlag);
824 if (childNeedsDistributionRecalc() && !insertionPoint->childNeedsDistributionRecalc())
825 insertionPoint->markAncestorsWithChildNeedsDistributionRecalc();
826 return InsertionDone;
829 inline void Node::removedFrom(ContainerNode* insertionPoint)
831 ASSERT(insertionPoint->inDocument() || isContainerNode());
832 if (insertionPoint->inDocument())
833 clearFlag(InDocumentFlag);
834 if (isInShadowTree() && !treeScope().rootNode().isShadowRoot())
835 clearFlag(IsInShadowTreeFlag);
838 inline void Element::invalidateStyleAttribute()
840 ASSERT(elementData());
841 elementData()->m_styleAttributeIsDirty = true;
844 inline const StylePropertySet* Element::presentationAttributeStyle()
848 if (elementData()->m_presentationAttributeStyleIsDirty)
849 updatePresentationAttributeStyle();
850 // Need to call elementData() again since updatePresentationAttributeStyle()
851 // might swap it with a UniqueElementData.
852 return elementData()->presentationAttributeStyle();
855 inline void Element::setTagNameForCreateElementNS(const QualifiedName& tagName)
857 // We expect this method to be called only to reset the prefix.
858 ASSERT(tagName.localName() == m_tagName.localName());
859 ASSERT(tagName.namespaceURI() == m_tagName.namespaceURI());
863 inline bool isShadowHost(const Node* node)
865 return node && node->isElementNode() && toElement(node)->shadow();
868 inline bool isShadowHost(const Element* element)
870 return element && element->shadow();
873 // These macros do the same as their NODE equivalents but additionally provide a template specialization
874 // for isElementOfType<>() so that the Traversal<> API works for these Element types.
875 #define DEFINE_ELEMENT_TYPE_CASTS(thisType, predicate) \
876 template <> inline bool isElementOfType<const thisType>(const Element& element) { return element.predicate; } \
877 DEFINE_NODE_TYPE_CASTS(thisType, predicate)
879 #define DEFINE_ELEMENT_TYPE_CASTS_WITH_FUNCTION(thisType) \
880 template <> inline bool isElementOfType<const thisType>(const Element& element) { return is##thisType(element); } \
881 DEFINE_NODE_TYPE_CASTS_WITH_FUNCTION(thisType)
883 #define DECLARE_ELEMENT_FACTORY_WITH_TAGNAME(T) \
884 static PassRefPtrWillBeRawPtr<T> create(const QualifiedName&, Document&)
885 #define DEFINE_ELEMENT_FACTORY_WITH_TAGNAME(T) \
886 PassRefPtrWillBeRawPtr<T> T::create(const QualifiedName& tagName, Document& document) \
888 return adoptRefWillBeNoop(new T(tagName, document)); \