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 Apple Inc. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
29 #include "ExceptionCodePlaceholder.h"
30 #include "FragmentScriptingPermission.h"
31 #include "NamedNodeMap.h"
32 #include "ScrollTypes.h"
41 class ElementRareData;
44 class WebKitAnimationList;
46 enum SpellcheckAttributeState {
47 SpellcheckAttributeTrue,
48 SpellcheckAttributeFalse,
49 SpellcheckAttributeDefault
52 class Element : public ContainerNode {
54 static PassRefPtr<Element> create(const QualifiedName&, Document*);
57 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
58 DEFINE_ATTRIBUTE_EVENT_LISTENER(change);
59 DEFINE_ATTRIBUTE_EVENT_LISTENER(click);
60 DEFINE_ATTRIBUTE_EVENT_LISTENER(contextmenu);
61 DEFINE_ATTRIBUTE_EVENT_LISTENER(dblclick);
62 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragenter);
63 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragover);
64 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragleave);
65 DEFINE_ATTRIBUTE_EVENT_LISTENER(drop);
66 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragstart);
67 DEFINE_ATTRIBUTE_EVENT_LISTENER(drag);
68 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragend);
69 DEFINE_ATTRIBUTE_EVENT_LISTENER(input);
70 DEFINE_ATTRIBUTE_EVENT_LISTENER(invalid);
71 DEFINE_ATTRIBUTE_EVENT_LISTENER(keydown);
72 DEFINE_ATTRIBUTE_EVENT_LISTENER(keypress);
73 DEFINE_ATTRIBUTE_EVENT_LISTENER(keyup);
74 DEFINE_ATTRIBUTE_EVENT_LISTENER(mousedown);
75 DEFINE_ATTRIBUTE_EVENT_LISTENER(mousemove);
76 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseout);
77 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseover);
78 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseup);
79 DEFINE_ATTRIBUTE_EVENT_LISTENER(mousewheel);
80 DEFINE_ATTRIBUTE_EVENT_LISTENER(scroll);
81 DEFINE_ATTRIBUTE_EVENT_LISTENER(select);
82 DEFINE_ATTRIBUTE_EVENT_LISTENER(submit);
84 // These four attribute event handler attributes are overridden by HTMLBodyElement
85 // and HTMLFrameSetElement to forward to the DOMWindow.
86 DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(blur);
87 DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(error);
88 DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(focus);
89 DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(load);
92 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecut);
93 DEFINE_ATTRIBUTE_EVENT_LISTENER(cut);
94 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecopy);
95 DEFINE_ATTRIBUTE_EVENT_LISTENER(copy);
96 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforepaste);
97 DEFINE_ATTRIBUTE_EVENT_LISTENER(paste);
98 DEFINE_ATTRIBUTE_EVENT_LISTENER(reset);
99 DEFINE_ATTRIBUTE_EVENT_LISTENER(search);
100 DEFINE_ATTRIBUTE_EVENT_LISTENER(selectstart);
101 #if ENABLE(TOUCH_EVENTS)
102 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchstart);
103 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchmove);
104 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchend);
105 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchcancel);
107 #if ENABLE(FULLSCREEN_API)
108 DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitfullscreenchange);
111 bool hasAttribute(const QualifiedName&) const;
112 const AtomicString& getAttribute(const QualifiedName&) const;
113 void setAttribute(const QualifiedName&, const AtomicString& value, ExceptionCode&);
114 void removeAttribute(const QualifiedName&, ExceptionCode&);
116 // Typed getters and setters for language bindings.
117 int getIntegralAttribute(const QualifiedName& attributeName) const;
118 void setIntegralAttribute(const QualifiedName& attributeName, int value);
119 unsigned getUnsignedIntegralAttribute(const QualifiedName& attributeName) const;
120 void setUnsignedIntegralAttribute(const QualifiedName& attributeName, unsigned value);
122 // Call this to get the value of an attribute that is known not to be the style
123 // attribute or one of the SVG animatable attributes.
124 bool fastHasAttribute(const QualifiedName&) const;
125 const AtomicString& fastGetAttribute(const QualifiedName&) const;
127 bool hasAttributes() const;
129 bool hasAttribute(const String& name) const;
130 bool hasAttributeNS(const String& namespaceURI, const String& localName) const;
132 const AtomicString& getAttribute(const String& name) const;
133 const AtomicString& getAttributeNS(const String& namespaceURI, const String& localName) const;
135 void setAttribute(const AtomicString& name, const AtomicString& value, ExceptionCode&);
136 void setAttributeNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, const AtomicString& value, ExceptionCode&, FragmentScriptingPermission = FragmentScriptingAllowed);
138 bool isIdAttributeName(const QualifiedName&) const;
139 const AtomicString& getIdAttribute() const;
140 void setIdAttribute(const AtomicString&);
142 // Call this to get the value of the id attribute for style resolution purposes.
143 // The value will already be lowercased if the document is in compatibility mode,
144 // so this function is not suitable for non-style uses.
145 const AtomicString& idForStyleResolution() const;
147 void scrollIntoView(bool alignToTop = true);
148 void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
150 void scrollByLines(int lines);
151 void scrollByPages(int pages);
157 Element* offsetParent();
162 virtual int scrollLeft();
163 virtual int scrollTop();
164 virtual void setScrollLeft(int);
165 virtual void setScrollTop(int);
166 virtual int scrollWidth();
167 virtual int scrollHeight();
169 LayoutRect boundsInWindowSpace();
171 PassRefPtr<ClientRectList> getClientRects();
172 PassRefPtr<ClientRect> getBoundingClientRect();
174 // Returns the absolute bounding box translated into screen coordinates:
175 LayoutRect screenRect() const;
177 void removeAttribute(const String& name, ExceptionCode&);
178 void removeAttributeNS(const String& namespaceURI, const String& localName, ExceptionCode&);
180 PassRefPtr<Attr> getAttributeNode(const String& name);
181 PassRefPtr<Attr> getAttributeNodeNS(const String& namespaceURI, const String& localName);
182 PassRefPtr<Attr> setAttributeNode(Attr*, ExceptionCode&);
183 PassRefPtr<Attr> setAttributeNodeNS(Attr*, ExceptionCode&);
184 PassRefPtr<Attr> removeAttributeNode(Attr*, ExceptionCode&);
186 virtual CSSStyleDeclaration* style();
188 const QualifiedName& tagQName() const { return m_tagName; }
189 String tagName() const { return nodeName(); }
190 bool hasTagName(const QualifiedName& tagName) const { return m_tagName.matches(tagName); }
192 // A fast function for checking the local name against another atomic string.
193 bool hasLocalName(const AtomicString& other) const { return m_tagName.localName() == other; }
194 bool hasLocalName(const QualifiedName& other) const { return m_tagName.localName() == other.localName(); }
196 const AtomicString& localName() const { return m_tagName.localName(); }
197 const AtomicString& prefix() const { return m_tagName.prefix(); }
198 const AtomicString& namespaceURI() const { return m_tagName.namespaceURI(); }
200 virtual KURL baseURI() const;
202 virtual String nodeName() const;
204 PassRefPtr<Element> cloneElementWithChildren();
205 PassRefPtr<Element> cloneElementWithoutChildren();
207 void normalizeAttributes();
208 String nodeNamePreservingCase() const;
210 // convenience methods which ignore exceptions
211 void setAttribute(const QualifiedName&, const AtomicString& value);
212 void setBooleanAttribute(const QualifiedName& name, bool);
214 NamedNodeMap* attributes(bool readonly = false) const;
216 // This method is called whenever an attribute is added, changed or removed.
217 virtual void attributeChanged(Attribute*, bool preserveDecls = false);
219 void setAttributeMap(PassRefPtr<NamedNodeMap>, FragmentScriptingPermission = FragmentScriptingAllowed);
220 NamedNodeMap* attributeMap() const { return m_attributeMap.get(); }
222 virtual void copyNonAttributeProperties(const Element* source);
224 virtual void attach();
225 virtual void detach();
226 virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
227 void recalcStyle(StyleChange = NoChange);
229 ShadowRoot* shadowRoot() const;
230 void setShadowRoot(PassRefPtr<ShadowRoot>, ExceptionCode&);
231 ShadowRoot* ensureShadowRoot();
232 void removeShadowRoot();
234 virtual const AtomicString& shadowPseudoId() const;
235 void setShadowPseudoId(const AtomicString&, ExceptionCode& = ASSERT_NO_EXCEPTION);
237 RenderStyle* computedStyle(PseudoId = NOPSEUDO);
239 void setStyleAffectedByEmpty();
240 bool styleAffectedByEmpty() const;
242 AtomicString computeInheritedLanguage() const;
244 void dispatchAttrRemovalEvent(Attribute*);
245 void dispatchAttrAdditionEvent(Attribute*);
247 virtual void accessKeyAction(bool /*sendToAnyEvent*/) { }
249 virtual bool isURLAttribute(Attribute*) const;
251 KURL getURLAttribute(const QualifiedName&) const;
252 KURL getNonEmptyURLAttribute(const QualifiedName&) const;
254 virtual const QualifiedName& imageSourceAttributeName() const;
255 virtual String target() const { return String(); }
257 virtual void focus(bool restorePreviousSelection = true);
258 virtual void updateFocusAppearance(bool restorePreviousSelection);
264 virtual String title() const;
266 void updateId(const AtomicString& oldId, const AtomicString& newId);
268 LayoutSize minimumSizeForResizing() const;
269 void setMinimumSizeForResizing(const LayoutSize&);
271 // Use Document::registerForDocumentActivationCallbacks() to subscribe to these
272 virtual void documentWillBecomeInactive() { }
273 virtual void documentDidBecomeActive() { }
275 // Use Document::registerForMediaVolumeCallbacks() to subscribe to this
276 virtual void mediaVolumeDidChange() { }
278 // Use Document::registerForPrivateBrowsingStateChangedCallbacks() to subscribe to this.
279 virtual void privateBrowsingStateDidChange() { }
281 virtual void didBecomeFullscreenElement() { }
282 virtual void willStopBeingFullscreenElement() { }
284 bool isFinishedParsingChildren() const { return isParsingChildrenFinished(); }
285 virtual void finishParsingChildren();
286 virtual void beginParsingChildren();
288 // ElementTraversal API
289 Element* firstElementChild() const;
290 Element* lastElementChild() const;
291 Element* previousElementSibling() const;
292 Element* nextElementSibling() const;
293 unsigned childElementCount() const;
295 bool webkitMatchesSelector(const String& selectors, ExceptionCode&);
297 DOMTokenList* classList();
298 DOMTokenList* optionalClassList() const;
300 DOMStringMap* dataset();
303 virtual bool isMathMLElement() const { return false; }
305 static bool isMathMLElement() { return false; }
309 virtual bool isMediaElement() const { return false; }
312 #if ENABLE(INPUT_SPEECH)
313 virtual bool isInputFieldSpeechButtonElement() const { return false; }
316 virtual bool isFormControlElement() const { return false; }
317 virtual bool isEnabledFormControl() const { return true; }
318 virtual bool isReadOnlyFormControl() const { return false; }
319 virtual bool isSpinButtonElement() const { return false; }
320 virtual bool isTextFormControl() const { return false; }
321 virtual bool isOptionalFormControl() const { return false; }
322 virtual bool isRequiredFormControl() const { return false; }
323 virtual bool isDefaultButtonForForm() const { return false; }
324 virtual bool willValidate() const { return false; }
325 virtual bool isValidFormControlElement() { return false; }
326 virtual bool hasUnacceptableValue() const { return false; }
327 virtual bool isInRange() const { return false; }
328 virtual bool isOutOfRange() const { return false; }
329 virtual bool isFrameElementBase() const { return false; }
331 virtual bool canContainRangeEndPoint() const { return true; }
333 virtual const AtomicString& formControlName() const { return nullAtom; }
334 virtual const AtomicString& formControlType() const { return nullAtom; }
336 virtual bool shouldSaveAndRestoreFormControlState() const { return true; }
337 virtual bool saveFormControlState(String&) const { return false; }
338 virtual void restoreFormControlState(const String&) { }
340 virtual bool wasChangedSinceLastFormControlChangeEvent() const;
341 virtual void setChangedSinceLastFormControlChangeEvent(bool);
342 virtual void dispatchFormControlChangeEvent() { }
345 virtual bool childShouldCreateRenderer(Node*) const;
348 #if ENABLE(FULLSCREEN_API)
350 ALLOW_KEYBOARD_INPUT = 1
353 void webkitRequestFullScreen(unsigned short flags);
354 virtual bool containsFullScreenElement() const;
355 virtual void setContainsFullScreenElement(bool);
356 virtual void setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(bool);
359 virtual bool isSpellCheckingEnabled() const;
361 PassRefPtr<WebKitAnimationList> webkitGetAnimations() const;
363 PassRefPtr<RenderStyle> styleForRenderer();
366 Element(const QualifiedName& tagName, Document* document, ConstructionType type)
367 : ContainerNode(document, type)
372 virtual void willRemove();
373 virtual void insertedIntoDocument();
374 virtual void removedFromDocument();
375 virtual void insertedIntoTree(bool);
376 virtual void removedFromTree(bool);
377 virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
378 virtual bool willRecalcStyle(StyleChange) { return true; }
379 virtual void didRecalcStyle(StyleChange) { }
380 virtual PassRefPtr<RenderStyle> customStyleForRenderer();
382 // The implementation of Element::attributeChanged() calls the following two functions.
383 // They are separated to allow a different flow of control in StyledElement::attributeChanged().
384 void recalcStyleIfNeededAfterAttributeChanged(Attribute*);
385 void updateAfterAttributeChanged(Attribute*);
387 void idAttributeChanged(Attribute*);
390 void scrollByUnits(int units, ScrollGranularity);
392 virtual void setPrefix(const AtomicString&, ExceptionCode&);
393 virtual NodeType nodeType() const;
394 virtual bool childTypeAllowed(NodeType) const;
396 virtual PassRefPtr<Attribute> createAttribute(const QualifiedName&, const AtomicString& value);
399 virtual void formatForDebugger(char* buffer, unsigned length) const;
402 bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
404 void createAttributeMap() const;
406 virtual void updateStyleAttribute() const { }
409 virtual void updateAnimatedSVGAttribute(const QualifiedName&) const { }
412 void cancelFocusAppearanceUpdate();
414 virtual const AtomicString& virtualPrefix() const { return prefix(); }
415 virtual const AtomicString& virtualLocalName() const { return localName(); }
416 virtual const AtomicString& virtualNamespaceURI() const { return namespaceURI(); }
417 virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) { return computedStyle(pseudoElementSpecifier); }
419 // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
421 virtual PassRefPtr<Node> cloneNode(bool deep);
422 virtual PassRefPtr<Element> cloneElementWithoutAttributesAndChildren();
424 QualifiedName m_tagName;
425 virtual OwnPtr<NodeRareData> createRareData();
427 ElementRareData* rareData() const;
428 ElementRareData* ensureRareData();
430 SpellcheckAttributeState spellcheckAttributeState() const;
433 mutable RefPtr<NamedNodeMap> m_attributeMap;
436 inline Element* toElement(Node* node)
438 ASSERT(!node || node->isElementNode());
439 return static_cast<Element*>(node);
442 inline const Element* toElement(const Node* node)
444 ASSERT(!node || node->isElementNode());
445 return static_cast<const Element*>(node);
448 // This will catch anyone doing an unnecessary cast.
449 void toElement(const Element*);
451 inline bool Node::hasTagName(const QualifiedName& name) const
453 return isElementNode() && toElement(this)->hasTagName(name);
456 inline bool Node::hasLocalName(const AtomicString& name) const
458 return isElementNode() && toElement(this)->hasLocalName(name);
461 inline bool Node::hasAttributes() const
463 return isElementNode() && toElement(this)->hasAttributes();
466 inline NamedNodeMap* Node::attributes() const
468 return isElementNode() ? toElement(this)->attributes() : 0;
471 inline Element* Node::parentElement() const
473 ContainerNode* parent = parentNode();
474 return parent && parent->isElementNode() ? toElement(parent) : 0;
477 inline NamedNodeMap* Element::attributes(bool readonly) const
479 if (!isStyleAttributeValid())
480 updateStyleAttribute();
483 if (!areSVGAttributesValid())
484 updateAnimatedSVGAttribute(anyQName());
487 if (!readonly && !m_attributeMap)
488 createAttributeMap();
489 return m_attributeMap.get();
492 inline void Element::updateId(const AtomicString& oldId, const AtomicString& newId)
500 TreeScope* scope = treeScope();
501 if (!oldId.isEmpty())
502 scope->removeElementById(oldId, this);
503 if (!newId.isEmpty())
504 scope->addElementById(newId, this);
507 inline bool Element::fastHasAttribute(const QualifiedName& name) const
509 return m_attributeMap && m_attributeMap->getAttributeItem(name);
512 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
514 if (m_attributeMap) {
515 if (Attribute* attribute = m_attributeMap->getAttributeItem(name))
516 return attribute->value();
521 inline const AtomicString& Element::idForStyleResolution() const
524 return m_attributeMap->idForStyleResolution();
527 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
529 // FIXME: This check is probably not correct for the case where the document has an id attribute
530 // with a non-null namespace, because it will return false, a false negative, if the prefixes
531 // don't match but the local name and namespace both do. However, since this has been like this
532 // for a while and the code paths may be hot, we'll have to measure performance if we fix it.
533 return attributeName == document()->idAttributeName();
536 inline const AtomicString& Element::getIdAttribute() const
538 return fastGetAttribute(document()->idAttributeName());
541 inline void Element::setIdAttribute(const AtomicString& value)
543 setAttribute(document()->idAttributeName(), value);
546 inline Element* firstElementChild(const ContainerNode* container)
548 ASSERT_ARG(container, container);
549 Node* child = container->firstChild();
550 while (child && !child->isElementNode())
551 child = child->nextSibling();
552 return static_cast<Element*>(child);