1760c8f479625ea1e61f97b52c1f0944e8d94fcc
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / Element.h
1 /*
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.
7  *
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.
12  *
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.
17  *
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.
22  *
23  */
24
25 #ifndef Element_h
26 #define Element_h
27
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"
38
39 namespace WebCore {
40
41 class ActiveAnimations;
42 class Attr;
43 class Attribute;
44 class ClientRect;
45 class ClientRectList;
46 class CustomElementDefinition;
47 class DOMStringMap;
48 class DOMTokenList;
49 class ElementRareData;
50 class ElementShadow;
51 class ExceptionState;
52 class Image;
53 class InputMethodContext;
54 class IntSize;
55 class Locale;
56 class MutableStylePropertySet;
57 class PropertySetCSSStyleDeclaration;
58 class PseudoElement;
59 class ShadowRoot;
60 class StylePropertySet;
61
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
70 };
71 typedef int AffectedSelectorMask;
72
73 enum SpellcheckAttributeState {
74     SpellcheckAttributeTrue,
75     SpellcheckAttributeFalse,
76     SpellcheckAttributeDefault
77 };
78
79 enum ElementFlags {
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,
87
88     NumberOfElementFlags = 7, // Required size of bitfield used to store the flags.
89 };
90
91 class Element : public ContainerNode {
92 public:
93     static PassRefPtr<Element> create(const QualifiedName&, Document*);
94     virtual ~Element();
95
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);
111
112     bool hasAttribute(const QualifiedName&) const;
113     const AtomicString& getAttribute(const QualifiedName&) const;
114
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);
118
119     void removeAttribute(const QualifiedName&);
120
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);
128
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;
133 #ifndef NDEBUG
134     bool fastAttributeLookupAllowed(const QualifiedName&) const;
135 #endif
136
137 #ifdef DUMP_NODE_STATISTICS
138     bool hasNamedNodeMap() const;
139 #endif
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;
144
145     bool hasAttribute(const AtomicString& name) const;
146     bool hasAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
147
148     const AtomicString& getAttribute(const AtomicString& name) const;
149     const AtomicString& getAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
150
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&);
154
155     bool isIdAttributeName(const QualifiedName&) const;
156     const AtomicString& getIdAttribute() const;
157     void setIdAttribute(const AtomicString&);
158
159     const AtomicString& getNameAttribute() const;
160     const AtomicString& getClassAttribute() const;
161
162     bool shouldIgnoreAttributeCase() const;
163
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;
168
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
171     // performance.
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); }
177
178     void scrollIntoView(bool alignToTop = true);
179     void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
180
181     void scrollByLines(int lines);
182     void scrollByPages(int pages);
183
184     int offsetLeft();
185     int offsetTop();
186     int offsetWidth();
187     int offsetHeight();
188
189     // FIXME: Replace uses of offsetParent in the platform with calls
190     // to the render layer and merge offsetParentForBindings and offsetParent.
191     Element* offsetParentForBindings();
192
193     Element* offsetParent();
194     int clientLeft();
195     int clientTop();
196     int clientWidth();
197     int clientHeight();
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();
206
207     IntRect boundsInRootViewSpace();
208
209     PassRefPtr<ClientRectList> getClientRects();
210     PassRefPtr<ClientRect> getBoundingClientRect();
211
212     // Returns the absolute bounding box translated into screen coordinates:
213     IntRect screenRect() const;
214
215     virtual void didMoveToNewDocument(Document&) OVERRIDE;
216
217     void removeAttribute(const AtomicString& name);
218     void removeAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName);
219
220     PassRefPtr<Attr> detachAttribute(size_t index);
221
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&);
227
228     PassRefPtr<Attr> attrIfExists(const QualifiedName&);
229     PassRefPtr<Attr> ensureAttr(const QualifiedName&);
230
231     const Vector<RefPtr<Attr> >& attrNodeList();
232
233     CSSStyleDeclaration* style();
234
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); }
238
239     // Should be called only by Document::createElementNS to fix up m_tagName immediately after construction.
240     void setTagNameForCreateElementNS(const QualifiedName&);
241
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(); }
245
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(); }
249
250     const AtomicString& locateNamespacePrefix(const AtomicString& namespaceURI) const;
251
252     virtual KURL baseURI() const OVERRIDE FINAL;
253
254     virtual String nodeName() const OVERRIDE;
255
256     PassRefPtr<Element> cloneElementWithChildren();
257     PassRefPtr<Element> cloneElementWithoutChildren();
258
259     void scheduleLayerUpdate();
260
261     void normalizeAttributes();
262
263     void setBooleanAttribute(const QualifiedName& name, bool);
264
265     virtual const StylePropertySet* additionalPresentationAttributeStyle() { return 0; }
266     void invalidateStyleAttribute();
267
268     const StylePropertySet* inlineStyle() const { return elementData() ? elementData()->m_inlineStyle.get() : 0; }
269
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();
276
277     void synchronizeStyleAttributeInternal() const;
278
279     const StylePropertySet* presentationAttributeStyle();
280     virtual bool isPresentationAttribute(const QualifiedName&) const { return false; }
281     virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) { }
282
283     // For exposing to DOM only.
284     NamedNodeMap* attributes() const;
285
286     enum AttributeModificationReason {
287         ModifiedDirectly,
288         ModifiedByCloning
289     };
290
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&) { }
294
295     virtual bool hasLegalLinkAttribute(const QualifiedName&) const;
296     virtual const QualifiedName& subResourceAttributeName() const;
297
298     // Only called by the parser immediately after element construction.
299     void parserSetAttributes(const Vector<Attribute>&);
300
301     // Remove attributes that might introduce scripting from the vector leaving the element unchanged.
302     void stripScriptingAttributes(Vector<Attribute>&) const;
303
304     const ElementData* elementData() const { return m_elementData.get(); }
305     UniqueElementData& ensureUniqueElementData();
306
307     void synchronizeAllAttributes() const;
308
309     // Clones attributes only.
310     void cloneAttributesFromElement(const Element&);
311
312     // Clones all attribute-derived data, including subclass specifics (through copyNonAttributeProperties.)
313     void cloneDataFromElement(const Element&);
314
315     bool hasEquivalentAttributes(const Element* other) const;
316
317     virtual void copyNonAttributePropertiesFromElement(const Element&) { }
318
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();
327
328     bool supportsStyleSharing() const;
329
330     ElementShadow* shadow() const;
331     ElementShadow& ensureShadow();
332     PassRefPtr<ShadowRoot> createShadowRoot(ExceptionState&);
333     ShadowRoot* shadowRoot() const;
334     ShadowRoot* youngestShadowRoot() const;
335
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;
341
342     RenderStyle* computedStyle(PseudoId = NOPSEUDO);
343
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); }
347
348     void setIsInCanvasSubtree(bool value) { setElementFlag(IsInCanvasSubtree, value); }
349     bool isInCanvasSubtree() const { return hasElementFlag(IsInCanvasSubtree); }
350
351     unsigned childIndex() const { return hasRareData() ? rareDataChildIndex() : 0; }
352     void setChildIndex(unsigned);
353
354     bool isUpgradedCustomElement() { return customElementState() == Upgraded; }
355     bool isUnresolvedCustomElement() { return customElementState() == WaitingForUpgrade; }
356
357     AtomicString computeInheritedLanguage() const;
358     Locale& locale() const;
359
360     virtual void accessKeyAction(bool /*sendToAnyEvent*/) { }
361
362     virtual bool isURLAttribute(const Attribute&) const { return false; }
363     virtual bool isHTMLContentAttribute(const Attribute&) const { return false; }
364
365     KURL getURLAttribute(const QualifiedName&) const;
366     KURL getNonEmptyURLAttribute(const QualifiedName&) const;
367
368     virtual const AtomicString imageSourceURL() const;
369     virtual Image* imageContents() { return 0; }
370
371     virtual void focus(bool restorePreviousSelection = true, FocusType = FocusTypeNone);
372     virtual void updateFocusAppearance(bool restorePreviousSelection);
373     virtual void blur();
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);
387
388     String innerText();
389     String outerText();
390     String innerHTML() const;
391     String outerHTML() const;
392     void setInnerHTML(const String&, ExceptionState&);
393     void setOuterHTML(const String&, ExceptionState&);
394
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&);
398
399     String textFromChildren();
400
401     virtual String title() const { return String(); }
402
403     virtual const AtomicString& shadowPseudoId() const;
404     void setShadowPseudoId(const AtomicString&);
405
406     LayoutSize minimumSizeForResizing() const;
407     void setMinimumSizeForResizing(const LayoutSize&);
408
409     virtual void didBecomeFullscreenElement() { }
410     virtual void willStopBeingFullscreenElement() { }
411
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();
419
420     void beginParsingChildren() { setIsFinishedParsingChildren(false); }
421
422     PseudoElement* pseudoElement(PseudoId) const;
423     RenderObject* pseudoElementRenderer(PseudoId) const;
424
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; }
429
430     DOMTokenList& classList();
431
432     DOMStringMap& dataset();
433
434 #if ENABLE(INPUT_SPEECH)
435     virtual bool isInputFieldSpeechButtonElement() const { return false; }
436 #endif
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; }
441 #endif
442
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; }
455
456     virtual bool canContainRangeEndPoint() const OVERRIDE { return true; }
457
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; }
461
462     bool hasPendingResources() const { return hasElementFlag(HasPendingResources); }
463     void setHasPendingResources() { setElementFlag(HasPendingResources); }
464     void clearHasPendingResources() { clearElementFlag(HasPendingResources); }
465     virtual void buildPendingResource() { };
466
467     void setCustomElementDefinition(PassRefPtr<CustomElementDefinition>);
468     CustomElementDefinition* customElementDefinition() const;
469
470     enum {
471         ALLOW_KEYBOARD_INPUT = 1 << 0,
472         LEGACY_MOZILLA_REQUEST = 1 << 1,
473     };
474
475     void webkitRequestFullScreen(unsigned short flags);
476     bool containsFullScreenElement() const { return hasElementFlag(ContainsFullScreenElement); }
477     void setContainsFullScreenElement(bool);
478     void setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(bool);
479
480     // W3C API
481     void webkitRequestFullscreen();
482
483     bool isInTopLayer() const { return hasElementFlag(IsInTopLayer); }
484     void setIsInTopLayer(bool);
485
486     void webkitRequestPointerLock();
487
488     bool isSpellCheckingEnabled() const;
489
490     // FIXME: public for RenderTreeBuilder, we shouldn't expose this though.
491     PassRefPtr<RenderStyle> styleForRenderer();
492
493     bool hasID() const;
494     bool hasClass() const;
495     const SpaceSplitString& classNames() const;
496
497     IntSize savedLayerScrollOffset() const;
498     void setSavedLayerScrollOffset(const IntSize&);
499
500     ActiveAnimations* activeAnimations() const;
501     ActiveAnimations& ensureActiveAnimations();
502     bool hasActiveAnimations() const;
503
504     InputMethodContext& inputMethodContext();
505     bool hasInputMethodContext() const;
506
507     void setPrefix(const AtomicString&, ExceptionState&);
508
509     void synchronizeAttribute(const AtomicString& localName) const;
510
511     MutableStylePropertySet& ensureMutableInlineStyle();
512     void clearMutableInlineStyleIfEmpty();
513
514 protected:
515     Element(const QualifiedName& tagName, Document* document, ConstructionType type)
516         : ContainerNode(document, type)
517         , m_tagName(tagName)
518     {
519         ScriptWrappable::init(this);
520     }
521
522     void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, CSSValueID identifier);
523     void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, double value, CSSPrimitiveValue::UnitTypes);
524     void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, const String& value);
525
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;
530
531     virtual void willRecalcStyle(StyleRecalcChange);
532     virtual void didRecalcStyle(StyleRecalcChange);
533     virtual PassRefPtr<RenderStyle> customStyleForRenderer();
534
535     virtual bool shouldRegisterAsNamedItem() const { return false; }
536     virtual bool shouldRegisterAsExtraNamedItem() const { return false; }
537
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);
549
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);
554
555     PassRefPtr<RenderStyle> originalStyleForRenderer();
556
557     Node* insertAdjacent(const String& where, Node* newChild, ExceptionState&);
558
559 private:
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;
564
565     void styleAttributeChanged(const AtomicString& newStyleString, AttributeModificationReason);
566
567     void updatePresentationAttributeStyle();
568
569     void inlineStyleChanged();
570     PropertySetCSSStyleDeclaration* inlineStyleCSSOMWrapper();
571     void setInlineStyleFromString(const AtomicString&);
572
573     StyleRecalcChange recalcOwnStyle(StyleRecalcChange);
574     void recalcChildStyle(StyleRecalcChange);
575
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*);
580
581     void updatePseudoElement(PseudoId, StyleRecalcChange);
582
583     inline void createPseudoElementIfNeeded(PseudoId);
584
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; }
589
590     // FIXME: Remove the need for Attr to call willModifyAttribute/didModifyAttribute.
591     friend class Attr;
592
593     enum SynchronizationOfLazyAttribute { NotInSynchronizationOfLazyAttribute = 0, InSynchronizationOfLazyAttribute };
594
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&);
599
600     void synchronizeAttribute(const QualifiedName&) const;
601
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);
606
607     void scrollByUnits(int units, ScrollGranularity);
608
609     virtual NodeType nodeType() const OVERRIDE FINAL;
610     virtual bool childTypeAllowed(NodeType) const OVERRIDE FINAL;
611
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);
616
617 #ifndef NDEBUG
618     virtual void formatForDebugger(char* buffer, unsigned length) const OVERRIDE;
619 #endif
620
621     bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
622
623     void cancelFocusAppearanceUpdate();
624
625     virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) OVERRIDE { return computedStyle(pseudoElementSpecifier); }
626
627     inline void updateCallbackSelectors(RenderStyle* oldStyle, RenderStyle* newStyle);
628     inline void removeCallbackSelectors();
629     inline void addCallbackSelectors();
630
631     // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
632     // are used instead.
633     virtual PassRefPtr<Node> cloneNode(bool deep) OVERRIDE;
634     virtual PassRefPtr<Element> cloneElementWithoutAttributesAndChildren();
635
636     QualifiedName m_tagName;
637
638     unsigned rareDataChildIndex() const;
639
640     SpellcheckAttributeState spellcheckAttributeState() const;
641
642     void updateNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
643     void updateExtraNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
644
645     void createUniqueElementData();
646
647     bool shouldInvalidateDistributionWhenAttributeChanged(ElementShadow*, const QualifiedName&, const AtomicString&);
648
649     ElementRareData* elementRareData() const;
650     ElementRareData& ensureElementRareData();
651
652     void detachAllAttrNodesFromElement();
653     void detachAttrNodeFromElementWithValue(Attr*, const AtomicString& value);
654     void detachAttrNodeAtIndex(Attr*, size_t index);
655
656     bool isJavaScriptURLAttribute(const Attribute&) const;
657
658     RefPtr<ElementData> m_elementData;
659 };
660
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; }
665
666 // Type casting.
667 template<typename T> inline T& toElement(Node& node)
668 {
669     ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
670     return static_cast<T&>(node);
671 }
672 template<typename T> inline T* toElement(Node* node)
673 {
674     ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
675     return static_cast<T*>(node);
676 }
677 template<typename T> inline const T& toElement(const Node& node)
678 {
679     ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
680     return static_cast<const T&>(node);
681 }
682 template<typename T> inline const T* toElement(const Node* node)
683 {
684     ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
685     return static_cast<const T*>(node);
686 }
687 template<typename T, typename U> inline T* toElement(const RefPtr<U>& node) { return toElement<T>(node.get()); }
688
689 inline bool isDisabledFormControl(const Node* node)
690 {
691     return node->isElementNode() && toElement(node)->isDisabledFormControl();
692 }
693
694 inline bool Node::hasTagName(const QualifiedName& name) const
695 {
696     return isElementNode() && toElement(this)->hasTagName(name);
697 }
698
699 inline Element* Node::parentElement() const
700 {
701     ContainerNode* parent = parentNode();
702     return parent && parent->isElementNode() ? toElement(parent) : 0;
703 }
704
705 inline bool Element::fastHasAttribute(const QualifiedName& name) const
706 {
707     ASSERT(fastAttributeLookupAllowed(name));
708     return elementData() && getAttributeItem(name);
709 }
710
711 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
712 {
713     ASSERT(fastAttributeLookupAllowed(name));
714     if (elementData()) {
715         if (const Attribute* attribute = getAttributeItem(name))
716             return attribute->value();
717     }
718     return nullAtom;
719 }
720
721 inline bool Element::hasAttributesWithoutUpdate() const
722 {
723     return elementData() && !elementData()->isEmpty();
724 }
725
726 inline const AtomicString& Element::idForStyleResolution() const
727 {
728     ASSERT(hasID());
729     return elementData()->idForStyleResolution();
730 }
731
732 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
733 {
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;
739 }
740
741 inline const AtomicString& Element::getIdAttribute() const
742 {
743     return hasID() ? fastGetAttribute(HTMLNames::idAttr) : nullAtom;
744 }
745
746 inline const AtomicString& Element::getNameAttribute() const
747 {
748     return hasName() ? fastGetAttribute(HTMLNames::nameAttr) : nullAtom;
749 }
750
751 inline const AtomicString& Element::getClassAttribute() const
752 {
753     if (!hasClass())
754         return nullAtom;
755     if (isSVGElement())
756         return getAttribute(HTMLNames::classAttr);
757     return fastGetAttribute(HTMLNames::classAttr);
758 }
759
760 inline bool Element::shouldIgnoreAttributeCase() const
761 {
762     return isHTMLElement() && document().isHTMLDocument();
763 }
764
765 inline void Element::setIdAttribute(const AtomicString& value)
766 {
767     setAttribute(HTMLNames::idAttr, value);
768 }
769
770 inline const SpaceSplitString& Element::classNames() const
771 {
772     ASSERT(hasClass());
773     ASSERT(elementData());
774     return elementData()->classNames();
775 }
776
777 inline size_t Element::attributeCount() const
778 {
779     ASSERT(elementData());
780     return elementData()->length();
781 }
782
783 inline const Attribute& Element::attributeItem(unsigned index) const
784 {
785     ASSERT(elementData());
786     return elementData()->attributeItem(index);
787 }
788
789 inline const Attribute* Element::getAttributeItem(const QualifiedName& name) const
790 {
791     ASSERT(elementData());
792     return elementData()->getAttributeItem(name);
793 }
794
795 inline bool Element::hasID() const
796 {
797     return elementData() && elementData()->hasID();
798 }
799
800 inline bool Element::hasClass() const
801 {
802     return elementData() && elementData()->hasClass();
803 }
804
805 inline UniqueElementData& Element::ensureUniqueElementData()
806 {
807     if (!elementData() || !elementData()->isUnique())
808         createUniqueElementData();
809     return static_cast<UniqueElementData&>(*m_elementData);
810 }
811
812 // Put here to make them inline.
813 inline bool Node::hasID() const
814 {
815     return isElementNode() && toElement(this)->hasID();
816 }
817
818 inline bool Node::hasClass() const
819 {
820     return isElementNode() && toElement(this)->hasClass();
821 }
822
823 inline Node::InsertionNotificationRequest Node::insertedInto(ContainerNode* insertionPoint)
824 {
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;
833 }
834
835 inline void Node::removedFrom(ContainerNode* insertionPoint)
836 {
837     ASSERT(insertionPoint->inDocument() || isContainerNode());
838     if (insertionPoint->inDocument())
839         clearFlag(InDocumentFlag);
840     if (isInShadowTree() && !treeScope().rootNode().isShadowRoot())
841         clearFlag(IsInShadowTreeFlag);
842 }
843
844 inline void Element::invalidateStyleAttribute()
845 {
846     ASSERT(elementData());
847     elementData()->m_styleAttributeIsDirty = true;
848 }
849
850 inline const StylePropertySet* Element::presentationAttributeStyle()
851 {
852     if (!elementData())
853         return 0;
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();
859 }
860
861 inline void Element::setTagNameForCreateElementNS(const QualifiedName& tagName)
862 {
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());
866     m_tagName = tagName;
867 }
868
869 inline bool isShadowHost(const Node* node)
870 {
871     return node && node->isElementNode() && toElement(node)->shadow();
872 }
873
874 inline bool isShadowHost(const Element* element)
875 {
876     return element && element->shadow();
877 }
878
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)
884
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)
888
889 } // namespace
890
891 #endif