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