OSDN Git Service

am 68b5dbab: (-s ours) am 7d1dae3f: am 58348114: Merge "DO NOT MERGE cursor ring...
[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(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);
80
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);
87
88     // WebKit extensions
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);
103 #endif
104 #if ENABLE(FULLSCREEN_API)
105     DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitfullscreenchange);
106 #endif
107
108     virtual PassRefPtr<DocumentFragment> deprecatedCreateContextualFragment(const String&, FragmentScriptingPermission = FragmentScriptingAllowed);
109
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&);
114
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);
120
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;
125
126     bool hasAttributes() const;
127
128     bool hasAttribute(const String& name) const;
129     bool hasAttributeNS(const String& namespaceURI, const String& localName) const;
130
131     const AtomicString& getAttribute(const String& name) const;
132     const AtomicString& getAttributeNS(const String& namespaceURI, const String& localName) const;
133
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);
136
137     bool isIdAttributeName(const QualifiedName&) const;
138     const AtomicString& getIdAttribute() const;
139     void setIdAttribute(const AtomicString&);
140
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;
145
146     void scrollIntoView(bool alignToTop = true);
147     void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
148
149     void scrollByLines(int lines);
150     void scrollByPages(int pages);
151
152     int offsetLeft();
153     int offsetTop();
154     int offsetWidth();
155     int offsetHeight();
156     Element* offsetParent();
157     int clientLeft();
158     int clientTop();
159     int clientWidth();
160     int clientHeight();
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;
167
168     IntRect boundsInWindowSpace() const;
169
170     PassRefPtr<ClientRectList> getClientRects() const;
171     PassRefPtr<ClientRect> getBoundingClientRect() const;
172     
173     // Returns the absolute bounding box translated into screen coordinates:
174     IntRect screenRect() const;
175
176     void removeAttribute(const String& name, ExceptionCode&);
177     void removeAttributeNS(const String& namespaceURI, const String& localName, ExceptionCode&);
178
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&);
184     
185     virtual CSSStyleDeclaration* style();
186
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); }
190     
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(); }
194
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(); }
198
199     virtual KURL baseURI() const;
200
201     virtual String nodeName() const;
202
203     PassRefPtr<Element> cloneElementWithChildren();
204     PassRefPtr<Element> cloneElementWithoutChildren();
205
206     void normalizeAttributes();
207     String nodeNamePreservingCase() const;
208
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);
215
216     NamedNodeMap* attributes(bool readonly = false) const;
217
218     // This method is called whenever an attribute is added, changed or removed.
219     virtual void attributeChanged(Attribute*, bool preserveDecls = false);
220
221     void setAttributeMap(PassRefPtr<NamedNodeMap>, FragmentScriptingPermission = FragmentScriptingAllowed);
222     NamedNodeMap* attributeMap() const { return m_attributeMap.get(); }
223
224     virtual void copyNonAttributeProperties(const Element* /*source*/) { }
225
226     virtual void attach();
227     virtual void detach();
228     virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
229     virtual void recalcStyle(StyleChange = NoChange);
230
231     Node* shadowRoot();
232     void setShadowRoot(PassRefPtr<Node>);
233
234     RenderStyle* computedStyle(PseudoId = NOPSEUDO);
235
236     AtomicString computeInheritedLanguage() const;
237
238     void dispatchAttrRemovalEvent(Attribute*);
239     void dispatchAttrAdditionEvent(Attribute*);
240
241     virtual void accessKeyAction(bool /*sendToAnyEvent*/) { }
242
243     virtual bool isURLAttribute(Attribute*) const;
244
245     KURL getURLAttribute(const QualifiedName&) const;
246     KURL getNonEmptyURLAttribute(const QualifiedName&) const;
247
248     virtual const QualifiedName& imageSourceAttributeName() const;
249     virtual String target() const { return String(); }
250
251     virtual void focus(bool restorePreviousSelection = true);
252     virtual void updateFocusAppearance(bool restorePreviousSelection);
253     void blur();
254
255     String innerText() const;
256     String outerText() const;
257  
258     virtual String title() const;
259
260     String openTagStartToString() const;
261
262     void updateId(const AtomicString& oldId, const AtomicString& newId);
263
264     IntSize minimumSizeForResizing() const;
265     void setMinimumSizeForResizing(const IntSize&);
266
267     // Use Document::registerForDocumentActivationCallbacks() to subscribe to these
268     virtual void documentWillBecomeInactive() { }
269     virtual void documentDidBecomeActive() { }
270
271     // Use Document::registerForMediaVolumeCallbacks() to subscribe to this
272     virtual void mediaVolumeDidChange() { }
273
274     bool isFinishedParsingChildren() const { return isParsingChildrenFinished(); }
275     virtual void finishParsingChildren();
276     virtual void beginParsingChildren() { clearIsParsingChildrenFinished(); }
277
278     // ElementTraversal API
279     Element* firstElementChild() const;
280     Element* lastElementChild() const;
281     Element* previousElementSibling() const;
282     Element* nextElementSibling() const;
283     unsigned childElementCount() const;
284
285     bool webkitMatchesSelector(const String& selectors, ExceptionCode&);
286
287     DOMTokenList* classList();
288     DOMTokenList* optionalClassList() const;
289
290     DOMStringMap* dataset();
291     DOMStringMap* optionalDataset() const;
292
293 #if ENABLE(MATHML)
294     virtual bool isMathMLElement() const { return false; }
295 #else
296     static bool isMathMLElement() { return false; }
297 #endif
298
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; }
312
313     virtual bool formControlValueMatchesRenderer() const { return false; }
314     virtual void setFormControlValueMatchesRenderer(bool) { }
315
316     virtual const AtomicString& formControlName() const { return nullAtom; }
317     virtual const AtomicString& formControlType() const { return nullAtom; }
318
319     virtual bool shouldSaveAndRestoreFormControlState() const { return true; }
320     virtual bool saveFormControlState(String&) const { return false; }
321     virtual void restoreFormControlState(const String&) { }
322
323     virtual void dispatchFormControlChangeEvent() { }
324
325 #if ENABLE(SVG)
326     virtual bool childShouldCreateRenderer(Node*) const; 
327 #endif
328     
329 #if ENABLE(FULLSCREEN_API)
330     enum {
331         ALLOW_KEYBOARD_INPUT = 1
332     };
333     
334     void webkitRequestFullScreen(unsigned short flags);
335 #endif
336
337     virtual bool isSpellCheckingEnabled() const;
338
339 protected:
340     Element(const QualifiedName& tagName, Document* document, ConstructionType type)
341         : ContainerNode(document, type)
342         , m_tagName(tagName)
343     {
344     }
345
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);
351
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*);
356
357 private:
358     void scrollByUnits(int units, ScrollGranularity);
359
360     virtual void setPrefix(const AtomicString&, ExceptionCode&);
361     virtual NodeType nodeType() const;
362     virtual bool childTypeAllowed(NodeType);
363
364     virtual PassRefPtr<Attribute> createAttribute(const QualifiedName&, const AtomicString& value);
365     
366 #ifndef NDEBUG
367     virtual void formatForDebugger(char* buffer, unsigned length) const;
368 #endif
369
370     bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
371
372     void createAttributeMap() const;
373
374     virtual void updateStyleAttribute() const { }
375
376 #if ENABLE(SVG)
377     virtual void updateAnimatedSVGAttribute(const QualifiedName&) const { }
378 #endif
379
380     void cancelFocusAppearanceUpdate();
381
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); }
386     
387     // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
388     // are used instead.
389     virtual PassRefPtr<Node> cloneNode(bool deep);
390     virtual PassRefPtr<Element> cloneElementWithoutAttributesAndChildren() const;
391
392     QualifiedName m_tagName;
393     virtual NodeRareData* createRareData();
394
395     ElementRareData* rareData() const;
396     ElementRareData* ensureRareData();
397
398     SpellcheckAttributeState spellcheckAttributeState() const;
399     void removeShadowRoot();
400
401 private:
402     mutable RefPtr<NamedNodeMap> m_attributeMap;
403 };
404     
405 inline Element* toElement(Node* node)
406 {
407     ASSERT(!node || node->isElementNode());
408     return static_cast<Element*>(node);
409 }
410
411 inline const Element* toElement(const Node* node)
412 {
413     ASSERT(!node || node->isElementNode());
414     return static_cast<const Element*>(node);
415 }
416
417 // This will catch anyone doing an unnecessary cast.
418 void toElement(const Element*);
419
420 inline bool Node::hasTagName(const QualifiedName& name) const
421 {
422     return isElementNode() && toElement(this)->hasTagName(name);
423 }
424
425 inline bool Node::hasAttributes() const
426 {
427     return isElementNode() && toElement(this)->hasAttributes();
428 }
429
430 inline NamedNodeMap* Node::attributes() const
431 {
432     return isElementNode() ? toElement(this)->attributes() : 0;
433 }
434
435 inline Element* Node::parentElement() const
436 {
437     ContainerNode* parent = parentNode();
438     return parent && parent->isElementNode() ? toElement(parent) : 0;
439 }
440
441 inline NamedNodeMap* Element::attributes(bool readonly) const
442 {
443     if (!isStyleAttributeValid())
444         updateStyleAttribute();
445
446 #if ENABLE(SVG)
447     if (!areSVGAttributesValid())
448         updateAnimatedSVGAttribute(anyQName());
449 #endif
450
451     if (!readonly && !m_attributeMap)
452         createAttributeMap();
453     return m_attributeMap.get();
454 }
455
456 inline void Element::updateId(const AtomicString& oldId, const AtomicString& newId)
457 {
458     if (!inDocument())
459         return;
460
461     if (oldId == newId)
462         return;
463
464     Document* doc = document();
465     if (!oldId.isEmpty())
466         doc->removeElementById(oldId, this);
467     if (!newId.isEmpty())
468         doc->addElementById(newId, this);
469 }
470
471 inline bool Element::fastHasAttribute(const QualifiedName& name) const
472 {
473     return m_attributeMap && m_attributeMap->getAttributeItem(name);
474 }
475
476 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
477 {
478     if (m_attributeMap) {
479         if (Attribute* attribute = m_attributeMap->getAttributeItem(name))
480             return attribute->value();
481     }
482     return nullAtom;
483 }
484
485 inline const AtomicString& Element::idForStyleResolution() const
486 {
487     ASSERT(hasID());
488     return m_attributeMap->idForStyleResolution();
489 }
490
491 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
492 {
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();
498 }
499
500 inline const AtomicString& Element::getIdAttribute() const
501 {
502     return fastGetAttribute(document()->idAttributeName());
503 }
504
505 inline void Element::setIdAttribute(const AtomicString& value)
506 {
507     setAttribute(document()->idAttributeName(), value);
508 }
509
510 } // namespace
511
512 #endif