OSDN Git Service

Merge WebKit at r80534: Intial 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, 2011 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     // Use Document::registerForPrivateBrowsingStateChangedCallbacks() to subscribe to this.
278     virtual void privateBrowsingStateDidChange() { }
279
280     bool isFinishedParsingChildren() const { return isParsingChildrenFinished(); }
281     virtual void finishParsingChildren();
282     virtual void beginParsingChildren();
283
284     // ElementTraversal API
285     Element* firstElementChild() const;
286     Element* lastElementChild() const;
287     Element* previousElementSibling() const;
288     Element* nextElementSibling() const;
289     unsigned childElementCount() const;
290
291     bool webkitMatchesSelector(const String& selectors, ExceptionCode&);
292
293     DOMTokenList* classList();
294     DOMTokenList* optionalClassList() const;
295
296     DOMStringMap* dataset();
297     DOMStringMap* optionalDataset() const;
298
299 #if ENABLE(MATHML)
300     virtual bool isMathMLElement() const { return false; }
301 #else
302     static bool isMathMLElement() { return false; }
303 #endif
304
305     virtual bool isFormControlElement() const { return false; }
306     virtual bool isEnabledFormControl() const { return true; }
307     virtual bool isReadOnlyFormControl() const { return false; }
308     virtual bool isSpinButtonElement() const { return false; }
309     virtual bool isTextFormControl() const { return false; }
310     virtual bool isOptionalFormControl() const { return false; }
311     virtual bool isRequiredFormControl() const { return false; }
312     virtual bool isDefaultButtonForForm() const { return false; }
313     virtual bool willValidate() const { return false; }
314     virtual bool isValidFormControlElement() { return false; }
315     virtual bool hasUnacceptableValue() const { return false; }
316     virtual bool isInRange() const { return false; }
317     virtual bool isOutOfRange() const { return false; }
318
319     virtual bool formControlValueMatchesRenderer() const { return false; }
320     virtual void setFormControlValueMatchesRenderer(bool) { }
321
322     virtual const AtomicString& formControlName() const { return nullAtom; }
323     virtual const AtomicString& formControlType() const { return nullAtom; }
324
325     virtual bool shouldSaveAndRestoreFormControlState() const { return true; }
326     virtual bool saveFormControlState(String&) const { return false; }
327     virtual void restoreFormControlState(const String&) { }
328
329     virtual bool wasChangedSinceLastFormControlChangeEvent() const;
330     virtual void setChangedSinceLastFormControlChangeEvent(bool);
331     virtual void dispatchFormControlChangeEvent() { }
332
333 #if ENABLE(SVG)
334     virtual bool childShouldCreateRenderer(Node*) const; 
335 #endif
336     
337 #if ENABLE(FULLSCREEN_API)
338     enum {
339         ALLOW_KEYBOARD_INPUT = 1
340     };
341     
342     void webkitRequestFullScreen(unsigned short flags);
343 #endif
344
345     virtual bool isSpellCheckingEnabled() const;
346
347 protected:
348     Element(const QualifiedName& tagName, Document* document, ConstructionType type)
349         : ContainerNode(document, type)
350         , m_tagName(tagName)
351     {
352     }
353
354     virtual void insertedIntoDocument();
355     virtual void removedFromDocument();
356     virtual void insertedIntoTree(bool);
357     virtual void removedFromTree(bool);
358     virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
359
360     // The implementation of Element::attributeChanged() calls the following two functions.
361     // They are separated to allow a different flow of control in StyledElement::attributeChanged().
362     void recalcStyleIfNeededAfterAttributeChanged(Attribute*);
363     void updateAfterAttributeChanged(Attribute*);
364
365 private:
366     void scrollByUnits(int units, ScrollGranularity);
367
368     virtual void setPrefix(const AtomicString&, ExceptionCode&);
369     virtual NodeType nodeType() const;
370     virtual bool childTypeAllowed(NodeType);
371
372     virtual PassRefPtr<Attribute> createAttribute(const QualifiedName&, const AtomicString& value);
373     
374 #ifndef NDEBUG
375     virtual void formatForDebugger(char* buffer, unsigned length) const;
376 #endif
377
378     bool pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle);
379
380     void createAttributeMap() const;
381
382     virtual void updateStyleAttribute() const { }
383
384 #if ENABLE(SVG)
385     virtual void updateAnimatedSVGAttribute(const QualifiedName&) const { }
386 #endif
387
388     void cancelFocusAppearanceUpdate();
389
390     virtual const AtomicString& virtualPrefix() const { return prefix(); }
391     virtual const AtomicString& virtualLocalName() const { return localName(); }
392     virtual const AtomicString& virtualNamespaceURI() const { return namespaceURI(); }
393     virtual RenderStyle* virtualComputedStyle(PseudoId pseudoElementSpecifier = NOPSEUDO) { return computedStyle(pseudoElementSpecifier); }
394     
395     // cloneNode is private so that non-virtual cloneElementWithChildren and cloneElementWithoutChildren
396     // are used instead.
397     virtual PassRefPtr<Node> cloneNode(bool deep);
398     virtual PassRefPtr<Element> cloneElementWithoutAttributesAndChildren() const;
399
400     QualifiedName m_tagName;
401     virtual NodeRareData* createRareData();
402
403     ElementRareData* rareData() const;
404     ElementRareData* ensureRareData();
405
406     SpellcheckAttributeState spellcheckAttributeState() const;
407     void removeShadowRoot();
408
409 private:
410     mutable RefPtr<NamedNodeMap> m_attributeMap;
411 };
412     
413 inline Element* toElement(Node* node)
414 {
415     ASSERT(!node || node->isElementNode());
416     return static_cast<Element*>(node);
417 }
418
419 inline const Element* toElement(const Node* node)
420 {
421     ASSERT(!node || node->isElementNode());
422     return static_cast<const Element*>(node);
423 }
424
425 // This will catch anyone doing an unnecessary cast.
426 void toElement(const Element*);
427
428 inline bool Node::hasTagName(const QualifiedName& name) const
429 {
430     return isElementNode() && toElement(this)->hasTagName(name);
431 }
432     
433 inline bool Node::hasLocalName(const AtomicString& name) const
434 {
435     return isElementNode() && toElement(this)->hasLocalName(name);
436 }
437
438 inline bool Node::hasAttributes() const
439 {
440     return isElementNode() && toElement(this)->hasAttributes();
441 }
442
443 inline NamedNodeMap* Node::attributes() const
444 {
445     return isElementNode() ? toElement(this)->attributes() : 0;
446 }
447
448 inline Element* Node::parentElement() const
449 {
450     ContainerNode* parent = parentNode();
451     return parent && parent->isElementNode() ? toElement(parent) : 0;
452 }
453
454 inline NamedNodeMap* Element::attributes(bool readonly) const
455 {
456     if (!isStyleAttributeValid())
457         updateStyleAttribute();
458
459 #if ENABLE(SVG)
460     if (!areSVGAttributesValid())
461         updateAnimatedSVGAttribute(anyQName());
462 #endif
463
464     if (!readonly && !m_attributeMap)
465         createAttributeMap();
466     return m_attributeMap.get();
467 }
468
469 inline void Element::updateId(const AtomicString& oldId, const AtomicString& newId)
470 {
471     if (!inDocument())
472         return;
473
474     if (oldId == newId)
475         return;
476
477     Document* doc = document();
478     if (!oldId.isEmpty())
479         doc->removeElementById(oldId, this);
480     if (!newId.isEmpty())
481         doc->addElementById(newId, this);
482 }
483
484 inline bool Element::fastHasAttribute(const QualifiedName& name) const
485 {
486     return m_attributeMap && m_attributeMap->getAttributeItem(name);
487 }
488
489 inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
490 {
491     if (m_attributeMap) {
492         if (Attribute* attribute = m_attributeMap->getAttributeItem(name))
493             return attribute->value();
494     }
495     return nullAtom;
496 }
497
498 inline const AtomicString& Element::idForStyleResolution() const
499 {
500     ASSERT(hasID());
501     return m_attributeMap->idForStyleResolution();
502 }
503
504 inline bool Element::isIdAttributeName(const QualifiedName& attributeName) const
505 {
506     // FIXME: This check is probably not correct for the case where the document has an id attribute
507     // with a non-null namespace, because it will return false, a false negative, if the prefixes
508     // don't match but the local name and namespace both do. However, since this has been like this
509     // for a while and the code paths may be hot, we'll have to measure performance if we fix it.
510     return attributeName == document()->idAttributeName();
511 }
512
513 inline const AtomicString& Element::getIdAttribute() const
514 {
515     return fastGetAttribute(document()->idAttributeName());
516 }
517
518 inline void Element::setIdAttribute(const AtomicString& value)
519 {
520     setAttribute(document()->idAttributeName(), value);
521 }
522
523 inline const AtomicString& Element::shadowPseudoId() const
524 {
525     return nullAtom;
526 }
527     
528 inline Element* firstElementChild(const ContainerNode* container)
529 {
530     ASSERT_ARG(container, container);
531     Node* child = container->firstChild();
532     while (child && !child->isElementNode())
533         child = child->nextSibling();
534     return static_cast<Element*>(child);
535 }
536
537 } // namespace
538
539 #endif