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 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 "FragmentScriptingPermission.h"
30 #include "NamedNodeMap.h"
31 #include "ScrollTypes.h"
40 class ElementRareData;
43 enum SpellcheckAttributeState {
44 SpellcheckAttributeTrue,
45 SpellcheckAttributeFalse,
46 SpellcheckAttributeDefault
49 class Element : public ContainerNode {
51 static PassRefPtr<Element> create(const QualifiedName&, Document*);
54 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
55 DEFINE_ATTRIBUTE_EVENT_LISTENER(change);
56 DEFINE_ATTRIBUTE_EVENT_LISTENER(click);
57 DEFINE_ATTRIBUTE_EVENT_LISTENER(contextmenu);
58 DEFINE_ATTRIBUTE_EVENT_LISTENER(dblclick);
59 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragenter);
60 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragover);
61 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragleave);
62 DEFINE_ATTRIBUTE_EVENT_LISTENER(drop);
63 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragstart);
64 DEFINE_ATTRIBUTE_EVENT_LISTENER(drag);
65 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragend);
66 DEFINE_ATTRIBUTE_EVENT_LISTENER(input);
67 DEFINE_ATTRIBUTE_EVENT_LISTENER(invalid);
68 DEFINE_ATTRIBUTE_EVENT_LISTENER(keydown);
69 DEFINE_ATTRIBUTE_EVENT_LISTENER(keypress);
70 DEFINE_ATTRIBUTE_EVENT_LISTENER(keyup);
71 DEFINE_ATTRIBUTE_EVENT_LISTENER(mousedown);
72 DEFINE_ATTRIBUTE_EVENT_LISTENER(mousemove);
73 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseout);
74 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseover);
75 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseup);
76 DEFINE_ATTRIBUTE_EVENT_LISTENER(mousewheel);
77 DEFINE_ATTRIBUTE_EVENT_LISTENER(scroll);
78 DEFINE_ATTRIBUTE_EVENT_LISTENER(select);
79 DEFINE_ATTRIBUTE_EVENT_LISTENER(submit);
81 // These four attribute event handler attributes are overridden by HTMLBodyElement
82 // and HTMLFrameSetElement to forward to the DOMWindow.
83 DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(blur);
84 DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(error);
85 DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(focus);
86 DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(load);
89 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecut);
90 DEFINE_ATTRIBUTE_EVENT_LISTENER(cut);
91 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecopy);
92 DEFINE_ATTRIBUTE_EVENT_LISTENER(copy);
93 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforepaste);
94 DEFINE_ATTRIBUTE_EVENT_LISTENER(paste);
95 DEFINE_ATTRIBUTE_EVENT_LISTENER(reset);
96 DEFINE_ATTRIBUTE_EVENT_LISTENER(search);
97 DEFINE_ATTRIBUTE_EVENT_LISTENER(selectstart);
98 #if ENABLE(TOUCH_EVENTS)
99 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchstart);
100 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchmove);
101 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchend);
102 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchcancel);
104 #if ENABLE(FULLSCREEN_API)
105 DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitfullscreenchange);
108 virtual PassRefPtr<DocumentFragment> deprecatedCreateContextualFragment(const String&, FragmentScriptingPermission = FragmentScriptingAllowed);
110 bool hasAttribute(const QualifiedName&) const;
111 const AtomicString& getAttribute(const QualifiedName&) const;
112 void setAttribute(const QualifiedName&, const AtomicString& value, ExceptionCode&);
113 void removeAttribute(const QualifiedName&, ExceptionCode&);
115 // Typed getters and setters for language bindings.
116 int getIntegralAttribute(const QualifiedName& attributeName) const;
117 void setIntegralAttribute(const QualifiedName& attributeName, int value);
118 unsigned getUnsignedIntegralAttribute(const QualifiedName& attributeName) const;
119 void setUnsignedIntegralAttribute(const QualifiedName& attributeName, unsigned value);
121 // Call this to get the value of an attribute that is known not to be the style
122 // attribute or one of the SVG animatable attributes.
123 bool fastHasAttribute(const QualifiedName&) const;
124 const AtomicString& fastGetAttribute(const QualifiedName&) const;
126 bool hasAttributes() const;
128 bool hasAttribute(const String& name) const;
129 bool hasAttributeNS(const String& namespaceURI, const String& localName) const;
131 const AtomicString& getAttribute(const String& name) const;
132 const AtomicString& getAttributeNS(const String& namespaceURI, const String& localName) const;
134 void setAttribute(const AtomicString& name, const AtomicString& value, ExceptionCode&);
135 void setAttributeNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, const AtomicString& value, ExceptionCode&, FragmentScriptingPermission = FragmentScriptingAllowed);
137 bool isIdAttributeName(const QualifiedName&) const;
138 const AtomicString& getIdAttribute() const;
139 void setIdAttribute(const AtomicString&);
141 // Call this to get the value of the id attribute for style resolution purposes.
142 // The value will already be lowercased if the document is in compatibility mode,
143 // so this function is not suitable for non-style uses.
144 const AtomicString& idForStyleResolution() const;
146 void scrollIntoView(bool alignToTop = true);
147 void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
149 void scrollByLines(int lines);
150 void scrollByPages(int pages);
156 Element* offsetParent();
161 virtual int scrollLeft() const;
162 virtual int scrollTop() const;
163 virtual void setScrollLeft(int);
164 virtual void setScrollTop(int);
165 virtual int scrollWidth() const;
166 virtual int scrollHeight() const;
168 IntRect boundsInWindowSpace() const;
170 PassRefPtr<ClientRectList> getClientRects() const;
171 PassRefPtr<ClientRect> getBoundingClientRect() const;
173 // Returns the absolute bounding box translated into screen coordinates:
174 IntRect screenRect() const;
176 void removeAttribute(const String& name, ExceptionCode&);
177 void removeAttributeNS(const String& namespaceURI, const String& localName, ExceptionCode&);
179 PassRefPtr<Attr> getAttributeNode(const String& name);
180 PassRefPtr<Attr> getAttributeNodeNS(const String& namespaceURI, const String& localName);
181 PassRefPtr<Attr> setAttributeNode(Attr*, ExceptionCode&);
182 PassRefPtr<Attr> setAttributeNodeNS(Attr*, ExceptionCode&);
183 PassRefPtr<Attr> removeAttributeNode(Attr*, ExceptionCode&);
185 virtual CSSStyleDeclaration* style();
187 const QualifiedName& tagQName() const { return m_tagName; }
188 String tagName() const { return nodeName(); }
189 bool hasTagName(const QualifiedName& tagName) const { return m_tagName.matches(tagName); }
191 // A fast function for checking the local name against another atomic string.
192 bool hasLocalName(const AtomicString& other) const { return m_tagName.localName() == other; }
193 bool hasLocalName(const QualifiedName& other) const { return m_tagName.localName() == other.localName(); }
195 const AtomicString& localName() const { return m_tagName.localName(); }
196 const AtomicString& prefix() const { return m_tagName.prefix(); }
197 const AtomicString& namespaceURI() const { return m_tagName.namespaceURI(); }
199 virtual KURL baseURI() const;
201 virtual String nodeName() const;
203 PassRefPtr<Element> cloneElementWithChildren();
204 PassRefPtr<Element> cloneElementWithoutChildren();
206 void normalizeAttributes();
207 String nodeNamePreservingCase() const;
209 // convenience methods which ignore exceptions
210 void setAttribute(const QualifiedName&, const AtomicString& value);
211 void setBooleanAttribute(const QualifiedName& name, bool);
212 // Please don't use setCStringAttribute in performance-sensitive code;
213 // use a static AtomicString value instead to avoid the conversion overhead.
214 void setCStringAttribute(const QualifiedName&, const char* cStringValue);
216 NamedNodeMap* attributes(bool readonly = false) const;
218 // This method is called whenever an attribute is added, changed or removed.
219 virtual void attributeChanged(Attribute*, bool preserveDecls = false);
221 void setAttributeMap(PassRefPtr<NamedNodeMap>, FragmentScriptingPermission = FragmentScriptingAllowed);
222 NamedNodeMap* attributeMap() const { return m_attributeMap.get(); }
224 virtual void copyNonAttributeProperties(const Element* /*source*/) { }
226 virtual void attach();
227 virtual void detach();
228 virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
229 virtual void recalcStyle(StyleChange = NoChange);
232 void setShadowRoot(PassRefPtr<Node>);
234 RenderStyle* computedStyle(PseudoId = NOPSEUDO);
236 AtomicString computeInheritedLanguage() const;
238 void dispatchAttrRemovalEvent(Attribute*);
239 void dispatchAttrAdditionEvent(Attribute*);
241 virtual void accessKeyAction(bool /*sendToAnyEvent*/) { }
243 virtual bool isURLAttribute(Attribute*) const;
245 KURL getURLAttribute(const QualifiedName&) const;
246 KURL getNonEmptyURLAttribute(const QualifiedName&) const;
248 virtual const QualifiedName& imageSourceAttributeName() const;
249 virtual String target() const { return String(); }
251 virtual void focus(bool restorePreviousSelection = true);
252 virtual void updateFocusAppearance(bool restorePreviousSelection);
255 String innerText() const;
256 String outerText() const;
258 virtual String title() const;
260 String openTagStartToString() const;
262 void updateId(const AtomicString& oldId, const AtomicString& newId);
264 IntSize minimumSizeForResizing() const;
265 void setMinimumSizeForResizing(const IntSize&);
267 // Use Document::registerForDocumentActivationCallbacks() to subscribe to these
268 virtual void documentWillBecomeInactive() { }
269 virtual void documentDidBecomeActive() { }
271 // Use Document::registerForMediaVolumeCallbacks() to subscribe to this
272 virtual void mediaVolumeDidChange() { }
274 bool isFinishedParsingChildren() const { return isParsingChildrenFinished(); }
275 virtual void finishParsingChildren();
276 virtual void beginParsingChildren() { clearIsParsingChildrenFinished(); }
278 // ElementTraversal API
279 Element* firstElementChild() const;
280 Element* lastElementChild() const;
281 Element* previousElementSibling() const;
282 Element* nextElementSibling() const;
283 unsigned childElementCount() const;
285 bool webkitMatchesSelector(const String& selectors, ExceptionCode&);
287 DOMTokenList* classList();
288 DOMTokenList* optionalClassList() const;
290 DOMStringMap* dataset();
291 DOMStringMap* optionalDataset() const;
294 virtual bool isMathMLElement() const { return false; }
296 static bool isMathMLElement() { return false; }
299 virtual bool isFormControlElement() const { return false; }
300 virtual bool isEnabledFormControl() const { return true; }
301 virtual bool isReadOnlyFormControl() const { return false; }
302 virtual bool isSpinButtonElement() const { return false; }
303 virtual bool isTextFormControl() const { return false; }
304 virtual bool isOptionalFormControl() const { return false; }
305 virtual bool isRequiredFormControl() const { return false; }
306 virtual bool isDefaultButtonForForm() const { return false; }
307 virtual bool willValidate() const { return false; }
308 virtual bool isValidFormControlElement() { return false; }
309 virtual bool hasUnacceptableValue() const { return false; }
310 virtual bool isInRange() const { return false; }
311 virtual bool isOutOfRange() const { return false; }
313 virtual bool formControlValueMatchesRenderer() const { return false; }
314 virtual void setFormControlValueMatchesRenderer(bool) { }
316 virtual const AtomicString& formControlName() const { return nullAtom; }
317 virtual const AtomicString& formControlType() const { return nullAtom; }
319 virtual bool shouldSaveAndRestoreFormControlState() const { return true; }
320 virtual bool saveFormControlState(String&) const { return false; }
321 virtual void restoreFormControlState(const String&) { }
323 virtual void dispatchFormControlChangeEvent() { }
326 virtual bool childShouldCreateRenderer(Node*) const;
329 #if ENABLE(FULLSCREEN_API)
331 ALLOW_KEYBOARD_INPUT = 1
334 void webkitRequestFullScreen(unsigned short flags);
337 virtual bool isSpellCheckingEnabled() const;
340 Element(const QualifiedName& tagName, Document* document, ConstructionType type)
341 : ContainerNode(document, type)
346 virtual void insertedIntoDocument();
347 virtual void removedFromDocument();
348 virtual void insertedIntoTree(bool);
349 virtual void removedFromTree(bool);
350 virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
352 // The implementation of Element::attributeChanged() calls the following two functions.
353 // They are separated to allow a different flow of control in StyledElement::attributeChanged().
354 void recalcStyleIfNeededAfterAttributeChanged(Attribute*);
355 void updateAfterAttributeChanged(Attribute*);
358 void scrollByUnits(int units, ScrollGranularity);
360 virtual void setPrefix(const AtomicString&, ExceptionCode&);
361 virtual NodeType nodeType() const;
362 virtual bool childTypeAllowed(NodeType);
364 virtual PassRefPtr<Attribute> createAttribute(const QualifiedName&, const AtomicString& value);
367 virtual void formatForDebugger(char* buffer, unsigned length) const;
370 bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
372 void createAttributeMap() const;
374 virtual void updateStyleAttribute() const { }
377 virtual void updateAnimatedSVGAttribute(const QualifiedName&) const { }
380 void cancelFocusAppearanceUpdate();
382 virtual const AtomicString& virtualPrefix() const { return prefix(); }
383 virtual const AtomicString& virtualLocalName() const { return localName(); }
384 virtual const AtomicString& virtualNamespaceURI() const { return namespaceURI(); }
385 virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) { return computedStyle(pseudoElementSpecifier); }
387 // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
389 virtual PassRefPtr<Node> cloneNode(bool deep);
390 virtual PassRefPtr<Element> cloneElementWithoutAttributesAndChildren() const;
392 QualifiedName m_tagName;
393 virtual NodeRareData* createRareData();
395 ElementRareData* rareData() const;
396 ElementRareData* ensureRareData();
398 SpellcheckAttributeState spellcheckAttributeState() const;
399 void removeShadowRoot();
402 mutable RefPtr<NamedNodeMap> m_attributeMap;
405 inline Element* toElement(Node* node)
407 ASSERT(!node || node->isElementNode());
408 return static_cast<Element*>(node);
411 inline const Element* toElement(const Node* node)
413 ASSERT(!node || node->isElementNode());
414 return static_cast<const Element*>(node);
417 // This will catch anyone doing an unnecessary cast.
418 void toElement(const Element*);
420 inline bool Node::hasTagName(const QualifiedName& name) const
422 return isElementNode() && toElement(this)->hasTagName(name);
425 inline bool Node::hasAttributes() const
427 return isElementNode() && toElement(this)->hasAttributes();
430 inline NamedNodeMap* Node::attributes() const
432 return isElementNode() ? toElement(this)->attributes() : 0;
435 inline Element* Node::parentElement() const
437 ContainerNode* parent = parentNode();
438 return parent && parent->isElementNode() ? toElement(parent) : 0;
441 inline NamedNodeMap* Element::attributes(bool readonly) const
443 if (!isStyleAttributeValid())
444 updateStyleAttribute();
447 if (!areSVGAttributesValid())
448 updateAnimatedSVGAttribute(anyQName());
451 if (!readonly && !m_attributeMap)
452 createAttributeMap();
453 return m_attributeMap.get();
456 inline void Element::updateId(const AtomicString& oldId, const AtomicString& newId)
464 Document* doc = document();
465 if (!oldId.isEmpty())
466 doc->removeElementById(oldId, this);
467 if (!newId.isEmpty())
468 doc->addElementById(newId, this);
471 inline bool Element::fastHasAttribute(const QualifiedName& name) const
473 return m_attributeMap && m_attributeMap->getAttributeItem(name);
476 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
478 if (m_attributeMap) {
479 if (Attribute* attribute = m_attributeMap->getAttributeItem(name))
480 return attribute->value();
485 inline const AtomicString& Element::idForStyleResolution() const
488 return m_attributeMap->idForStyleResolution();
491 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
493 // FIXME: This check is probably not correct for the case where the document has an id attribute
494 // with a non-null namespace, because it will return false, a false negative, if the prefixes
495 // don't match but the local name and namespace both do. However, since this has been like this
496 // for a while and the code paths may be hot, we'll have to measure performance if we fix it.
497 return attributeName == document()->idAttributeName();
500 inline const AtomicString& Element::getIdAttribute() const
502 return fastGetAttribute(document()->idAttributeName());
505 inline void Element::setIdAttribute(const AtomicString& value)
507 setAttribute(document()->idAttributeName(), value);