f298a23c6f26069154a7448f773f8f77a03ff3f6
[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> removeAttributeNode(Attr*, ExceptionState&);
226
227     PassRefPtr<Attr> attrIfExists(const QualifiedName&);
228     PassRefPtr<Attr> ensureAttr(const QualifiedName&);
229
230     const Vector<RefPtr<Attr> >& attrNodeList();
231
232     CSSStyleDeclaration* style();
233
234     const QualifiedName& tagQName() const { return m_tagName; }
235     String tagName() const { return nodeName(); }
236     bool hasTagName(const QualifiedName& tagName) const { return m_tagName.matches(tagName); }
237
238     // Should be called only by Document::createElementNS to fix up m_tagName immediately after construction.
239     void setTagNameForCreateElementNS(const QualifiedName&);
240
241     // A fast function for checking the local name against another atomic string.
242     bool hasLocalName(const AtomicString& other) const { return m_tagName.localName() == other; }
243     bool hasLocalName(const QualifiedName& other) const { return m_tagName.localName() == other.localName(); }
244
245     virtual const AtomicString& localName() const OVERRIDE FINAL { return m_tagName.localName(); }
246     const AtomicString& prefix() const { return m_tagName.prefix(); }
247     virtual const AtomicString& namespaceURI() const OVERRIDE FINAL { return m_tagName.namespaceURI(); }
248
249     const AtomicString& locateNamespacePrefix(const AtomicString& namespaceURI) const;
250
251     virtual KURL baseURI() const OVERRIDE FINAL;
252
253     virtual String nodeName() const OVERRIDE;
254
255     PassRefPtr<Element> cloneElementWithChildren();
256     PassRefPtr<Element> cloneElementWithoutChildren();
257
258     void scheduleLayerUpdate();
259
260     void normalizeAttributes();
261
262     void setBooleanAttribute(const QualifiedName& name, bool);
263
264     virtual const StylePropertySet* additionalPresentationAttributeStyle() { return 0; }
265     void invalidateStyleAttribute();
266
267     const StylePropertySet* inlineStyle() const { return elementData() ? elementData()->m_inlineStyle.get() : 0; }
268
269     bool setInlineStyleProperty(CSSPropertyID, CSSValueID identifier, bool important = false);
270     bool setInlineStyleProperty(CSSPropertyID, CSSPropertyID identifier, bool important = false);
271     bool setInlineStyleProperty(CSSPropertyID, double value, CSSPrimitiveValue::UnitTypes, bool important = false);
272     bool setInlineStyleProperty(CSSPropertyID, const String& value, bool important = false);
273     bool removeInlineStyleProperty(CSSPropertyID);
274     void removeAllInlineStyleProperties();
275
276     void synchronizeStyleAttributeInternal() const;
277
278     const StylePropertySet* presentationAttributeStyle();
279     virtual bool isPresentationAttribute(const QualifiedName&) const { return false; }
280     virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) { }
281
282     // For exposing to DOM only.
283     NamedNodeMap* attributes() const;
284
285     enum AttributeModificationReason {
286         ModifiedDirectly,
287         ModifiedByCloning
288     };
289
290     // This method is called whenever an attribute is added, changed or removed.
291     virtual void attributeChanged(const QualifiedName&, const AtomicString&, AttributeModificationReason = ModifiedDirectly);
292     virtual void parseAttribute(const QualifiedName&, const AtomicString&) { }
293
294     virtual bool hasLegalLinkAttribute(const QualifiedName&) const;
295     virtual const QualifiedName& subResourceAttributeName() const;
296
297     // Only called by the parser immediately after element construction.
298     void parserSetAttributes(const Vector<Attribute>&);
299
300     // Remove attributes that might introduce scripting from the vector leaving the element unchanged.
301     void stripScriptingAttributes(Vector<Attribute>&) const;
302
303     const ElementData* elementData() const { return m_elementData.get(); }
304     UniqueElementData& ensureUniqueElementData();
305
306     void synchronizeAllAttributes() const;
307
308     // Clones attributes only.
309     void cloneAttributesFromElement(const Element&);
310
311     // Clones all attribute-derived data, including subclass specifics (through copyNonAttributeProperties.)
312     void cloneDataFromElement(const Element&);
313
314     bool hasEquivalentAttributes(const Element* other) const;
315
316     virtual void copyNonAttributePropertiesFromElement(const Element&) { }
317
318     virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
319     virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
320     virtual RenderObject* createRenderer(RenderStyle*);
321     virtual bool rendererIsNeeded(const RenderStyle&);
322     void recalcStyle(StyleRecalcChange, Text* nextTextSibling = 0);
323     void didAffectSelector(AffectedSelectorMask);
324     void setAnimationStyleChange(bool);
325     void setNeedsAnimationStyleRecalc();
326
327     bool supportsStyleSharing() const;
328
329     ElementShadow* shadow() const;
330     ElementShadow& ensureShadow();
331     PassRefPtr<ShadowRoot> createShadowRoot(ExceptionState&);
332     ShadowRoot* shadowRoot() const;
333     ShadowRoot* youngestShadowRoot() const;
334
335     bool hasAuthorShadowRoot() const { return shadowRoot(); }
336     virtual void didAddShadowRoot(ShadowRoot&);
337     ShadowRoot* userAgentShadowRoot() const;
338     ShadowRoot& ensureUserAgentShadowRoot();
339     bool isInDescendantTreeOf(const Element* shadowHost) const;
340
341     RenderStyle* computedStyle(PseudoId = NOPSEUDO);
342
343     // Methods for indicating the style is affected by dynamic updates (e.g., children changing, our position changing in our sibling list, etc.)
344     bool styleAffectedByEmpty() const { return hasElementFlag(StyleAffectedByEmpty); }
345     void setStyleAffectedByEmpty() { setElementFlag(StyleAffectedByEmpty); }
346
347     void setIsInCanvasSubtree(bool value) { setElementFlag(IsInCanvasSubtree, value); }
348     bool isInCanvasSubtree() const { return hasElementFlag(IsInCanvasSubtree); }
349
350     unsigned childIndex() const { return hasRareData() ? rareDataChildIndex() : 0; }
351     void setChildIndex(unsigned);
352
353     bool isUpgradedCustomElement() { return customElementState() == Upgraded; }
354     bool isUnresolvedCustomElement() { return customElementState() == WaitingForUpgrade; }
355
356     AtomicString computeInheritedLanguage() const;
357     Locale& locale() const;
358
359     virtual void accessKeyAction(bool /*sendToAnyEvent*/) { }
360
361     virtual bool isURLAttribute(const Attribute&) const { return false; }
362     virtual bool isHTMLContentAttribute(const Attribute&) const { return false; }
363
364     KURL getURLAttribute(const QualifiedName&) const;
365     KURL getNonEmptyURLAttribute(const QualifiedName&) const;
366
367     virtual const AtomicString imageSourceURL() const;
368     virtual Image* imageContents() { return 0; }
369
370     virtual void focus(bool restorePreviousSelection = true, FocusType = FocusTypeNone);
371     virtual void updateFocusAppearance(bool restorePreviousSelection);
372     virtual void blur();
373     // Whether this element can receive focus at all. Most elements are not
374     // focusable but some elements, such as form controls and links, are. Unlike
375     // rendererIsFocusable(), this method may be called when layout is not up to
376     // date, so it must not use the renderer to determine focusability.
377     virtual bool supportsFocus() const { return hasElementFlag(TabIndexWasSetExplicitly); }
378     // Whether the node can actually be focused.
379     bool isFocusable() const;
380     virtual bool isKeyboardFocusable() const;
381     virtual bool isMouseFocusable() const;
382     virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType);
383     virtual void dispatchBlurEvent(Element* newFocusedElement);
384     void dispatchFocusInEvent(const AtomicString& eventType, Element* oldFocusedElement);
385     void dispatchFocusOutEvent(const AtomicString& eventType, Element* newFocusedElement);
386
387     String innerText();
388     String outerText();
389     String innerHTML() const;
390     String outerHTML() const;
391     void setInnerHTML(const String&, ExceptionState&);
392     void setOuterHTML(const String&, ExceptionState&);
393
394     Element* insertAdjacentElement(const String& where, Element* newChild, ExceptionState&);
395     void insertAdjacentText(const String& where, const String& text, ExceptionState&);
396     void insertAdjacentHTML(const String& where, const String& html, ExceptionState&);
397
398     String textFromChildren();
399
400     virtual String title() const { return String(); }
401
402     virtual const AtomicString& shadowPseudoId() const;
403     void setShadowPseudoId(const AtomicString&);
404
405     LayoutSize minimumSizeForResizing() const;
406     void setMinimumSizeForResizing(const LayoutSize&);
407
408     virtual void didBecomeFullscreenElement() { }
409     virtual void willStopBeingFullscreenElement() { }
410
411     // Called by the parser when this element's close tag is reached,
412     // signaling that all child tags have been parsed and added.
413     // This is needed for <applet> and <object> elements, which can't lay themselves out
414     // until they know all of their nested <param>s. [Radar 3603191, 4040848].
415     // Also used for script elements and some SVG elements for similar purposes,
416     // but making parsing a special case in this respect should be avoided if possible.
417     virtual void finishParsingChildren();
418
419     void beginParsingChildren() { setIsFinishedParsingChildren(false); }
420
421     PseudoElement* pseudoElement(PseudoId) const;
422     RenderObject* pseudoElementRenderer(PseudoId) const;
423
424     virtual bool matchesReadOnlyPseudoClass() const { return false; }
425     virtual bool matchesReadWritePseudoClass() const { return false; }
426     bool matches(const String& selectors, ExceptionState&);
427     virtual bool shouldAppearIndeterminate() const { return false; }
428
429     DOMTokenList& classList();
430
431     DOMStringMap& dataset();
432
433 #if ENABLE(INPUT_SPEECH)
434     virtual bool isInputFieldSpeechButtonElement() const { return false; }
435 #endif
436 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
437     virtual bool isDateTimeEditElement() const { return false; }
438     virtual bool isDateTimeFieldElement() const { return false; }
439     virtual bool isPickerIndicatorElement() const { return false; }
440 #endif
441
442     virtual bool isFormControlElement() const { return false; }
443     virtual bool isSpinButtonElement() const { return false; }
444     virtual bool isTextFormControl() const { return false; }
445     virtual bool isOptionalFormControl() const { return false; }
446     virtual bool isRequiredFormControl() const { return false; }
447     virtual bool isDefaultButtonForForm() const { return false; }
448     virtual bool willValidate() const { return false; }
449     virtual bool isValidFormControlElement() { return false; }
450     virtual bool isInRange() const { return false; }
451     virtual bool isOutOfRange() const { return false; }
452     virtual bool isPasswordGeneratorButtonElement() const { return false; }
453     virtual bool isClearButtonElement() const { return false; }
454
455     virtual bool canContainRangeEndPoint() const OVERRIDE { return true; }
456
457     // Used for disabled form elements; if true, prevents mouse events from being dispatched
458     // to event listeners, and prevents DOMActivate events from being sent at all.
459     virtual bool isDisabledFormControl() const { return false; }
460
461     bool hasPendingResources() const { return hasElementFlag(HasPendingResources); }
462     void setHasPendingResources() { setElementFlag(HasPendingResources); }
463     void clearHasPendingResources() { clearElementFlag(HasPendingResources); }
464     virtual void buildPendingResource() { };
465
466     void setCustomElementDefinition(PassRefPtr<CustomElementDefinition>);
467     CustomElementDefinition* customElementDefinition() const;
468
469     enum {
470         ALLOW_KEYBOARD_INPUT = 1 << 0,
471         LEGACY_MOZILLA_REQUEST = 1 << 1,
472     };
473
474     void webkitRequestFullScreen(unsigned short flags);
475     bool containsFullScreenElement() const { return hasElementFlag(ContainsFullScreenElement); }
476     void setContainsFullScreenElement(bool);
477     void setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(bool);
478
479     // W3C API
480     void webkitRequestFullscreen();
481
482     bool isInTopLayer() const { return hasElementFlag(IsInTopLayer); }
483     void setIsInTopLayer(bool);
484
485     void webkitRequestPointerLock();
486
487     bool isSpellCheckingEnabled() const;
488
489     // FIXME: public for RenderTreeBuilder, we shouldn't expose this though.
490     PassRefPtr<RenderStyle> styleForRenderer();
491
492     bool hasID() const;
493     bool hasClass() const;
494     const SpaceSplitString& classNames() const;
495
496     IntSize savedLayerScrollOffset() const;
497     void setSavedLayerScrollOffset(const IntSize&);
498
499     ActiveAnimations* activeAnimations() const;
500     ActiveAnimations& ensureActiveAnimations();
501     bool hasActiveAnimations() const;
502
503     InputMethodContext& inputMethodContext();
504     bool hasInputMethodContext() const;
505
506     void setPrefix(const AtomicString&, ExceptionState&);
507
508     void synchronizeAttribute(const AtomicString& localName) const;
509
510     MutableStylePropertySet& ensureMutableInlineStyle();
511     void clearMutableInlineStyleIfEmpty();
512
513 protected:
514     Element(const QualifiedName& tagName, Document* document, ConstructionType type)
515         : ContainerNode(document, type)
516         , m_tagName(tagName)
517     {
518         ScriptWrappable::init(this);
519     }
520
521     void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, CSSValueID identifier);
522     void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, double value, CSSPrimitiveValue::UnitTypes);
523     void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, const String& value);
524
525     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
526     virtual void removedFrom(ContainerNode*) OVERRIDE;
527     virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0) OVERRIDE;
528     virtual void removeAllEventListeners() OVERRIDE;
529
530     virtual void willRecalcStyle(StyleRecalcChange);
531     virtual void didRecalcStyle(StyleRecalcChange);
532     virtual PassRefPtr<RenderStyle> customStyleForRenderer();
533
534     virtual bool shouldRegisterAsNamedItem() const { return false; }
535     virtual bool shouldRegisterAsExtraNamedItem() const { return false; }
536
537     void clearTabIndexExplicitlyIfNeeded();
538     void setTabIndexExplicitly(short);
539     virtual short tabIndex() const OVERRIDE;
540     // Subclasses may override this method to affect focusability. Unlike
541     // supportsFocus, this method must be called on an up-to-date layout, so it
542     // may use the renderer to reason about focusability. This method cannot be
543     // moved to RenderObject because some focusable nodes don't have renderers,
544     // e.g., HTMLOptionElement.
545     virtual bool rendererIsFocusable() const;
546     PassRefPtr<HTMLCollection> ensureCachedHTMLCollection(CollectionType);
547     HTMLCollection* cachedHTMLCollection(CollectionType);
548
549     // classAttributeChanged() exists to share code between
550     // parseAttribute (called via setAttribute()) and
551     // svgAttributeChanged (called when element.className.baseValue is set)
552     void classAttributeChanged(const AtomicString& newClassString);
553
554     PassRefPtr<RenderStyle> originalStyleForRenderer();
555
556     Node* insertAdjacent(const String& where, Node* newChild, ExceptionState&);
557
558 private:
559     bool hasElementFlag(ElementFlags mask) const { return hasRareData() && hasElementFlagInternal(mask); }
560     void setElementFlag(ElementFlags, bool value = true);
561     void clearElementFlag(ElementFlags);
562     bool hasElementFlagInternal(ElementFlags) const;
563
564     void styleAttributeChanged(const AtomicString& newStyleString, AttributeModificationReason);
565
566     void updatePresentationAttributeStyle();
567
568     void inlineStyleChanged();
569     PropertySetCSSStyleDeclaration* inlineStyleCSSOMWrapper();
570     void setInlineStyleFromString(const AtomicString&);
571
572     StyleRecalcChange recalcOwnStyle(StyleRecalcChange);
573     void recalcChildStyle(StyleRecalcChange);
574
575     // FIXME: These methods should all be renamed to something better than "check",
576     // since it's not clear that they alter the style bits of siblings and children.
577     void checkForSiblingStyleChanges(bool finishedParsingCallback, Node* beforeChange, Node* afterChange, int childCountDelta);
578     inline void checkForEmptyStyleChange(RenderStyle*);
579
580     void updatePseudoElement(PseudoId, StyleRecalcChange);
581
582     inline void createPseudoElementIfNeeded(PseudoId);
583
584     // FIXME: Everyone should allow author shadows.
585     virtual bool areAuthorShadowsAllowed() const { return true; }
586     virtual void didAddUserAgentShadowRoot(ShadowRoot&) { }
587     virtual bool alwaysCreateUserAgentShadowRoot() const { return false; }
588
589     // FIXME: Remove the need for Attr to call willModifyAttribute/didModifyAttribute.
590     friend class Attr;
591
592     enum SynchronizationOfLazyAttribute { NotInSynchronizationOfLazyAttribute = 0, InSynchronizationOfLazyAttribute };
593
594     void didAddAttribute(const QualifiedName&, const AtomicString&);
595     void willModifyAttribute(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue);
596     void didModifyAttribute(const QualifiedName&, const AtomicString&);
597     void didRemoveAttribute(const QualifiedName&);
598
599     void synchronizeAttribute(const QualifiedName&) const;
600
601     void updateId(const AtomicString& oldId, const AtomicString& newId);
602     void updateId(TreeScope&, const AtomicString& oldId, const AtomicString& newId);
603     void updateName(const AtomicString& oldName, const AtomicString& newName);
604     void updateLabel(TreeScope&, const AtomicString& oldForAttributeValue, const AtomicString& newForAttributeValue);
605
606     void scrollByUnits(int units, ScrollGranularity);
607
608     virtual NodeType nodeType() const OVERRIDE FINAL;
609     virtual bool childTypeAllowed(NodeType) const OVERRIDE FINAL;
610
611     void setAttributeInternal(size_t index, const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
612     void addAttributeInternal(const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
613     void removeAttributeInternal(size_t index, SynchronizationOfLazyAttribute);
614     void attributeChangedFromParserOrByCloning(const QualifiedName&, const AtomicString&, AttributeModificationReason);
615
616 #ifndef NDEBUG
617     virtual void formatForDebugger(char* buffer, unsigned length) const OVERRIDE;
618 #endif
619
620     bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
621
622     void cancelFocusAppearanceUpdate();
623
624     virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) OVERRIDE { return computedStyle(pseudoElementSpecifier); }
625
626     inline void updateCallbackSelectors(RenderStyle* oldStyle, RenderStyle* newStyle);
627     inline void removeCallbackSelectors();
628     inline void addCallbackSelectors();
629
630     // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
631     // are used instead.
632     virtual PassRefPtr<Node> cloneNode(bool deep) OVERRIDE;
633     virtual PassRefPtr<Element> cloneElementWithoutAttributesAndChildren();
634
635     QualifiedName m_tagName;
636
637     unsigned rareDataChildIndex() const;
638
639     SpellcheckAttributeState spellcheckAttributeState() const;
640
641     void updateNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
642     void updateExtraNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
643
644     void createUniqueElementData();
645
646     bool shouldInvalidateDistributionWhenAttributeChanged(ElementShadow*, const QualifiedName&, const AtomicString&);
647
648     ElementRareData* elementRareData() const;
649     ElementRareData& ensureElementRareData();
650
651     void detachAllAttrNodesFromElement();
652     void detachAttrNodeFromElementWithValue(Attr*, const AtomicString& value);
653     void detachAttrNodeAtIndex(Attr*, size_t index);
654
655     bool isJavaScriptURLAttribute(const Attribute&) const;
656
657     RefPtr<ElementData> m_elementData;
658 };
659
660 DEFINE_NODE_TYPE_CASTS(Element, isElementNode());
661 template <typename T> bool isElementOfType(const Element&);
662 template <typename T> inline bool isElementOfType(const Node& node) { return node.isElementNode() && isElementOfType<const T>(toElement(node)); }
663 template <> inline bool isElementOfType<const Element>(const Element&) { return true; }
664
665 // Type casting.
666 template<typename T> inline T& toElement(Node& node)
667 {
668     ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
669     return static_cast<T&>(node);
670 }
671 template<typename T> inline T* toElement(Node* node)
672 {
673     ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
674     return static_cast<T*>(node);
675 }
676 template<typename T> inline const T& toElement(const Node& node)
677 {
678     ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
679     return static_cast<const T&>(node);
680 }
681 template<typename T> inline const T* toElement(const Node* node)
682 {
683     ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
684     return static_cast<const T*>(node);
685 }
686 template<typename T, typename U> inline T* toElement(const RefPtr<U>& node) { return toElement<T>(node.get()); }
687
688 inline bool isDisabledFormControl(const Node* node)
689 {
690     return node->isElementNode() && toElement(node)->isDisabledFormControl();
691 }
692
693 inline bool Node::hasTagName(const QualifiedName& name) const
694 {
695     return isElementNode() && toElement(this)->hasTagName(name);
696 }
697
698 inline Element* Node::parentElement() const
699 {
700     ContainerNode* parent = parentNode();
701     return parent && parent->isElementNode() ? toElement(parent) : 0;
702 }
703
704 inline bool Element::fastHasAttribute(const QualifiedName& name) const
705 {
706     ASSERT(fastAttributeLookupAllowed(name));
707     return elementData() && getAttributeItem(name);
708 }
709
710 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
711 {
712     ASSERT(fastAttributeLookupAllowed(name));
713     if (elementData()) {
714         if (const Attribute* attribute = getAttributeItem(name))
715             return attribute->value();
716     }
717     return nullAtom;
718 }
719
720 inline bool Element::hasAttributesWithoutUpdate() const
721 {
722     return elementData() && !elementData()->isEmpty();
723 }
724
725 inline const AtomicString& Element::idForStyleResolution() const
726 {
727     ASSERT(hasID());
728     return elementData()->idForStyleResolution();
729 }
730
731 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
732 {
733     // FIXME: This check is probably not correct for the case where the document has an id attribute
734     // with a non-null namespace, because it will return false, a false negative, if the prefixes
735     // don't match but the local name and namespace both do. However, since this has been like this
736     // for a while and the code paths may be hot, we'll have to measure performance if we fix it.
737     return attributeName == HTMLNames::idAttr;
738 }
739
740 inline const AtomicString& Element::getIdAttribute() const
741 {
742     return hasID() ? fastGetAttribute(HTMLNames::idAttr) : nullAtom;
743 }
744
745 inline const AtomicString& Element::getNameAttribute() const
746 {
747     return hasName() ? fastGetAttribute(HTMLNames::nameAttr) : nullAtom;
748 }
749
750 inline const AtomicString& Element::getClassAttribute() const
751 {
752     if (!hasClass())
753         return nullAtom;
754     if (isSVGElement())
755         return getAttribute(HTMLNames::classAttr);
756     return fastGetAttribute(HTMLNames::classAttr);
757 }
758
759 inline bool Element::shouldIgnoreAttributeCase() const
760 {
761     return isHTMLElement() && document().isHTMLDocument();
762 }
763
764 inline void Element::setIdAttribute(const AtomicString& value)
765 {
766     setAttribute(HTMLNames::idAttr, value);
767 }
768
769 inline const SpaceSplitString& Element::classNames() const
770 {
771     ASSERT(hasClass());
772     ASSERT(elementData());
773     return elementData()->classNames();
774 }
775
776 inline size_t Element::attributeCount() const
777 {
778     ASSERT(elementData());
779     return elementData()->length();
780 }
781
782 inline const Attribute& Element::attributeItem(unsigned index) const
783 {
784     ASSERT(elementData());
785     return elementData()->attributeItem(index);
786 }
787
788 inline const Attribute* Element::getAttributeItem(const QualifiedName& name) const
789 {
790     ASSERT(elementData());
791     return elementData()->getAttributeItem(name);
792 }
793
794 inline bool Element::hasID() const
795 {
796     return elementData() && elementData()->hasID();
797 }
798
799 inline bool Element::hasClass() const
800 {
801     return elementData() && elementData()->hasClass();
802 }
803
804 inline UniqueElementData& Element::ensureUniqueElementData()
805 {
806     if (!elementData() || !elementData()->isUnique())
807         createUniqueElementData();
808     return static_cast<UniqueElementData&>(*m_elementData);
809 }
810
811 // Put here to make them inline.
812 inline bool Node::hasID() const
813 {
814     return isElementNode() && toElement(this)->hasID();
815 }
816
817 inline bool Node::hasClass() const
818 {
819     return isElementNode() && toElement(this)->hasClass();
820 }
821
822 inline Node::InsertionNotificationRequest Node::insertedInto(ContainerNode* insertionPoint)
823 {
824     ASSERT(insertionPoint->inDocument() || isContainerNode());
825     if (insertionPoint->inDocument())
826         setFlag(InDocumentFlag);
827     if (parentOrShadowHostNode()->isInShadowTree())
828         setFlag(IsInShadowTreeFlag);
829     if (childNeedsDistributionRecalc() && !insertionPoint->childNeedsDistributionRecalc())
830         insertionPoint->markAncestorsWithChildNeedsDistributionRecalc();
831     return InsertionDone;
832 }
833
834 inline void Node::removedFrom(ContainerNode* insertionPoint)
835 {
836     ASSERT(insertionPoint->inDocument() || isContainerNode());
837     if (insertionPoint->inDocument())
838         clearFlag(InDocumentFlag);
839     if (isInShadowTree() && !treeScope().rootNode().isShadowRoot())
840         clearFlag(IsInShadowTreeFlag);
841 }
842
843 inline void Element::invalidateStyleAttribute()
844 {
845     ASSERT(elementData());
846     elementData()->m_styleAttributeIsDirty = true;
847 }
848
849 inline const StylePropertySet* Element::presentationAttributeStyle()
850 {
851     if (!elementData())
852         return 0;
853     if (elementData()->m_presentationAttributeStyleIsDirty)
854         updatePresentationAttributeStyle();
855     // Need to call elementData() again since updatePresentationAttributeStyle()
856     // might swap it with a UniqueElementData.
857     return elementData()->presentationAttributeStyle();
858 }
859
860 inline void Element::setTagNameForCreateElementNS(const QualifiedName& tagName)
861 {
862     // We expect this method to be called only to reset the prefix.
863     ASSERT(tagName.localName() == m_tagName.localName());
864     ASSERT(tagName.namespaceURI() == m_tagName.namespaceURI());
865     m_tagName = tagName;
866 }
867
868 inline bool isShadowHost(const Node* node)
869 {
870     return node && node->isElementNode() && toElement(node)->shadow();
871 }
872
873 inline bool isShadowHost(const Element* element)
874 {
875     return element && element->shadow();
876 }
877
878 // These macros do the same as their NODE equivalents but additionally provide a template specialization
879 // for isElementOfType<>() so that the Traversal<> API works for these Element types.
880 #define DEFINE_ELEMENT_TYPE_CASTS(thisType, predicate) \
881     template <> inline bool isElementOfType<const thisType>(const Element& element) { return element.predicate; } \
882     DEFINE_NODE_TYPE_CASTS(thisType, predicate)
883
884 #define DEFINE_ELEMENT_TYPE_CASTS_WITH_FUNCTION(thisType) \
885     template <> inline bool isElementOfType<const thisType>(const Element& element) { return is##thisType(element); } \
886     DEFINE_NODE_TYPE_CASTS_WITH_FUNCTION(thisType)
887
888 } // namespace
889
890 #endif