2 This file is part of the KDE libraries
4 Copyright (C) 1997 Martin Jones (mjones@kde.org)
5 (C) 1997 Torben Weis (weis@kde.org)
6 (C) 1999,2001 Lars Knoll (knoll@kde.org)
7 (C) 2000,2001 Dirk Mueller (mueller@kde.org)
8 Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Library General Public License
21 along with this library; see the file COPYING.LIB. If not, write to
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.
27 #include "HTMLParser.h"
29 #include "CSSPropertyNames.h"
30 #include "CSSValueKeywords.h"
32 #include "DocumentFragment.h"
34 #include "HTMLBodyElement.h"
35 #include "HTMLCanvasElement.h"
36 #include "HTMLDivElement.h"
37 #include "HTMLDListElement.h"
38 #include "HTMLElementFactory.h"
39 #include "HTMLFormElement.h"
40 #include "HTMLHeadElement.h"
41 #include "HTMLHRElement.h"
42 #include "HTMLHtmlElement.h"
43 #include "HTMLIsIndexElement.h"
44 #include "HTMLMapElement.h"
45 #include "HTMLNames.h"
46 #include "HTMLTableCellElement.h"
47 #include "HTMLTableRowElement.h"
48 #include "HTMLTableSectionElement.h"
49 #include "HTMLTokenizer.h"
50 #include "LocalizedStrings.h"
55 using namespace HTMLNames;
57 const UChar nonBreakingSpace = 0xa0;
66 HTMLStackElem(const AtomicString& t, int lvl, Node* n, bool r, HTMLStackElem* nx)
69 , strayTableContent(false)
84 bool strayTableContent;
93 * The parser parses tokenized input into the document, building up the
94 * document tree. If the document is wellformed, parsing it is
96 * Unfortunately, people can't write wellformed HTML documents, so the parser
97 * has to be tolerant about errors.
99 * We have to take care of the following error conditions:
100 * 1. The element being added is explicitly forbidden inside some outer tag.
101 * In this case we should close all tags up to the one, which forbids
102 * the element, and add it afterwards.
103 * 2. We are not allowed to add the element directly. It could be, that
104 * the person writing the document forgot some tag inbetween (or that the
105 * tag inbetween is optional...) This could be the case with the following
106 * tags: HTML HEAD BODY TBODY TR TD LI (did I forget any?)
107 * 3. We wan't to add a block element inside to an inline element. Close all
108 * inline elements up to the next higher block element.
109 * 4. If this doesn't help close elements, until we are allowed to add the
110 * element or ignore the tag.
113 HTMLParser::HTMLParser(Document* doc)
116 , didRefCurrent(false)
123 HTMLParser::HTMLParser(DocumentFragment* frag)
124 : document(frag->document())
126 , didRefCurrent(false)
135 HTMLParser::~HTMLParser()
142 void HTMLParser::reset()
149 haveFrameSet = false;
152 inStrayTableContent = 0;
160 discard_until = nullAtom;
163 void HTMLParser::setCurrent(Node* newCurrent)
165 bool didRefNewCurrent = newCurrent && newCurrent != doc();
166 if (didRefNewCurrent)
170 current = newCurrent;
171 didRefCurrent = didRefNewCurrent;
174 PassRefPtr<Node> HTMLParser::parseToken(Token *t)
176 if (!discard_until.isNull()) {
177 if (t->tagName == discard_until && !t->beginTag)
178 discard_until = nullAtom;
180 // do not skip </iframe>
181 if (!discard_until.isNull() || (current->localName() != t->tagName))
185 // Apparently some sites use </br> instead of <br>. Be compatible with IE and Firefox and treat this like <br>.
186 if (t->isCloseTag(brTag) && doc()->inCompatMode())
194 // ignore spaces, if we're not inside a paragraph or other inline code
195 if (t->tagName == textAtom && t->text) {
196 if (inBody && !skipMode() && current->localName() != styleTag && current->localName() != titleTag &&
197 current->localName() != scriptTag && !t->text->containsOnlyWhitespace())
201 RefPtr<Node> n = getNode(t);
202 // just to be sure, and to catch currently unimplemented stuff
207 if (n->isHTMLElement()) {
208 HTMLElement* e = static_cast<HTMLElement*>(n.get());
209 e->setAttributeMap(t->attrs.get());
211 // take care of optional close tags
212 if (e->endTagRequirement() == TagStatusOptional)
213 popBlock(t->tagName);
215 if (isHeaderTag(t->tagName))
216 // Do not allow two header tags to be nested if the intervening tags are inlines.
217 popNestedHeaderTag();
220 if (!insertNode(n.get(), t->flat)) {
221 // we couldn't insert the node
223 if (n->isElementNode()) {
224 Element* e = static_cast<Element*>(n.get());
225 e->setAttributeMap(0);
242 static bool isTableSection(Node* n)
244 return n->hasTagName(tbodyTag) || n->hasTagName(tfootTag) || n->hasTagName(theadTag);
247 static bool isTablePart(Node* n)
249 return n->hasTagName(trTag) || n->hasTagName(tdTag) || n->hasTagName(thTag) ||
253 static bool isTableRelated(Node* n)
255 return n->hasTagName(tableTag) || isTablePart(n);
258 bool HTMLParser::insertNode(Node *n, bool flat)
260 RefPtr<Node> protectNode(n);
262 const AtomicString& localName = n->localName();
263 int tagPriority = n->isHTMLElement() ? static_cast<HTMLElement*>(n)->tagPriority() : 0;
265 // let's be stupid and just try to insert it.
266 // this should work if the document is well-formed
267 Node* newNode = current->addChild(n);
269 return handleError(n, flat, localName, tagPriority); // Try to handle the error.
271 // don't push elements without end tags (e.g., <img>) on the stack
272 bool parentAttached = current->attached();
273 if (tagPriority > 0 && !flat) {
274 pushBlock(localName, tagPriority);
275 if (newNode == current)
278 // The pushBlock function transfers ownership of current to the block stack
279 // so we're guaranteed that didRefCurrent is false. The code below is an
280 // optimized version of setCurrent that takes advantage of that fact and also
281 // assumes that newNode is neither 0 nor a pointer to the document.
282 ASSERT(!didRefCurrent);
285 didRefCurrent = true;
287 if (parentAttached && !n->attached() && !m_fragment)
290 if (parentAttached && !n->attached() && !m_fragment)
298 bool HTMLParser::handleError(Node* n, bool flat, const AtomicString& localName, int tagPriority)
300 // Error handling code. This is just ad hoc handling of specific parent/child combinations.
302 bool handled = false;
304 // 1. Check out the element's tag name to decide how to deal with errors.
305 if (n->isTextNode()) {
306 if (current->hasTagName(selectTag))
308 } else if (n->isHTMLElement()) {
309 HTMLElement* h = static_cast<HTMLElement*>(n);
310 if (h->hasLocalName(trTag) || h->hasLocalName(thTag) || h->hasLocalName(tdTag)) {
311 if (inStrayTableContent && !isTableRelated(current)) {
312 // pop out to the nearest enclosing table-related tag.
313 while (blockStack && !isTableRelated(current))
315 return insertNode(n);
317 } else if (h->hasLocalName(headTag)) {
318 if (!current->isDocumentNode() && !current->hasTagName(htmlTag))
320 } else if (h->hasLocalName(metaTag) || h->hasLocalName(linkTag) || h->hasLocalName(baseTag)) {
324 if (head->addChild(n)) {
325 if (!n->attached() && !m_fragment)
331 } else if (h->hasLocalName(htmlTag)) {
332 if (!current->isDocumentNode() ) {
333 if (doc()->firstChild()->hasTagName(htmlTag)) {
334 // we have another <HTML> element.... apply attributes to existing one
335 // make sure we don't overwrite already existing attributes
336 NamedAttrMap *map = static_cast<Element*>(n)->attributes(true);
337 Element *existingHTML = static_cast<Element*>(doc()->firstChild());
338 NamedAttrMap *bmap = existingHTML->attributes(false);
339 for (unsigned l = 0; map && l < map->length(); ++l) {
340 Attribute* it = map->attributeItem(l);
341 if (!bmap->getAttributeItem(it->name()))
342 existingHTML->setAttribute(it->name(), it->value());
347 } else if (h->hasLocalName(titleTag) || h->hasLocalName(styleTag)) {
351 Node* newNode = head->addChild(n);
353 setSkipMode(h->tagQName());
356 pushBlock(localName, tagPriority);
358 if (!n->attached() && !m_fragment)
363 setSkipMode(h->tagQName());
366 } else if (h->hasLocalName(bodyTag)) {
367 if (inBody && doc()->body()) {
368 // we have another <BODY> element.... apply attributes to existing one
369 // make sure we don't overwrite already existing attributes
370 // some sites use <body bgcolor=rightcolor>...<body bgcolor=wrongcolor>
371 NamedAttrMap *map = static_cast<Element*>(n)->attributes(true);
372 Element *existingBody = doc()->body();
373 NamedAttrMap *bmap = existingBody->attributes(false);
374 for (unsigned l = 0; map && l < map->length(); ++l) {
375 Attribute* it = map->attributeItem(l);
376 if (!bmap->getAttributeItem(it->name()))
377 existingBody->setAttribute(it->name(), it->value());
381 else if (!current->isDocumentNode())
383 } else if (h->hasLocalName(inputTag)) {
384 if (equalIgnoringCase(h->getAttribute(typeAttr), "hidden") && form) {
386 if (!n->attached() && !m_fragment)
390 } else if (h->hasLocalName(ddTag) || h->hasLocalName(dtTag)) {
391 e = new HTMLDListElement(document);
396 } else if (h->hasLocalName(areaTag)) {
399 if (!n->attached() && !m_fragment)
405 } else if (h->hasLocalName(captionTag)) {
406 if (isTablePart(current)) {
407 Node* tsection = current;
408 if (current->hasTagName(trTag))
409 tsection = current->parent();
410 else if (current->hasTagName(tdTag) || current->hasTagName(thTag))
411 tsection = current->parent()->parent();
412 Node* table = tsection->parent();
413 ExceptionCode ec = 0;
414 table->insertBefore(n, tsection, ec);
415 pushBlock(localName, tagPriority);
417 inStrayTableContent++;
418 blockStack->strayTableContent = true;
421 } else if (h->hasLocalName(theadTag) || h->hasLocalName(tbodyTag) ||
422 h->hasLocalName(tfootTag) || h->hasLocalName(colgroupTag)) {
423 if (isTableRelated(current)) {
424 while (blockStack && isTablePart(current))
426 return insertNode(n);
431 // 2. Next we examine our currently active element to do some further error handling.
432 if (current->isHTMLElement()) {
433 HTMLElement* h = static_cast<HTMLElement*>(current);
434 const AtomicString& currentTagName = current->localName();
435 if (h->hasLocalName(htmlTag)) {
436 HTMLElement* elt = n->isHTMLElement() ? static_cast<HTMLElement*>(n) : 0;
437 if (elt && (elt->hasLocalName(scriptTag) || elt->hasLocalName(styleTag) ||
438 elt->hasLocalName(metaTag) || elt->hasLocalName(linkTag) ||
439 elt->hasLocalName(objectTag) || elt->hasLocalName(embedTag) ||
440 elt->hasLocalName(titleTag) || elt->hasLocalName(isindexTag) ||
441 elt->hasLocalName(baseTag))) {
443 head = new HTMLHeadElement(document);
449 if (n->isTextNode()) {
450 Text *t = static_cast<Text *>(n);
451 if (t->containsOnlyWhitespace())
455 e = new HTMLBodyElement(document);
461 } else if (h->hasLocalName(headTag)) {
462 if (n->hasTagName(htmlTag))
465 // This means the body starts here...
467 popBlock(currentTagName);
468 e = new HTMLBodyElement(document);
474 } else if (h->hasLocalName(addressTag) || h->hasLocalName(dlTag) || h->hasLocalName(dtTag)
475 || h->hasLocalName(fontTag) || h->hasLocalName(styleTag) || h->hasLocalName(titleTag)) {
476 popBlock(currentTagName);
478 } else if (h->hasLocalName(captionTag)) {
479 // Illegal content in a caption. Close the caption and try again.
480 popBlock(currentTagName);
482 return insertNode(n, flat);
483 } else if (h->hasLocalName(tableTag) || h->hasLocalName(trTag) || isTableSection(h)) {
484 if (n->hasTagName(tableTag)) {
485 popBlock(localName); // end the table
486 handled = true; // ...and start a new one
488 bool possiblyMoveStrayContent = true;
489 ExceptionCode ec = 0;
490 if (n->isTextNode()) {
491 Text *t = static_cast<Text *>(n);
492 if (t->containsOnlyWhitespace())
494 StringImpl *i = t->string();
495 unsigned int pos = 0;
496 while (pos < i->length() && ((*i)[pos] == ' ' || (*i)[pos] == nonBreakingSpace))
498 if (pos == i->length())
499 possiblyMoveStrayContent = false;
501 if (possiblyMoveStrayContent) {
502 Node *node = current;
503 Node *parent = node->parentNode();
504 // A script may have removed the current node's parent from the DOM
505 // http://bugzilla.opendarwin.org/show_bug.cgi?id=7137
506 // FIXME: we should do real recovery here and re-parent with the correct node.
509 Node *grandparent = parent->parentNode();
511 if (n->isTextNode() ||
512 (h->hasLocalName(trTag) &&
513 isTableSection(parent) && grandparent->hasTagName(tableTag)) ||
514 ((!n->hasTagName(tdTag) && !n->hasTagName(thTag) &&
515 !n->hasTagName(formTag) && !n->hasTagName(scriptTag)) && isTableSection(node) &&
516 parent->hasTagName(tableTag))) {
517 node = (node->hasTagName(tableTag)) ? node :
518 ((node->hasTagName(trTag)) ? grandparent : parent);
519 Node *parent = node->parentNode();
522 parent->insertBefore(n, node, ec);
524 if (n->isHTMLElement() && tagPriority > 0 &&
525 !flat && static_cast<HTMLElement*>(n)->endTagRequirement() != TagStatusForbidden)
527 pushBlock(localName, tagPriority);
529 inStrayTableContent++;
530 blockStack->strayTableContent = true;
537 if (current->hasTagName(trTag))
538 e = new HTMLTableCellElement(tdTag, document);
539 else if (current->hasTagName(tableTag))
540 e = new HTMLTableSectionElement(tbodyTag, document, true); // implicit
542 e = new HTMLTableRowElement(document);
549 } else if (h->hasLocalName(objectTag)) {
550 setSkipMode(objectTag);
552 } else if (h->hasLocalName(ulTag) || h->hasLocalName(olTag) ||
553 h->hasLocalName(dirTag) || h->hasLocalName(menuTag)) {
554 e = new HTMLDivElement(document);
557 } else if (h->hasLocalName(selectTag)) {
560 } else if (h->hasLocalName(pTag) || isHeaderTag(currentTagName)) {
562 popBlock(currentTagName);
565 } else if (h->hasLocalName(optionTag) || h->hasLocalName(optgroupTag)) {
566 if (localName == optgroupTag) {
567 popBlock(currentTagName);
569 } else if (localName == selectTag) {
570 // IE treats a nested select as </select>. Let's do the same
573 } else if (h->hasLocalName(colgroupTag)) {
574 if (!n->isTextNode()) {
575 popBlock(currentTagName);
578 } else if (!h->hasLocalName(bodyTag)) {
579 if (isInline(current)) {
584 } else if (current->isDocumentNode()) {
585 if (current->firstChild() == 0 || !current->firstChild()->isHTMLElement()) {
586 e = new HTMLHtmlElement(document);
592 // 3. If we couldn't handle the error, just return false and attempt to error-correct again.
595 return insertNode(n);
598 typedef bool (HTMLParser::*CreateErrorCheckFunc)(Token* t, RefPtr<Node>&);
599 typedef HashMap<AtomicStringImpl*, CreateErrorCheckFunc> FunctionMap;
601 bool HTMLParser::textCreateErrorCheck(Token* t, RefPtr<Node>& result)
603 result = new Text(document, t->text.get());
607 bool HTMLParser::commentCreateErrorCheck(Token* t, RefPtr<Node>& result)
609 result = new Comment(document, t->text.get());
613 bool HTMLParser::headCreateErrorCheck(Token* t, RefPtr<Node>& result)
615 if (!head || current->localName() == htmlTag) {
616 head = new HTMLHeadElement(document);
622 bool HTMLParser::bodyCreateErrorCheck(Token* t, RefPtr<Node>& result)
624 // body no longer allowed if we have a frameset
632 bool HTMLParser::framesetCreateErrorCheck(Token* t, RefPtr<Node>& result)
635 if (inBody && !haveFrameSet && !haveContent) {
637 // ### actually for IE document.body returns the now hidden "body" element
638 // we can't implement that behaviour now because it could cause too many
639 // regressions and the headaches are not worth the work as long as there is
640 // no site actually relying on that detail (Dirk)
642 doc()->body()->setAttribute(styleAttr, "display:none");
645 if ((haveContent || haveFrameSet) && current->localName() == htmlTag)
652 bool HTMLParser::iframeCreateErrorCheck(Token* t, RefPtr<Node>& result)
654 // a bit of a special case, since the frame is inlined
655 setSkipMode(iframeTag);
659 bool HTMLParser::formCreateErrorCheck(Token* t, RefPtr<Node>& result)
661 // Only create a new form if we're not already inside one.
662 // This is consistent with other browsers' behavior.
664 form = new HTMLFormElement(document);
670 bool HTMLParser::isindexCreateErrorCheck(Token* t, RefPtr<Node>& result)
672 Node *n = handleIsindex(t);
682 bool HTMLParser::selectCreateErrorCheck(Token* t, RefPtr<Node>& result)
688 bool HTMLParser::ddCreateErrorCheck(Token* t, RefPtr<Node>& result)
695 bool HTMLParser::dtCreateErrorCheck(Token* t, RefPtr<Node>& result)
702 bool HTMLParser::nestedCreateErrorCheck(Token* t, RefPtr<Node>& result)
704 popBlock(t->tagName);
708 bool HTMLParser::nestedStyleCreateErrorCheck(Token* t, RefPtr<Node>& result)
710 return allowNestedRedundantTag(t->tagName);
713 bool HTMLParser::tableCellCreateErrorCheck(Token* t, RefPtr<Node>& result)
720 bool HTMLParser::tableSectionCreateErrorCheck(Token* t, RefPtr<Node>& result)
728 bool HTMLParser::noembedCreateErrorCheck(Token* t, RefPtr<Node>& result)
730 setSkipMode(noembedTag);
734 bool HTMLParser::noframesCreateErrorCheck(Token* t, RefPtr<Node>& result)
736 setSkipMode(noframesTag);
740 bool HTMLParser::noscriptCreateErrorCheck(Token* t, RefPtr<Node>& result)
742 if (!m_fragment && document->frame() && document->frame()->jScriptEnabled())
743 setSkipMode(noscriptTag);
747 bool HTMLParser::mapCreateErrorCheck(Token* t, RefPtr<Node>& result)
749 map = new HTMLMapElement(document);
754 bool HTMLParser::canvasCreateErrorCheck(Token* t, RefPtr<Node>& result)
756 if (!m_fragment && document->frame() && document->frame()->jScriptEnabled())
757 setSkipMode(canvasTag);
761 PassRefPtr<Node> HTMLParser::getNode(Token* t)
763 // Init our error handling table.
764 static FunctionMap gFunctionMap;
765 if (gFunctionMap.isEmpty()) {
766 gFunctionMap.set(aTag.localName().impl(), &HTMLParser::nestedCreateErrorCheck);
767 gFunctionMap.set(bTag.localName().impl(), &HTMLParser::nestedStyleCreateErrorCheck);
768 gFunctionMap.set(bigTag.localName().impl(), &HTMLParser::nestedStyleCreateErrorCheck);
769 gFunctionMap.set(bodyTag.localName().impl(), &HTMLParser::bodyCreateErrorCheck);
770 gFunctionMap.set(buttonTag.localName().impl(), &HTMLParser::nestedCreateErrorCheck);
771 gFunctionMap.set(canvasTag.localName().impl(), &HTMLParser::canvasCreateErrorCheck);
772 gFunctionMap.set(commentAtom.impl(), &HTMLParser::commentCreateErrorCheck);
773 gFunctionMap.set(ddTag.localName().impl(), &HTMLParser::ddCreateErrorCheck);
774 gFunctionMap.set(dtTag.localName().impl(), &HTMLParser::dtCreateErrorCheck);
775 gFunctionMap.set(formTag.localName().impl(), &HTMLParser::formCreateErrorCheck);
776 gFunctionMap.set(framesetTag.localName().impl(), &HTMLParser::framesetCreateErrorCheck);
777 gFunctionMap.set(headTag.localName().impl(), &HTMLParser::headCreateErrorCheck);
778 gFunctionMap.set(iTag.localName().impl(), &HTMLParser::nestedStyleCreateErrorCheck);
779 gFunctionMap.set(iframeTag.localName().impl(), &HTMLParser::iframeCreateErrorCheck);
780 gFunctionMap.set(isindexTag.localName().impl(), &HTMLParser::isindexCreateErrorCheck);
781 gFunctionMap.set(liTag.localName().impl(), &HTMLParser::nestedCreateErrorCheck);
782 gFunctionMap.set(mapTag.localName().impl(), &HTMLParser::mapCreateErrorCheck);
783 gFunctionMap.set(nobrTag.localName().impl(), &HTMLParser::nestedCreateErrorCheck);
784 gFunctionMap.set(noembedTag.localName().impl(), &HTMLParser::noembedCreateErrorCheck);
785 gFunctionMap.set(noframesTag.localName().impl(), &HTMLParser::noframesCreateErrorCheck);
786 gFunctionMap.set(noscriptTag.localName().impl(), &HTMLParser::noscriptCreateErrorCheck);
787 gFunctionMap.set(sTag.localName().impl(), &HTMLParser::nestedStyleCreateErrorCheck);
788 gFunctionMap.set(selectTag.localName().impl(), &HTMLParser::selectCreateErrorCheck);
789 gFunctionMap.set(smallTag.localName().impl(), &HTMLParser::nestedStyleCreateErrorCheck);
790 gFunctionMap.set(strikeTag.localName().impl(), &HTMLParser::nestedStyleCreateErrorCheck);
791 gFunctionMap.set(tbodyTag.localName().impl(), &HTMLParser::tableSectionCreateErrorCheck);
792 gFunctionMap.set(tdTag.localName().impl(), &HTMLParser::tableCellCreateErrorCheck);
793 gFunctionMap.set(textAtom.impl(), &HTMLParser::textCreateErrorCheck);
794 gFunctionMap.set(tfootTag.localName().impl(), &HTMLParser::tableSectionCreateErrorCheck);
795 gFunctionMap.set(thTag.localName().impl(), &HTMLParser::tableCellCreateErrorCheck);
796 gFunctionMap.set(theadTag.localName().impl(), &HTMLParser::tableSectionCreateErrorCheck);
797 gFunctionMap.set(trTag.localName().impl(), &HTMLParser::nestedCreateErrorCheck);
798 gFunctionMap.set(ttTag.localName().impl(), &HTMLParser::nestedStyleCreateErrorCheck);
799 gFunctionMap.set(uTag.localName().impl(), &HTMLParser::nestedStyleCreateErrorCheck);
800 gFunctionMap.set(wbrTag.localName().impl(), &HTMLParser::nestedCreateErrorCheck);
805 if (CreateErrorCheckFunc errorCheckFunc = gFunctionMap.get(t->tagName.impl()))
806 proceed = (this->*errorCheckFunc)(t, result);
808 result = HTMLElementFactory::createHTMLElement(t->tagName, doc(), form);
809 return result.release();
812 #define MAX_REDUNDANT 20
814 bool HTMLParser::allowNestedRedundantTag(const AtomicString& _tagName)
816 // www.liceo.edu.mx is an example of a site that achieves a level of nesting of
817 // about 1500 tags, all from a bunch of <b>s. We will only allow at most 20
818 // nested tags of the same type before just ignoring them all together.
820 for (HTMLStackElem* curr = blockStack;
821 i < MAX_REDUNDANT && curr && curr->tagName == _tagName;
822 curr = curr->next, i++);
823 return i != MAX_REDUNDANT;
826 void HTMLParser::processCloseTag(Token *t)
828 // Support for really broken html.
829 // we never close the body tag, since some stupid web pages close it before the actual end of the doc.
830 // let's rely on the end() call to close things.
831 if (t->tagName == htmlTag || t->tagName == bodyTag)
834 if (t->tagName == formTag)
836 else if (t->tagName == mapTag)
838 else if (t->tagName == selectTag)
841 HTMLStackElem* oldElem = blockStack;
842 popBlock(t->tagName);
843 if (oldElem == blockStack && t->tagName == pTag) {
844 // We encountered a stray </p>. Amazingly Gecko, WinIE, and MacIE all treat
845 // this as a valid break, i.e., <p></p>. So go ahead and make the empty
849 popBlock(t->tagName);
853 bool HTMLParser::isHeaderTag(const AtomicString& tagName)
855 static HashSet<AtomicStringImpl*> headerTags;
856 if (headerTags.isEmpty()) {
857 headerTags.add(h1Tag.localName().impl());
858 headerTags.add(h2Tag.localName().impl());
859 headerTags.add(h3Tag.localName().impl());
860 headerTags.add(h4Tag.localName().impl());
861 headerTags.add(h5Tag.localName().impl());
862 headerTags.add(h6Tag.localName().impl());
865 return headerTags.contains(tagName.impl());
868 void HTMLParser::popNestedHeaderTag()
870 // This function only cares about checking for nested headers that have only inlines in between them.
871 Node* currNode = current;
872 for (HTMLStackElem* curr = blockStack; curr; curr = curr->next) {
873 if (isHeaderTag(curr->tagName)) {
874 popBlock(curr->tagName);
877 if (currNode && !isInline(currNode))
879 currNode = curr->node;
883 bool HTMLParser::isInline(Node* node) const
885 if (node->isTextNode())
888 if (node->isHTMLElement()) {
889 HTMLElement* e = static_cast<HTMLElement*>(node);
890 if (e->hasLocalName(aTag) || e->hasLocalName(fontTag) || e->hasLocalName(ttTag) ||
891 e->hasLocalName(uTag) || e->hasLocalName(bTag) || e->hasLocalName(iTag) ||
892 e->hasLocalName(sTag) || e->hasLocalName(strikeTag) || e->hasLocalName(bigTag) ||
893 e->hasLocalName(smallTag) || e->hasLocalName(emTag) || e->hasLocalName(strongTag) ||
894 e->hasLocalName(dfnTag) || e->hasLocalName(codeTag) || e->hasLocalName(sampTag) ||
895 e->hasLocalName(kbdTag) || e->hasLocalName(varTag) || e->hasLocalName(citeTag) ||
896 e->hasLocalName(abbrTag) || e->hasLocalName(acronymTag) || e->hasLocalName(subTag) ||
897 e->hasLocalName(supTag) || e->hasLocalName(spanTag) || e->hasLocalName(nobrTag) ||
898 e->hasLocalName(wbrTag) || e->hasLocalName(noframesTag) || e->hasLocalName(nolayerTag) ||
899 e->hasLocalName(noembedTag) || (e->hasLocalName(noscriptTag) && !m_fragment && document->frame() && document->frame()->jScriptEnabled()))
906 bool HTMLParser::isResidualStyleTag(const AtomicString& tagName)
908 static HashSet<AtomicStringImpl*> residualStyleTags;
909 if (residualStyleTags.isEmpty()) {
910 residualStyleTags.add(aTag.localName().impl());
911 residualStyleTags.add(fontTag.localName().impl());
912 residualStyleTags.add(ttTag.localName().impl());
913 residualStyleTags.add(uTag.localName().impl());
914 residualStyleTags.add(bTag.localName().impl());
915 residualStyleTags.add(iTag.localName().impl());
916 residualStyleTags.add(sTag.localName().impl());
917 residualStyleTags.add(strikeTag.localName().impl());
918 residualStyleTags.add(bigTag.localName().impl());
919 residualStyleTags.add(smallTag.localName().impl());
920 residualStyleTags.add(emTag.localName().impl());
921 residualStyleTags.add(strongTag.localName().impl());
922 residualStyleTags.add(dfnTag.localName().impl());
923 residualStyleTags.add(codeTag.localName().impl());
924 residualStyleTags.add(sampTag.localName().impl());
925 residualStyleTags.add(kbdTag.localName().impl());
926 residualStyleTags.add(varTag.localName().impl());
927 residualStyleTags.add(nobrTag.localName().impl());
928 residualStyleTags.add(wbrTag.localName().impl());
931 return residualStyleTags.contains(tagName.impl());
934 bool HTMLParser::isAffectedByResidualStyle(const AtomicString& tagName)
936 if (isResidualStyleTag(tagName))
939 static HashSet<AtomicStringImpl*> affectedBlockTags;
940 if (affectedBlockTags.isEmpty()) {
941 affectedBlockTags.add(addressTag.localName().impl());
942 affectedBlockTags.add(blockquoteTag.localName().impl());
943 affectedBlockTags.add(centerTag.localName().impl());
944 affectedBlockTags.add(ddTag.localName().impl());
945 affectedBlockTags.add(divTag.localName().impl());
946 affectedBlockTags.add(dlTag.localName().impl());
947 affectedBlockTags.add(dtTag.localName().impl());
948 affectedBlockTags.add(formTag.localName().impl());
949 affectedBlockTags.add(h1Tag.localName().impl());
950 affectedBlockTags.add(h2Tag.localName().impl());
951 affectedBlockTags.add(h3Tag.localName().impl());
952 affectedBlockTags.add(h4Tag.localName().impl());
953 affectedBlockTags.add(h5Tag.localName().impl());
954 affectedBlockTags.add(h6Tag.localName().impl());
955 affectedBlockTags.add(liTag.localName().impl());
956 affectedBlockTags.add(listingTag.localName().impl());
957 affectedBlockTags.add(olTag.localName().impl());
958 affectedBlockTags.add(pTag.localName().impl());
959 affectedBlockTags.add(preTag.localName().impl());
960 affectedBlockTags.add(ulTag.localName().impl());
963 return affectedBlockTags.contains(tagName.impl());
966 void HTMLParser::handleResidualStyleCloseTagAcrossBlocks(HTMLStackElem* elem)
968 // Find the element that crosses over to a higher level. For now, if there is more than
969 // one, we will just give up and not attempt any sort of correction. It's highly unlikely that
970 // there will be more than one, since <p> tags aren't allowed to be nested.
971 ExceptionCode ec = 0;
972 HTMLStackElem* curr = blockStack;
973 HTMLStackElem* maxElem = 0;
974 HTMLStackElem* prev = 0;
975 HTMLStackElem* prevMaxElem = 0;
976 while (curr && curr != elem) {
977 if (curr->level > elem->level) {
988 if (!curr || !maxElem || !isAffectedByResidualStyle(maxElem->tagName)) return;
990 Node* residualElem = prev->node;
991 Node* blockElem = prevMaxElem ? prevMaxElem->node : current;
992 Node* parentElem = elem->node;
994 // Check to see if the reparenting that is going to occur is allowed according to the DOM.
995 // FIXME: We should either always allow it or perform an additional fixup instead of
996 // just bailing here.
997 // Example: <p><font><center>blah</font></center></p> isn't doing a fixup right now.
998 if (!parentElem->childAllowed(blockElem))
1001 if (maxElem->node->parentNode() != elem->node) {
1002 // Walk the stack and remove any elements that aren't residual style tags. These
1003 // are basically just being closed up. Example:
1004 // <font><span>Moo<p>Goo</font></p>.
1005 // In the above example, the <span> doesn't need to be reopened. It can just close.
1006 HTMLStackElem* currElem = maxElem->next;
1007 HTMLStackElem* prevElem = maxElem;
1008 while (currElem != elem) {
1009 HTMLStackElem* nextElem = currElem->next;
1010 if (!isResidualStyleTag(currElem->tagName)) {
1011 prevElem->next = nextElem;
1012 prevElem->derefNode();
1013 prevElem->node = currElem->node;
1014 prevElem->didRefNode = currElem->didRefNode;
1018 prevElem = currElem;
1019 currElem = nextElem;
1022 // We have to reopen residual tags in between maxElem and elem. An example of this case is:
1023 // <font><i>Moo<p>Foo</font>.
1024 // In this case, we need to transform the part before the <p> into:
1025 // <font><i>Moo</i></font><i>
1026 // so that the <i> will remain open. This involves the modification of elements
1027 // in the block stack.
1028 // This will also affect how we ultimately reparent the block, since we want it to end up
1029 // under the reopened residual tags (e.g., the <i> in the above example.)
1030 RefPtr<Node> prevNode = 0;
1032 while (currElem->node != residualElem) {
1033 if (isResidualStyleTag(currElem->node->localName())) {
1034 // Create a clone of this element.
1035 // We call release to get a raw pointer since we plan to hand over ownership to currElem.
1036 Node* currNode = currElem->node->cloneNode(false).release();
1038 // Change the stack element's node to point to the clone.
1039 // The stack element adopts the reference we obtained above by calling release().
1040 currElem->derefNode();
1041 currElem->node = currNode;
1042 currElem->didRefNode = true;
1044 // Attach the previous node as a child of this new node.
1046 currNode->appendChild(prevNode, ec);
1047 else // The new parent for the block element is going to be the innermost clone.
1048 parentElem = currNode;
1050 prevNode = currNode;
1053 currElem = currElem->next;
1056 // Now append the chain of new residual style elements if one exists.
1058 elem->node->appendChild(prevNode, ec);
1061 // Check if the block is still in the tree. If it isn't, then we don't
1062 // want to remove it from its parent (that would crash) or insert it into
1063 // a new parent later. See http://bugzilla.opendarwin.org/show_bug.cgi?id=6778
1064 bool isBlockStillInTree = blockElem->parentNode();
1066 // We need to make a clone of |residualElem| and place it just inside |blockElem|.
1067 // All content of |blockElem| is reparented to be under this clone. We then
1068 // reparent |blockElem| using real DOM calls so that attachment/detachment will
1069 // be performed to fix up the rendering tree.
1070 // So for this example: <b>...<p>Foo</b>Goo</p>
1071 // The end result will be: <b>...</b><p><b>Foo</b>Goo</p>
1073 // Step 1: Remove |blockElem| from its parent, doing a batch detach of all the kids.
1075 form->setPreserveAcrossRemove(true);
1076 if (isBlockStillInTree)
1077 blockElem->parentNode()->removeChild(blockElem, ec);
1079 // Step 2: Clone |residualElem|.
1080 RefPtr<Node> newNode = residualElem->cloneNode(false); // Shallow clone. We don't pick up the same kids.
1082 // Step 3: Place |blockElem|'s children under |newNode|. Remove all of the children of |blockElem|
1083 // before we've put |newElem| into the document. That way we'll only do one attachment of all
1084 // the new content (instead of a bunch of individual attachments).
1085 Node* currNode = blockElem->firstChild();
1087 Node* nextNode = currNode->nextSibling();
1088 newNode->appendChild(currNode, ec);
1089 currNode = nextNode;
1092 // Step 4: Place |newNode| under |blockElem|. |blockElem| is still out of the document, so no
1093 // attachment can occur yet.
1094 blockElem->appendChild(newNode.release(), ec);
1096 // Step 5: Reparent |blockElem|. Now the full attachment of the fixed up tree takes place.
1097 if (isBlockStillInTree)
1098 parentElem->appendChild(blockElem, ec);
1100 // Step 6: Elide |elem|, since it is effectively no longer open. Also update
1101 // the node associated with the previous stack element so that when it gets popped,
1102 // it doesn't make the residual element the next current node.
1103 HTMLStackElem* currElem = maxElem;
1104 HTMLStackElem* prevElem = 0;
1105 while (currElem != elem) {
1106 prevElem = currElem;
1107 currElem = currElem->next;
1109 prevElem->next = elem->next;
1110 prevElem->derefNode();
1111 prevElem->node = elem->node;
1112 prevElem->didRefNode = elem->didRefNode;
1115 // Step 7: Reopen intermediate inlines, e.g., <b><p><i>Foo</b>Goo</p>.
1116 // In the above example, Goo should stay italic.
1118 HTMLStackElem* residualStyleStack = 0;
1119 while (curr && curr != maxElem) {
1120 // We will actually schedule this tag for reopening
1121 // after we complete the close of this entire block.
1122 if (isResidualStyleTag(curr->tagName))
1123 // We've overloaded the use of stack elements and are just reusing the
1124 // struct with a slightly different meaning to the variables. Instead of chaining
1125 // from innermost to outermost, we build up a list of all the tags we need to reopen
1126 // from the outermost to the innermost, i.e., residualStyleStack will end up pointing
1127 // to the outermost tag we need to reopen.
1128 // We also set curr->node to be the actual element that corresponds to the ID stored in
1129 // curr->id rather than the node that you should pop to when the element gets pulled off
1131 moveOneBlockToStack(residualStyleStack);
1138 reopenResidualStyleTags(residualStyleStack, 0); // FIXME: Deal with stray table content some day
1139 // if it becomes necessary to do so.
1142 form->setPreserveAcrossRemove(false);
1145 void HTMLParser::reopenResidualStyleTags(HTMLStackElem* elem, Node* malformedTableParent)
1147 // Loop for each tag that needs to be reopened.
1149 // Create a shallow clone of the DOM node for this element.
1150 RefPtr<Node> newNode = elem->node->cloneNode(false);
1152 // Append the new node. In the malformed table case, we need to insert before the table,
1153 // which will be the last child.
1154 ExceptionCode ec = 0;
1155 if (malformedTableParent)
1156 malformedTableParent->insertBefore(newNode, malformedTableParent->lastChild(), ec);
1158 current->appendChild(newNode, ec);
1159 // FIXME: Is it really OK to ignore the exceptions here?
1161 // Now push a new stack element for this node we just created.
1162 pushBlock(elem->tagName, elem->level);
1164 // Set our strayTableContent boolean if needed, so that the reopened tag also knows
1165 // that it is inside a malformed table.
1166 blockStack->strayTableContent = malformedTableParent != 0;
1167 if (blockStack->strayTableContent)
1168 inStrayTableContent++;
1170 // Clear our malformed table parent variable.
1171 malformedTableParent = 0;
1173 // Update |current| manually to point to the new node.
1174 setCurrent(newNode.get());
1176 // Advance to the next tag that needs to be reopened.
1177 HTMLStackElem* next = elem->next;
1184 void HTMLParser::pushBlock(const AtomicString& tagName, int level)
1186 blockStack = new HTMLStackElem(tagName, level, current, didRefCurrent, blockStack);
1187 didRefCurrent = false;
1190 void HTMLParser::popBlock(const AtomicString& _tagName)
1192 HTMLStackElem *Elem = blockStack;
1196 while (Elem && (Elem->tagName != _tagName)) {
1197 if (maxLevel < Elem->level)
1198 maxLevel = Elem->level;
1205 if (maxLevel > Elem->level) {
1206 // We didn't match because the tag is in a different scope, e.g.,
1207 // <b><p>Foo</b>. Try to correct the problem.
1208 if (!isResidualStyleTag(_tagName))
1210 return handleResidualStyleCloseTagAcrossBlocks(Elem);
1213 bool isAffectedByStyle = isAffectedByResidualStyle(Elem->tagName);
1214 HTMLStackElem* residualStyleStack = 0;
1215 Node* malformedTableParent = 0;
1219 if (Elem->tagName == _tagName) {
1220 int strayTable = inStrayTableContent;
1224 // This element was the root of some malformed content just inside an implicit or
1225 // explicit <tbody> or <tr>.
1226 // If we end up needing to reopen residual style tags, the root of the reopened chain
1227 // must also know that it is the root of malformed content inside a <tbody>/<tr>.
1228 if (strayTable && (inStrayTableContent < strayTable) && residualStyleStack) {
1229 Node* curr = current;
1230 while (curr && !curr->hasTagName(tableTag))
1231 curr = curr->parentNode();
1232 malformedTableParent = curr ? curr->parentNode() : 0;
1236 if (form && Elem->tagName == formTag)
1237 // A <form> is being closed prematurely (and this is
1238 // malformed HTML). Set an attribute on the form to clear out its
1240 form->setMalformed(true);
1242 // Schedule this tag for reopening
1243 // after we complete the close of this entire block.
1244 if (isAffectedByStyle && isResidualStyleTag(Elem->tagName))
1245 // We've overloaded the use of stack elements and are just reusing the
1246 // struct with a slightly different meaning to the variables. Instead of chaining
1247 // from innermost to outermost, we build up a list of all the tags we need to reopen
1248 // from the outermost to the innermost, i.e., residualStyleStack will end up pointing
1249 // to the outermost tag we need to reopen.
1250 // We also set Elem->node to be the actual element that corresponds to the ID stored in
1251 // Elem->id rather than the node that you should pop to when the element gets pulled off
1253 moveOneBlockToStack(residualStyleStack);
1260 reopenResidualStyleTags(residualStyleStack, malformedTableParent);
1263 inline HTMLStackElem* HTMLParser::popOneBlockCommon()
1265 HTMLStackElem* elem = blockStack;
1267 // Form elements restore their state during the parsing process.
1268 // Also, a few elements (<applet>, <object>) need to know when all child elements (<param>s) are available.
1269 if (current && elem->node != current)
1270 current->closeRenderer();
1272 blockStack = elem->next;
1273 current = elem->node;
1274 didRefCurrent = elem->didRefNode;
1276 if (elem->strayTableContent)
1277 inStrayTableContent--;
1282 void HTMLParser::popOneBlock()
1284 // Store the current node before popOneBlockCommon overwrites it.
1285 Node* lastCurrent = current;
1286 bool didRefLastCurrent = didRefCurrent;
1288 delete popOneBlockCommon();
1290 if (didRefLastCurrent)
1291 lastCurrent->deref();
1294 void HTMLParser::moveOneBlockToStack(HTMLStackElem*& head)
1296 // We'll be using the stack element we're popping, but for the current node.
1297 // See the two callers for details.
1299 // Store the current node before popOneBlockCommon overwrites it.
1300 Node* lastCurrent = current;
1301 bool didRefLastCurrent = didRefCurrent;
1303 // Pop the block, but don't deref the current node as popOneBlock does because
1304 // we'll be using the pointer in the new stack element.
1305 HTMLStackElem* elem = popOneBlockCommon();
1307 // Transfer the current node into the stack element.
1308 // No need to deref the old elem->node because popOneBlockCommon transferred
1309 // it into the current/didRefCurrent fields.
1310 elem->node = lastCurrent;
1311 elem->didRefNode = didRefLastCurrent;
1316 void HTMLParser::popInlineBlocks()
1318 while (blockStack && isInline(current))
1322 void HTMLParser::freeBlock()
1328 void HTMLParser::createHead()
1330 if (head || !doc()->firstChild())
1333 head = new HTMLHeadElement(document);
1334 HTMLElement* body = doc()->body();
1335 ExceptionCode ec = 0;
1336 doc()->firstChild()->insertBefore(head, body, ec);
1341 Node* HTMLParser::handleIsindex(Token* t)
1343 Node* n = new HTMLDivElement(document);
1345 NamedMappedAttrMap* attrs = t->attrs.get();
1347 RefPtr<HTMLIsIndexElement> isIndex = new HTMLIsIndexElement(document, form);
1348 isIndex->setAttributeMap(attrs);
1349 isIndex->setAttribute(typeAttr, "khtml_isindex");
1351 String text = searchableIndexIntroduction();
1353 if (Attribute *a = attrs->getAttributeItem(promptAttr))
1354 text = a->value().domString() + " ";
1358 n->addChild(new HTMLHRElement(document));
1359 n->addChild(new Text(document, text));
1360 n->addChild(isIndex.get());
1361 n->addChild(new HTMLHRElement(document));
1366 void HTMLParser::startBody()
1373 insertNode(isindex.get(), true /* don't decend into this node */);
1378 void HTMLParser::finished()
1380 // In the case of a completely empty document, here's the place to create the HTML element.
1381 if (current && current->isDocumentNode() && !current->firstChild())
1382 insertNode(new HTMLHtmlElement(document));
1384 // This ensures that "current" is not left pointing to a node when the document is destroyed.
1388 // Warning, this may delete the tokenizer and parser, so don't try to do anything else after this.
1390 document->finishedParsing();