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(appletTag)
52 || item->hasTagName(captionTag)
53 || item->hasTagName(marqueeTag)
54 || item->hasTagName(objectTag)
55 || item->hasTagName(tableTag)
56 || item->hasTagName(tdTag)
57 || item->hasTagName(thTag)
58 || item->hasTagName(MathMLNames::miTag)
59 || item->hasTagName(MathMLNames::moTag)
60 || item->hasTagName(MathMLNames::mnTag)
61 || item->hasTagName(MathMLNames::msTag)
62 || item->hasTagName(MathMLNames::mtextTag)
63 || item->hasTagName(MathMLNames::annotation_xmlTag)
64 || item->hasTagName(SVGNames::foreignObjectTag)
65 || item->hasTagName(SVGNames::descTag)
66 || item->hasTagName(SVGNames::titleTag)
67 || item->hasTagName(templateTag)
71 inline bool isListItemScopeMarker(HTMLStackItem* item)
73 return isScopeMarker(item)
74 || item->hasTagName(olTag)
75 || item->hasTagName(ulTag);
78 inline bool isTableScopeMarker(HTMLStackItem* item)
80 return item->hasTagName(tableTag)
81 || item->hasTagName(templateTag)
85 inline bool isTableBodyScopeMarker(HTMLStackItem* item)
87 return item->hasTagName(tbodyTag)
88 || item->hasTagName(tfootTag)
89 || item->hasTagName(theadTag)
90 || item->hasTagName(templateTag)
94 inline bool isTableRowScopeMarker(HTMLStackItem* item)
96 return item->hasTagName(trTag)
97 || item->hasTagName(templateTag)
101 inline bool isForeignContentScopeMarker(HTMLStackItem* item)
103 return HTMLElementStack::isMathMLTextIntegrationPoint(item)
104 || HTMLElementStack::isHTMLIntegrationPoint(item)
105 || item->isInHTMLNamespace();
108 inline bool isButtonScopeMarker(HTMLStackItem* item)
110 return isScopeMarker(item)
111 || item->hasTagName(buttonTag);
114 inline bool isSelectScopeMarker(HTMLStackItem* item)
116 return !item->hasTagName(optgroupTag)
117 && !item->hasTagName(optionTag);
122 HTMLElementStack::ElementRecord::ElementRecord(PassRefPtrWillBeRawPtr<HTMLStackItem> item, PassOwnPtrWillBeRawPtr<ElementRecord> next)
130 HTMLElementStack::ElementRecord::~ElementRecord()
135 void HTMLElementStack::ElementRecord::replaceElement(PassRefPtrWillBeRawPtr<HTMLStackItem> item)
138 ASSERT(!m_item || m_item->isElementNode());
139 // FIXME: Should this call finishParsingChildren?
143 bool HTMLElementStack::ElementRecord::isAbove(ElementRecord* other) const
145 for (ElementRecord* below = next(); below; below = below->next()) {
152 void HTMLElementStack::ElementRecord::trace(Visitor* visitor)
155 visitor->trace(m_item);
156 visitor->trace(m_next);
160 HTMLElementStack::HTMLElementStack()
161 : m_rootNode(nullptr)
162 , m_headElement(nullptr)
163 , m_bodyElement(nullptr)
168 HTMLElementStack::~HTMLElementStack()
172 bool HTMLElementStack::hasOnlyOneElement() const
174 return !topRecord()->next();
177 bool HTMLElementStack::secondElementIsHTMLBodyElement() const
179 // This is used the fragment case of <body> and <frameset> in the "in body"
181 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#parsing-main-inbody
183 // If we have a body element, it must always be the second element on the
184 // stack, as we always start with an html element, and any other element
185 // would cause the implicit creation of a body element.
186 return !!m_bodyElement;
189 void HTMLElementStack::popHTMLHeadElement()
191 ASSERT(top() == m_headElement);
192 m_headElement = nullptr;
196 void HTMLElementStack::popHTMLBodyElement()
198 ASSERT(top() == m_bodyElement);
199 m_bodyElement = nullptr;
203 void HTMLElementStack::popAll()
205 m_rootNode = nullptr;
206 m_headElement = nullptr;
207 m_bodyElement = nullptr;
210 Node& node = *topNode();
211 if (node.isElementNode())
212 toElement(node).finishParsingChildren();
213 m_top = m_top->releaseNext();
217 void HTMLElementStack::pop()
219 ASSERT(!topStackItem()->hasTagName(HTMLNames::headTag));
223 void HTMLElementStack::popUntil(const AtomicString& tagName)
225 while (!topStackItem()->matchesHTMLTag(tagName)) {
226 // pop() will ASSERT if a <body>, <head> or <html> will be popped.
231 void HTMLElementStack::popUntilPopped(const AtomicString& tagName)
237 void HTMLElementStack::popUntilNumberedHeaderElementPopped()
239 while (!topStackItem()->isNumberedHeaderElement())
244 void HTMLElementStack::popUntil(Element* element)
246 while (top() != element)
250 void HTMLElementStack::popUntilPopped(Element* element)
256 void HTMLElementStack::popUntilTableScopeMarker()
258 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#clear-the-stack-back-to-a-table-context
259 while (!isTableScopeMarker(topStackItem()))
263 void HTMLElementStack::popUntilTableBodyScopeMarker()
265 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#clear-the-stack-back-to-a-table-body-context
266 while (!isTableBodyScopeMarker(topStackItem()))
270 void HTMLElementStack::popUntilTableRowScopeMarker()
272 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#clear-the-stack-back-to-a-table-row-context
273 while (!isTableRowScopeMarker(topStackItem()))
277 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#mathml-text-integration-point
278 bool HTMLElementStack::isMathMLTextIntegrationPoint(HTMLStackItem* item)
280 if (!item->isElementNode())
282 return item->hasTagName(MathMLNames::miTag)
283 || item->hasTagName(MathMLNames::moTag)
284 || item->hasTagName(MathMLNames::mnTag)
285 || item->hasTagName(MathMLNames::msTag)
286 || item->hasTagName(MathMLNames::mtextTag);
289 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#html-integration-point
290 bool HTMLElementStack::isHTMLIntegrationPoint(HTMLStackItem* item)
292 if (!item->isElementNode())
294 if (item->hasTagName(MathMLNames::annotation_xmlTag)) {
295 Attribute* encodingAttr = item->getAttributeItem(MathMLNames::encodingAttr);
297 const String& encoding = encodingAttr->value();
298 return equalIgnoringCase(encoding, "text/html")
299 || equalIgnoringCase(encoding, "application/xhtml+xml");
303 return item->hasTagName(SVGNames::foreignObjectTag)
304 || item->hasTagName(SVGNames::descTag)
305 || item->hasTagName(SVGNames::titleTag);
308 void HTMLElementStack::popUntilForeignContentScopeMarker()
310 while (!isForeignContentScopeMarker(topStackItem()))
314 void HTMLElementStack::pushRootNode(PassRefPtrWillBeRawPtr<HTMLStackItem> rootItem)
316 ASSERT(rootItem->isDocumentFragmentNode());
317 pushRootNodeCommon(rootItem);
320 void HTMLElementStack::pushHTMLHtmlElement(PassRefPtrWillBeRawPtr<HTMLStackItem> item)
322 ASSERT(item->hasTagName(htmlTag));
323 pushRootNodeCommon(item);
326 void HTMLElementStack::pushRootNodeCommon(PassRefPtrWillBeRawPtr<HTMLStackItem> rootItem)
330 m_rootNode = rootItem->node();
331 pushCommon(rootItem);
334 void HTMLElementStack::pushHTMLHeadElement(PassRefPtrWillBeRawPtr<HTMLStackItem> item)
336 ASSERT(item->hasTagName(HTMLNames::headTag));
337 ASSERT(!m_headElement);
338 m_headElement = item->element();
342 void HTMLElementStack::pushHTMLBodyElement(PassRefPtrWillBeRawPtr<HTMLStackItem> item)
344 ASSERT(item->hasTagName(HTMLNames::bodyTag));
345 ASSERT(!m_bodyElement);
346 m_bodyElement = item->element();
350 void HTMLElementStack::push(PassRefPtrWillBeRawPtr<HTMLStackItem> item)
352 ASSERT(!item->hasTagName(htmlTag));
353 ASSERT(!item->hasTagName(headTag));
354 ASSERT(!item->hasTagName(bodyTag));
359 void HTMLElementStack::insertAbove(PassRefPtrWillBeRawPtr<HTMLStackItem> item, ElementRecord* recordBelow)
364 ASSERT(!item->hasTagName(htmlTag));
365 ASSERT(!item->hasTagName(headTag));
366 ASSERT(!item->hasTagName(bodyTag));
368 if (recordBelow == m_top) {
373 for (ElementRecord* recordAbove = m_top.get(); recordAbove; recordAbove = recordAbove->next()) {
374 if (recordAbove->next() != recordBelow)
378 recordAbove->setNext(adoptPtrWillBeNoop(new ElementRecord(item, recordAbove->releaseNext())));
379 recordAbove->next()->element()->beginParsingChildren();
382 ASSERT_NOT_REACHED();
385 HTMLElementStack::ElementRecord* HTMLElementStack::topRecord() const
391 HTMLStackItem* HTMLElementStack::oneBelowTop() const
393 // We should never call this if there are fewer than 2 elements on the stack.
395 ASSERT(m_top->next());
396 if (m_top->next()->stackItem()->isElementNode())
397 return m_top->next()->stackItem().get();
401 void HTMLElementStack::removeHTMLHeadElement(Element* element)
403 ASSERT(m_headElement == element);
404 if (m_top->element() == element) {
405 popHTMLHeadElement();
408 m_headElement = nullptr;
409 removeNonTopCommon(element);
412 void HTMLElementStack::remove(Element* element)
414 ASSERT(!isHTMLHeadElement(element));
415 if (m_top->element() == element) {
419 removeNonTopCommon(element);
422 HTMLElementStack::ElementRecord* HTMLElementStack::find(Element* element) const
424 for (ElementRecord* pos = m_top.get(); pos; pos = pos->next()) {
425 if (pos->node() == element)
431 HTMLElementStack::ElementRecord* HTMLElementStack::topmost(const AtomicString& tagName) const
433 for (ElementRecord* pos = m_top.get(); pos; pos = pos->next()) {
434 if (pos->stackItem()->matchesHTMLTag(tagName))
440 bool HTMLElementStack::contains(Element* element) const
442 return !!find(element);
445 bool HTMLElementStack::contains(const AtomicString& tagName) const
447 return !!topmost(tagName);
450 template <bool isMarker(HTMLStackItem*)>
451 bool inScopeCommon(HTMLElementStack::ElementRecord* top, const AtomicString& targetTag)
453 for (HTMLElementStack::ElementRecord* pos = top; pos; pos = pos->next()) {
454 HTMLStackItem* item = pos->stackItem().get();
455 if (item->matchesHTMLTag(targetTag))
460 ASSERT_NOT_REACHED(); // <html> is always on the stack and is a scope marker.
464 bool HTMLElementStack::hasNumberedHeaderElementInScope() const
466 for (ElementRecord* record = m_top.get(); record; record = record->next()) {
467 HTMLStackItem* item = record->stackItem().get();
468 if (item->isNumberedHeaderElement())
470 if (isScopeMarker(item))
473 ASSERT_NOT_REACHED(); // <html> is always on the stack and is a scope marker.
477 bool HTMLElementStack::inScope(Element* targetElement) const
479 for (ElementRecord* pos = m_top.get(); pos; pos = pos->next()) {
480 HTMLStackItem* item = pos->stackItem().get();
481 if (item->node() == targetElement)
483 if (isScopeMarker(item))
486 ASSERT_NOT_REACHED(); // <html> is always on the stack and is a scope marker.
490 bool HTMLElementStack::inScope(const AtomicString& targetTag) const
492 return inScopeCommon<isScopeMarker>(m_top.get(), targetTag);
495 bool HTMLElementStack::inScope(const QualifiedName& tagName) const
497 return inScope(tagName.localName());
500 bool HTMLElementStack::inListItemScope(const AtomicString& targetTag) const
502 return inScopeCommon<isListItemScopeMarker>(m_top.get(), targetTag);
505 bool HTMLElementStack::inListItemScope(const QualifiedName& tagName) const
507 return inListItemScope(tagName.localName());
510 bool HTMLElementStack::inTableScope(const AtomicString& targetTag) const
512 return inScopeCommon<isTableScopeMarker>(m_top.get(), targetTag);
515 bool HTMLElementStack::inTableScope(const QualifiedName& tagName) const
517 return inTableScope(tagName.localName());
520 bool HTMLElementStack::inButtonScope(const AtomicString& targetTag) const
522 return inScopeCommon<isButtonScopeMarker>(m_top.get(), targetTag);
525 bool HTMLElementStack::inButtonScope(const QualifiedName& tagName) const
527 return inButtonScope(tagName.localName());
530 bool HTMLElementStack::inSelectScope(const AtomicString& targetTag) const
532 return inScopeCommon<isSelectScopeMarker>(m_top.get(), targetTag);
535 bool HTMLElementStack::inSelectScope(const QualifiedName& tagName) const
537 return inSelectScope(tagName.localName());
540 bool HTMLElementStack::hasTemplateInHTMLScope() const
542 return inScopeCommon<isRootNode>(m_top.get(), templateTag.localName());
545 Element* HTMLElementStack::htmlElement() const
548 return toElement(m_rootNode);
551 Element* HTMLElementStack::headElement() const
553 ASSERT(m_headElement);
554 return m_headElement;
557 Element* HTMLElementStack::bodyElement() const
559 ASSERT(m_bodyElement);
560 return m_bodyElement;
563 ContainerNode* HTMLElementStack::rootNode() const
569 void HTMLElementStack::pushCommon(PassRefPtrWillBeRawPtr<HTMLStackItem> item)
574 m_top = adoptPtrWillBeNoop(new ElementRecord(item, m_top.release()));
577 void HTMLElementStack::popCommon()
579 ASSERT(!topStackItem()->hasTagName(htmlTag));
580 ASSERT(!topStackItem()->hasTagName(headTag) || !m_headElement);
581 ASSERT(!topStackItem()->hasTagName(bodyTag) || !m_bodyElement);
582 top()->finishParsingChildren();
583 m_top = m_top->releaseNext();
588 void HTMLElementStack::removeNonTopCommon(Element* element)
590 ASSERT(!isHTMLHtmlElement(element));
591 ASSERT(!isHTMLBodyElement(element));
592 ASSERT(top() != element);
593 for (ElementRecord* pos = m_top.get(); pos; pos = pos->next()) {
594 if (pos->next()->element() == element) {
595 // FIXME: Is it OK to call finishParsingChildren()
596 // when the children aren't actually finished?
597 element->finishParsingChildren();
598 pos->setNext(pos->next()->releaseNext());
603 ASSERT_NOT_REACHED();
606 HTMLElementStack::ElementRecord* HTMLElementStack::furthestBlockForFormattingElement(Element* formattingElement) const
608 ElementRecord* furthestBlock = 0;
609 for (ElementRecord* pos = m_top.get(); pos; pos = pos->next()) {
610 if (pos->element() == formattingElement)
611 return furthestBlock;
612 if (pos->stackItem()->isSpecialNode())
615 ASSERT_NOT_REACHED();
619 void HTMLElementStack::trace(Visitor* visitor)
621 visitor->trace(m_top);
622 visitor->trace(m_rootNode);
623 visitor->trace(m_headElement);
624 visitor->trace(m_bodyElement);
629 void HTMLElementStack::show()
631 for (ElementRecord* record = m_top.get(); record; record = record->next())
632 record->element()->showNode();