Upstream version 9.38.198.0
[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-2011, 2013, 2014 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 "core/CSSPropertyNames.h"
29 #include "core/HTMLNames.h"
30 #include "core/css/CSSPrimitiveValue.h"
31 #include "core/dom/Attribute.h"
32 #include "core/dom/ContainerNode.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/heap/Handle.h"
38
39 namespace blink {
40
41 class ActiveAnimations;
42 class Attr;
43 class Attribute;
44 class CSSStyleDeclaration;
45 class ClientRect;
46 class ClientRectList;
47 class CustomElementDefinition;
48 class DOMStringMap;
49 class DOMTokenList;
50 class Document;
51 class ElementRareData;
52 class ElementShadow;
53 class ExceptionState;
54 class Image;
55 class InputMethodContext;
56 class IntSize;
57 class Locale;
58 class MutableStylePropertySet;
59 class PropertySetCSSStyleDeclaration;
60 class PseudoElement;
61 class ShadowRoot;
62 class StylePropertySet;
63
64 enum AffectedSelectorType {
65     AffectedSelectorChecked = 1,
66     AffectedSelectorEnabled = 1 << 1,
67     AffectedSelectorDisabled = 1 << 2,
68     AffectedSelectorIndeterminate = 1 << 3,
69     AffectedSelectorLink = 1 << 4,
70     AffectedSelectorTarget = 1 << 5,
71     AffectedSelectorVisited = 1 << 6
72 };
73 typedef int AffectedSelectorMask;
74
75 enum SpellcheckAttributeState {
76     SpellcheckAttributeTrue,
77     SpellcheckAttributeFalse,
78     SpellcheckAttributeDefault
79 };
80
81 enum ElementFlags {
82     TabIndexWasSetExplicitly = 1 << 0,
83     StyleAffectedByEmpty = 1 << 1,
84     IsInCanvasSubtree = 1 << 2,
85     ContainsFullScreenElement = 1 << 3,
86     IsInTopLayer = 1 << 4,
87     HasPendingResources = 1 << 5,
88
89     NumberOfElementFlags = 6, // Required size of bitfield used to store the flags.
90 };
91
92 class Element : public ContainerNode {
93 public:
94     static PassRefPtrWillBeRawPtr<Element> create(const QualifiedName&, Document*);
95     virtual ~Element();
96
97     DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecopy);
98     DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecut);
99     DEFINE_ATTRIBUTE_EVENT_LISTENER(beforepaste);
100     DEFINE_ATTRIBUTE_EVENT_LISTENER(copy);
101     DEFINE_ATTRIBUTE_EVENT_LISTENER(cut);
102     DEFINE_ATTRIBUTE_EVENT_LISTENER(paste);
103     DEFINE_ATTRIBUTE_EVENT_LISTENER(search);
104     DEFINE_ATTRIBUTE_EVENT_LISTENER(selectstart);
105     DEFINE_ATTRIBUTE_EVENT_LISTENER(touchcancel);
106     DEFINE_ATTRIBUTE_EVENT_LISTENER(touchend);
107     DEFINE_ATTRIBUTE_EVENT_LISTENER(touchmove);
108     DEFINE_ATTRIBUTE_EVENT_LISTENER(touchstart);
109     DEFINE_ATTRIBUTE_EVENT_LISTENER(wheel);
110
111     bool hasAttribute(const QualifiedName&) const;
112     const AtomicString& getAttribute(const QualifiedName&) const;
113
114     // Passing nullAtom as the second parameter removes the attribute when calling either of these set methods.
115     void setAttribute(const QualifiedName&, const AtomicString& value);
116     void setSynchronizedLazyAttribute(const QualifiedName&, const AtomicString& value);
117
118     void removeAttribute(const QualifiedName&);
119
120     // Typed getters and setters for language bindings.
121     int getIntegralAttribute(const QualifiedName& attributeName) const;
122     void setIntegralAttribute(const QualifiedName& attributeName, int value);
123     unsigned getUnsignedIntegralAttribute(const QualifiedName& attributeName) const;
124     void setUnsignedIntegralAttribute(const QualifiedName& attributeName, unsigned value);
125     double getFloatingPointAttribute(const QualifiedName& attributeName, double fallbackValue = std::numeric_limits<double>::quiet_NaN()) const;
126     void setFloatingPointAttribute(const QualifiedName& attributeName, double value);
127
128     // Call this to get the value of an attribute that is known not to be the style
129     // attribute or one of the SVG animatable attributes.
130     bool fastHasAttribute(const QualifiedName&) const;
131     const AtomicString& fastGetAttribute(const QualifiedName&) const;
132 #if ENABLE(ASSERT)
133     bool fastAttributeLookupAllowed(const QualifiedName&) const;
134 #endif
135
136 #ifdef DUMP_NODE_STATISTICS
137     bool hasNamedNodeMap() const;
138 #endif
139     bool hasAttributes() const;
140
141     bool hasAttribute(const AtomicString& name) const;
142     bool hasAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
143
144     const AtomicString& getAttribute(const AtomicString& name) const;
145     const AtomicString& getAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
146
147     void setAttribute(const AtomicString& name, const AtomicString& value, ExceptionState&);
148     static bool parseAttributeName(QualifiedName&, const AtomicString& namespaceURI, const AtomicString& qualifiedName, ExceptionState&);
149     void setAttributeNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, const AtomicString& value, ExceptionState&);
150
151     bool isIdAttributeName(const QualifiedName&) const;
152     const AtomicString& getIdAttribute() const;
153     void setIdAttribute(const AtomicString&);
154
155     const AtomicString& getNameAttribute() const;
156     const AtomicString& getClassAttribute() const;
157
158     bool shouldIgnoreAttributeCase() const;
159
160     // Call this to get the value of the id attribute for style resolution purposes.
161     // The value will already be lowercased if the document is in compatibility mode,
162     // so this function is not suitable for non-style uses.
163     const AtomicString& idForStyleResolution() const;
164
165     // This getter takes care of synchronizing all attributes before returning the
166     // AttributeCollection. If the Element has no attributes, an empty AttributeCollection
167     // will be returned. This is not a trivial getter and its return value should be cached
168     // for performance.
169     AttributeCollection attributes() const;
170     // This variant will not update the potentially invalid attributes. To be used when not interested
171     // in style attribute or one of the SVG animation attributes.
172     AttributeCollection attributesWithoutUpdate() const;
173
174     void scrollIntoView(bool alignToTop = true);
175     void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
176
177     int offsetLeft();
178     int offsetTop();
179     int offsetWidth();
180     int offsetHeight();
181
182     // FIXME: Replace uses of offsetParent in the platform with calls
183     // to the render layer and merge offsetParentForBindings and offsetParent.
184     Element* offsetParentForBindings();
185
186     Element* offsetParent();
187     int clientLeft();
188     int clientTop();
189     int clientWidth();
190     int clientHeight();
191     virtual int scrollLeft();
192     virtual int scrollTop();
193     virtual void setScrollLeft(int);
194     virtual void setScrollLeft(const Dictionary& scrollOptionsHorizontal, ExceptionState&);
195     virtual void setScrollTop(int);
196     virtual void setScrollTop(const Dictionary& scrollOptionsVertical, ExceptionState&);
197     virtual int scrollWidth();
198     virtual int scrollHeight();
199
200     IntRect boundsInRootViewSpace();
201
202     PassRefPtrWillBeRawPtr<ClientRectList> getClientRects();
203     PassRefPtrWillBeRawPtr<ClientRect> getBoundingClientRect();
204
205     // Returns the absolute bounding box translated into screen coordinates:
206     IntRect screenRect() const;
207
208     virtual void didMoveToNewDocument(Document&) OVERRIDE;
209
210     void removeAttribute(const AtomicString& name);
211     void removeAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName);
212
213     PassRefPtrWillBeRawPtr<Attr> detachAttribute(size_t index);
214
215     PassRefPtrWillBeRawPtr<Attr> getAttributeNode(const AtomicString& name);
216     PassRefPtrWillBeRawPtr<Attr> getAttributeNodeNS(const AtomicString& namespaceURI, const AtomicString& localName);
217     PassRefPtrWillBeRawPtr<Attr> setAttributeNode(Attr*, ExceptionState&);
218     PassRefPtrWillBeRawPtr<Attr> setAttributeNodeNS(Attr*, ExceptionState&);
219     PassRefPtrWillBeRawPtr<Attr> removeAttributeNode(Attr*, ExceptionState&);
220
221     PassRefPtrWillBeRawPtr<Attr> attrIfExists(const QualifiedName&);
222     PassRefPtrWillBeRawPtr<Attr> ensureAttr(const QualifiedName&);
223
224     WillBeHeapVector<RefPtrWillBeMember<Attr> >* attrNodeList();
225
226     CSSStyleDeclaration* style();
227
228     const QualifiedName& tagQName() const { return m_tagName; }
229     String tagName() const { return nodeName(); }
230
231     bool hasTagName(const QualifiedName& tagName) const { return m_tagName.matches(tagName); }
232     bool hasTagName(const HTMLQualifiedName& tagName) const { return ContainerNode::hasTagName(tagName); }
233     bool hasTagName(const SVGQualifiedName& tagName) const { return ContainerNode::hasTagName(tagName); }
234
235     // Should be called only by Document::createElementNS to fix up m_tagName immediately after construction.
236     void setTagNameForCreateElementNS(const QualifiedName&);
237
238     // A fast function for checking the local name against another atomic string.
239     bool hasLocalName(const AtomicString& other) const { return m_tagName.localName() == other; }
240
241     virtual const AtomicString& localName() const OVERRIDE FINAL { return m_tagName.localName(); }
242     const AtomicString& prefix() const { return m_tagName.prefix(); }
243     virtual const AtomicString& namespaceURI() const OVERRIDE FINAL { return m_tagName.namespaceURI(); }
244
245     const AtomicString& locateNamespacePrefix(const AtomicString& namespaceURI) const;
246
247     virtual KURL baseURI() const OVERRIDE FINAL;
248
249     virtual String nodeName() const OVERRIDE;
250
251     PassRefPtrWillBeRawPtr<Element> cloneElementWithChildren();
252     PassRefPtrWillBeRawPtr<Element> cloneElementWithoutChildren();
253
254     void scheduleSVGFilterLayerUpdateHack();
255
256     void normalizeAttributes();
257
258     void setBooleanAttribute(const QualifiedName& name, bool);
259
260     virtual const StylePropertySet* additionalPresentationAttributeStyle() { return 0; }
261     void invalidateStyleAttribute();
262
263     const StylePropertySet* inlineStyle() const { return elementData() ? elementData()->m_inlineStyle.get() : 0; }
264
265     bool setInlineStyleProperty(CSSPropertyID, CSSValueID identifier, bool important = false);
266     bool setInlineStyleProperty(CSSPropertyID, double value, CSSPrimitiveValue::UnitType, bool important = false);
267     bool setInlineStyleProperty(CSSPropertyID, const String& value, bool important = false);
268     bool removeInlineStyleProperty(CSSPropertyID);
269     void removeAllInlineStyleProperties();
270
271     void synchronizeStyleAttributeInternal() const;
272
273     const StylePropertySet* presentationAttributeStyle();
274     virtual bool isPresentationAttribute(const QualifiedName&) const { return false; }
275     virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) { }
276
277     // For exposing to DOM only.
278     NamedNodeMap* attributesForBindings() const;
279
280     enum AttributeModificationReason {
281         ModifiedDirectly,
282         ModifiedByCloning
283     };
284
285     // This method is called whenever an attribute is added, changed or removed.
286     virtual void attributeWillChange(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue) { }
287     virtual void attributeChanged(const QualifiedName&, const AtomicString&, AttributeModificationReason = ModifiedDirectly);
288     virtual void parseAttribute(const QualifiedName&, const AtomicString&);
289
290     virtual bool hasLegalLinkAttribute(const QualifiedName&) const;
291     virtual const QualifiedName& subResourceAttributeName() const;
292
293     // Only called by the parser immediately after element construction.
294     void parserSetAttributes(const Vector<Attribute>&);
295
296     // Remove attributes that might introduce scripting from the vector leaving the element unchanged.
297     void stripScriptingAttributes(Vector<Attribute>&) const;
298
299     bool sharesSameElementData(const Element& other) const { return elementData() == other.elementData(); }
300
301     // Clones attributes only.
302     void cloneAttributesFromElement(const Element&);
303
304     // Clones all attribute-derived data, including subclass specifics (through copyNonAttributeProperties.)
305     void cloneDataFromElement(const Element&);
306
307     bool hasEquivalentAttributes(const Element* other) const;
308
309     virtual void copyNonAttributePropertiesFromElement(const Element&) { }
310
311     virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
312     virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
313     virtual RenderObject* createRenderer(RenderStyle*);
314     virtual bool rendererIsNeeded(const RenderStyle&);
315     void recalcStyle(StyleRecalcChange, Text* nextTextSibling = 0);
316     void didAffectSelector(AffectedSelectorMask);
317     void setAnimationStyleChange(bool);
318     void setNeedsAnimationStyleRecalc();
319
320     void setNeedsCompositingUpdate();
321
322     bool supportsStyleSharing() const;
323
324     ElementShadow* shadow() const;
325     ElementShadow& ensureShadow();
326     PassRefPtrWillBeRawPtr<ShadowRoot> createShadowRoot(ExceptionState&);
327     ShadowRoot* shadowRoot() const;
328     ShadowRoot* youngestShadowRoot() const;
329
330     bool hasAuthorShadowRoot() const { return shadowRoot(); }
331     ShadowRoot* userAgentShadowRoot() const;
332     ShadowRoot& ensureUserAgentShadowRoot();
333     virtual void willAddFirstAuthorShadowRoot() { }
334
335     bool isInDescendantTreeOf(const Element* shadowHost) const;
336
337     RenderStyle* computedStyle(PseudoId = NOPSEUDO);
338
339     // Methods for indicating the style is affected by dynamic updates (e.g., children changing, our position changing in our sibling list, etc.)
340     bool styleAffectedByEmpty() const { return hasElementFlag(StyleAffectedByEmpty); }
341     void setStyleAffectedByEmpty() { setElementFlag(StyleAffectedByEmpty); }
342
343     void setIsInCanvasSubtree(bool value) { setElementFlag(IsInCanvasSubtree, value); }
344     bool isInCanvasSubtree() const { return hasElementFlag(IsInCanvasSubtree); }
345
346     bool isUpgradedCustomElement() { return customElementState() == Upgraded; }
347     bool isUnresolvedCustomElement() { return customElementState() == WaitingForUpgrade; }
348
349     AtomicString computeInheritedLanguage() const;
350     Locale& locale() const;
351
352     virtual void accessKeyAction(bool /*sendToAnyEvent*/) { }
353
354     virtual bool isURLAttribute(const Attribute&) const { return false; }
355     virtual bool isHTMLContentAttribute(const Attribute&) const { return false; }
356
357     virtual bool isLiveLink() const { return false; }
358     KURL hrefURL() const;
359
360     KURL getURLAttribute(const QualifiedName&) const;
361     KURL getNonEmptyURLAttribute(const QualifiedName&) const;
362
363     virtual const AtomicString imageSourceURL() const;
364     virtual Image* imageContents() { return 0; }
365
366     virtual void focus(bool restorePreviousSelection = true, FocusType = FocusTypeNone);
367     virtual void updateFocusAppearance(bool restorePreviousSelection);
368     virtual void blur();
369     // Whether this element can receive focus at all. Most elements are not
370     // focusable but some elements, such as form controls and links, are. Unlike
371     // rendererIsFocusable(), this method may be called when layout is not up to
372     // date, so it must not use the renderer to determine focusability.
373     virtual bool supportsFocus() const;
374     // Whether the node can actually be focused.
375     bool isFocusable() const;
376     virtual bool isKeyboardFocusable() const;
377     virtual bool isMouseFocusable() const;
378     virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType);
379     virtual void dispatchBlurEvent(Element* newFocusedElement);
380     void dispatchFocusInEvent(const AtomicString& eventType, Element* oldFocusedElement);
381     void dispatchFocusOutEvent(const AtomicString& eventType, Element* newFocusedElement);
382
383     String innerText();
384     String outerText();
385     String innerHTML() const;
386     String outerHTML() const;
387     void setInnerHTML(const String&, ExceptionState&);
388     void setOuterHTML(const String&, ExceptionState&);
389
390     Element* insertAdjacentElement(const String& where, Element* newChild, ExceptionState&);
391     void insertAdjacentText(const String& where, const String& text, ExceptionState&);
392     void insertAdjacentHTML(const String& where, const String& html, ExceptionState&);
393
394     String textFromChildren();
395
396     virtual String title() const { return String(); }
397
398     virtual const AtomicString& shadowPseudoId() const;
399     void setShadowPseudoId(const AtomicString&);
400
401     LayoutSize minimumSizeForResizing() const;
402     void setMinimumSizeForResizing(const LayoutSize&);
403
404     virtual void didBecomeFullscreenElement() { }
405     virtual void willStopBeingFullscreenElement() { }
406
407     // Called by the parser when this element's close tag is reached,
408     // signaling that all child tags have been parsed and added.
409     // This is needed for <applet> and <object> elements, which can't lay themselves out
410     // until they know all of their nested <param>s. [Radar 3603191, 4040848].
411     // Also used for script elements and some SVG elements for similar purposes,
412     // but making parsing a special case in this respect should be avoided if possible.
413     virtual void finishParsingChildren();
414
415     void beginParsingChildren() { setIsFinishedParsingChildren(false); }
416
417     PseudoElement* pseudoElement(PseudoId) const;
418     RenderObject* pseudoElementRenderer(PseudoId) const;
419
420     virtual bool matchesReadOnlyPseudoClass() const { return false; }
421     virtual bool matchesReadWritePseudoClass() const { return false; }
422     bool matches(const String& selectors, ExceptionState&);
423     virtual bool shouldAppearIndeterminate() const { return false; }
424
425     DOMTokenList& classList();
426
427     DOMStringMap& dataset();
428
429 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
430     virtual bool isDateTimeEditElement() const { return false; }
431     virtual bool isDateTimeFieldElement() const { return false; }
432     virtual bool isPickerIndicatorElement() const { return false; }
433 #endif
434
435     virtual bool isFormControlElement() const { return false; }
436     virtual bool isSpinButtonElement() const { return false; }
437     virtual bool isTextFormControl() const { return false; }
438     virtual bool isOptionalFormControl() const { return false; }
439     virtual bool isRequiredFormControl() const { return false; }
440     virtual bool isDefaultButtonForForm() const { return false; }
441     virtual bool willValidate() const { return false; }
442     virtual bool isValidFormControlElement() { return false; }
443     virtual bool isInRange() const { return false; }
444     virtual bool isOutOfRange() const { return false; }
445     virtual bool isClearButtonElement() const { return false; }
446
447     virtual bool canContainRangeEndPoint() const OVERRIDE { return true; }
448
449     // Used for disabled form elements; if true, prevents mouse events from being dispatched
450     // to event listeners, and prevents DOMActivate events from being sent at all.
451     virtual bool isDisabledFormControl() const { return false; }
452
453     bool hasPendingResources() const { return hasElementFlag(HasPendingResources); }
454     void setHasPendingResources() { setElementFlag(HasPendingResources); }
455     void clearHasPendingResources() { clearElementFlag(HasPendingResources); }
456     virtual void buildPendingResource() { };
457
458     void setCustomElementDefinition(PassRefPtr<CustomElementDefinition>);
459     CustomElementDefinition* customElementDefinition() const;
460
461     bool containsFullScreenElement() const { return hasElementFlag(ContainsFullScreenElement); }
462     void setContainsFullScreenElement(bool);
463     void setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(bool);
464
465     bool isInTopLayer() const { return hasElementFlag(IsInTopLayer); }
466     void setIsInTopLayer(bool);
467
468     void requestPointerLock();
469
470     bool isSpellCheckingEnabled() const;
471
472     // FIXME: public for RenderTreeBuilder, we shouldn't expose this though.
473     PassRefPtr<RenderStyle> styleForRenderer();
474
475     bool hasID() const;
476     bool hasClass() const;
477     const SpaceSplitString& classNames() const;
478
479     IntSize savedLayerScrollOffset() const;
480     void setSavedLayerScrollOffset(const IntSize&);
481
482     ActiveAnimations* activeAnimations() const;
483     ActiveAnimations& ensureActiveAnimations();
484     bool hasActiveAnimations() const;
485
486     InputMethodContext& inputMethodContext();
487     bool hasInputMethodContext() const;
488
489     void setPrefix(const AtomicString&, ExceptionState&);
490
491     void synchronizeAttribute(const AtomicString& localName) const;
492
493     MutableStylePropertySet& ensureMutableInlineStyle();
494     void clearMutableInlineStyleIfEmpty();
495
496     void setTabIndex(int);
497     virtual short tabIndex() const OVERRIDE;
498
499     virtual void trace(Visitor*) OVERRIDE;
500
501 protected:
502     Element(const QualifiedName& tagName, Document*, ConstructionType);
503
504     const ElementData* elementData() const { return m_elementData.get(); }
505     UniqueElementData& ensureUniqueElementData();
506
507     void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, CSSValueID identifier);
508     void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, double value, CSSPrimitiveValue::UnitType);
509     void addPropertyToPresentationAttributeStyle(MutableStylePropertySet*, CSSPropertyID, const String& value);
510
511     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
512     virtual void removedFrom(ContainerNode*) OVERRIDE;
513     virtual void childrenChanged(const ChildrenChange&) OVERRIDE;
514
515     virtual void willRecalcStyle(StyleRecalcChange);
516     virtual void didRecalcStyle(StyleRecalcChange);
517     virtual PassRefPtr<RenderStyle> customStyleForRenderer();
518
519     virtual bool shouldRegisterAsNamedItem() const { return false; }
520     virtual bool shouldRegisterAsExtraNamedItem() const { return false; }
521
522     bool supportsSpatialNavigationFocus() const;
523
524     void clearTabIndexExplicitlyIfNeeded();
525     void setTabIndexExplicitly(short);
526     // Subclasses may override this method to affect focusability. Unlike
527     // supportsFocus, this method must be called on an up-to-date layout, so it
528     // may use the renderer to reason about focusability. This method cannot be
529     // moved to RenderObject because some focusable nodes don't have renderers,
530     // e.g., HTMLOptionElement.
531     virtual bool rendererIsFocusable() const;
532
533     // classAttributeChanged() exists to share code between
534     // parseAttribute (called via setAttribute()) and
535     // svgAttributeChanged (called when element.className.baseValue is set)
536     void classAttributeChanged(const AtomicString& newClassString);
537
538     PassRefPtr<RenderStyle> originalStyleForRenderer();
539
540     Node* insertAdjacent(const String& where, Node* newChild, ExceptionState&);
541
542 private:
543     bool hasElementFlag(ElementFlags mask) const { return hasRareData() && hasElementFlagInternal(mask); }
544     void setElementFlag(ElementFlags, bool value = true);
545     void clearElementFlag(ElementFlags);
546     bool hasElementFlagInternal(ElementFlags) const;
547
548     bool isElementNode() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
549     bool isDocumentFragment() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
550     bool isDocumentNode() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
551
552     void styleAttributeChanged(const AtomicString& newStyleString, AttributeModificationReason);
553
554     void updatePresentationAttributeStyle();
555
556     void inlineStyleChanged();
557     PropertySetCSSStyleDeclaration* inlineStyleCSSOMWrapper();
558     void setInlineStyleFromString(const AtomicString&);
559
560     StyleRecalcChange recalcOwnStyle(StyleRecalcChange);
561     void recalcChildStyle(StyleRecalcChange);
562
563     inline void checkForEmptyStyleChange();
564
565     void updatePseudoElement(PseudoId, StyleRecalcChange);
566
567     inline void createPseudoElementIfNeeded(PseudoId);
568
569     // FIXME: Everyone should allow author shadows.
570     virtual bool areAuthorShadowsAllowed() const { return true; }
571     virtual void didAddUserAgentShadowRoot(ShadowRoot&) { }
572     virtual bool alwaysCreateUserAgentShadowRoot() const { return false; }
573
574     // FIXME: Remove the need for Attr to call willModifyAttribute/didModifyAttribute.
575     friend class Attr;
576
577     enum SynchronizationOfLazyAttribute { NotInSynchronizationOfLazyAttribute = 0, InSynchronizationOfLazyAttribute };
578
579     void didAddAttribute(const QualifiedName&, const AtomicString&);
580     void willModifyAttribute(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue);
581     void didModifyAttribute(const QualifiedName&, const AtomicString&);
582     void didRemoveAttribute(const QualifiedName&);
583
584     void synchronizeAllAttributes() const;
585     void synchronizeAttribute(const QualifiedName&) const;
586
587     void updateId(const AtomicString& oldId, const AtomicString& newId);
588     void updateId(TreeScope&, const AtomicString& oldId, const AtomicString& newId);
589     void updateName(const AtomicString& oldName, const AtomicString& newName);
590
591     virtual NodeType nodeType() const OVERRIDE FINAL;
592     virtual bool childTypeAllowed(NodeType) const OVERRIDE FINAL;
593
594     void setAttributeInternal(size_t index, const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
595     void appendAttributeInternal(const QualifiedName&, const AtomicString& value, SynchronizationOfLazyAttribute);
596     void removeAttributeInternal(size_t index, SynchronizationOfLazyAttribute);
597     void attributeChangedFromParserOrByCloning(const QualifiedName&, const AtomicString&, AttributeModificationReason);
598
599 #ifndef NDEBUG
600     virtual void formatForDebugger(char* buffer, unsigned length) const OVERRIDE;
601 #endif
602
603     bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
604
605     void cancelFocusAppearanceUpdate();
606
607     virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) OVERRIDE { return computedStyle(pseudoElementSpecifier); }
608
609     inline void updateCallbackSelectors(RenderStyle* oldStyle, RenderStyle* newStyle);
610     inline void removeCallbackSelectors();
611     inline void addCallbackSelectors();
612
613     // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
614     // are used instead.
615     virtual PassRefPtrWillBeRawPtr<Node> cloneNode(bool deep) OVERRIDE;
616     virtual PassRefPtrWillBeRawPtr<Element> cloneElementWithoutAttributesAndChildren();
617
618     QualifiedName m_tagName;
619
620     SpellcheckAttributeState spellcheckAttributeState() const;
621
622     void updateNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
623     void updateExtraNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName);
624
625     void createUniqueElementData();
626
627     bool shouldInvalidateDistributionWhenAttributeChanged(ElementShadow*, const QualifiedName&, const AtomicString&);
628
629     ElementRareData* elementRareData() const;
630     ElementRareData& ensureElementRareData();
631
632     WillBeHeapVector<RefPtrWillBeMember<Attr> >& ensureAttrNodeList();
633     void removeAttrNodeList();
634     void detachAllAttrNodesFromElement();
635     void detachAttrNodeFromElementWithValue(Attr*, const AtomicString& value);
636     void detachAttrNodeAtIndex(Attr*, size_t index);
637
638     bool isJavaScriptURLAttribute(const Attribute&) const;
639
640     RefPtrWillBeMember<ElementData> m_elementData;
641 };
642
643 DEFINE_NODE_TYPE_CASTS(Element, isElementNode());
644 template <typename T> bool isElementOfType(const Node&);
645 template <> inline bool isElementOfType<const Element>(const Node& node) { return node.isElementNode(); }
646 template <typename T> inline bool isElementOfType(const Element& element) { return isElementOfType<T>(static_cast<const Node&>(element)); }
647 template <> inline bool isElementOfType<const Element>(const Element&) { return true; }
648
649 // Type casting.
650 template<typename T> inline T& toElement(Node& node)
651 {
652     ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
653     return static_cast<T&>(node);
654 }
655 template<typename T> inline T* toElement(Node* node)
656 {
657     ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
658     return static_cast<T*>(node);
659 }
660 template<typename T> inline const T& toElement(const Node& node)
661 {
662     ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const T>(node));
663     return static_cast<const T&>(node);
664 }
665 template<typename T> inline const T* toElement(const Node* node)
666 {
667     ASSERT_WITH_SECURITY_IMPLICATION(!node || isElementOfType<const T>(*node));
668     return static_cast<const T*>(node);
669 }
670 template<typename T, typename U> inline T* toElement(const RefPtr<U>& node) { return toElement<T>(node.get()); }
671
672 inline bool isDisabledFormControl(const Node* node)
673 {
674     return node->isElementNode() && toElement(node)->isDisabledFormControl();
675 }
676
677 inline Element* Node::parentElement() const
678 {
679     ContainerNode* parent = parentNode();
680     return parent && parent->isElementNode() ? toElement(parent) : 0;
681 }
682
683 inline bool Element::fastHasAttribute(const QualifiedName& name) const
684 {
685     ASSERT(fastAttributeLookupAllowed(name));
686     return elementData() && elementData()->attributes().findIndex(name) != kNotFound;
687 }
688
689 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
690 {
691     ASSERT(fastAttributeLookupAllowed(name));
692     if (elementData()) {
693         if (const Attribute* attribute = elementData()->attributes().find(name))
694             return attribute->value();
695     }
696     return nullAtom;
697 }
698
699 inline AttributeCollection Element::attributes() const
700 {
701     if (!elementData())
702         return AttributeCollection();
703     synchronizeAllAttributes();
704     return elementData()->attributes();
705 }
706
707 inline AttributeCollection Element::attributesWithoutUpdate() const
708 {
709     if (!elementData())
710         return AttributeCollection();
711     return elementData()->attributes();
712 }
713
714 inline bool Element::hasAttributes() const
715 {
716     return !attributes().isEmpty();
717 }
718
719 inline const AtomicString& Element::idForStyleResolution() const
720 {
721     ASSERT(hasID());
722     return elementData()->idForStyleResolution();
723 }
724
725 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
726 {
727     // FIXME: This check is probably not correct for the case where the document has an id attribute
728     // with a non-null namespace, because it will return false, a false negative, if the prefixes
729     // don't match but the local name and namespace both do. However, since this has been like this
730     // for a while and the code paths may be hot, we'll have to measure performance if we fix it.
731     return attributeName == HTMLNames::idAttr;
732 }
733
734 inline const AtomicString& Element::getIdAttribute() const
735 {
736     return hasID() ? fastGetAttribute(HTMLNames::idAttr) : nullAtom;
737 }
738
739 inline const AtomicString& Element::getNameAttribute() const
740 {
741     return hasName() ? fastGetAttribute(HTMLNames::nameAttr) : nullAtom;
742 }
743
744 inline const AtomicString& Element::getClassAttribute() const
745 {
746     if (!hasClass())
747         return nullAtom;
748     if (isSVGElement())
749         return getAttribute(HTMLNames::classAttr);
750     return fastGetAttribute(HTMLNames::classAttr);
751 }
752
753 inline void Element::setIdAttribute(const AtomicString& value)
754 {
755     setAttribute(HTMLNames::idAttr, value);
756 }
757
758 inline const SpaceSplitString& Element::classNames() const
759 {
760     ASSERT(hasClass());
761     ASSERT(elementData());
762     return elementData()->classNames();
763 }
764
765 inline bool Element::hasID() const
766 {
767     return elementData() && elementData()->hasID();
768 }
769
770 inline bool Element::hasClass() const
771 {
772     return elementData() && elementData()->hasClass();
773 }
774
775 inline UniqueElementData& Element::ensureUniqueElementData()
776 {
777     if (!elementData() || !elementData()->isUnique())
778         createUniqueElementData();
779     return toUniqueElementData(*m_elementData);
780 }
781
782 inline Node::InsertionNotificationRequest Node::insertedInto(ContainerNode* insertionPoint)
783 {
784     ASSERT(!childNeedsStyleInvalidation());
785     ASSERT(!needsStyleInvalidation());
786     ASSERT(insertionPoint->inDocument() || isContainerNode());
787     if (insertionPoint->inDocument())
788         setFlag(InDocumentFlag);
789     if (parentOrShadowHostNode()->isInShadowTree())
790         setFlag(IsInShadowTreeFlag);
791     if (childNeedsDistributionRecalc() && !insertionPoint->childNeedsDistributionRecalc())
792         insertionPoint->markAncestorsWithChildNeedsDistributionRecalc();
793     return InsertionDone;
794 }
795
796 inline void Node::removedFrom(ContainerNode* insertionPoint)
797 {
798     ASSERT(insertionPoint->inDocument() || isContainerNode());
799     if (insertionPoint->inDocument())
800         clearFlag(InDocumentFlag);
801     if (isInShadowTree() && !treeScope().rootNode().isShadowRoot())
802         clearFlag(IsInShadowTreeFlag);
803 }
804
805 inline void Element::invalidateStyleAttribute()
806 {
807     ASSERT(elementData());
808     elementData()->m_styleAttributeIsDirty = true;
809 }
810
811 inline const StylePropertySet* Element::presentationAttributeStyle()
812 {
813     if (!elementData())
814         return 0;
815     if (elementData()->m_presentationAttributeStyleIsDirty)
816         updatePresentationAttributeStyle();
817     // Need to call elementData() again since updatePresentationAttributeStyle()
818     // might swap it with a UniqueElementData.
819     return elementData()->presentationAttributeStyle();
820 }
821
822 inline void Element::setTagNameForCreateElementNS(const QualifiedName& tagName)
823 {
824     // We expect this method to be called only to reset the prefix.
825     ASSERT(tagName.localName() == m_tagName.localName());
826     ASSERT(tagName.namespaceURI() == m_tagName.namespaceURI());
827     m_tagName = tagName;
828 }
829
830 inline bool isShadowHost(const Node* node)
831 {
832     return node && node->isElementNode() && toElement(node)->shadow();
833 }
834
835 inline bool isShadowHost(const Element* element)
836 {
837     return element && element->shadow();
838 }
839
840 // These macros do the same as their NODE equivalents but additionally provide a template specialization
841 // for isElementOfType<>() so that the Traversal<> API works for these Element types.
842 #define DEFINE_ELEMENT_TYPE_CASTS(thisType, predicate) \
843     template <> inline bool isElementOfType<const thisType>(const Node& node) { return node.predicate; } \
844     DEFINE_NODE_TYPE_CASTS(thisType, predicate)
845
846 #define DEFINE_ELEMENT_TYPE_CASTS_WITH_FUNCTION(thisType) \
847     template <> inline bool isElementOfType<const thisType>(const Node& node) { return is##thisType(node); } \
848     DEFINE_NODE_TYPE_CASTS_WITH_FUNCTION(thisType)
849
850 #define DECLARE_ELEMENT_FACTORY_WITH_TAGNAME(T) \
851     static PassRefPtrWillBeRawPtr<T> create(const QualifiedName&, Document&)
852 #define DEFINE_ELEMENT_FACTORY_WITH_TAGNAME(T) \
853     PassRefPtrWillBeRawPtr<T> T::create(const QualifiedName& tagName, Document& document) \
854     { \
855         return adoptRefWillBeNoop(new T(tagName, document)); \
856     }
857
858 } // namespace
859
860 #endif