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> setAttributeNodeNS(Attr*, ExceptionState&);
226 PassRefPtr<Attr> removeAttributeNode(Attr*, ExceptionState&);
228 PassRefPtr<Attr> attrIfExists(const QualifiedName&);
229 PassRefPtr<Attr> ensureAttr(const QualifiedName&);
231 const Vector<RefPtr<Attr> >& attrNodeList();
233 CSSStyleDeclaration* style();
235 const QualifiedName& tagQName() const { return m_tagName; }
236 String tagName() const { return nodeName(); }
237 bool hasTagName(const QualifiedName& tagName) const { return m_tagName.matches(tagName); }
239 // Should be called only by Document::createElementNS to fix up m_tagName immediately after construction.
240 void setTagNameForCreateElementNS(const QualifiedName&);
242 // A fast function for checking the local name against another atomic string.
243 bool hasLocalName(const AtomicString& other) const { return m_tagName.localName() == other; }
244 bool hasLocalName(const QualifiedName& other) const { return m_tagName.localName() == other.localName(); }
246 virtual const AtomicString& localName() const OVERRIDE FINAL { return m_tagName.localName(); }
247 const AtomicString& prefix() const { return m_tagName.prefix(); }
248 virtual const AtomicString& namespaceURI() const OVERRIDE FINAL { return m_tagName.namespaceURI(); }
250 const AtomicString& locateNamespacePrefix(const AtomicString& namespaceURI) const;
252 virtual KURL baseURI() const OVERRIDE FINAL;
254 virtual String nodeName() const OVERRIDE;
256 PassRefPtr<Element> cloneElementWithChildren();
257 PassRefPtr<Element> cloneElementWithoutChildren();
259 void scheduleLayerUpdate();
261 void normalizeAttributes();
263 void setBooleanAttribute(const QualifiedName& name, bool);
265 virtual const StylePropertySet* additionalPresentationAttributeStyle() { return 0; }
266 void invalidateStyleAttribute();
268 const StylePropertySet* inlineStyle() const { return elementData() ? elementData()->m_inlineStyle.get() : 0; }
270 bool setInlineStyleProperty(CSSPropertyID, CSSValueID identifier, bool important = false);
271 bool setInlineStyleProperty(CSSPropertyID, CSSPropertyID identifier, bool important = false);
272 bool setInlineStyleProperty(CSSPropertyID, double value, CSSPrimitiveValue::UnitTypes, bool important = false);
273 bool setInlineStyleProperty(CSSPropertyID, const String& value, bool important = false);
274 bool removeInlineStyleProperty(CSSPropertyID);
275 void removeAllInlineStyleProperties();
277 void synchronizeStyleAttributeInternal() const;
279 const StylePropertySet* presentationAttributeStyle();
280 virtual bool isPresentationAttribute(const QualifiedName&) const { return false; }
281 virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) { }
283 // For exposing to DOM only.
284 NamedNodeMap* attributes() const;
286 enum AttributeModificationReason {
291 // This method is called whenever an attribute is added, changed or removed.
292 virtual void attributeChanged(const QualifiedName&, const AtomicString&, AttributeModificationReason = ModifiedDirectly);
293 virtual void parseAttribute(const QualifiedName&, const AtomicString&) { }
295 virtual bool hasLegalLinkAttribute(const QualifiedName&) const;
296 virtual const QualifiedName& subResourceAttributeName() const;
298 // Only called by the parser immediately after element construction.
299 void parserSetAttributes(const Vector<Attribute>&);
301 // Remove attributes that might introduce scripting from the vector leaving the element unchanged.
302 void stripScriptingAttributes(Vector<Attribute>&) const;
304 const ElementData* elementData() const { return m_elementData.get(); }
305 UniqueElementData& ensureUniqueElementData();
307 void synchronizeAllAttributes() const;
309 // Clones attributes only.
310 void cloneAttributesFromElement(const Element&);
312 // Clones all attribute-derived data, including subclass specifics (through copyNonAttributeProperties.)
313 void cloneDataFromElement(const Element&);
315 bool hasEquivalentAttributes(const Element* other) const;
317 virtual void copyNonAttributePropertiesFromElement(const Element&) { }
319 virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
320 virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
321 virtual RenderObject* createRenderer(RenderStyle*);
322 virtual bool rendererIsNeeded(const RenderStyle&);
323 void recalcStyle(StyleRecalcChange, Text* nextTextSibling = 0);
324 void didAffectSelector(AffectedSelectorMask);
325 void setAnimationStyleChange(bool);
326 void setNeedsAnimationStyleRecalc();
328 bool supportsStyleSharing() const;
330 ElementShadow* shadow() const;
331 ElementShadow& ensureShadow();
332 PassRefPtr<ShadowRoot> createShadowRoot(ExceptionState&);
333 ShadowRoot* shadowRoot() const;
334 ShadowRoot* youngestShadowRoot() const;
336 bool hasAuthorShadowRoot() const { return shadowRoot(); }
337 virtual void didAddShadowRoot(ShadowRoot&);
338 ShadowRoot* userAgentShadowRoot() const;
339 ShadowRoot& ensureUserAgentShadowRoot();
340 bool isInDescendantTreeOf(const Element* shadowHost) const;
342 RenderStyle* computedStyle(PseudoId = NOPSEUDO);
344 // Methods for indicating the style is affected by dynamic updates (e.g., children changing, our position changing in our sibling list, etc.)
345 bool styleAffectedByEmpty() const { return hasElementFlag(StyleAffectedByEmpty); }
346 void setStyleAffectedByEmpty() { setElementFlag(StyleAffectedByEmpty); }
348 void setIsInCanvasSubtree(bool value) { setElementFlag(IsInCanvasSubtree, value); }
349 bool isInCanvasSubtree() const { return hasElementFlag(IsInCanvasSubtree); }
351 unsigned childIndex() const { return hasRareData() ? rareDataChildIndex() : 0; }
352 void setChildIndex(unsigned);
354 bool isUpgradedCustomElement() { return customElementState() == Upgraded; }
355 bool isUnresolvedCustomElement() { return customElementState() == WaitingForUpgrade; }
357 AtomicString computeInheritedLanguage() const;
358 Locale& locale() const;
360 virtual void accessKeyAction(bool /*sendToAnyEvent*/) { }
362 virtual bool isURLAttribute(const Attribute&) const { return false; }
363 virtual bool isHTMLContentAttribute(const Attribute&) const { return false; }
365 KURL getURLAttribute(const QualifiedName&) const;
366 KURL getNonEmptyURLAttribute(const QualifiedName&) const;
368 virtual const AtomicString imageSourceURL() const;
369 virtual Image* imageContents() { return 0; }
371 virtual void focus(bool restorePreviousSelection = true, FocusType = FocusTypeNone);
372 virtual void updateFocusAppearance(bool restorePreviousSelection);
374 // Whether this element can receive focus at all. Most elements are not
375 // focusable but some elements, such as form controls and links, are. Unlike
376 // rendererIsFocusable(), this method may be called when layout is not up to
377 // date, so it must not use the renderer to determine focusability.
378 virtual bool supportsFocus() const { return hasElementFlag(TabIndexWasSetExplicitly); }
379 // Whether the node can actually be focused.
380 bool isFocusable() const;
381 virtual bool isKeyboardFocusable() const;
382 virtual bool isMouseFocusable() const;
383 virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType);
384 virtual void dispatchBlurEvent(Element* newFocusedElement);
385 void dispatchFocusInEvent(const AtomicString& eventType, Element* oldFocusedElement);
386 void dispatchFocusOutEvent(const AtomicString& eventType, Element* newFocusedElement);
390 String innerHTML() const;
391 String outerHTML() const;
392 void setInnerHTML(const String&, ExceptionState&);
393 void setOuterHTML(const String&, ExceptionState&);
395 Element* insertAdjacentElement(const String& where, Element* newChild, ExceptionState&);
396 void insertAdjacentText(const String& where, const String& text, ExceptionState&);
397 void insertAdjacentHTML(const String& where, const String& html, ExceptionState&);
399 String textFromChildren();
401 virtual String title() const { return String(); }
403 virtual const AtomicString& shadowPseudoId() const;
404 void setShadowPseudoId(const AtomicString&);
406 LayoutSize minimumSizeForResizing() const;
407 void setMinimumSizeForResizing(const LayoutSize&);
409 virtual void didBecomeFullscreenElement() { }
410 virtual void willStopBeingFullscreenElement() { }
412 // Called by the parser when this element's close tag is reached,
413 // signaling that all child tags have been parsed and added.
414 // This is needed for <applet> and <object> elements, which can't lay themselves out
415 // until they know all of their nested <param>s. [Radar 3603191, 4040848].
416 // Also used for script elements and some SVG elements for similar purposes,
417 // but making parsing a special case in this respect should be avoided if possible.
418 virtual void finishParsingChildren();
420 void beginParsingChildren() { setIsFinishedParsingChildren(false); }
422 PseudoElement* pseudoElement(PseudoId) const;
423 RenderObject* pseudoElementRenderer(PseudoId) const;
425 virtual bool matchesReadOnlyPseudoClass() const { return false; }
426 virtual bool matchesReadWritePseudoClass() const { return false; }
427 bool matches(const String& selectors, ExceptionState&);
428 virtual bool shouldAppearIndeterminate() const { return false; }
430 DOMTokenList& classList();
432 DOMStringMap& dataset();
434 #if ENABLE(INPUT_SPEECH)
435 virtual bool isInputFieldSpeechButtonElement() const { return false; }
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 isPasswordGeneratorButtonElement() const { return false; }
454 virtual bool isClearButtonElement() const { return false; }
456 virtual bool canContainRangeEndPoint() const OVERRIDE { return true; }
458 // Used for disabled form elements; if true, prevents mouse events from being dispatched
459 // to event listeners, and prevents DOMActivate events from being sent at all.
460 virtual bool isDisabledFormControl() const { return false; }
462 bool hasPendingResources() const { return hasElementFlag(HasPendingResources); }
463 void setHasPendingResources() { setElementFlag(HasPendingResources); }
464 void clearHasPendingResources() { clearElementFlag(HasPendingResources); }
465 virtual void buildPendingResource() { };
467 void setCustomElementDefinition(PassRefPtr<CustomElementDefinition>);
468 CustomElementDefinition* customElementDefinition() const;
471 ALLOW_KEYBOARD_INPUT = 1 << 0,
472 LEGACY_MOZILLA_REQUEST = 1 << 1,
475 void webkitRequestFullScreen(unsigned short flags);
476 bool containsFullScreenElement() const { return hasElementFlag(ContainsFullScreenElement); }
477 void setContainsFullScreenElement(bool);
478 void setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(bool);
481 void webkitRequestFullscreen();
483 bool isInTopLayer() const { return hasElementFlag(IsInTopLayer); }
484 void setIsInTopLayer(bool);
486 void webkitRequestPointerLock();
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();
515 Element(const QualifiedName& tagName, Document* document, ConstructionType type)
516 : ContainerNode(document, type)
519 ScriptWrappable::init(this);
522 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, CSSValueID identifier);
523 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, double value, CSSPrimitiveValue::UnitTypes);
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;
529 virtual void removeAllEventListeners() OVERRIDE;
531 virtual void willRecalcStyle(StyleRecalcChange);
532 virtual void didRecalcStyle(StyleRecalcChange);
533 virtual PassRefPtr<RenderStyle> customStyleForRenderer();
535 virtual bool shouldRegisterAsNamedItem() const { return false; }
536 virtual bool shouldRegisterAsExtraNamedItem() const { return false; }
538 void clearTabIndexExplicitlyIfNeeded();
539 void setTabIndexExplicitly(short);
540 virtual short tabIndex() const OVERRIDE;
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 PassRefPtr<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 // FIXME: These methods should all be renamed to something better than "check",
577 // since it's not clear that they alter the style bits of siblings and children.
578 void checkForSiblingStyleChanges(bool finishedParsingCallback, Node* beforeChange, Node* afterChange, int childCountDelta);
579 inline void checkForEmptyStyleChange(RenderStyle*);
581 void updatePseudoElement(PseudoId, StyleRecalcChange);
583 inline void createPseudoElementIfNeeded(PseudoId);
585 // FIXME: Everyone should allow author shadows.
586 virtual bool areAuthorShadowsAllowed() const { return true; }
587 virtual void didAddUserAgentShadowRoot(ShadowRoot&) { }
588 virtual bool alwaysCreateUserAgentShadowRoot() const { return false; }
590 // FIXME: Remove the need for Attr to call willModifyAttribute/didModifyAttribute.
593 enum SynchronizationOfLazyAttribute { NotInSynchronizationOfLazyAttribute = 0, InSynchronizationOfLazyAttribute };
595 void didAddAttribute(const QualifiedName&, const AtomicString&);
596 void willModifyAttribute(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue);
597 void didModifyAttribute(const QualifiedName&, const AtomicString&);
598 void didRemoveAttribute(const QualifiedName&);
600 void synchronizeAttribute(const QualifiedName&) const;
602 void updateId(const AtomicString& oldId, const AtomicString& newId);
603 void updateId(TreeScope&, const AtomicString& oldId, const AtomicString& newId);
604 void updateName(const AtomicString& oldName, const AtomicString& newName);
605 void updateLabel(TreeScope&, const AtomicString& oldForAttributeValue, const AtomicString& newForAttributeValue);
607 void scrollByUnits(int units, ScrollGranularity);
609 virtual NodeType nodeType() const OVERRIDE FINAL;
610 virtual bool childTypeAllowed(NodeType) const OVERRIDE FINAL;
612 void setAttributeInternal(size_t index, const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
613 void addAttributeInternal(const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
614 void removeAttributeInternal(size_t index, SynchronizationOfLazyAttribute);
615 void attributeChangedFromParserOrByCloning(const QualifiedName&, const AtomicString&, AttributeModificationReason);
618 virtual void formatForDebugger(char* buffer, unsigned length) const OVERRIDE;
621 bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
623 void cancelFocusAppearanceUpdate();
625 virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) OVERRIDE { return computedStyle(pseudoElementSpecifier); }
627 inline void updateCallbackSelectors(RenderStyle* oldStyle, RenderStyle* newStyle);
628 inline void removeCallbackSelectors();
629 inline void addCallbackSelectors();
631 // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
633 virtual PassRefPtr<Node> cloneNode(bool deep) OVERRIDE;
634 virtual PassRefPtr<Element> cloneElementWithoutAttributesAndChildren();
636 QualifiedName m_tagName;
638 unsigned rareDataChildIndex() const;
640 SpellcheckAttributeState spellcheckAttributeState() const;
642 void updateNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
643 void updateExtraNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
645 void createUniqueElementData();
647 bool shouldInvalidateDistributionWhenAttributeChanged(ElementShadow*, const QualifiedName&, const AtomicString&);
649 ElementRareData* elementRareData() const;
650 ElementRareData& ensureElementRareData();
652 void detachAllAttrNodesFromElement();
653 void detachAttrNodeFromElementWithValue(Attr*, const AtomicString& value);
654 void detachAttrNodeAtIndex(Attr*, size_t index);
656 bool isJavaScriptURLAttribute(const Attribute&) const;
658 RefPtr<ElementData> m_elementData;
661 DEFINE_NODE_TYPE_CASTS(Element, isElementNode());
662 template <typename T> bool isElementOfType(const Element&);
663 template <typename T> inline bool isElementOfType(const Node& node) { return node.isElementNode() && isElementOfType<const T>(toElement(node)); }
664 template <> inline bool isElementOfType<const Element>(const Element&) { return true; }
667 template<typename T> inline T& toElement(Node& node)
669 ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
670 return static_cast<T&>(node);
672 template<typename T> inline T* toElement(Node* node)
674 ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
675 return static_cast<T*>(node);
677 template<typename T> inline const T& toElement(const Node& node)
679 ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
680 return static_cast<const T&>(node);
682 template<typename T> inline const T* toElement(const Node* node)
684 ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
685 return static_cast<const T*>(node);
687 template<typename T, typename U> inline T* toElement(const RefPtr<U>& node) { return toElement<T>(node.get()); }
689 inline bool isDisabledFormControl(const Node* node)
691 return node->isElementNode() && toElement(node)->isDisabledFormControl();
694 inline bool Node::hasTagName(const QualifiedName& name) const
696 return isElementNode() && toElement(this)->hasTagName(name);
699 inline Element* Node::parentElement() const
701 ContainerNode* parent = parentNode();
702 return parent && parent->isElementNode() ? toElement(parent) : 0;
705 inline bool Element::fastHasAttribute(const QualifiedName& name) const
707 ASSERT(fastAttributeLookupAllowed(name));
708 return elementData() && getAttributeItem(name);
711 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
713 ASSERT(fastAttributeLookupAllowed(name));
715 if (const Attribute* attribute = getAttributeItem(name))
716 return attribute->value();
721 inline bool Element::hasAttributesWithoutUpdate() const
723 return elementData() && !elementData()->isEmpty();
726 inline const AtomicString& Element::idForStyleResolution() const
729 return elementData()->idForStyleResolution();
732 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
734 // FIXME: This check is probably not correct for the case where the document has an id attribute
735 // with a non-null namespace, because it will return false, a false negative, if the prefixes
736 // don't match but the local name and namespace both do. However, since this has been like this
737 // for a while and the code paths may be hot, we'll have to measure performance if we fix it.
738 return attributeName == HTMLNames::idAttr;
741 inline const AtomicString& Element::getIdAttribute() const
743 return hasID() ? fastGetAttribute(HTMLNames::idAttr) : nullAtom;
746 inline const AtomicString& Element::getNameAttribute() const
748 return hasName() ? fastGetAttribute(HTMLNames::nameAttr) : nullAtom;
751 inline const AtomicString& Element::getClassAttribute() const
756 return getAttribute(HTMLNames::classAttr);
757 return fastGetAttribute(HTMLNames::classAttr);
760 inline bool Element::shouldIgnoreAttributeCase() const
762 return isHTMLElement() && document().isHTMLDocument();
765 inline void Element::setIdAttribute(const AtomicString& value)
767 setAttribute(HTMLNames::idAttr, value);
770 inline const SpaceSplitString& Element::classNames() const
773 ASSERT(elementData());
774 return elementData()->classNames();
777 inline size_t Element::attributeCount() const
779 ASSERT(elementData());
780 return elementData()->length();
783 inline const Attribute& Element::attributeItem(unsigned index) const
785 ASSERT(elementData());
786 return elementData()->attributeItem(index);
789 inline const Attribute* Element::getAttributeItem(const QualifiedName& name) const
791 ASSERT(elementData());
792 return elementData()->getAttributeItem(name);
795 inline bool Element::hasID() const
797 return elementData() && elementData()->hasID();
800 inline bool Element::hasClass() const
802 return elementData() && elementData()->hasClass();
805 inline UniqueElementData& Element::ensureUniqueElementData()
807 if (!elementData() || !elementData()->isUnique())
808 createUniqueElementData();
809 return static_cast<UniqueElementData&>(*m_elementData);
812 // Put here to make them inline.
813 inline bool Node::hasID() const
815 return isElementNode() && toElement(this)->hasID();
818 inline bool Node::hasClass() const
820 return isElementNode() && toElement(this)->hasClass();
823 inline Node::InsertionNotificationRequest Node::insertedInto(ContainerNode* insertionPoint)
825 ASSERT(insertionPoint->inDocument() || isContainerNode());
826 if (insertionPoint->inDocument())
827 setFlag(InDocumentFlag);
828 if (parentOrShadowHostNode()->isInShadowTree())
829 setFlag(IsInShadowTreeFlag);
830 if (childNeedsDistributionRecalc() && !insertionPoint->childNeedsDistributionRecalc())
831 insertionPoint->markAncestorsWithChildNeedsDistributionRecalc();
832 return InsertionDone;
835 inline void Node::removedFrom(ContainerNode* insertionPoint)
837 ASSERT(insertionPoint->inDocument() || isContainerNode());
838 if (insertionPoint->inDocument())
839 clearFlag(InDocumentFlag);
840 if (isInShadowTree() && !treeScope().rootNode().isShadowRoot())
841 clearFlag(IsInShadowTreeFlag);
844 inline void Element::invalidateStyleAttribute()
846 ASSERT(elementData());
847 elementData()->m_styleAttributeIsDirty = true;
850 inline const StylePropertySet* Element::presentationAttributeStyle()
854 if (elementData()->m_presentationAttributeStyleIsDirty)
855 updatePresentationAttributeStyle();
856 // Need to call elementData() again since updatePresentationAttributeStyle()
857 // might swap it with a UniqueElementData.
858 return elementData()->presentationAttributeStyle();
861 inline void Element::setTagNameForCreateElementNS(const QualifiedName& tagName)
863 // We expect this method to be called only to reset the prefix.
864 ASSERT(tagName.localName() == m_tagName.localName());
865 ASSERT(tagName.namespaceURI() == m_tagName.namespaceURI());
869 inline bool isShadowHost(const Node* node)
871 return node && node->isElementNode() && toElement(node)->shadow();
874 inline bool isShadowHost(const Element* element)
876 return element && element->shadow();
879 // These macros do the same as their NODE equivalents but additionally provide a template specialization
880 // for isElementOfType<>() so that the Traversal<> API works for these Element types.
881 #define DEFINE_ELEMENT_TYPE_CASTS(thisType, predicate) \
882 template <> inline bool isElementOfType<const thisType>(const Element& element) { return element.predicate; } \
883 DEFINE_NODE_TYPE_CASTS(thisType, predicate)
885 #define DEFINE_ELEMENT_TYPE_CASTS_WITH_FUNCTION(thisType) \
886 template <> inline bool isElementOfType<const thisType>(const Element& element) { return is##thisType(element); } \
887 DEFINE_NODE_TYPE_CASTS_WITH_FUNCTION(thisType)