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.
27 #include "HTMLTreeBuilder.h"
30 #include "DocumentFragment.h"
31 #include "DocumentType.h"
34 #include "HTMLDocument.h"
35 #include "HTMLElementFactory.h"
36 #include "HTMLHtmlElement.h"
37 #include "HTMLNames.h"
38 #include "HTMLScriptElement.h"
39 #include "HTMLToken.h"
40 #include "HTMLTokenizer.h"
41 #include "LegacyHTMLDocumentParser.h"
42 #include "LegacyHTMLTreeBuilder.h"
43 #include "LocalizedStrings.h"
45 #include "MathMLNames.h"
47 #include "NotImplemented.h"
51 #include "ScriptController.h"
54 #include <wtf/UnusedParam.h>
58 using namespace HTMLNames;
62 bool hasImpliedEndTag(Element* element)
64 return element->hasTagName(ddTag)
65 || element->hasTagName(dtTag)
66 || element->hasTagName(liTag)
67 || element->hasTagName(optionTag)
68 || element->hasTagName(optgroupTag)
69 || element->hasTagName(pTag)
70 || element->hasTagName(rpTag)
71 || element->hasTagName(rtTag);
76 template<typename ChildType>
77 PassRefPtr<ChildType> HTMLConstructionSite::attach(Node* parent, PassRefPtr<ChildType> prpChild)
79 RefPtr<ChildType> child = prpChild;
81 if (m_redirectAttachToFosterParent) {
82 fosterParent(child.get());
83 return child.release();
86 parent->parserAddChild(child);
87 // It's slightly unfortunate that we need to hold a reference to child
88 // here to call attach(). We should investigate whether we can rely on
89 // |parent| to hold a ref at this point. In the common case (at least
90 // for elements), however, we'll get to use this ref in the stack of
93 return child.release();
96 HTMLConstructionSite::HTMLConstructionSite(Document* document, FragmentScriptingPermission scriptingPermission)
97 : m_document(document)
98 , m_fragmentScriptingPermission(scriptingPermission)
99 , m_redirectAttachToFosterParent(false)
103 HTMLConstructionSite::~HTMLConstructionSite()
107 void HTMLConstructionSite::insertHTMLHtmlStartTagBeforeHTML(AtomicHTMLToken& token)
109 RefPtr<Element> element = HTMLHtmlElement::create(m_document);
110 element->setAttributeMap(token.takeAtributes(), m_fragmentScriptingPermission);
111 m_openElements.pushHTMLHtmlElement(attach(m_document, element.release()));
114 void HTMLConstructionSite::mergeAttributesFromTokenIntoElement(AtomicHTMLToken& token, Element* element)
116 if (!token.attributes())
119 NamedNodeMap* attributes = element->attributes(false);
120 for (unsigned i = 0; i < token.attributes()->length(); ++i) {
121 Attribute* attribute = token.attributes()->attributeItem(i);
122 if (!attributes->getAttributeItem(attribute->name()))
123 element->setAttribute(attribute->name(), attribute->value());
127 void HTMLConstructionSite::insertHTMLHtmlStartTagInBody(AtomicHTMLToken& token)
129 // FIXME: parse error
130 mergeAttributesFromTokenIntoElement(token, m_openElements.htmlElement());
133 void HTMLConstructionSite::insertHTMLBodyStartTagInBody(AtomicHTMLToken& token)
135 // FIXME: parse error
136 notImplemented(); // fragment case
137 mergeAttributesFromTokenIntoElement(token, m_openElements.bodyElement());
140 void HTMLConstructionSite::insertDoctype(AtomicHTMLToken& token)
142 ASSERT(token.type() == HTMLToken::DOCTYPE);
143 attach(m_document, DocumentType::create(m_document, token.name(), String::adopt(token.publicIdentifier()), String::adopt(token.systemIdentifier())));
144 // FIXME: Move quirks mode detection from DocumentType element to here.
146 if (token.forceQuirks())
147 m_document->setParseMode(Document::Compat);
150 void HTMLConstructionSite::insertComment(AtomicHTMLToken& token)
152 ASSERT(token.type() == HTMLToken::Comment);
153 attach(currentElement(), Comment::create(m_document, token.comment()));
156 void HTMLConstructionSite::insertCommentOnDocument(AtomicHTMLToken& token)
158 ASSERT(token.type() == HTMLToken::Comment);
159 attach(m_document, Comment::create(m_document, token.comment()));
162 void HTMLConstructionSite::insertCommentOnHTMLHtmlElement(AtomicHTMLToken& token)
164 ASSERT(token.type() == HTMLToken::Comment);
165 attach(m_openElements.htmlElement(), Comment::create(m_document, token.comment()));
168 PassRefPtr<Element> HTMLConstructionSite::createHTMLElementAndAttachToCurrent(AtomicHTMLToken& token)
170 ASSERT(token.type() == HTMLToken::StartTag);
171 return attach(currentElement(), createHTMLElement(token));
174 void HTMLConstructionSite::insertHTMLHtmlElement(AtomicHTMLToken& token)
176 ASSERT(!m_redirectAttachToFosterParent);
177 m_openElements.pushHTMLHtmlElement(createHTMLElementAndAttachToCurrent(token));
180 void HTMLConstructionSite::insertHTMLHeadElement(AtomicHTMLToken& token)
182 ASSERT(!m_redirectAttachToFosterParent);
183 m_head = createHTMLElementAndAttachToCurrent(token);
184 m_openElements.pushHTMLHeadElement(m_head);
187 void HTMLConstructionSite::insertHTMLBodyElement(AtomicHTMLToken& token)
189 ASSERT(!m_redirectAttachToFosterParent);
190 m_openElements.pushHTMLBodyElement(createHTMLElementAndAttachToCurrent(token));
193 void HTMLConstructionSite::insertHTMLElement(AtomicHTMLToken& token)
195 m_openElements.push(createHTMLElementAndAttachToCurrent(token));
198 void HTMLConstructionSite::insertSelfClosingHTMLElement(AtomicHTMLToken& token)
200 ASSERT(token.type() == HTMLToken::StartTag);
201 attach(currentElement(), createHTMLElement(token));
202 // FIXME: Do we want to acknowledge the token's self-closing flag?
203 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#acknowledge-self-closing-flag
206 void HTMLConstructionSite::insertFormattingElement(AtomicHTMLToken& token)
208 // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#the-stack-of-open-elements
209 // Possible active formatting elements include:
210 // a, b, big, code, em, font, i, nobr, s, small, strike, strong, tt, and u.
211 insertHTMLElement(token);
212 m_activeFormattingElements.append(currentElement());
215 void HTMLConstructionSite::insertScriptElement(AtomicHTMLToken& token)
217 RefPtr<HTMLScriptElement> element = HTMLScriptElement::create(scriptTag, m_document, true);
218 element->setAttributeMap(token.takeAtributes(), m_fragmentScriptingPermission);
219 m_openElements.push(attach(currentElement(), element.release()));
222 void HTMLConstructionSite::insertTextNode(AtomicHTMLToken& token)
224 if (Node* lastChild = currentElement()->lastChild()) {
225 if (lastChild->isTextNode()) {
226 // FIXME: We're only supposed to append to this text node if it
227 // was the last text node inserted by the parser.
228 CharacterData* textNode = static_cast<CharacterData*>(lastChild);
229 textNode->parserAppendData(token.characters());
233 attach(currentElement(), Text::create(m_document, token.characters()));
236 PassRefPtr<Element> HTMLConstructionSite::createHTMLElement(AtomicHTMLToken& token)
238 RefPtr<Element> element = HTMLElementFactory::createHTMLElement(QualifiedName(nullAtom, token.name(), xhtmlNamespaceURI), m_document, 0);
239 element->setAttributeMap(token.takeAtributes(), m_fragmentScriptingPermission);
240 return element.release();
243 bool HTMLConstructionSite::indexOfFirstUnopenFormattingElement(unsigned& firstUnopenElementIndex) const
245 if (m_activeFormattingElements.isEmpty())
247 unsigned index = m_activeFormattingElements.size();
250 const HTMLFormattingElementList::Entry& entry = m_activeFormattingElements.at(index);
251 if (entry.isMarker() || m_openElements.contains(entry.element())) {
252 firstUnopenElementIndex = index + 1;
253 return firstUnopenElementIndex < m_activeFormattingElements.size();
256 firstUnopenElementIndex = index;
260 void HTMLConstructionSite::reconstructTheActiveFormattingElements()
262 unsigned firstUnopenElementIndex;
263 if (!indexOfFirstUnopenFormattingElement(firstUnopenElementIndex))
266 unsigned unopenEntryIndex = firstUnopenElementIndex;
267 ASSERT(unopenEntryIndex < m_activeFormattingElements.size());
268 for (; unopenEntryIndex < m_activeFormattingElements.size(); ++unopenEntryIndex) {
269 HTMLFormattingElementList::Entry& unopenedEntry = m_activeFormattingElements.at(unopenEntryIndex);
270 // FIXME: We're supposed to save the original token in the entry.
271 AtomicHTMLToken fakeToken(HTMLToken::StartTag, unopenedEntry.element()->localName());
272 insertHTMLElement(fakeToken);
273 unopenedEntry.replaceElement(currentElement());
277 void HTMLConstructionSite::generateImpliedEndTagsWithExclusion(const AtomicString& tagName)
279 while (hasImpliedEndTag(currentElement()) && !currentElement()->hasLocalName(tagName))
280 m_openElements.pop();
283 void HTMLConstructionSite::generateImpliedEndTags()
285 while (hasImpliedEndTag(currentElement()))
286 m_openElements.pop();
289 void HTMLConstructionSite::fosterParent(Node* node)
291 Element* fosterParentElement = 0;
292 HTMLElementStack::ElementRecord* lastTableElementRecord = m_openElements.topmost(tableTag.localName());
293 if (lastTableElementRecord) {
294 Element* lastTableElement = lastTableElementRecord->element();
295 if (lastTableElement->parent()) {
296 // FIXME: We need an insertHTMLElement which does not send mutation events.
297 ExceptionCode ec = 0;
298 lastTableElement->parent()->insertBefore(node, lastTableElement, ec);
299 // FIXME: Do we need to call attach()?
303 fosterParentElement = lastTableElementRecord->next()->element();
306 fosterParentElement = m_openElements.bottom(); // <html> element
309 fosterParentElement->parserAddChild(node);
310 // FIXME: Do we need to call attach()?