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 // FIXME: It's confusing that HTMLConstructionSite::attach does the magic
82 // redirection to the foster parent but HTMLConstructionSite::attachAtSite
83 // doesn't. It feels like we're missing a concept somehow.
84 if (m_redirectAttachToFosterParent) {
85 fosterParent(child.get());
86 return child.release();
89 parent->parserAddChild(child);
90 // It's slightly unfortunate that we need to hold a reference to child
91 // here to call attach(). We should investigate whether we can rely on
92 // |parent| to hold a ref at this point. In the common case (at least
93 // for elements), however, we'll get to use this ref in the stack of
96 return child.release();
99 void HTMLConstructionSite::attachAtSite(const AttachmentSite& site, PassRefPtr<Node> child)
101 if (site.nextChild) {
102 // FIXME: We need an insertElement which does not send mutation events.
103 ExceptionCode ec = 0;
104 site.parent->insertBefore(child, site.nextChild, ec);
105 // FIXME: Do we need to call attach()?
109 site.parent->parserAddChild(child);
110 // FIXME: Do we need to call attach()?
113 HTMLConstructionSite::HTMLConstructionSite(Document* document, FragmentScriptingPermission scriptingPermission)
114 : m_document(document)
115 , m_fragmentScriptingPermission(scriptingPermission)
116 , m_redirectAttachToFosterParent(false)
120 HTMLConstructionSite::~HTMLConstructionSite()
124 void HTMLConstructionSite::insertHTMLHtmlStartTagBeforeHTML(AtomicHTMLToken& token)
126 RefPtr<Element> element = HTMLHtmlElement::create(m_document);
127 element->setAttributeMap(token.takeAtributes(), m_fragmentScriptingPermission);
128 m_openElements.pushHTMLHtmlElement(attach(m_document, element.release()));
131 void HTMLConstructionSite::mergeAttributesFromTokenIntoElement(AtomicHTMLToken& token, Element* element)
133 if (!token.attributes())
136 NamedNodeMap* attributes = element->attributes(false);
137 for (unsigned i = 0; i < token.attributes()->length(); ++i) {
138 Attribute* attribute = token.attributes()->attributeItem(i);
139 if (!attributes->getAttributeItem(attribute->name()))
140 element->setAttribute(attribute->name(), attribute->value());
144 void HTMLConstructionSite::insertHTMLHtmlStartTagInBody(AtomicHTMLToken& token)
146 // FIXME: parse error
147 mergeAttributesFromTokenIntoElement(token, m_openElements.htmlElement());
150 void HTMLConstructionSite::insertHTMLBodyStartTagInBody(AtomicHTMLToken& token)
152 // FIXME: parse error
153 notImplemented(); // fragment case
154 mergeAttributesFromTokenIntoElement(token, m_openElements.bodyElement());
157 void HTMLConstructionSite::insertDoctype(AtomicHTMLToken& token)
159 ASSERT(token.type() == HTMLToken::DOCTYPE);
160 attach(m_document, DocumentType::create(m_document, token.name(), String::adopt(token.publicIdentifier()), String::adopt(token.systemIdentifier())));
161 // FIXME: Move quirks mode detection from DocumentType element to here.
163 if (token.forceQuirks())
164 m_document->setParseMode(Document::Compat);
167 void HTMLConstructionSite::insertComment(AtomicHTMLToken& token)
169 ASSERT(token.type() == HTMLToken::Comment);
170 attach(currentElement(), Comment::create(m_document, token.comment()));
173 void HTMLConstructionSite::insertCommentOnDocument(AtomicHTMLToken& token)
175 ASSERT(token.type() == HTMLToken::Comment);
176 attach(m_document, Comment::create(m_document, token.comment()));
179 void HTMLConstructionSite::insertCommentOnHTMLHtmlElement(AtomicHTMLToken& token)
181 ASSERT(token.type() == HTMLToken::Comment);
182 attach(m_openElements.htmlElement(), Comment::create(m_document, token.comment()));
185 PassRefPtr<Element> HTMLConstructionSite::createHTMLElementAndAttachToCurrent(AtomicHTMLToken& token)
187 ASSERT(token.type() == HTMLToken::StartTag);
188 return attach(currentElement(), createHTMLElement(token));
191 void HTMLConstructionSite::insertHTMLHtmlElement(AtomicHTMLToken& token)
193 ASSERT(!m_redirectAttachToFosterParent);
194 m_openElements.pushHTMLHtmlElement(createHTMLElementAndAttachToCurrent(token));
197 void HTMLConstructionSite::insertHTMLHeadElement(AtomicHTMLToken& token)
199 ASSERT(!m_redirectAttachToFosterParent);
200 m_head = createHTMLElementAndAttachToCurrent(token);
201 m_openElements.pushHTMLHeadElement(m_head);
204 void HTMLConstructionSite::insertHTMLBodyElement(AtomicHTMLToken& token)
206 ASSERT(!m_redirectAttachToFosterParent);
207 m_openElements.pushHTMLBodyElement(createHTMLElementAndAttachToCurrent(token));
210 void HTMLConstructionSite::insertHTMLElement(AtomicHTMLToken& token)
212 m_openElements.push(createHTMLElementAndAttachToCurrent(token));
215 void HTMLConstructionSite::insertSelfClosingHTMLElement(AtomicHTMLToken& token)
217 ASSERT(token.type() == HTMLToken::StartTag);
218 attach(currentElement(), createHTMLElement(token));
219 // FIXME: Do we want to acknowledge the token's self-closing flag?
220 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#acknowledge-self-closing-flag
223 void HTMLConstructionSite::insertFormattingElement(AtomicHTMLToken& token)
225 // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#the-stack-of-open-elements
226 // Possible active formatting elements include:
227 // a, b, big, code, em, font, i, nobr, s, small, strike, strong, tt, and u.
228 insertHTMLElement(token);
229 m_activeFormattingElements.append(currentElement());
232 void HTMLConstructionSite::insertScriptElement(AtomicHTMLToken& token)
234 RefPtr<HTMLScriptElement> element = HTMLScriptElement::create(scriptTag, m_document, true);
235 element->setAttributeMap(token.takeAtributes(), m_fragmentScriptingPermission);
236 m_openElements.push(attach(currentElement(), element.release()));
239 void HTMLConstructionSite::insertForeignElement(AtomicHTMLToken& token, const AtomicString& namespaceURI)
241 ASSERT(token.type() == HTMLToken::StartTag);
242 notImplemented(); // parseError when xmlns or xmlns:xlink are wrong.
244 RefPtr<Element> element = attach(currentElement(), createElement(token, namespaceURI));
245 if (!token.selfClosing())
246 m_openElements.push(element);
249 void HTMLConstructionSite::insertTextNode(const String& characters)
252 site.parent = currentElement();
254 if (m_redirectAttachToFosterParent)
255 findFosterSite(site);
257 Node* previousChild = site.nextChild ? site.nextChild->previousSibling() : site.parent->lastChild();
258 if (previousChild && previousChild->isTextNode()) {
259 // FIXME: We're only supposed to append to this text node if it
260 // was the last text node inserted by the parser.
261 CharacterData* textNode = static_cast<CharacterData*>(previousChild);
262 textNode->parserAppendData(characters);
266 attachAtSite(site, Text::create(m_document, characters));
269 PassRefPtr<Element> HTMLConstructionSite::createElement(AtomicHTMLToken& token, const AtomicString& namespaceURI)
271 QualifiedName tagName(nullAtom, token.name(), namespaceURI);
272 RefPtr<Element> element = m_document->createElement(tagName, true);
273 element->setAttributeMap(token.takeAtributes(), m_fragmentScriptingPermission);
274 return element.release();
277 PassRefPtr<Element> HTMLConstructionSite::createHTMLElement(AtomicHTMLToken& token)
279 RefPtr<Element> element = createElement(token, xhtmlNamespaceURI);
280 ASSERT(element->isHTMLElement());
281 return element.release();
284 bool HTMLConstructionSite::indexOfFirstUnopenFormattingElement(unsigned& firstUnopenElementIndex) const
286 if (m_activeFormattingElements.isEmpty())
288 unsigned index = m_activeFormattingElements.size();
291 const HTMLFormattingElementList::Entry& entry = m_activeFormattingElements.at(index);
292 if (entry.isMarker() || m_openElements.contains(entry.element())) {
293 firstUnopenElementIndex = index + 1;
294 return firstUnopenElementIndex < m_activeFormattingElements.size();
297 firstUnopenElementIndex = index;
301 void HTMLConstructionSite::reconstructTheActiveFormattingElements()
303 unsigned firstUnopenElementIndex;
304 if (!indexOfFirstUnopenFormattingElement(firstUnopenElementIndex))
307 unsigned unopenEntryIndex = firstUnopenElementIndex;
308 ASSERT(unopenEntryIndex < m_activeFormattingElements.size());
309 for (; unopenEntryIndex < m_activeFormattingElements.size(); ++unopenEntryIndex) {
310 HTMLFormattingElementList::Entry& unopenedEntry = m_activeFormattingElements.at(unopenEntryIndex);
311 // FIXME: We're supposed to save the original token in the entry.
312 AtomicHTMLToken fakeToken(HTMLToken::StartTag, unopenedEntry.element()->localName());
313 insertHTMLElement(fakeToken);
314 unopenedEntry.replaceElement(currentElement());
318 void HTMLConstructionSite::generateImpliedEndTagsWithExclusion(const AtomicString& tagName)
320 while (hasImpliedEndTag(currentElement()) && !currentElement()->hasLocalName(tagName))
321 m_openElements.pop();
324 void HTMLConstructionSite::generateImpliedEndTags()
326 while (hasImpliedEndTag(currentElement()))
327 m_openElements.pop();
330 void HTMLConstructionSite::findFosterSite(AttachmentSite& site)
332 HTMLElementStack::ElementRecord* lastTableElementRecord = m_openElements.topmost(tableTag.localName());
333 if (lastTableElementRecord) {
334 Element* lastTableElement = lastTableElementRecord->element();
335 if (Node* parent = lastTableElement->parent()) {
336 site.parent = parent;
337 site.nextChild = lastTableElement;
340 site.parent = lastTableElementRecord->next()->element();
345 site.parent = m_openElements.bottom(); // <html> element
349 void HTMLConstructionSite::fosterParent(Node* node)
352 findFosterSite(site);
353 attachAtSite(site, node);