2 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef HTMLElementStack_h
27 #define HTMLElementStack_h
29 #include <wtf/Forward.h>
30 #include <wtf/Noncopyable.h>
31 #include <wtf/OwnPtr.h>
32 #include <wtf/PassOwnPtr.h>
33 #include <wtf/RefPtr.h>
40 // NOTE: The HTML5 spec uses a backwards (grows downward) stack. We're using
41 // more standard (grows upwards) stack terminology here.
42 class HTMLElementStack : public Noncopyable {
47 class ElementRecord : public Noncopyable {
49 ~ElementRecord(); // Public for ~PassOwnPtr()
51 Element* element() const { return m_element.get(); }
52 void replaceElement(PassRefPtr<Element>);
54 bool isAbove(ElementRecord*) const;
56 ElementRecord* next() const { return m_next.get(); }
59 friend class HTMLElementStack;
61 ElementRecord(PassRefPtr<Element>, PassOwnPtr<ElementRecord>);
63 PassOwnPtr<ElementRecord> releaseNext() { return m_next.release(); }
64 void setNext(PassOwnPtr<ElementRecord> next) { m_next = next; }
66 RefPtr<Element> m_element;
67 OwnPtr<ElementRecord> m_next;
70 // Inlining this function is a (small) performance win on the parsing
74 ASSERT(m_top->element());
75 return m_top->element();
78 Element* oneBelowTop() const;
79 ElementRecord* topRecord() const;
80 Element* bottom() const;
81 ElementRecord* find(Element*) const;
82 ElementRecord* topmost(const AtomicString& tagName) const;
84 void insertAbove(PassRefPtr<Element>, ElementRecord*);
86 void push(PassRefPtr<Element>);
87 void pushHTMLHtmlElement(PassRefPtr<Element>);
88 void pushHTMLHeadElement(PassRefPtr<Element>);
89 void pushHTMLBodyElement(PassRefPtr<Element>);
92 void popUntil(const AtomicString& tagName);
93 void popUntil(Element*);
94 void popUntilPopped(const AtomicString& tagName);
95 void popUntilPopped(Element*);
96 void popUntilNumberedHeaderElementPopped();
97 void popUntilTableScopeMarker(); // "clear the stack back to a table context" in the spec.
98 void popUntilTableBodyScopeMarker(); // "clear the stack back to a table body context" in the spec.
99 void popUntilTableRowScopeMarker(); // "clear the stack back to a table row context" in the spec.
100 void popUntilForeignContentScopeMarker();
101 void popHTMLHeadElement();
102 void popHTMLBodyElement();
105 void remove(Element*);
106 void removeHTMLHeadElement(Element*);
108 bool contains(Element*) const;
109 bool contains(const AtomicString& tagName) const;
111 bool inScope(Element*) const;
112 bool inScope(const AtomicString& tagName) const;
113 bool inScope(const QualifiedName&) const;
114 bool inListItemScope(const AtomicString& tagName) const;
115 bool inListItemScope(const QualifiedName&) const;
116 bool inTableScope(const AtomicString& tagName) const;
117 bool inTableScope(const QualifiedName&) const;
118 bool inButtonScope(const AtomicString& tagName) const;
119 bool inButtonScope(const QualifiedName&) const;
121 bool hasOnlyHTMLElementsInScope() const;
122 bool hasNumberedHeaderElementInScope() const;
124 bool hasOnlyOneElement() const;
125 bool secondElementIsHTMLBodyElement() const;
127 Element* htmlElement() const;
128 Element* headElement() const;
129 Element* bodyElement() const;
136 void pushCommon(PassRefPtr<Element>);
138 void removeNonTopCommon(Element*);
140 OwnPtr<ElementRecord> m_top;
142 // We remember <html>, <head> and <body> as they are pushed. Their
143 // ElementRecords keep them alive. <html> is never popped.
144 // FIXME: We don't currently require type-specific information about
145 // these elements so we haven't yet bothered to plumb the types all the
146 // way down through createElement, etc.
147 Element* m_htmlElement;
148 Element* m_headElement;
149 Element* m_bodyElement;
152 } // namespace WebCore
154 #endif // HTMLElementStack_h