OSDN Git Service

Merge WebKit at r78450: Initial merge by git.
[android-x86/external-webkit.git] / Source / WebCore / 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 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 "Document.h"
29 #include "FragmentScriptingPermission.h"
30 #include "NamedNodeMap.h"
31 #include "ScrollTypes.h"
32
33 namespace WebCore {
34
35 class Attribute;
36 class ClientRect;
37 class ClientRectList;
38 class DOMStringMap;
39 class DOMTokenList;
40 class ElementRareData;
41 class IntSize;
42
43 enum SpellcheckAttributeState {
44     SpellcheckAttributeTrue,
45     SpellcheckAttributeFalse,
46     SpellcheckAttributeDefault
47 };
48
49 class Element : public ContainerNode {
50 public:
51     static PassRefPtr<Element> create(const QualifiedName&, Document*);
52     virtual ~Element();
53
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(formchange);
67     DEFINE_ATTRIBUTE_EVENT_LISTENER(forminput);
68     DEFINE_ATTRIBUTE_EVENT_LISTENER(input);
69     DEFINE_ATTRIBUTE_EVENT_LISTENER(invalid);
70     DEFINE_ATTRIBUTE_EVENT_LISTENER(keydown);
71     DEFINE_ATTRIBUTE_EVENT_LISTENER(keypress);
72     DEFINE_ATTRIBUTE_EVENT_LISTENER(keyup);
73     DEFINE_ATTRIBUTE_EVENT_LISTENER(mousedown);
74     DEFINE_ATTRIBUTE_EVENT_LISTENER(mousemove);
75     DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseout);
76     DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseover);
77     DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseup);
78     DEFINE_ATTRIBUTE_EVENT_LISTENER(mousewheel);
79     DEFINE_ATTRIBUTE_EVENT_LISTENER(scroll);
80     DEFINE_ATTRIBUTE_EVENT_LISTENER(select);
81     DEFINE_ATTRIBUTE_EVENT_LISTENER(submit);
82
83     // These four attribute event handler attributes are overridden by HTMLBodyElement
84     // and HTMLFrameSetElement to forward to the DOMWindow.
85     DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(blur);
86     DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(error);
87     DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(focus);
88     DECLARE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(load);
89
90     // WebKit extensions
91     DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecut);
92     DEFINE_ATTRIBUTE_EVENT_LISTENER(cut);
93     DEFINE_ATTRIBUTE_EVENT_LISTENER(beforecopy);
94     DEFINE_ATTRIBUTE_EVENT_LISTENER(copy);
95     DEFINE_ATTRIBUTE_EVENT_LISTENER(beforepaste);
96     DEFINE_ATTRIBUTE_EVENT_LISTENER(paste);
97     DEFINE_ATTRIBUTE_EVENT_LISTENER(reset);
98     DEFINE_ATTRIBUTE_EVENT_LISTENER(search);
99     DEFINE_ATTRIBUTE_EVENT_LISTENER(selectstart);
100 #if ENABLE(TOUCH_EVENTS)
101     DEFINE_ATTRIBUTE_EVENT_LISTENER(touchstart);
102     DEFINE_ATTRIBUTE_EVENT_LISTENER(touchmove);
103     DEFINE_ATTRIBUTE_EVENT_LISTENER(touchend);
104     DEFINE_ATTRIBUTE_EVENT_LISTENER(touchcancel);
105 #endif
106 #if ENABLE(FULLSCREEN_API)
107     DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitfullscreenchange);
108 #endif
109
110     virtual PassRefPtr<DocumentFragment> deprecatedCreateContextualFragment(const String&, FragmentScriptingPermission = FragmentScriptingAllowed);
111
112     bool hasAttribute(const QualifiedName&) const;
113     const AtomicString& getAttribute(const QualifiedName&) const;
114     void setAttribute(const QualifiedName&, const AtomicString& value, ExceptionCode&);
115     void removeAttribute(const QualifiedName&, ExceptionCode&);
116
117     // Typed getters and setters for language bindings.
118     int getIntegralAttribute(const QualifiedName& attributeName) const;
119     void setIntegralAttribute(const QualifiedName& attributeName, int value);
120     unsigned getUnsignedIntegralAttribute(const QualifiedName& attributeName) const;
121     void setUnsignedIntegralAttribute(const QualifiedName& attributeName, unsigned value);
122
123     // Call this to get the value of an attribute that is known not to be the style
124     // attribute or one of the SVG animatable attributes.
125     bool fastHasAttribute(const QualifiedName&) const;
126     const AtomicString& fastGetAttribute(const QualifiedName&) const;
127
128     bool hasAttributes() const;
129
130     bool hasAttribute(const String& name) const;
131     bool hasAttributeNS(const String& namespaceURI, const String& localName) const;
132
133     const AtomicString& getAttribute(const String& name) const;
134     const AtomicString& getAttributeNS(const String& namespaceURI, const String& localName) const;
135
136     void setAttribute(const AtomicString& name, const AtomicString& value, ExceptionCode&);
137     void setAttributeNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, const AtomicString& value, ExceptionCode&, FragmentScriptingPermission = FragmentScriptingAllowed);
138
139     bool isIdAttributeName(const QualifiedName&) const;
140     const AtomicString& getIdAttribute() const;
141     void setIdAttribute(const AtomicString&);
142
143     // Call this to get the value of the id attribute for style resolution purposes.
144     // The value will already be lowercased if the document is in compatibility mode,
145     // so this function is not suitable for non-style uses.
146     const AtomicString& idForStyleResolution() const;
147
148     void scrollIntoView(bool alignToTop = true);
149     void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
150
151     void scrollByLines(int lines);
152     void scrollByPages(int pages);
153
154     int offsetLeft();
155     int offsetTop();
156     int offsetWidth();
157     int offsetHeight();
158     Element* offsetParent();
159     int clientLeft();
160     int clientTop();
161     int clientWidth();
162     int clientHeight();
163     virtual int scrollLeft() const;
164     virtual int scrollTop() const;
165     virtual void setScrollLeft(int);
166     virtual void setScrollTop(int);
167     virtual int scrollWidth() const;
168     virtual int scrollHeight() const;
169
170     IntRect boundsInWindowSpace() const;
171
172     PassRefPtr<ClientRectList> getClientRects() const;
173     PassRefPtr<ClientRect> getBoundingClientRect() const;
174     
175     // Returns the absolute bounding box translated into screen coordinates:
176     IntRect screenRect() const;
177
178     void removeAttribute(const String& name, ExceptionCode&);
179     void removeAttributeNS(const String& namespaceURI, const String& localName, ExceptionCode&);
180
181     PassRefPtr<Attr> getAttributeNode(const String& name);
182     PassRefPtr<Attr> getAttributeNodeNS(const String& namespaceURI, const String& localName);
183     PassRefPtr<Attr> setAttributeNode(Attr*, ExceptionCode&);
184     PassRefPtr<Attr> setAttributeNodeNS(Attr*, ExceptionCode&);
185     PassRefPtr<Attr> removeAttributeNode(Attr*, ExceptionCode&);
186     
187     virtual CSSStyleDeclaration* style();
188
189     const QualifiedName& tagQName() const { return m_tagName; }
190     String tagName() const { return nodeName(); }
191     bool hasTagName(const QualifiedName& tagName) const { return m_tagName.matches(tagName); }
192     
193     // A fast function for checking the local name against another atomic string.
194     bool hasLocalName(const AtomicString& other) const { return m_tagName.localName() == other; }
195     bool hasLocalName(const QualifiedName& other) const { return m_tagName.localName() == other.localName(); }
196
197     const AtomicString& localName() const { return m_tagName.localName(); }
198     const AtomicString& prefix() const { return m_tagName.prefix(); }
199     const AtomicString& namespaceURI() const { return m_tagName.namespaceURI(); }
200
201     virtual KURL baseURI() const;
202
203     virtual String nodeName() const;
204
205     PassRefPtr<Element> cloneElementWithChildren();
206     PassRefPtr<Element> cloneElementWithoutChildren();
207
208     void normalizeAttributes();
209     String nodeNamePreservingCase() const;
210
211     // convenience methods which ignore exceptions
212     void setAttribute(const QualifiedName&, const AtomicString& value);
213     void setBooleanAttribute(const QualifiedName& name, bool);
214     // Please don't use setCStringAttribute in performance-sensitive code;
215     // use a static AtomicString value instead to avoid the conversion overhead.
216     void setCStringAttribute(const QualifiedName&, const char* cStringValue);
217
218     NamedNodeMap* attributes(bool readonly = false) const;
219
220     // This method is called whenever an attribute is added, changed or removed.
221     virtual void attributeChanged(Attribute*, bool preserveDecls = false);
222
223     void setAttributeMap(PassRefPtr<NamedNodeMap>, FragmentScriptingPermission = FragmentScriptingAllowed);
224     NamedNodeMap* attributeMap() const { return m_attributeMap.get(); }
225
226     virtual void copyNonAttributeProperties(const Element* /*source*/) { }
227
228     virtual void attach();
229     virtual void detach();
230     virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
231     virtual void recalcStyle(StyleChange = NoChange);
232
233     Node* shadowRoot();
234     void setShadowRoot(PassRefPtr<Node>);
235     virtual const AtomicString& shadowPseudoId() const;
236
237     RenderStyle* computedStyle(PseudoId = NOPSEUDO);
238
239     AtomicString computeInheritedLanguage() const;
240
241     void dispatchAttrRemovalEvent(Attribute*);
242     void dispatchAttrAdditionEvent(Attribute*);
243
244     virtual void accessKeyAction(bool /*sendToAnyEvent*/) { }
245
246     virtual bool isURLAttribute(Attribute*) const;
247
248     KURL getURLAttribute(const QualifiedName&) const;
249     KURL getNonEmptyURLAttribute(const QualifiedName&) const;
250
251     virtual const QualifiedName& imageSourceAttributeName() const;
252     virtual String target() const { return String(); }
253
254     virtual void focus(bool restorePreviousSelection = true);
255     virtual void updateFocusAppearance(bool restorePreviousSelection);
256     void blur();
257
258     String innerText() const;
259     String outerText() const;
260  
261     virtual String title() const;
262
263     String openTagStartToString() const;
264
265     void updateId(const AtomicString& oldId, const AtomicString& newId);
266
267     IntSize minimumSizeForResizing() const;
268     void setMinimumSizeForResizing(const IntSize&);
269
270     // Use Document::registerForDocumentActivationCallbacks() to subscribe to these
271     virtual void documentWillBecomeInactive() { }
272     virtual void documentDidBecomeActive() { }
273
274     // Use Document::registerForMediaVolumeCallbacks() to subscribe to this
275     virtual void mediaVolumeDidChange() { }
276
277     bool isFinishedParsingChildren() const { return isParsingChildrenFinished(); }
278     virtual void finishParsingChildren();
279     virtual void beginParsingChildren();
280
281     // ElementTraversal API
282     Element* firstElementChild() const;
283     Element* lastElementChild() const;
284     Element* previousElementSibling() const;
285     Element* nextElementSibling() const;
286     unsigned childElementCount() const;
287
288     bool webkitMatchesSelector(const String& selectors, ExceptionCode&);
289
290     DOMTokenList* classList();
291     DOMTokenList* optionalClassList() const;
292
293     DOMStringMap* dataset();
294     DOMStringMap* optionalDataset() const;
295
296 #if ENABLE(MATHML)
297     virtual bool isMathMLElement() const { return false; }
298 #else
299     static bool isMathMLElement() { return false; }
300 #endif
301
302     virtual bool isFormControlElement() const { return false; }
303     virtual bool isEnabledFormControl() const { return true; }
304     virtual bool isReadOnlyFormControl() const { return false; }
305     virtual bool isSpinButtonElement() const { return false; }
306     virtual bool isTextFormControl() const { return false; }
307     virtual bool isOptionalFormControl() const { return false; }
308     virtual bool isRequiredFormControl() const { return false; }
309     virtual bool isDefaultButtonForForm() const { return false; }
310     virtual bool willValidate() const { return false; }
311     virtual bool isValidFormControlElement() { return false; }
312     virtual bool hasUnacceptableValue() const { return false; }
313     virtual bool isInRange() const { return false; }
314     virtual bool isOutOfRange() const { return false; }
315
316     virtual bool formControlValueMatchesRenderer() const { return false; }
317     virtual void setFormControlValueMatchesRenderer(bool) { }
318
319     virtual const AtomicString& formControlName() const { return nullAtom; }
320     virtual const AtomicString& formControlType() const { return nullAtom; }
321
322     virtual bool shouldSaveAndRestoreFormControlState() const { return true; }
323     virtual bool saveFormControlState(String&) const { return false; }
324     virtual void restoreFormControlState(const String&) { }
325
326     virtual void dispatchFormControlChangeEvent() { }
327
328 #if ENABLE(SVG)
329     virtual bool childShouldCreateRenderer(Node*) const; 
330 #endif
331     
332 #if ENABLE(FULLSCREEN_API)
333     enum {
334         ALLOW_KEYBOARD_INPUT = 1
335     };
336     
337     void webkitRequestFullScreen(unsigned short flags);
338 #endif
339
340     virtual bool isSpellCheckingEnabled() const;
341
342 protected:
343     Element(const QualifiedName& tagName, Document* document, ConstructionType type)
344         : ContainerNode(document, type)
345         , m_tagName(tagName)
346     {
347     }
348
349     virtual void insertedIntoDocument();
350     virtual void removedFromDocument();
351     virtual void insertedIntoTree(bool);
352     virtual void removedFromTree(bool);
353     virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
354
355     // The implementation of Element::attributeChanged() calls the following two functions.
356     // They are separated to allow a different flow of control in StyledElement::attributeChanged().
357     void recalcStyleIfNeededAfterAttributeChanged(Attribute*);
358     void updateAfterAttributeChanged(Attribute*);
359
360 private:
361     void scrollByUnits(int units, ScrollGranularity);
362
363     virtual void setPrefix(const AtomicString&, ExceptionCode&);
364     virtual NodeType nodeType() const;
365     virtual bool childTypeAllowed(NodeType);
366
367     virtual PassRefPtr<Attribute> createAttribute(const QualifiedName&, const AtomicString& value);
368     
369 #ifndef NDEBUG
370     virtual void formatForDebugger(char* buffer, unsigned length) const;
371 #endif
372
373     bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
374
375     void createAttributeMap() const;
376
377     virtual void updateStyleAttribute() const { }
378
379 #if ENABLE(SVG)
380     virtual void updateAnimatedSVGAttribute(const QualifiedName&) const { }
381 #endif
382
383     void cancelFocusAppearanceUpdate();
384
385     virtual const AtomicString& virtualPrefix() const { return prefix(); }
386     virtual const AtomicString& virtualLocalName() const { return localName(); }
387     virtual const AtomicString& virtualNamespaceURI() const { return namespaceURI(); }
388     virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) { return computedStyle(pseudoElementSpecifier); }
389     
390     // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
391     // are used instead.
392     virtual PassRefPtr<Node> cloneNode(bool deep);
393     virtual PassRefPtr<Element> cloneElementWithoutAttributesAndChildren() const;
394
395     QualifiedName m_tagName;
396     virtual NodeRareData* createRareData();
397
398     ElementRareData* rareData() const;
399     ElementRareData* ensureRareData();
400
401     SpellcheckAttributeState spellcheckAttributeState() const;
402     void removeShadowRoot();
403
404 private:
405     mutable RefPtr<NamedNodeMap> m_attributeMap;
406 };
407     
408 inline Element* toElement(Node* node)
409 {
410     ASSERT(!node || node->isElementNode());
411     return static_cast<Element*>(node);
412 }
413
414 inline const Element* toElement(const Node* node)
415 {
416     ASSERT(!node || node->isElementNode());
417     return static_cast<const Element*>(node);
418 }
419
420 // This will catch anyone doing an unnecessary cast.
421 void toElement(const Element*);
422
423 inline bool Node::hasTagName(const QualifiedName& name) const
424 {
425     return isElementNode() && toElement(this)->hasTagName(name);
426 }
427
428 inline bool Node::hasAttributes() const
429 {
430     return isElementNode() && toElement(this)->hasAttributes();
431 }
432
433 inline NamedNodeMap* Node::attributes() const
434 {
435     return isElementNode() ? toElement(this)->attributes() : 0;
436 }
437
438 inline Element* Node::parentElement() const
439 {
440     ContainerNode* parent = parentNode();
441     return parent && parent->isElementNode() ? toElement(parent) : 0;
442 }
443
444 inline NamedNodeMap* Element::attributes(bool readonly) const
445 {
446     if (!isStyleAttributeValid())
447         updateStyleAttribute();
448
449 #if ENABLE(SVG)
450     if (!areSVGAttributesValid())
451         updateAnimatedSVGAttribute(anyQName());
452 #endif
453
454     if (!readonly && !m_attributeMap)
455         createAttributeMap();
456     return m_attributeMap.get();
457 }
458
459 inline void Element::updateId(const AtomicString& oldId, const AtomicString& newId)
460 {
461     if (!inDocument())
462         return;
463
464     if (oldId == newId)
465         return;
466
467     Document* doc = document();
468     if (!oldId.isEmpty())
469         doc->removeElementById(oldId, this);
470     if (!newId.isEmpty())
471         doc->addElementById(newId, this);
472 }
473
474 inline bool Element::fastHasAttribute(const QualifiedName& name) const
475 {
476     return m_attributeMap && m_attributeMap->getAttributeItem(name);
477 }
478
479 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
480 {
481     if (m_attributeMap) {
482         if (Attribute* attribute = m_attributeMap->getAttributeItem(name))
483             return attribute->value();
484     }
485     return nullAtom;
486 }
487
488 inline const AtomicString& Element::idForStyleResolution() const
489 {
490     ASSERT(hasID());
491     return m_attributeMap->idForStyleResolution();
492 }
493
494 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
495 {
496     // FIXME: This check is probably not correct for the case where the document has an id attribute
497     // with a non-null namespace, because it will return false, a false negative, if the prefixes
498     // don't match but the local name and namespace both do. However, since this has been like this
499     // for a while and the code paths may be hot, we'll have to measure performance if we fix it.
500     return attributeName == document()->idAttributeName();
501 }
502
503 inline const AtomicString& Element::getIdAttribute() const
504 {
505     return fastGetAttribute(document()->idAttributeName());
506 }
507
508 inline void Element::setIdAttribute(const AtomicString& value)
509 {
510     setAttribute(document()->idAttributeName(), value);
511 }
512
513 inline const AtomicString& Element::shadowPseudoId() const
514 {
515     return nullAtom;
516 }
517     
518 inline Element* firstElementChild(const ContainerNode* container)
519 {
520     ASSERT_ARG(container, container);
521     Node* child = container->firstChild();
522     while (child && !child->isElementNode())
523         child = child->nextSibling();
524     return static_cast<Element*>(child);
525 }
526
527 } // namespace
528
529 #endif