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>);
233 virtual AtomicString shadowPseudoId() const;
235 RenderStyle* computedStyle(PseudoId = NOPSEUDO);
237 AtomicString computeInheritedLanguage() const;
239 void dispatchAttrRemovalEvent(Attribute*);
240 void dispatchAttrAdditionEvent(Attribute*);
242 virtual void accessKeyAction(bool /*sendToAnyEvent*/) { }
244 virtual bool isURLAttribute(Attribute*) const;
246 KURL getURLAttribute(const QualifiedName&) const;
247 KURL getNonEmptyURLAttribute(const QualifiedName&) const;
249 virtual const QualifiedName& imageSourceAttributeName() const;
250 virtual String target() const { return String(); }
252 virtual void focus(bool restorePreviousSelection = true);
253 virtual void updateFocusAppearance(bool restorePreviousSelection);
256 String innerText() const;
257 String outerText() const;
259 virtual String title() const;
261 String openTagStartToString() const;
263 void updateId(const AtomicString& oldId, const AtomicString& newId);
265 IntSize minimumSizeForResizing() const;
266 void setMinimumSizeForResizing(const IntSize&);
268 // Use Document::registerForDocumentActivationCallbacks() to subscribe to these
269 virtual void documentWillBecomeInactive() { }
270 virtual void documentDidBecomeActive() { }
272 // Use Document::registerForMediaVolumeCallbacks() to subscribe to this
273 virtual void mediaVolumeDidChange() { }
275 bool isFinishedParsingChildren() const { return isParsingChildrenFinished(); }
276 virtual void finishParsingChildren();
277 virtual void beginParsingChildren() { clearIsParsingChildrenFinished(); }
279 // ElementTraversal API
280 Element* firstElementChild() const;
281 Element* lastElementChild() const;
282 Element* previousElementSibling() const;
283 Element* nextElementSibling() const;
284 unsigned childElementCount() const;
286 bool webkitMatchesSelector(const String& selectors, ExceptionCode&);
288 DOMTokenList* classList();
289 DOMTokenList* optionalClassList() const;
291 DOMStringMap* dataset();
292 DOMStringMap* optionalDataset() const;
295 virtual bool isMathMLElement() const { return false; }
297 static bool isMathMLElement() { return false; }
300 virtual bool isFormControlElement() const { return false; }
301 virtual bool isEnabledFormControl() const { return true; }
302 virtual bool isReadOnlyFormControl() const { return false; }
303 virtual bool isSpinButtonElement() const { return false; }
304 virtual bool isTextFormControl() const { return false; }
305 virtual bool isOptionalFormControl() const { return false; }
306 virtual bool isRequiredFormControl() const { return false; }
307 virtual bool isDefaultButtonForForm() const { return false; }
308 virtual bool willValidate() const { return false; }
309 virtual bool isValidFormControlElement() { return false; }
310 virtual bool hasUnacceptableValue() const { return false; }
311 virtual bool isInRange() const { return false; }
312 virtual bool isOutOfRange() const { return false; }
314 virtual bool formControlValueMatchesRenderer() const { return false; }
315 virtual void setFormControlValueMatchesRenderer(bool) { }
317 virtual const AtomicString& formControlName() const { return nullAtom; }
318 virtual const AtomicString& formControlType() const { return nullAtom; }
320 virtual bool shouldSaveAndRestoreFormControlState() const { return true; }
321 virtual bool saveFormControlState(String&) const { return false; }
322 virtual void restoreFormControlState(const String&) { }
324 virtual void dispatchFormControlChangeEvent() { }
327 virtual bool childShouldCreateRenderer(Node*) const;
330 #if ENABLE(FULLSCREEN_API)
332 ALLOW_KEYBOARD_INPUT = 1
335 void webkitRequestFullScreen(unsigned short flags);
338 virtual bool isSpellCheckingEnabled() const;
341 Element(const QualifiedName& tagName, Document* document, ConstructionType type)
342 : ContainerNode(document, type)
347 virtual void insertedIntoDocument();
348 virtual void removedFromDocument();
349 virtual void insertedIntoTree(bool);
350 virtual void removedFromTree(bool);
351 virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
353 // The implementation of Element::attributeChanged() calls the following two functions.
354 // They are separated to allow a different flow of control in StyledElement::attributeChanged().
355 void recalcStyleIfNeededAfterAttributeChanged(Attribute*);
356 void updateAfterAttributeChanged(Attribute*);
359 void scrollByUnits(int units, ScrollGranularity);
361 virtual void setPrefix(const AtomicString&, ExceptionCode&);
362 virtual NodeType nodeType() const;
363 virtual bool childTypeAllowed(NodeType);
365 virtual PassRefPtr<Attribute> createAttribute(const QualifiedName&, const AtomicString& value);
368 virtual void formatForDebugger(char* buffer, unsigned length) const;
371 bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
373 void createAttributeMap() const;
375 virtual void updateStyleAttribute() const { }
378 virtual void updateAnimatedSVGAttribute(const QualifiedName&) const { }
381 void cancelFocusAppearanceUpdate();
383 virtual const AtomicString& virtualPrefix() const { return prefix(); }
384 virtual const AtomicString& virtualLocalName() const { return localName(); }
385 virtual const AtomicString& virtualNamespaceURI() const { return namespaceURI(); }
386 virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) { return computedStyle(pseudoElementSpecifier); }
388 // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
390 virtual PassRefPtr<Node> cloneNode(bool deep);
391 virtual PassRefPtr<Element> cloneElementWithoutAttributesAndChildren() const;
393 QualifiedName m_tagName;
394 virtual NodeRareData* createRareData();
396 ElementRareData* rareData() const;
397 ElementRareData* ensureRareData();
399 SpellcheckAttributeState spellcheckAttributeState() const;
400 void removeShadowRoot();
403 mutable RefPtr<NamedNodeMap> m_attributeMap;
406 inline Element* toElement(Node* node)
408 ASSERT(!node || node->isElementNode());
409 return static_cast<Element*>(node);
412 inline const Element* toElement(const Node* node)
414 ASSERT(!node || node->isElementNode());
415 return static_cast<const Element*>(node);
418 // This will catch anyone doing an unnecessary cast.
419 void toElement(const Element*);
421 inline bool Node::hasTagName(const QualifiedName& name) const
423 return isElementNode() && toElement(this)->hasTagName(name);
426 inline bool Node::hasAttributes() const
428 return isElementNode() && toElement(this)->hasAttributes();
431 inline NamedNodeMap* Node::attributes() const
433 return isElementNode() ? toElement(this)->attributes() : 0;
436 inline Element* Node::parentElement() const
438 ContainerNode* parent = parentNode();
439 return parent && parent->isElementNode() ? toElement(parent) : 0;
442 inline NamedNodeMap* Element::attributes(bool readonly) const
444 if (!isStyleAttributeValid())
445 updateStyleAttribute();
448 if (!areSVGAttributesValid())
449 updateAnimatedSVGAttribute(anyQName());
452 if (!readonly && !m_attributeMap)
453 createAttributeMap();
454 return m_attributeMap.get();
457 inline void Element::updateId(const AtomicString& oldId, const AtomicString& newId)
465 Document* doc = document();
466 if (!oldId.isEmpty())
467 doc->removeElementById(oldId, this);
468 if (!newId.isEmpty())
469 doc->addElementById(newId, this);
472 inline bool Element::fastHasAttribute(const QualifiedName& name) const
474 return m_attributeMap && m_attributeMap->getAttributeItem(name);
477 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
479 if (m_attributeMap) {
480 if (Attribute* attribute = m_attributeMap->getAttributeItem(name))
481 return attribute->value();
486 inline const AtomicString& Element::idForStyleResolution() const
489 return m_attributeMap->idForStyleResolution();
492 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
494 // FIXME: This check is probably not correct for the case where the document has an id attribute
495 // with a non-null namespace, because it will return false, a false negative, if the prefixes
496 // don't match but the local name and namespace both do. However, since this has been like this
497 // for a while and the code paths may be hot, we'll have to measure performance if we fix it.
498 return attributeName == document()->idAttributeName();
501 inline const AtomicString& Element::getIdAttribute() const
503 return fastGetAttribute(document()->idAttributeName());
506 inline void Element::setIdAttribute(const AtomicString& value)
508 setAttribute(document()->idAttributeName(), value);
511 inline AtomicString Element::shadowPseudoId() const
513 return AtomicString();