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 "CSSPropertyNames.h"
29 #include "HTMLNames.h"
30 #include "core/css/CSSPrimitiveValue.h"
31 #include "core/dom/Attribute.h"
32 #include "core/dom/Document.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/scroll/ScrollTypes.h"
41 class ActiveAnimations;
46 class CustomElementDefinition;
49 class ElementRareData;
53 class InputMethodContext;
56 class MutableStylePropertySet;
57 class PropertySetCSSStyleDeclaration;
60 class StylePropertySet;
62 enum AffectedSelectorType {
63 AffectedSelectorChecked = 1,
64 AffectedSelectorEnabled = 1 << 1,
65 AffectedSelectorDisabled = 1 << 2,
66 AffectedSelectorIndeterminate = 1 << 3,
67 AffectedSelectorLink = 1 << 4,
68 AffectedSelectorTarget = 1 << 5,
69 AffectedSelectorVisited = 1 << 6
71 typedef int AffectedSelectorMask;
73 enum SpellcheckAttributeState {
74 SpellcheckAttributeTrue,
75 SpellcheckAttributeFalse,
76 SpellcheckAttributeDefault
80 TabIndexWasSetExplicitly = 1 << 0,
81 NeedsFocusAppearanceUpdateSoonAfterAttach = 1 << 1,
82 StyleAffectedByEmpty = 1 << 2,
83 IsInCanvasSubtree = 1 << 3,
84 ContainsFullScreenElement = 1 << 4,
85 IsInTopLayer = 1 << 5,
86 HasPendingResources = 1 << 6,
88 NumberOfElementFlags = 7, // Required size of bitfield used to store the flags.
91 class Element : public ContainerNode {
93 static PassRefPtr<Element> create(const QualifiedName&, Document*);
96 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecopy);
97 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecut);
98 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforepaste);
99 DEFINE_ATTRIBUTE_EVENT_LISTENER(copy);
100 DEFINE_ATTRIBUTE_EVENT_LISTENER(cut);
101 DEFINE_ATTRIBUTE_EVENT_LISTENER(paste);
102 DEFINE_ATTRIBUTE_EVENT_LISTENER(search);
103 DEFINE_ATTRIBUTE_EVENT_LISTENER(selectstart);
104 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchcancel);
105 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchend);
106 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchmove);
107 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchstart);
108 DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitfullscreenchange);
109 DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitfullscreenerror);
110 DEFINE_ATTRIBUTE_EVENT_LISTENER(wheel);
112 bool hasAttribute(const QualifiedName&) const;
113 const AtomicString& getAttribute(const QualifiedName&) const;
115 // Passing nullAtom as the second parameter removes the attribute when calling either of these set methods.
116 void setAttribute(const QualifiedName&, const AtomicString& value);
117 void setSynchronizedLazyAttribute(const QualifiedName&, const AtomicString& value);
119 void removeAttribute(const QualifiedName&);
121 // Typed getters and setters for language bindings.
122 int getIntegralAttribute(const QualifiedName& attributeName) const;
123 void setIntegralAttribute(const QualifiedName& attributeName, int value);
124 unsigned getUnsignedIntegralAttribute(const QualifiedName& attributeName) const;
125 void setUnsignedIntegralAttribute(const QualifiedName& attributeName, unsigned value);
126 double getFloatingPointAttribute(const QualifiedName& attributeName, double fallbackValue = std::numeric_limits<double>::quiet_NaN()) const;
127 void setFloatingPointAttribute(const QualifiedName& attributeName, double value);
129 // Call this to get the value of an attribute that is known not to be the style
130 // attribute or one of the SVG animatable attributes.
131 bool fastHasAttribute(const QualifiedName&) const;
132 const AtomicString& fastGetAttribute(const QualifiedName&) const;
134 bool fastAttributeLookupAllowed(const QualifiedName&) const;
137 #ifdef DUMP_NODE_STATISTICS
138 bool hasNamedNodeMap() const;
140 bool hasAttributes() const;
141 // This variant will not update the potentially invalid attributes. To be used when not interested
142 // in style attribute or one of the SVG animation attributes.
143 bool hasAttributesWithoutUpdate() const;
145 bool hasAttribute(const AtomicString& name) const;
146 bool hasAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
148 const AtomicString& getAttribute(const AtomicString& name) const;
149 const AtomicString& getAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
151 void setAttribute(const AtomicString& name, const AtomicString& value, ExceptionState&);
152 static bool parseAttributeName(QualifiedName&, const AtomicString& namespaceURI, const AtomicString& qualifiedName, ExceptionState&);
153 void setAttributeNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, const AtomicString& value, ExceptionState&);
155 bool isIdAttributeName(const QualifiedName&) const;
156 const AtomicString& getIdAttribute() const;
157 void setIdAttribute(const AtomicString&);
159 const AtomicString& getNameAttribute() const;
160 const AtomicString& getClassAttribute() const;
162 bool shouldIgnoreAttributeCase() const;
164 // Call this to get the value of the id attribute for style resolution purposes.
165 // The value will already be lowercased if the document is in compatibility mode,
166 // so this function is not suitable for non-style uses.
167 const AtomicString& idForStyleResolution() const;
169 // Internal methods that assume the existence of attribute storage, one should use hasAttributes()
170 // before calling them. This is not a trivial getter and its return value should be cached for
172 size_t attributeCount() const;
173 const Attribute& attributeItem(unsigned index) const;
174 const Attribute* getAttributeItem(const QualifiedName&) const;
175 size_t getAttributeItemIndex(const QualifiedName& name) const { return elementData()->getAttributeItemIndex(name); }
176 size_t getAttributeItemIndex(const AtomicString& name, bool shouldIgnoreAttributeCase) const { return elementData()->getAttributeItemIndex(name, shouldIgnoreAttributeCase); }
178 void scrollIntoView(bool alignToTop = true);
179 void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
181 void scrollByLines(int lines);
182 void scrollByPages(int pages);
189 // FIXME: Replace uses of offsetParent in the platform with calls
190 // to the render layer and merge offsetParentForBindings and offsetParent.
191 Element* offsetParentForBindings();
193 Element* offsetParent();
198 virtual int scrollLeft();
199 virtual int scrollTop();
200 virtual void setScrollLeft(int);
201 virtual void setScrollLeft(const Dictionary& scrollOptionsHorizontal, ExceptionState&);
202 virtual void setScrollTop(int);
203 virtual void setScrollTop(const Dictionary& scrollOptionsVertical, ExceptionState&);
204 virtual int scrollWidth();
205 virtual int scrollHeight();
207 IntRect boundsInRootViewSpace();
209 PassRefPtr<ClientRectList> getClientRects();
210 PassRefPtr<ClientRect> getBoundingClientRect();
212 // Returns the absolute bounding box translated into screen coordinates:
213 IntRect screenRect() const;
215 virtual void didMoveToNewDocument(Document&) OVERRIDE;
217 void removeAttribute(const AtomicString& name);
218 void removeAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName);
220 PassRefPtr<Attr> detachAttribute(size_t index);
222 PassRefPtr<Attr> getAttributeNode(const AtomicString& name);
223 PassRefPtr<Attr> getAttributeNodeNS(const AtomicString& namespaceURI, const AtomicString& localName);
224 PassRefPtr<Attr> setAttributeNode(Attr*, ExceptionState&);
225 PassRefPtr<Attr> removeAttributeNode(Attr*, ExceptionState&);
227 PassRefPtr<Attr> attrIfExists(const QualifiedName&);
228 PassRefPtr<Attr> ensureAttr(const QualifiedName&);
230 const Vector<RefPtr<Attr> >& attrNodeList();
232 CSSStyleDeclaration* style();
234 const QualifiedName& tagQName() const { return m_tagName; }
235 String tagName() const { return nodeName(); }
236 bool hasTagName(const QualifiedName& tagName) const { return m_tagName.matches(tagName); }
238 // Should be called only by Document::createElementNS to fix up m_tagName immediately after construction.
239 void setTagNameForCreateElementNS(const QualifiedName&);
241 // A fast function for checking the local name against another atomic string.
242 bool hasLocalName(const AtomicString& other) const { return m_tagName.localName() == other; }
243 bool hasLocalName(const QualifiedName& other) const { return m_tagName.localName() == other.localName(); }
245 virtual const AtomicString& localName() const OVERRIDE FINAL { return m_tagName.localName(); }
246 const AtomicString& prefix() const { return m_tagName.prefix(); }
247 virtual const AtomicString& namespaceURI() const OVERRIDE FINAL { return m_tagName.namespaceURI(); }
249 const AtomicString& locateNamespacePrefix(const AtomicString& namespaceURI) const;
251 virtual KURL baseURI() const OVERRIDE FINAL;
253 virtual String nodeName() const OVERRIDE;
255 PassRefPtr<Element> cloneElementWithChildren();
256 PassRefPtr<Element> cloneElementWithoutChildren();
258 void scheduleLayerUpdate();
260 void normalizeAttributes();
262 void setBooleanAttribute(const QualifiedName& name, bool);
264 virtual const StylePropertySet* additionalPresentationAttributeStyle() { return 0; }
265 void invalidateStyleAttribute();
267 const StylePropertySet* inlineStyle() const { return elementData() ? elementData()->m_inlineStyle.get() : 0; }
269 bool setInlineStyleProperty(CSSPropertyID, CSSValueID identifier, bool important = false);
270 bool setInlineStyleProperty(CSSPropertyID, CSSPropertyID identifier, bool important = false);
271 bool setInlineStyleProperty(CSSPropertyID, double value, CSSPrimitiveValue::UnitTypes, bool important = false);
272 bool setInlineStyleProperty(CSSPropertyID, const String& value, bool important = false);
273 bool removeInlineStyleProperty(CSSPropertyID);
274 void removeAllInlineStyleProperties();
276 void synchronizeStyleAttributeInternal() const;
278 const StylePropertySet* presentationAttributeStyle();
279 virtual bool isPresentationAttribute(const QualifiedName&) const { return false; }
280 virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) { }
282 // For exposing to DOM only.
283 NamedNodeMap* attributes() const;
285 enum AttributeModificationReason {
290 // This method is called whenever an attribute is added, changed or removed.
291 virtual void attributeChanged(const QualifiedName&, const AtomicString&, AttributeModificationReason = ModifiedDirectly);
292 virtual void parseAttribute(const QualifiedName&, const AtomicString&) { }
294 virtual bool hasLegalLinkAttribute(const QualifiedName&) const;
295 virtual const QualifiedName& subResourceAttributeName() const;
297 // Only called by the parser immediately after element construction.
298 void parserSetAttributes(const Vector<Attribute>&);
300 // Remove attributes that might introduce scripting from the vector leaving the element unchanged.
301 void stripScriptingAttributes(Vector<Attribute>&) const;
303 const ElementData* elementData() const { return m_elementData.get(); }
304 UniqueElementData& ensureUniqueElementData();
306 void synchronizeAllAttributes() const;
308 // Clones attributes only.
309 void cloneAttributesFromElement(const Element&);
311 // Clones all attribute-derived data, including subclass specifics (through copyNonAttributeProperties.)
312 void cloneDataFromElement(const Element&);
314 bool hasEquivalentAttributes(const Element* other) const;
316 virtual void copyNonAttributePropertiesFromElement(const Element&) { }
318 virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
319 virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
320 virtual RenderObject* createRenderer(RenderStyle*);
321 virtual bool rendererIsNeeded(const RenderStyle&);
322 void recalcStyle(StyleRecalcChange, Text* nextTextSibling = 0);
323 void didAffectSelector(AffectedSelectorMask);
324 void setAnimationStyleChange(bool);
325 void setNeedsAnimationStyleRecalc();
327 bool supportsStyleSharing() const;
329 ElementShadow* shadow() const;
330 ElementShadow& ensureShadow();
331 PassRefPtr<ShadowRoot> createShadowRoot(ExceptionState&);
332 ShadowRoot* shadowRoot() const;
333 ShadowRoot* youngestShadowRoot() const;
335 bool hasAuthorShadowRoot() const { return shadowRoot(); }
336 virtual void didAddShadowRoot(ShadowRoot&);
337 ShadowRoot* userAgentShadowRoot() const;
338 ShadowRoot& ensureUserAgentShadowRoot();
339 bool isInDescendantTreeOf(const Element* shadowHost) const;
341 RenderStyle* computedStyle(PseudoId = NOPSEUDO);
343 // Methods for indicating the style is affected by dynamic updates (e.g., children changing, our position changing in our sibling list, etc.)
344 bool styleAffectedByEmpty() const { return hasElementFlag(StyleAffectedByEmpty); }
345 void setStyleAffectedByEmpty() { setElementFlag(StyleAffectedByEmpty); }
347 void setIsInCanvasSubtree(bool value) { setElementFlag(IsInCanvasSubtree, value); }
348 bool isInCanvasSubtree() const { return hasElementFlag(IsInCanvasSubtree); }
350 unsigned childIndex() const { return hasRareData() ? rareDataChildIndex() : 0; }
351 void setChildIndex(unsigned);
353 bool isUpgradedCustomElement() { return customElementState() == Upgraded; }
354 bool isUnresolvedCustomElement() { return customElementState() == WaitingForUpgrade; }
356 AtomicString computeInheritedLanguage() const;
357 Locale& locale() const;
359 virtual void accessKeyAction(bool /*sendToAnyEvent*/) { }
361 virtual bool isURLAttribute(const Attribute&) const { return false; }
362 virtual bool isHTMLContentAttribute(const Attribute&) const { return false; }
364 KURL getURLAttribute(const QualifiedName&) const;
365 KURL getNonEmptyURLAttribute(const QualifiedName&) const;
367 virtual const AtomicString imageSourceURL() const;
368 virtual Image* imageContents() { return 0; }
370 virtual void focus(bool restorePreviousSelection = true, FocusType = FocusTypeNone);
371 virtual void updateFocusAppearance(bool restorePreviousSelection);
373 // Whether this element can receive focus at all. Most elements are not
374 // focusable but some elements, such as form controls and links, are. Unlike
375 // rendererIsFocusable(), this method may be called when layout is not up to
376 // date, so it must not use the renderer to determine focusability.
377 virtual bool supportsFocus() const { return hasElementFlag(TabIndexWasSetExplicitly); }
378 // Whether the node can actually be focused.
379 bool isFocusable() const;
380 virtual bool isKeyboardFocusable() const;
381 virtual bool isMouseFocusable() const;
382 virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType);
383 virtual void dispatchBlurEvent(Element* newFocusedElement);
384 void dispatchFocusInEvent(const AtomicString& eventType, Element* oldFocusedElement);
385 void dispatchFocusOutEvent(const AtomicString& eventType, Element* newFocusedElement);
389 String innerHTML() const;
390 String outerHTML() const;
391 void setInnerHTML(const String&, ExceptionState&);
392 void setOuterHTML(const String&, ExceptionState&);
394 Element* insertAdjacentElement(const String& where, Element* newChild, ExceptionState&);
395 void insertAdjacentText(const String& where, const String& text, ExceptionState&);
396 void insertAdjacentHTML(const String& where, const String& html, ExceptionState&);
398 String textFromChildren();
400 virtual String title() const { return String(); }
402 virtual const AtomicString& shadowPseudoId() const;
403 void setShadowPseudoId(const AtomicString&);
405 LayoutSize minimumSizeForResizing() const;
406 void setMinimumSizeForResizing(const LayoutSize&);
408 virtual void didBecomeFullscreenElement() { }
409 virtual void willStopBeingFullscreenElement() { }
411 // Called by the parser when this element's close tag is reached,
412 // signaling that all child tags have been parsed and added.
413 // This is needed for <applet> and <object> elements, which can't lay themselves out
414 // until they know all of their nested <param>s. [Radar 3603191, 4040848].
415 // Also used for script elements and some SVG elements for similar purposes,
416 // but making parsing a special case in this respect should be avoided if possible.
417 virtual void finishParsingChildren();
419 void beginParsingChildren() { setIsFinishedParsingChildren(false); }
421 PseudoElement* pseudoElement(PseudoId) const;
422 RenderObject* pseudoElementRenderer(PseudoId) const;
424 virtual bool matchesReadOnlyPseudoClass() const { return false; }
425 virtual bool matchesReadWritePseudoClass() const { return false; }
426 bool matches(const String& selectors, ExceptionState&);
427 virtual bool shouldAppearIndeterminate() const { return false; }
429 DOMTokenList& classList();
431 DOMStringMap& dataset();
433 #if ENABLE(INPUT_SPEECH)
434 virtual bool isInputFieldSpeechButtonElement() const { return false; }
436 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
437 virtual bool isDateTimeEditElement() const { return false; }
438 virtual bool isDateTimeFieldElement() const { return false; }
439 virtual bool isPickerIndicatorElement() const { return false; }
442 virtual bool isFormControlElement() const { return false; }
443 virtual bool isSpinButtonElement() const { return false; }
444 virtual bool isTextFormControl() const { return false; }
445 virtual bool isOptionalFormControl() const { return false; }
446 virtual bool isRequiredFormControl() const { return false; }
447 virtual bool isDefaultButtonForForm() const { return false; }
448 virtual bool willValidate() const { return false; }
449 virtual bool isValidFormControlElement() { return false; }
450 virtual bool isInRange() const { return false; }
451 virtual bool isOutOfRange() const { return false; }
452 virtual bool isPasswordGeneratorButtonElement() 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();
487 bool isSpellCheckingEnabled() const;
489 // FIXME: public for RenderTreeBuilder, we shouldn't expose this though.
490 PassRefPtr<RenderStyle> styleForRenderer();
493 bool hasClass() const;
494 const SpaceSplitString& classNames() const;
496 IntSize savedLayerScrollOffset() const;
497 void setSavedLayerScrollOffset(const IntSize&);
499 ActiveAnimations* activeAnimations() const;
500 ActiveAnimations& ensureActiveAnimations();
501 bool hasActiveAnimations() const;
503 InputMethodContext& inputMethodContext();
504 bool hasInputMethodContext() const;
506 void setPrefix(const AtomicString&, ExceptionState&);
508 void synchronizeAttribute(const AtomicString& localName) const;
510 MutableStylePropertySet& ensureMutableInlineStyle();
511 void clearMutableInlineStyleIfEmpty();
514 Element(const QualifiedName& tagName, Document* document, ConstructionType type)
515 : ContainerNode(document, type)
518 ScriptWrappable::init(this);
521 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, CSSValueID identifier);
522 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, double value, CSSPrimitiveValue::UnitTypes);
523 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, const String& value);
525 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
526 virtual void removedFrom(ContainerNode*) OVERRIDE;
527 virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0) OVERRIDE;
528 virtual void removeAllEventListeners() 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 void clearTabIndexExplicitlyIfNeeded();
538 void setTabIndexExplicitly(short);
539 virtual short tabIndex() const OVERRIDE;
540 // Subclasses may override this method to affect focusability. Unlike
541 // supportsFocus, this method must be called on an up-to-date layout, so it
542 // may use the renderer to reason about focusability. This method cannot be
543 // moved to RenderObject because some focusable nodes don't have renderers,
544 // e.g., HTMLOptionElement.
545 virtual bool rendererIsFocusable() const;
546 PassRefPtr<HTMLCollection> ensureCachedHTMLCollection(CollectionType);
547 HTMLCollection* cachedHTMLCollection(CollectionType);
549 // classAttributeChanged() exists to share code between
550 // parseAttribute (called via setAttribute()) and
551 // svgAttributeChanged (called when element.className.baseValue is set)
552 void classAttributeChanged(const AtomicString& newClassString);
554 PassRefPtr<RenderStyle> originalStyleForRenderer();
556 Node* insertAdjacent(const String& where, Node* newChild, ExceptionState&);
559 bool hasElementFlag(ElementFlags mask) const { return hasRareData() && hasElementFlagInternal(mask); }
560 void setElementFlag(ElementFlags, bool value = true);
561 void clearElementFlag(ElementFlags);
562 bool hasElementFlagInternal(ElementFlags) const;
564 void styleAttributeChanged(const AtomicString& newStyleString, AttributeModificationReason);
566 void updatePresentationAttributeStyle();
568 void inlineStyleChanged();
569 PropertySetCSSStyleDeclaration* inlineStyleCSSOMWrapper();
570 void setInlineStyleFromString(const AtomicString&);
572 StyleRecalcChange recalcOwnStyle(StyleRecalcChange);
573 void recalcChildStyle(StyleRecalcChange);
575 // FIXME: These methods should all be renamed to something better than "check",
576 // since it's not clear that they alter the style bits of siblings and children.
577 void checkForSiblingStyleChanges(bool finishedParsingCallback, Node* beforeChange, Node* afterChange, int childCountDelta);
578 inline void checkForEmptyStyleChange(RenderStyle*);
580 void updatePseudoElement(PseudoId, StyleRecalcChange);
582 inline void createPseudoElementIfNeeded(PseudoId);
584 // FIXME: Everyone should allow author shadows.
585 virtual bool areAuthorShadowsAllowed() const { return true; }
586 virtual void didAddUserAgentShadowRoot(ShadowRoot&) { }
587 virtual bool alwaysCreateUserAgentShadowRoot() const { return false; }
589 // FIXME: Remove the need for Attr to call willModifyAttribute/didModifyAttribute.
592 enum SynchronizationOfLazyAttribute { NotInSynchronizationOfLazyAttribute = 0, InSynchronizationOfLazyAttribute };
594 void didAddAttribute(const QualifiedName&, const AtomicString&);
595 void willModifyAttribute(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue);
596 void didModifyAttribute(const QualifiedName&, const AtomicString&);
597 void didRemoveAttribute(const QualifiedName&);
599 void synchronizeAttribute(const QualifiedName&) const;
601 void updateId(const AtomicString& oldId, const AtomicString& newId);
602 void updateId(TreeScope&, const AtomicString& oldId, const AtomicString& newId);
603 void updateName(const AtomicString& oldName, const AtomicString& newName);
604 void updateLabel(TreeScope&, const AtomicString& oldForAttributeValue, const AtomicString& newForAttributeValue);
606 void scrollByUnits(int units, ScrollGranularity);
608 virtual NodeType nodeType() const OVERRIDE FINAL;
609 virtual bool childTypeAllowed(NodeType) const OVERRIDE FINAL;
611 void setAttributeInternal(size_t index, const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
612 void addAttributeInternal(const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
613 void removeAttributeInternal(size_t index, SynchronizationOfLazyAttribute);
614 void attributeChangedFromParserOrByCloning(const QualifiedName&, const AtomicString&, AttributeModificationReason);
617 virtual void formatForDebugger(char* buffer, unsigned length) const OVERRIDE;
620 bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
622 void cancelFocusAppearanceUpdate();
624 virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) OVERRIDE { return computedStyle(pseudoElementSpecifier); }
626 inline void updateCallbackSelectors(RenderStyle* oldStyle, RenderStyle* newStyle);
627 inline void removeCallbackSelectors();
628 inline void addCallbackSelectors();
630 // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
632 virtual PassRefPtr<Node> cloneNode(bool deep) OVERRIDE;
633 virtual PassRefPtr<Element> cloneElementWithoutAttributesAndChildren();
635 QualifiedName m_tagName;
637 unsigned rareDataChildIndex() const;
639 SpellcheckAttributeState spellcheckAttributeState() const;
641 void updateNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
642 void updateExtraNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
644 void createUniqueElementData();
646 bool shouldInvalidateDistributionWhenAttributeChanged(ElementShadow*, const QualifiedName&, const AtomicString&);
648 ElementRareData* elementRareData() const;
649 ElementRareData& ensureElementRareData();
651 void detachAllAttrNodesFromElement();
652 void detachAttrNodeFromElementWithValue(Attr*, const AtomicString& value);
653 void detachAttrNodeAtIndex(Attr*, size_t index);
655 bool isJavaScriptURLAttribute(const Attribute&) const;
657 RefPtr<ElementData> m_elementData;
660 DEFINE_NODE_TYPE_CASTS(Element, isElementNode());
661 template <typename T> bool isElementOfType(const Element&);
662 template <typename T> inline bool isElementOfType(const Node& node) { return node.isElementNode() && isElementOfType<const T>(toElement(node)); }
663 template <> inline bool isElementOfType<const Element>(const Element&) { return true; }
666 template<typename T> inline T& toElement(Node& node)
668 ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
669 return static_cast<T&>(node);
671 template<typename T> inline T* toElement(Node* node)
673 ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
674 return static_cast<T*>(node);
676 template<typename T> inline const T& toElement(const Node& node)
678 ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
679 return static_cast<const T&>(node);
681 template<typename T> inline const T* toElement(const Node* node)
683 ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
684 return static_cast<const T*>(node);
686 template<typename T, typename U> inline T* toElement(const RefPtr<U>& node) { return toElement<T>(node.get()); }
688 inline bool isDisabledFormControl(const Node* node)
690 return node->isElementNode() && toElement(node)->isDisabledFormControl();
693 inline bool Node::hasTagName(const QualifiedName& name) const
695 return isElementNode() && toElement(this)->hasTagName(name);
698 inline Element* Node::parentElement() const
700 ContainerNode* parent = parentNode();
701 return parent && parent->isElementNode() ? toElement(parent) : 0;
704 inline bool Element::fastHasAttribute(const QualifiedName& name) const
706 ASSERT(fastAttributeLookupAllowed(name));
707 return elementData() && getAttributeItem(name);
710 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
712 ASSERT(fastAttributeLookupAllowed(name));
714 if (const Attribute* attribute = getAttributeItem(name))
715 return attribute->value();
720 inline bool Element::hasAttributesWithoutUpdate() const
722 return elementData() && !elementData()->isEmpty();
725 inline const AtomicString& Element::idForStyleResolution() const
728 return elementData()->idForStyleResolution();
731 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
733 // FIXME: This check is probably not correct for the case where the document has an id attribute
734 // with a non-null namespace, because it will return false, a false negative, if the prefixes
735 // don't match but the local name and namespace both do. However, since this has been like this
736 // for a while and the code paths may be hot, we'll have to measure performance if we fix it.
737 return attributeName == HTMLNames::idAttr;
740 inline const AtomicString& Element::getIdAttribute() const
742 return hasID() ? fastGetAttribute(HTMLNames::idAttr) : nullAtom;
745 inline const AtomicString& Element::getNameAttribute() const
747 return hasName() ? fastGetAttribute(HTMLNames::nameAttr) : nullAtom;
750 inline const AtomicString& Element::getClassAttribute() const
755 return getAttribute(HTMLNames::classAttr);
756 return fastGetAttribute(HTMLNames::classAttr);
759 inline bool Element::shouldIgnoreAttributeCase() const
761 return isHTMLElement() && document().isHTMLDocument();
764 inline void Element::setIdAttribute(const AtomicString& value)
766 setAttribute(HTMLNames::idAttr, value);
769 inline const SpaceSplitString& Element::classNames() const
772 ASSERT(elementData());
773 return elementData()->classNames();
776 inline size_t Element::attributeCount() const
778 ASSERT(elementData());
779 return elementData()->length();
782 inline const Attribute& Element::attributeItem(unsigned index) const
784 ASSERT(elementData());
785 return elementData()->attributeItem(index);
788 inline const Attribute* Element::getAttributeItem(const QualifiedName& name) const
790 ASSERT(elementData());
791 return elementData()->getAttributeItem(name);
794 inline bool Element::hasID() const
796 return elementData() && elementData()->hasID();
799 inline bool Element::hasClass() const
801 return elementData() && elementData()->hasClass();
804 inline UniqueElementData& Element::ensureUniqueElementData()
806 if (!elementData() || !elementData()->isUnique())
807 createUniqueElementData();
808 return static_cast<UniqueElementData&>(*m_elementData);
811 // Put here to make them inline.
812 inline bool Node::hasID() const
814 return isElementNode() && toElement(this)->hasID();
817 inline bool Node::hasClass() const
819 return isElementNode() && toElement(this)->hasClass();
822 inline Node::InsertionNotificationRequest Node::insertedInto(ContainerNode* insertionPoint)
824 ASSERT(insertionPoint->inDocument() || isContainerNode());
825 if (insertionPoint->inDocument())
826 setFlag(InDocumentFlag);
827 if (parentOrShadowHostNode()->isInShadowTree())
828 setFlag(IsInShadowTreeFlag);
829 if (childNeedsDistributionRecalc() && !insertionPoint->childNeedsDistributionRecalc())
830 insertionPoint->markAncestorsWithChildNeedsDistributionRecalc();
831 return InsertionDone;
834 inline void Node::removedFrom(ContainerNode* insertionPoint)
836 ASSERT(insertionPoint->inDocument() || isContainerNode());
837 if (insertionPoint->inDocument())
838 clearFlag(InDocumentFlag);
839 if (isInShadowTree() && !treeScope().rootNode().isShadowRoot())
840 clearFlag(IsInShadowTreeFlag);
843 inline void Element::invalidateStyleAttribute()
845 ASSERT(elementData());
846 elementData()->m_styleAttributeIsDirty = true;
849 inline const StylePropertySet* Element::presentationAttributeStyle()
853 if (elementData()->m_presentationAttributeStyleIsDirty)
854 updatePresentationAttributeStyle();
855 // Need to call elementData() again since updatePresentationAttributeStyle()
856 // might swap it with a UniqueElementData.
857 return elementData()->presentationAttributeStyle();
860 inline void Element::setTagNameForCreateElementNS(const QualifiedName& tagName)
862 // We expect this method to be called only to reset the prefix.
863 ASSERT(tagName.localName() == m_tagName.localName());
864 ASSERT(tagName.namespaceURI() == m_tagName.namespaceURI());
868 inline bool isShadowHost(const Node* node)
870 return node && node->isElementNode() && toElement(node)->shadow();
873 inline bool isShadowHost(const Element* element)
875 return element && element->shadow();
878 // These macros do the same as their NODE equivalents but additionally provide a template specialization
879 // for isElementOfType<>() so that the Traversal<> API works for these Element types.
880 #define DEFINE_ELEMENT_TYPE_CASTS(thisType, predicate) \
881 template <> inline bool isElementOfType<const thisType>(const Element& element) { return element.predicate; } \
882 DEFINE_NODE_TYPE_CASTS(thisType, predicate)
884 #define DEFINE_ELEMENT_TYPE_CASTS_WITH_FUNCTION(thisType) \
885 template <> inline bool isElementOfType<const thisType>(const Element& element) { return is##thisType(element); } \
886 DEFINE_NODE_TYPE_CASTS_WITH_FUNCTION(thisType)