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;
48 class ElementRareData;
52 class InputMethodContext;
55 class MutableStylePropertySet;
56 class PropertySetCSSStyleDeclaration;
59 class StylePropertySet;
61 enum AffectedSelectorType {
62 AffectedSelectorChecked = 1,
63 AffectedSelectorEnabled = 1 << 1,
64 AffectedSelectorDisabled = 1 << 2,
65 AffectedSelectorIndeterminate = 1 << 3,
66 AffectedSelectorLink = 1 << 4,
67 AffectedSelectorTarget = 1 << 5,
68 AffectedSelectorVisited = 1 << 6
70 typedef int AffectedSelectorMask;
72 enum SpellcheckAttributeState {
73 SpellcheckAttributeTrue,
74 SpellcheckAttributeFalse,
75 SpellcheckAttributeDefault
78 class Element : public ContainerNode {
80 static PassRefPtr<Element> create(const QualifiedName&, Document*);
83 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecopy);
84 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecut);
85 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforepaste);
86 DEFINE_ATTRIBUTE_EVENT_LISTENER(copy);
87 DEFINE_ATTRIBUTE_EVENT_LISTENER(cut);
88 DEFINE_ATTRIBUTE_EVENT_LISTENER(paste);
89 DEFINE_ATTRIBUTE_EVENT_LISTENER(search);
90 DEFINE_ATTRIBUTE_EVENT_LISTENER(selectstart);
91 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchcancel);
92 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchend);
93 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchmove);
94 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchstart);
95 DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitfullscreenchange);
96 DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitfullscreenerror);
97 DEFINE_ATTRIBUTE_EVENT_LISTENER(wheel);
99 bool hasAttribute(const QualifiedName&) const;
100 const AtomicString& getAttribute(const QualifiedName&) const;
102 // Passing nullAtom as the second parameter removes the attribute when calling either of these set methods.
103 void setAttribute(const QualifiedName&, const AtomicString& value);
104 void setSynchronizedLazyAttribute(const QualifiedName&, const AtomicString& value);
106 void removeAttribute(const QualifiedName&);
108 // Typed getters and setters for language bindings.
109 int getIntegralAttribute(const QualifiedName& attributeName) const;
110 void setIntegralAttribute(const QualifiedName& attributeName, int value);
111 unsigned getUnsignedIntegralAttribute(const QualifiedName& attributeName) const;
112 void setUnsignedIntegralAttribute(const QualifiedName& attributeName, unsigned value);
113 double getFloatingPointAttribute(const QualifiedName& attributeName, double fallbackValue = std::numeric_limits<double>::quiet_NaN()) const;
114 void setFloatingPointAttribute(const QualifiedName& attributeName, double value);
116 // Call this to get the value of an attribute that is known not to be the style
117 // attribute or one of the SVG animatable attributes.
118 bool fastHasAttribute(const QualifiedName&) const;
119 const AtomicString& fastGetAttribute(const QualifiedName&) const;
121 bool fastAttributeLookupAllowed(const QualifiedName&) const;
124 #ifdef DUMP_NODE_STATISTICS
125 bool hasNamedNodeMap() const;
127 bool hasAttributes() const;
128 // This variant will not update the potentially invalid attributes. To be used when not interested
129 // in style attribute or one of the SVG animation attributes.
130 bool hasAttributesWithoutUpdate() const;
132 bool hasAttribute(const AtomicString& name) const;
133 bool hasAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
135 const AtomicString& getAttribute(const AtomicString& name) const;
136 const AtomicString& getAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
138 void setAttribute(const AtomicString& name, const AtomicString& value, ExceptionState&);
139 static bool parseAttributeName(QualifiedName&, const AtomicString& namespaceURI, const AtomicString& qualifiedName, ExceptionState&);
140 void setAttributeNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, const AtomicString& value, ExceptionState&);
142 bool isIdAttributeName(const QualifiedName&) const;
143 const AtomicString& getIdAttribute() const;
144 void setIdAttribute(const AtomicString&);
146 const AtomicString& getNameAttribute() const;
147 const AtomicString& getClassAttribute() const;
149 bool shouldIgnoreAttributeCase() const;
151 // Call this to get the value of the id attribute for style resolution purposes.
152 // The value will already be lowercased if the document is in compatibility mode,
153 // so this function is not suitable for non-style uses.
154 const AtomicString& idForStyleResolution() const;
156 // Internal methods that assume the existence of attribute storage, one should use hasAttributes()
157 // before calling them.
158 size_t attributeCount() const;
159 const Attribute* attributeItem(unsigned index) const;
160 const Attribute* getAttributeItem(const QualifiedName&) const;
161 size_t getAttributeItemIndex(const QualifiedName& name) const { return elementData()->getAttributeItemIndex(name); }
162 size_t getAttributeItemIndex(const AtomicString& name, bool shouldIgnoreAttributeCase) const { return elementData()->getAttributeItemIndex(name, shouldIgnoreAttributeCase); }
164 void scrollIntoView(bool alignToTop = true);
165 void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
167 void scrollByLines(int lines);
168 void scrollByPages(int pages);
175 // FIXME: Replace uses of offsetParent in the platform with calls
176 // to the render layer and merge offsetParentForBindings and offsetParent.
177 Element* offsetParentForBindings();
179 Element* offsetParent();
184 virtual int scrollLeft();
185 virtual int scrollTop();
186 virtual void setScrollLeft(int);
187 virtual void setScrollLeft(const Dictionary& scrollOptionsHorizontal, ExceptionState&);
188 virtual void setScrollTop(int);
189 virtual void setScrollTop(const Dictionary& scrollOptionsVertical, ExceptionState&);
190 virtual int scrollWidth();
191 virtual int scrollHeight();
193 IntRect boundsInRootViewSpace();
195 PassRefPtr<ClientRectList> getClientRects();
196 PassRefPtr<ClientRect> getBoundingClientRect();
198 // Returns the absolute bounding box translated into screen coordinates:
199 IntRect screenRect() const;
201 virtual void didMoveToNewDocument(Document&) OVERRIDE;
203 void removeAttribute(const AtomicString& name);
204 void removeAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName);
206 PassRefPtr<Attr> detachAttribute(size_t index);
208 PassRefPtr<Attr> getAttributeNode(const AtomicString& name);
209 PassRefPtr<Attr> getAttributeNodeNS(const AtomicString& namespaceURI, const AtomicString& localName);
210 PassRefPtr<Attr> setAttributeNode(Attr*, ExceptionState&);
211 PassRefPtr<Attr> removeAttributeNode(Attr*, ExceptionState&);
213 PassRefPtr<Attr> attrIfExists(const QualifiedName&);
214 PassRefPtr<Attr> ensureAttr(const QualifiedName&);
216 const Vector<RefPtr<Attr> >& attrNodeList();
218 CSSStyleDeclaration* style();
220 const QualifiedName& tagQName() const { return m_tagName; }
221 String tagName() const { return nodeName(); }
222 bool hasTagName(const QualifiedName& tagName) const { return m_tagName.matches(tagName); }
224 // Should be called only by Document::createElementNS to fix up m_tagName immediately after construction.
225 void setTagNameForCreateElementNS(const QualifiedName&);
227 // A fast function for checking the local name against another atomic string.
228 bool hasLocalName(const AtomicString& other) const { return m_tagName.localName() == other; }
229 bool hasLocalName(const QualifiedName& other) const { return m_tagName.localName() == other.localName(); }
231 virtual const AtomicString& localName() const OVERRIDE FINAL { return m_tagName.localName(); }
232 const AtomicString& prefix() const { return m_tagName.prefix(); }
233 virtual const AtomicString& namespaceURI() const OVERRIDE FINAL { return m_tagName.namespaceURI(); }
235 const AtomicString& locateNamespacePrefix(const AtomicString& namespaceURI) const;
237 virtual KURL baseURI() const OVERRIDE FINAL;
239 virtual String nodeName() const OVERRIDE;
241 PassRefPtr<Element> cloneElementWithChildren();
242 PassRefPtr<Element> cloneElementWithoutChildren();
244 void scheduleLayerUpdate();
246 void normalizeAttributes();
248 void setBooleanAttribute(const QualifiedName& name, bool);
250 virtual const StylePropertySet* additionalPresentationAttributeStyle() { return 0; }
251 void invalidateStyleAttribute();
253 const StylePropertySet* inlineStyle() const { return elementData() ? elementData()->m_inlineStyle.get() : 0; }
255 bool setInlineStyleProperty(CSSPropertyID, CSSValueID identifier, bool important = false);
256 bool setInlineStyleProperty(CSSPropertyID, CSSPropertyID identifier, bool important = false);
257 bool setInlineStyleProperty(CSSPropertyID, double value, CSSPrimitiveValue::UnitTypes, bool important = false);
258 bool setInlineStyleProperty(CSSPropertyID, const String& value, bool important = false);
259 bool removeInlineStyleProperty(CSSPropertyID);
260 void removeAllInlineStyleProperties();
262 void synchronizeStyleAttributeInternal() const;
264 const StylePropertySet* presentationAttributeStyle();
265 virtual bool isPresentationAttribute(const QualifiedName&) const { return false; }
266 virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) { }
268 // For exposing to DOM only.
269 NamedNodeMap* attributes() const;
271 enum AttributeModificationReason {
276 // This method is called whenever an attribute is added, changed or removed.
277 virtual void attributeChanged(const QualifiedName&, const AtomicString&, AttributeModificationReason = ModifiedDirectly);
278 virtual void parseAttribute(const QualifiedName&, const AtomicString&) { }
280 // Only called by the parser immediately after element construction.
281 void parserSetAttributes(const Vector<Attribute>&);
283 // Remove attributes that might introduce scripting from the vector leaving the element unchanged.
284 void stripScriptingAttributes(Vector<Attribute>&) const;
286 const ElementData* elementData() const { return m_elementData.get(); }
287 UniqueElementData* ensureUniqueElementData();
289 void synchronizeAllAttributes() const;
291 // Clones attributes only.
292 void cloneAttributesFromElement(const Element&);
294 // Clones all attribute-derived data, including subclass specifics (through copyNonAttributeProperties.)
295 void cloneDataFromElement(const Element&);
297 bool hasEquivalentAttributes(const Element* other) const;
299 virtual void copyNonAttributePropertiesFromElement(const Element&) { }
301 virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
302 virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
303 virtual RenderObject* createRenderer(RenderStyle*);
304 virtual bool rendererIsNeeded(const RenderStyle&);
305 void recalcStyle(StyleRecalcChange, Text* nextTextSibling = 0);
306 void didAffectSelector(AffectedSelectorMask);
307 void setAnimationStyleChange(bool);
308 void setNeedsAnimationStyleRecalc();
310 bool supportsStyleSharing() const;
312 ElementShadow* shadow() const;
313 ElementShadow& ensureShadow();
314 PassRefPtr<ShadowRoot> createShadowRoot(ExceptionState&);
315 ShadowRoot* shadowRoot() const;
316 ShadowRoot* youngestShadowRoot() const;
318 bool hasAuthorShadowRoot() const { return shadowRoot(); }
319 virtual void didAddShadowRoot(ShadowRoot&);
320 ShadowRoot* userAgentShadowRoot() const;
321 ShadowRoot& ensureUserAgentShadowRoot();
322 bool isInDescendantTreeOf(const Element* shadowHost) const;
324 RenderStyle* computedStyle(PseudoId = NOPSEUDO);
326 // Methods for indicating the style is affected by dynamic updates (e.g., children changing, our position changing in our sibling list, etc.)
327 bool styleAffectedByEmpty() const { return hasRareData() && rareDataStyleAffectedByEmpty(); }
328 bool childrenAffectedByFocus() const { return hasRareData() && rareDataChildrenAffectedByFocus(); }
329 bool childrenAffectedByHover() const { return hasRareData() && rareDataChildrenAffectedByHover(); }
330 bool childrenAffectedByActive() const { return hasRareData() && rareDataChildrenAffectedByActive(); }
331 bool childrenAffectedByDrag() const { return hasRareData() && rareDataChildrenAffectedByDrag(); }
332 bool childrenAffectedByPositionalRules() const { return hasRareData() && (rareDataChildrenAffectedByForwardPositionalRules() || rareDataChildrenAffectedByBackwardPositionalRules()); }
333 bool childrenAffectedByFirstChildRules() const { return hasRareData() && rareDataChildrenAffectedByFirstChildRules(); }
334 bool childrenAffectedByLastChildRules() const { return hasRareData() && rareDataChildrenAffectedByLastChildRules(); }
335 bool childrenAffectedByDirectAdjacentRules() const { return hasRareData() && rareDataChildrenAffectedByDirectAdjacentRules(); }
336 bool childrenAffectedByForwardPositionalRules() const { return hasRareData() && rareDataChildrenAffectedByForwardPositionalRules(); }
337 bool childrenAffectedByBackwardPositionalRules() const { return hasRareData() && rareDataChildrenAffectedByBackwardPositionalRules(); }
338 unsigned childIndex() const { return hasRareData() ? rareDataChildIndex() : 0; }
340 bool childrenSupportStyleSharing() const;
342 void setStyleAffectedByEmpty();
343 void setChildrenAffectedByFocus();
344 void setChildrenAffectedByHover();
345 void setChildrenAffectedByActive();
346 void setChildrenAffectedByDrag();
347 void setChildrenAffectedByFirstChildRules();
348 void setChildrenAffectedByLastChildRules();
349 void setChildrenAffectedByDirectAdjacentRules();
350 void setChildrenAffectedByForwardPositionalRules();
351 void setChildrenAffectedByBackwardPositionalRules();
352 void setChildIndex(unsigned);
354 void setIsInCanvasSubtree(bool);
355 bool isInCanvasSubtree() const;
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 using Node::isFinishedParsingChildren; // make public for SelectorChecker
417 // Called by the parser when this element's close tag is reached,
418 // signaling that all child tags have been parsed and added.
419 // This is needed for <applet> and <object> elements, which can't lay themselves out
420 // until they know all of their nested <param>s. [Radar 3603191, 4040848].
421 // Also used for script elements and some SVG elements for similar purposes,
422 // but making parsing a special case in this respect should be avoided if possible.
423 virtual void finishParsingChildren();
425 void beginParsingChildren() { setIsFinishedParsingChildren(false); }
427 PseudoElement* pseudoElement(PseudoId) const;
428 RenderObject* pseudoElementRenderer(PseudoId) const;
430 virtual bool matchesReadOnlyPseudoClass() const { return false; }
431 virtual bool matchesReadWritePseudoClass() const { return false; }
432 bool matches(const String& selectors, ExceptionState&);
433 virtual bool shouldAppearIndeterminate() const { return false; }
435 DOMTokenList* classList();
437 DOMStringMap* dataset();
439 virtual bool isMediaElement() const { return false; }
441 #if ENABLE(INPUT_SPEECH)
442 virtual bool isInputFieldSpeechButtonElement() const { return false; }
444 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
445 virtual bool isDateTimeEditElement() const { return false; }
446 virtual bool isDateTimeFieldElement() const { return false; }
447 virtual bool isPickerIndicatorElement() const { return false; }
450 virtual bool isFormControlElement() const { return false; }
451 virtual bool isSpinButtonElement() const { return false; }
452 virtual bool isTextFormControl() const { return false; }
453 virtual bool isOptionalFormControl() const { return false; }
454 virtual bool isRequiredFormControl() const { return false; }
455 virtual bool isDefaultButtonForForm() const { return false; }
456 virtual bool willValidate() const { return false; }
457 virtual bool isValidFormControlElement() { return false; }
458 virtual bool isInRange() const { return false; }
459 virtual bool isOutOfRange() const { return false; }
460 virtual bool isFrameElementBase() const { return false; }
461 virtual bool isPasswordGeneratorButtonElement() const { return false; }
462 virtual bool isClearButtonElement() const { return false; }
464 virtual bool canContainRangeEndPoint() const OVERRIDE { return true; }
466 // Used for disabled form elements; if true, prevents mouse events from being dispatched
467 // to event listeners, and prevents DOMActivate events from being sent at all.
468 virtual bool isDisabledFormControl() const { return false; }
470 bool hasPendingResources() const;
471 void setHasPendingResources();
472 void clearHasPendingResources();
473 virtual void buildPendingResource() { };
476 ALLOW_KEYBOARD_INPUT = 1 << 0,
477 LEGACY_MOZILLA_REQUEST = 1 << 1,
480 void webkitRequestFullScreen(unsigned short flags);
481 bool containsFullScreenElement() const;
482 void setContainsFullScreenElement(bool);
483 void setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(bool);
486 void webkitRequestFullscreen();
488 bool isInTopLayer() const;
489 void setIsInTopLayer(bool);
491 void webkitRequestPointerLock();
493 bool isSpellCheckingEnabled() const;
495 // FIXME: public for RenderTreeBuilder, we shouldn't expose this though.
496 PassRefPtr<RenderStyle> styleForRenderer();
499 bool hasClass() const;
500 const SpaceSplitString& classNames() const;
502 IntSize savedLayerScrollOffset() const;
503 void setSavedLayerScrollOffset(const IntSize&);
505 ActiveAnimations* activeAnimations() const;
506 ActiveAnimations* ensureActiveAnimations();
507 bool hasActiveAnimations() const;
509 InputMethodContext* inputMethodContext();
510 bool hasInputMethodContext() const;
512 void setPrefix(const AtomicString&, ExceptionState&);
514 void synchronizeAttribute(const AtomicString& localName) const;
516 MutableStylePropertySet* ensureMutableInlineStyle();
517 void clearMutableInlineStyleIfEmpty();
520 Element(const QualifiedName& tagName, Document* document, ConstructionType type)
521 : ContainerNode(document, type)
524 ScriptWrappable::init(this);
527 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, CSSValueID identifier);
528 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, double value, CSSPrimitiveValue::UnitTypes);
529 void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, const String& value);
531 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
532 virtual void removedFrom(ContainerNode*) OVERRIDE;
533 virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0) OVERRIDE;
534 virtual void removeAllEventListeners() OVERRIDE;
536 virtual void willRecalcStyle(StyleRecalcChange);
537 virtual void didRecalcStyle(StyleRecalcChange);
538 virtual PassRefPtr<RenderStyle> customStyleForRenderer();
540 virtual bool shouldRegisterAsNamedItem() const { return false; }
541 virtual bool shouldRegisterAsExtraNamedItem() const { return false; }
543 void clearTabIndexExplicitlyIfNeeded();
544 void setTabIndexExplicitly(short);
545 virtual short tabIndex() const OVERRIDE;
546 // Subclasses may override this method to affect focusability. Unlike
547 // supportsFocus, this method must be called on an up-to-date layout, so it
548 // may use the renderer to reason about focusability. This method cannot be
549 // moved to RenderObject because some focusable nodes don't have renderers,
550 // e.g., HTMLOptionElement.
551 virtual bool rendererIsFocusable() const;
552 PassRefPtr<HTMLCollection> ensureCachedHTMLCollection(CollectionType);
553 HTMLCollection* cachedHTMLCollection(CollectionType);
555 // classAttributeChanged() exists to share code between
556 // parseAttribute (called via setAttribute()) and
557 // svgAttributeChanged (called when element.className.baseValue is set)
558 void classAttributeChanged(const AtomicString& newClassString);
560 PassRefPtr<RenderStyle> originalStyleForRenderer();
562 Node* insertAdjacent(const String& where, Node* newChild, ExceptionState&);
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 checkForChildrenAdjacentRuleChanges();
579 void checkForSiblingStyleChanges(bool finishedParsingCallback, Node* beforeChange, Node* afterChange, int childCountDelta);
580 inline void checkForEmptyStyleChange(RenderStyle*);
582 void updatePseudoElement(PseudoId, StyleRecalcChange);
584 inline void createPseudoElementIfNeeded(PseudoId);
586 // FIXME: Everyone should allow author shadows.
587 virtual bool areAuthorShadowsAllowed() const { return true; }
588 virtual void didAddUserAgentShadowRoot(ShadowRoot&) { }
589 virtual bool alwaysCreateUserAgentShadowRoot() const { return false; }
591 // FIXME: Remove the need for Attr to call willModifyAttribute/didModifyAttribute.
594 enum SynchronizationOfLazyAttribute { NotInSynchronizationOfLazyAttribute = 0, InSynchronizationOfLazyAttribute };
596 void didAddAttribute(const QualifiedName&, const AtomicString&);
597 void willModifyAttribute(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue);
598 void didModifyAttribute(const QualifiedName&, const AtomicString&);
599 void didRemoveAttribute(const QualifiedName&);
601 void synchronizeAttribute(const QualifiedName&) const;
603 void updateId(const AtomicString& oldId, const AtomicString& newId);
604 void updateId(TreeScope&, const AtomicString& oldId, const AtomicString& newId);
605 void updateName(const AtomicString& oldName, const AtomicString& newName);
606 void updateLabel(TreeScope&, const AtomicString& oldForAttributeValue, const AtomicString& newForAttributeValue);
608 void scrollByUnits(int units, ScrollGranularity);
610 virtual NodeType nodeType() const OVERRIDE FINAL;
611 virtual bool childTypeAllowed(NodeType) const OVERRIDE FINAL;
613 void setAttributeInternal(size_t index, const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
614 void addAttributeInternal(const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
615 void removeAttributeInternal(size_t index, SynchronizationOfLazyAttribute);
616 void attributeChangedFromParserOrByCloning(const QualifiedName&, const AtomicString&, AttributeModificationReason);
619 virtual void formatForDebugger(char* buffer, unsigned length) const OVERRIDE;
622 bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
624 void cancelFocusAppearanceUpdate();
626 virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) OVERRIDE { return computedStyle(pseudoElementSpecifier); }
628 inline void updateCallbackSelectors(RenderStyle* oldStyle, RenderStyle* newStyle);
629 inline void removeCallbackSelectors();
630 inline void addCallbackSelectors();
632 // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
634 virtual PassRefPtr<Node> cloneNode(bool deep) OVERRIDE;
635 virtual PassRefPtr<Element> cloneElementWithoutAttributesAndChildren();
637 QualifiedName m_tagName;
638 bool rareDataStyleAffectedByEmpty() const;
639 bool rareDataChildrenAffectedByFocus() const;
640 bool rareDataChildrenAffectedByHover() const;
641 bool rareDataChildrenAffectedByActive() const;
642 bool rareDataChildrenAffectedByDrag() const;
643 bool rareDataChildrenAffectedByFirstChildRules() const;
644 bool rareDataChildrenAffectedByLastChildRules() const;
645 bool rareDataChildrenAffectedByDirectAdjacentRules() const;
646 bool rareDataChildrenAffectedByForwardPositionalRules() const;
647 bool rareDataChildrenAffectedByBackwardPositionalRules() const;
648 unsigned rareDataChildIndex() const;
650 SpellcheckAttributeState spellcheckAttributeState() const;
652 void updateNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
653 void updateExtraNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
655 void createUniqueElementData();
657 bool shouldInvalidateDistributionWhenAttributeChanged(ElementShadow*, const QualifiedName&, const AtomicString&);
659 ElementRareData* elementRareData() const;
660 ElementRareData& ensureElementRareData();
662 void detachAllAttrNodesFromElement();
663 void detachAttrNodeFromElementWithValue(Attr*, const AtomicString& value);
664 void detachAttrNodeAtIndex(Attr*, size_t index);
666 bool isJavaScriptURLAttribute(const Attribute&) const;
668 RefPtr<ElementData> m_elementData;
671 DEFINE_NODE_TYPE_CASTS(Element, isElementNode());
673 inline bool isDisabledFormControl(const Node* node)
675 return node->isElementNode() && toElement(node)->isDisabledFormControl();
678 inline bool Node::hasTagName(const QualifiedName& name) const
680 return isElementNode() && toElement(this)->hasTagName(name);
683 inline Element* Node::parentElement() const
685 ContainerNode* parent = parentNode();
686 return parent && parent->isElementNode() ? toElement(parent) : 0;
689 inline bool Element::fastHasAttribute(const QualifiedName& name) const
691 ASSERT(fastAttributeLookupAllowed(name));
692 return elementData() && getAttributeItem(name);
695 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
697 ASSERT(fastAttributeLookupAllowed(name));
699 if (const Attribute* attribute = getAttributeItem(name))
700 return attribute->value();
705 inline bool Element::hasAttributesWithoutUpdate() const
707 return elementData() && !elementData()->isEmpty();
710 inline const AtomicString& Element::idForStyleResolution() const
713 return elementData()->idForStyleResolution();
716 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
718 // FIXME: This check is probably not correct for the case where the document has an id attribute
719 // with a non-null namespace, because it will return false, a false negative, if the prefixes
720 // don't match but the local name and namespace both do. However, since this has been like this
721 // for a while and the code paths may be hot, we'll have to measure performance if we fix it.
722 return attributeName == HTMLNames::idAttr;
725 inline const AtomicString& Element::getIdAttribute() const
727 return hasID() ? fastGetAttribute(HTMLNames::idAttr) : nullAtom;
730 inline const AtomicString& Element::getNameAttribute() const
732 return hasName() ? fastGetAttribute(HTMLNames::nameAttr) : nullAtom;
735 inline const AtomicString& Element::getClassAttribute() const
740 return getAttribute(HTMLNames::classAttr);
741 return fastGetAttribute(HTMLNames::classAttr);
744 inline bool Element::shouldIgnoreAttributeCase() const
746 return isHTMLElement() && document().isHTMLDocument();
749 inline void Element::setIdAttribute(const AtomicString& value)
751 setAttribute(HTMLNames::idAttr, value);
754 inline const SpaceSplitString& Element::classNames() const
757 ASSERT(elementData());
758 return elementData()->classNames();
761 inline size_t Element::attributeCount() const
763 ASSERT(elementData());
764 return elementData()->length();
767 inline const Attribute* Element::attributeItem(unsigned index) const
769 ASSERT(elementData());
770 return elementData()->attributeItem(index);
773 inline const Attribute* Element::getAttributeItem(const QualifiedName& name) const
775 ASSERT(elementData());
776 return elementData()->getAttributeItem(name);
779 inline bool Element::hasID() const
781 return elementData() && elementData()->hasID();
784 inline bool Element::hasClass() const
786 return elementData() && elementData()->hasClass();
789 inline UniqueElementData* Element::ensureUniqueElementData()
791 if (!elementData() || !elementData()->isUnique())
792 createUniqueElementData();
793 return static_cast<UniqueElementData*>(m_elementData.get());
796 // Put here to make them inline.
797 inline bool Node::hasID() const
799 return isElementNode() && toElement(this)->hasID();
802 inline bool Node::hasClass() const
804 return isElementNode() && toElement(this)->hasClass();
807 inline Node::InsertionNotificationRequest Node::insertedInto(ContainerNode* insertionPoint)
809 ASSERT(insertionPoint->inDocument() || isContainerNode());
810 if (insertionPoint->inDocument())
811 setFlag(InDocumentFlag);
812 if (parentOrShadowHostNode()->isInShadowTree())
813 setFlag(IsInShadowTreeFlag);
814 if (childNeedsDistributionRecalc() && !insertionPoint->childNeedsDistributionRecalc())
815 insertionPoint->markAncestorsWithChildNeedsDistributionRecalc();
816 return InsertionDone;
819 inline void Node::removedFrom(ContainerNode* insertionPoint)
821 ASSERT(insertionPoint->inDocument() || isContainerNode());
822 if (insertionPoint->inDocument())
823 clearFlag(InDocumentFlag);
824 if (isInShadowTree() && !treeScope().rootNode().isShadowRoot())
825 clearFlag(IsInShadowTreeFlag);
828 inline void Element::invalidateStyleAttribute()
830 ASSERT(elementData());
831 elementData()->m_styleAttributeIsDirty = true;
834 inline const StylePropertySet* Element::presentationAttributeStyle()
838 if (elementData()->m_presentationAttributeStyleIsDirty)
839 updatePresentationAttributeStyle();
840 // Need to call elementData() again since updatePresentationAttributeStyle()
841 // might swap it with a UniqueElementData.
842 return elementData()->presentationAttributeStyle();
845 inline void Element::setTagNameForCreateElementNS(const QualifiedName& tagName)
847 // We expect this method to be called only to reset the prefix.
848 ASSERT(tagName.localName() == m_tagName.localName());
849 ASSERT(tagName.namespaceURI() == m_tagName.namespaceURI());
853 inline bool isShadowHost(const Node* node)
855 return node && node->isElementNode() && toElement(node)->shadow();
858 inline bool isShadowHost(const Element* element)
860 return element && element->shadow();