2 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
3 * Copyright (C) 2011 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "core/html/parser/HTMLElementStack.h"
30 #include "core/HTMLNames.h"
31 #include "core/MathMLNames.h"
32 #include "core/SVGNames.h"
33 #include "core/dom/Element.h"
34 #include "core/html/HTMLElement.h"
38 using namespace HTMLNames
;
43 inline bool isRootNode(HTMLStackItem
* item
)
45 return item
->isDocumentFragmentNode()
46 || item
->hasTagName(htmlTag
);
49 inline bool isScopeMarker(HTMLStackItem
* item
)
51 return item
->hasTagName(captionTag
)
52 || item
->hasTagName(marqueeTag
)
53 || item
->hasTagName(objectTag
)
54 || item
->hasTagName(tableTag
)
55 || item
->hasTagName(tdTag
)
56 || item
->hasTagName(thTag
)
57 || item
->hasTagName(MathMLNames::miTag
)
58 || item
->hasTagName(MathMLNames::moTag
)
59 || item
->hasTagName(MathMLNames::mnTag
)
60 || item
->hasTagName(MathMLNames::msTag
)
61 || item
->hasTagName(MathMLNames::mtextTag
)
62 || item
->hasTagName(MathMLNames::annotation_xmlTag
)
63 || item
->hasTagName(SVGNames::foreignObjectTag
)
64 || item
->hasTagName(SVGNames::descTag
)
65 || item
->hasTagName(SVGNames::titleTag
)
66 || item
->hasTagName(templateTag
)
70 inline bool isListItemScopeMarker(HTMLStackItem
* item
)
72 return isScopeMarker(item
)
73 || item
->hasTagName(olTag
)
74 || item
->hasTagName(ulTag
);
77 inline bool isTableScopeMarker(HTMLStackItem
* item
)
79 return item
->hasTagName(tableTag
)
80 || item
->hasTagName(templateTag
)
84 inline bool isTableBodyScopeMarker(HTMLStackItem
* item
)
86 return item
->hasTagName(tbodyTag
)
87 || item
->hasTagName(tfootTag
)
88 || item
->hasTagName(theadTag
)
89 || item
->hasTagName(templateTag
)
93 inline bool isTableRowScopeMarker(HTMLStackItem
* item
)
95 return item
->hasTagName(trTag
)
96 || item
->hasTagName(templateTag
)
100 inline bool isForeignContentScopeMarker(HTMLStackItem
* item
)
102 return HTMLElementStack::isMathMLTextIntegrationPoint(item
)
103 || HTMLElementStack::isHTMLIntegrationPoint(item
)
104 || item
->isInHTMLNamespace();
107 inline bool isButtonScopeMarker(HTMLStackItem
* item
)
109 return isScopeMarker(item
)
110 || item
->hasTagName(buttonTag
);
113 inline bool isSelectScopeMarker(HTMLStackItem
* item
)
115 return !item
->hasTagName(optgroupTag
)
116 && !item
->hasTagName(optionTag
);
121 HTMLElementStack::ElementRecord::ElementRecord(PassRefPtrWillBeRawPtr
<HTMLStackItem
> item
, PassOwnPtrWillBeRawPtr
<ElementRecord
> next
)
129 HTMLElementStack::ElementRecord::~ElementRecord()
134 void HTMLElementStack::ElementRecord::replaceElement(PassRefPtrWillBeRawPtr
<HTMLStackItem
> item
)
137 ASSERT(!m_item
|| m_item
->isElementNode());
138 // FIXME: Should this call finishParsingChildren?
142 bool HTMLElementStack::ElementRecord::isAbove(ElementRecord
* other
) const
144 for (ElementRecord
* below
= next(); below
; below
= below
->next()) {
151 DEFINE_TRACE(HTMLElementStack::ElementRecord
)
154 visitor
->trace(m_item
);
155 visitor
->trace(m_next
);
159 HTMLElementStack::HTMLElementStack()
160 : m_rootNode(nullptr)
161 , m_headElement(nullptr)
162 , m_bodyElement(nullptr)
167 HTMLElementStack::~HTMLElementStack()
171 bool HTMLElementStack::hasOnlyOneElement() const
173 return !topRecord()->next();
176 bool HTMLElementStack::secondElementIsHTMLBodyElement() const
178 // This is used the fragment case of <body> and <frameset> in the "in body"
180 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#parsing-main-inbody
182 // If we have a body element, it must always be the second element on the
183 // stack, as we always start with an html element, and any other element
184 // would cause the implicit creation of a body element.
185 return !!m_bodyElement
;
188 void HTMLElementStack::popHTMLHeadElement()
190 ASSERT(top() == m_headElement
);
191 m_headElement
= nullptr;
195 void HTMLElementStack::popHTMLBodyElement()
197 ASSERT(top() == m_bodyElement
);
198 m_bodyElement
= nullptr;
202 void HTMLElementStack::popAll()
204 m_rootNode
= nullptr;
205 m_headElement
= nullptr;
206 m_bodyElement
= nullptr;
209 Node
& node
= *topNode();
210 if (node
.isElementNode())
211 toElement(node
).finishParsingChildren();
212 m_top
= m_top
->releaseNext();
216 void HTMLElementStack::pop()
218 ASSERT(!topStackItem()->hasTagName(HTMLNames::headTag
));
222 void HTMLElementStack::popUntil(const AtomicString
& tagName
)
224 while (!topStackItem()->matchesHTMLTag(tagName
)) {
225 // pop() will ASSERT if a <body>, <head> or <html> will be popped.
230 void HTMLElementStack::popUntilPopped(const AtomicString
& tagName
)
236 void HTMLElementStack::popUntilNumberedHeaderElementPopped()
238 while (!topStackItem()->isNumberedHeaderElement())
243 void HTMLElementStack::popUntil(Element
* element
)
245 while (top() != element
)
249 void HTMLElementStack::popUntilPopped(Element
* element
)
255 void HTMLElementStack::popUntilTableScopeMarker()
257 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#clear-the-stack-back-to-a-table-context
258 while (!isTableScopeMarker(topStackItem()))
262 void HTMLElementStack::popUntilTableBodyScopeMarker()
264 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#clear-the-stack-back-to-a-table-body-context
265 while (!isTableBodyScopeMarker(topStackItem()))
269 void HTMLElementStack::popUntilTableRowScopeMarker()
271 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#clear-the-stack-back-to-a-table-row-context
272 while (!isTableRowScopeMarker(topStackItem()))
276 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#mathml-text-integration-point
277 bool HTMLElementStack::isMathMLTextIntegrationPoint(HTMLStackItem
* item
)
279 if (!item
->isElementNode())
281 return item
->hasTagName(MathMLNames::miTag
)
282 || item
->hasTagName(MathMLNames::moTag
)
283 || item
->hasTagName(MathMLNames::mnTag
)
284 || item
->hasTagName(MathMLNames::msTag
)
285 || item
->hasTagName(MathMLNames::mtextTag
);
288 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#html-integration-point
289 bool HTMLElementStack::isHTMLIntegrationPoint(HTMLStackItem
* item
)
291 if (!item
->isElementNode())
293 if (item
->hasTagName(MathMLNames::annotation_xmlTag
)) {
294 Attribute
* encodingAttr
= item
->getAttributeItem(MathMLNames::encodingAttr
);
296 const String
& encoding
= encodingAttr
->value();
297 return equalIgnoringCase(encoding
, "text/html")
298 || equalIgnoringCase(encoding
, "application/xhtml+xml");
302 return item
->hasTagName(SVGNames::foreignObjectTag
)
303 || item
->hasTagName(SVGNames::descTag
)
304 || item
->hasTagName(SVGNames::titleTag
);
307 void HTMLElementStack::popUntilForeignContentScopeMarker()
309 while (!isForeignContentScopeMarker(topStackItem()))
313 void HTMLElementStack::pushRootNode(PassRefPtrWillBeRawPtr
<HTMLStackItem
> rootItem
)
315 ASSERT(rootItem
->isDocumentFragmentNode());
316 pushRootNodeCommon(rootItem
);
319 void HTMLElementStack::pushHTMLHtmlElement(PassRefPtrWillBeRawPtr
<HTMLStackItem
> item
)
321 ASSERT(item
->hasTagName(htmlTag
));
322 pushRootNodeCommon(item
);
325 void HTMLElementStack::pushRootNodeCommon(PassRefPtrWillBeRawPtr
<HTMLStackItem
> rootItem
)
329 m_rootNode
= rootItem
->node();
330 pushCommon(rootItem
);
333 void HTMLElementStack::pushHTMLHeadElement(PassRefPtrWillBeRawPtr
<HTMLStackItem
> item
)
335 ASSERT(item
->hasTagName(HTMLNames::headTag
));
336 ASSERT(!m_headElement
);
337 m_headElement
= item
->element();
341 void HTMLElementStack::pushHTMLBodyElement(PassRefPtrWillBeRawPtr
<HTMLStackItem
> item
)
343 ASSERT(item
->hasTagName(HTMLNames::bodyTag
));
344 ASSERT(!m_bodyElement
);
345 m_bodyElement
= item
->element();
349 void HTMLElementStack::push(PassRefPtrWillBeRawPtr
<HTMLStackItem
> item
)
351 ASSERT(!item
->hasTagName(htmlTag
));
352 ASSERT(!item
->hasTagName(headTag
));
353 ASSERT(!item
->hasTagName(bodyTag
));
358 void HTMLElementStack::insertAbove(PassRefPtrWillBeRawPtr
<HTMLStackItem
> item
, ElementRecord
* recordBelow
)
363 ASSERT(!item
->hasTagName(htmlTag
));
364 ASSERT(!item
->hasTagName(headTag
));
365 ASSERT(!item
->hasTagName(bodyTag
));
367 if (recordBelow
== m_top
) {
372 for (ElementRecord
* recordAbove
= m_top
.get(); recordAbove
; recordAbove
= recordAbove
->next()) {
373 if (recordAbove
->next() != recordBelow
)
377 recordAbove
->setNext(adoptPtrWillBeNoop(new ElementRecord(item
, recordAbove
->releaseNext())));
378 recordAbove
->next()->element()->beginParsingChildren();
381 ASSERT_NOT_REACHED();
384 HTMLElementStack::ElementRecord
* HTMLElementStack::topRecord() const
390 HTMLStackItem
* HTMLElementStack::oneBelowTop() const
392 // We should never call this if there are fewer than 2 elements on the stack.
394 ASSERT(m_top
->next());
395 if (m_top
->next()->stackItem()->isElementNode())
396 return m_top
->next()->stackItem().get();
400 void HTMLElementStack::removeHTMLHeadElement(Element
* element
)
402 ASSERT(m_headElement
== element
);
403 if (m_top
->element() == element
) {
404 popHTMLHeadElement();
407 m_headElement
= nullptr;
408 removeNonTopCommon(element
);
411 void HTMLElementStack::remove(Element
* element
)
413 ASSERT(!isHTMLHeadElement(element
));
414 if (m_top
->element() == element
) {
418 removeNonTopCommon(element
);
421 HTMLElementStack::ElementRecord
* HTMLElementStack::find(Element
* element
) const
423 for (ElementRecord
* pos
= m_top
.get(); pos
; pos
= pos
->next()) {
424 if (pos
->node() == element
)
430 HTMLElementStack::ElementRecord
* HTMLElementStack::topmost(const AtomicString
& tagName
) const
432 for (ElementRecord
* pos
= m_top
.get(); pos
; pos
= pos
->next()) {
433 if (pos
->stackItem()->matchesHTMLTag(tagName
))
439 bool HTMLElementStack::contains(Element
* element
) const
441 return !!find(element
);
444 bool HTMLElementStack::contains(const AtomicString
& tagName
) const
446 return !!topmost(tagName
);
449 template <bool isMarker(HTMLStackItem
*)>
450 bool inScopeCommon(HTMLElementStack::ElementRecord
* top
, const AtomicString
& targetTag
)
452 for (HTMLElementStack::ElementRecord
* pos
= top
; pos
; pos
= pos
->next()) {
453 HTMLStackItem
* item
= pos
->stackItem().get();
454 if (item
->matchesHTMLTag(targetTag
))
459 ASSERT_NOT_REACHED(); // <html> is always on the stack and is a scope marker.
463 bool HTMLElementStack::hasNumberedHeaderElementInScope() const
465 for (ElementRecord
* record
= m_top
.get(); record
; record
= record
->next()) {
466 HTMLStackItem
* item
= record
->stackItem().get();
467 if (item
->isNumberedHeaderElement())
469 if (isScopeMarker(item
))
472 ASSERT_NOT_REACHED(); // <html> is always on the stack and is a scope marker.
476 bool HTMLElementStack::inScope(Element
* targetElement
) const
478 for (ElementRecord
* pos
= m_top
.get(); pos
; pos
= pos
->next()) {
479 HTMLStackItem
* item
= pos
->stackItem().get();
480 if (item
->node() == targetElement
)
482 if (isScopeMarker(item
))
485 ASSERT_NOT_REACHED(); // <html> is always on the stack and is a scope marker.
489 bool HTMLElementStack::inScope(const AtomicString
& targetTag
) const
491 return inScopeCommon
<isScopeMarker
>(m_top
.get(), targetTag
);
494 bool HTMLElementStack::inScope(const QualifiedName
& tagName
) const
496 return inScope(tagName
.localName());
499 bool HTMLElementStack::inListItemScope(const AtomicString
& targetTag
) const
501 return inScopeCommon
<isListItemScopeMarker
>(m_top
.get(), targetTag
);
504 bool HTMLElementStack::inListItemScope(const QualifiedName
& tagName
) const
506 return inListItemScope(tagName
.localName());
509 bool HTMLElementStack::inTableScope(const AtomicString
& targetTag
) const
511 return inScopeCommon
<isTableScopeMarker
>(m_top
.get(), targetTag
);
514 bool HTMLElementStack::inTableScope(const QualifiedName
& tagName
) const
516 return inTableScope(tagName
.localName());
519 bool HTMLElementStack::inButtonScope(const AtomicString
& targetTag
) const
521 return inScopeCommon
<isButtonScopeMarker
>(m_top
.get(), targetTag
);
524 bool HTMLElementStack::inButtonScope(const QualifiedName
& tagName
) const
526 return inButtonScope(tagName
.localName());
529 bool HTMLElementStack::inSelectScope(const AtomicString
& targetTag
) const
531 return inScopeCommon
<isSelectScopeMarker
>(m_top
.get(), targetTag
);
534 bool HTMLElementStack::inSelectScope(const QualifiedName
& tagName
) const
536 return inSelectScope(tagName
.localName());
539 bool HTMLElementStack::hasTemplateInHTMLScope() const
541 return inScopeCommon
<isRootNode
>(m_top
.get(), templateTag
.localName());
544 Element
* HTMLElementStack::htmlElement() const
547 return toElement(m_rootNode
);
550 Element
* HTMLElementStack::headElement() const
552 ASSERT(m_headElement
);
553 return m_headElement
;
556 Element
* HTMLElementStack::bodyElement() const
558 ASSERT(m_bodyElement
);
559 return m_bodyElement
;
562 ContainerNode
* HTMLElementStack::rootNode() const
568 void HTMLElementStack::pushCommon(PassRefPtrWillBeRawPtr
<HTMLStackItem
> item
)
573 m_top
= adoptPtrWillBeNoop(new ElementRecord(item
, m_top
.release()));
576 void HTMLElementStack::popCommon()
578 ASSERT(!topStackItem()->hasTagName(htmlTag
));
579 ASSERT(!topStackItem()->hasTagName(headTag
) || !m_headElement
);
580 ASSERT(!topStackItem()->hasTagName(bodyTag
) || !m_bodyElement
);
581 top()->finishParsingChildren();
582 m_top
= m_top
->releaseNext();
587 void HTMLElementStack::removeNonTopCommon(Element
* element
)
589 ASSERT(!isHTMLHtmlElement(element
));
590 ASSERT(!isHTMLBodyElement(element
));
591 ASSERT(top() != element
);
592 for (ElementRecord
* pos
= m_top
.get(); pos
; pos
= pos
->next()) {
593 if (pos
->next()->element() == element
) {
594 // FIXME: Is it OK to call finishParsingChildren()
595 // when the children aren't actually finished?
596 element
->finishParsingChildren();
597 pos
->setNext(pos
->next()->releaseNext());
602 ASSERT_NOT_REACHED();
605 HTMLElementStack::ElementRecord
* HTMLElementStack::furthestBlockForFormattingElement(Element
* formattingElement
) const
607 ElementRecord
* furthestBlock
= 0;
608 for (ElementRecord
* pos
= m_top
.get(); pos
; pos
= pos
->next()) {
609 if (pos
->element() == formattingElement
)
610 return furthestBlock
;
611 if (pos
->stackItem()->isSpecialNode())
614 ASSERT_NOT_REACHED();
618 DEFINE_TRACE(HTMLElementStack
)
620 visitor
->trace(m_top
);
621 visitor
->trace(m_rootNode
);
622 visitor
->trace(m_headElement
);
623 visitor
->trace(m_bodyElement
);
628 void HTMLElementStack::show()
630 for (ElementRecord
* record
= m_top
.get(); record
; record
= record
->next())
631 record
->element()->showNode();