#include "html/htmlparser.h"
#include "dom/dom_exception.h"
#include "html/html_baseimpl.h"
#include "html/html_blockimpl.h"
#include "html/html_canvasimpl.h"
#include "html/html_documentimpl.h"
#include "html/html_elementimpl.h"
#include "html/html_formimpl.h"
#include "html/html_headimpl.h"
#include "html/html_imageimpl.h"
#include "html/html_inlineimpl.h"
#include "html/html_listimpl.h"
#include "html/html_miscimpl.h"
#include "html/html_tableimpl.h"
#include "html/html_objectimpl.h"
#include "xml/dom_textimpl.h"
#include "xml/dom_nodeimpl.h"
#include "misc/htmlhashes.h"
#include "html/htmltokenizer.h"
#include "khtmlview.h"
#include "khtml_part.h"
#include "css/cssproperties.h"
#include "css/cssvalues.h"
#include "rendering/render_object.h"
#include <kdebug.h>
#include <klocale.h>
using namespace DOM;
using namespace khtml;
class HTMLStackElem
{
public:
HTMLStackElem( int _id,
int _level,
DOM::NodeImpl *_node,
HTMLStackElem * _next
)
:
id(_id),
level(_level),
strayTableContent(false),
node(_node),
next(_next)
{ }
int id;
int level;
bool strayTableContent;
NodeImpl *node;
HTMLStackElem *next;
};
KHTMLParser::KHTMLParser(KHTMLView *_parent, DocumentPtr *doc, bool includesComments)
: current(0), currentIsReferenced(false), includesCommentsInDOM(includesComments)
{
#if SPEED_DEBUG > 0
qt.start();
#endif
HTMLWidget = _parent;
document = doc;
document->ref();
blockStack = 0;
reset();
}
KHTMLParser::KHTMLParser(DOM::DocumentFragmentImpl *i, DocumentPtr *doc, bool includesComments)
: current(0), currentIsReferenced(false), includesCommentsInDOM(includesComments)
{
HTMLWidget = 0;
document = doc;
document->ref();
blockStack = 0;
reset();
setCurrent(i);
inBody = true;
}
KHTMLParser::~KHTMLParser()
{
#if SPEED_DEBUG > 0
kdDebug( ) << "TIME: parsing time was = " << qt.elapsed() << endl;
#endif
freeBlock();
setCurrent(0);
document->deref();
if (isindex)
isindex->deref();
}
void KHTMLParser::reset()
{
setCurrent(doc());
freeBlock();
memset(forbiddenTag, 0, sizeof(forbiddenTag));
inBody = false;
haveFrameSet = false;
haveContent = false;
inSelect = false;
inStrayTableContent = 0;
form = 0;
map = 0;
head = 0;
end = false;
isindex = 0;
discard_until = 0;
}
void KHTMLParser::setCurrent(DOM::NodeImpl *newCurrent)
{
bool newCurrentIsReferenced = newCurrent && newCurrent != doc();
if (newCurrentIsReferenced)
newCurrent->ref();
if (currentIsReferenced)
current->deref();
current = newCurrent;
currentIsReferenced = newCurrentIsReferenced;
}
void KHTMLParser::parseToken(Token *t)
{
if(discard_until) {
if(t->id == discard_until)
discard_until = 0;
if ( discard_until || current->id() + ID_CLOSE_TAG != t->id )
return;
}
#ifdef PARSER_DEBUG
kdDebug( 6035 ) << "\n\n==> parser: processing token " << getTagName(t->id).string() << "(" << t->id << ")"
<< " current = " << getTagName(current->id()).string() << "(" << current->id() << ")" << endl;
kdDebug(6035) << " inBody=" << inBody << " haveFrameSet=" << haveFrameSet << endl;
#endif
if (t->id == ID_BR + ID_CLOSE_TAG && doc()->inCompatMode())
t->id = ID_BR;
if (t->id > ID_CLOSE_TAG)
{
processCloseTag(t);
return;
}
if( t->id == ID_TEXT && t->text ) {
if(inBody && !skipMode() && current->id() != ID_STYLE
&& current->id() != ID_TITLE && current->id() != ID_SCRIPT &&
!t->text->containsOnlyWhitespace())
haveContent = true;
#ifdef PARSER_DEBUG
kdDebug(6035) << "length="<< t->text->l << " text='" << QConstString(t->text->s, t->text->l).string() << "'" << endl;
#endif
}
NodeImpl *n = getElement(t);
if(!n)
return;
Node protectNode(n);
if(n->isElementNode())
{
ElementImpl *e = static_cast<ElementImpl *>(n);
e->setAttributeMap(t->attrs);
if(endTagRequirement(e->id()) == DOM::OPTIONAL)
popBlock(t->id);
if (isHeaderTag(t->id))
popNestedHeaderTag();
}
while (blockStack && t->id <= ID_LAST_TAG && forbiddenTag[t->id]) {
#ifdef PARSER_DEBUG
kdDebug( 6035 ) << "t->id: " << t->id << " is forbidden :-( " << endl;
#endif
popOneBlock();
}
if (!insertNode(n, t->flat))
{
if(n->isElementNode())
{
ElementImpl *e = static_cast<ElementImpl *>(n);
e->setAttributeMap(0);
}
#ifdef PARSER_DEBUG
kdDebug( 6035 ) << "insertNode failed current=" << current->id() << ", new=" << n->id() << "!" << endl;
#endif
if (map == n)
{
#ifdef PARSER_DEBUG
kdDebug( 6035 ) << " --> resetting map!" << endl;
#endif
map = 0;
}
if (form == n)
{
#ifdef PARSER_DEBUG
kdDebug( 6035 ) << " --> resetting form!" << endl;
#endif
form = 0;
}
}
}
static bool isTableRelatedTag(int id)
{
return (id == ID_TR || id == ID_TD || id == ID_TABLE || id == ID_TBODY || id == ID_TFOOT || id == ID_THEAD ||
id == ID_TH);
}
bool KHTMLParser::insertNode(NodeImpl *n, bool flat)
{
Node protectNode(n);
int id = n->id();
#ifdef PARSER_DEBUG
NodeImpl *tmp = current;
#endif
NodeImpl *newNode = current->addChild(n);
if ( newNode ) {
#ifdef PARSER_DEBUG
kdDebug( 6035 ) << "added " << n->nodeName().string() << " to " << tmp->nodeName().string() << ", new current=" << newNode->nodeName().string() << endl;
#endif
if(tagPriority(id) != 0 && !flat)
{
pushBlock(id, tagPriority(id));
if (newNode == current)
popBlock(id);
else
setCurrent(newNode);
#if SPEED_DEBUG < 2
if(!n->attached() && HTMLWidget)
n->attach();
#endif
}
else {
#if SPEED_DEBUG < 2
if(!n->attached() && HTMLWidget)
n->attach();
if (n->maintainsState()) {
doc()->registerMaintainsState(n);
QStringList &states = doc()->restoreState();
if (!states.isEmpty())
n->restoreState(states);
}
n->closeRenderer();
#endif
}
return true;
} else {
#ifdef PARSER_DEBUG
kdDebug( 6035 ) << "ADDING NODE FAILED!!!! current = " << current->nodeName().string() << ", new = " << n->nodeName().string() << endl;
#endif
HTMLElementImpl *e;
bool handled = false;
switch(id)
{
case ID_TR:
case ID_TH:
case ID_TD:
if (inStrayTableContent && !isTableRelatedTag(current->id())) {
while (blockStack && !isTableRelatedTag(current->id()))
popOneBlock();
return insertNode(n);
}
break;
case ID_COMMENT:
break;
case ID_HEAD:
if (!current->isDocumentNode() && current->id() != ID_HTML )
return false;
break;
case ID_META:
case ID_LINK:
case ID_BASE:
if( !head )
createHead();
if( head ) {
if ( head->addChild(n) ) {
#if SPEED_DEBUG < 2
if(!n->attached() && HTMLWidget)
n->attach();
#endif
return true;
} else {
return false;
}
}
break;
case ID_HTML:
if (!current->isDocumentNode() ) {
if ( doc()->firstChild()->id() == ID_HTML) {
NamedAttrMapImpl *map = static_cast<ElementImpl*>(n)->attributes(true);
NamedAttrMapImpl *bmap = static_cast<ElementImpl*>(doc()->firstChild())->attributes(false);
bool changed = false;
for (unsigned long l = 0; map && l < map->length(); ++l) {
AttributeImpl* it = map->attributeItem(l);
changed = !bmap->getAttributeItem(it->id());
bmap->insertAttribute(it->clone(false));
}
if ( changed )
doc()->recalcStyle( NodeImpl::Inherit );
}
return false;
}
break;
case ID_TITLE:
case ID_STYLE:
if ( !head )
createHead();
if ( head ) {
DOM::NodeImpl *newNode = head->addChild(n);
if ( newNode ) {
pushBlock(id, tagPriority(id));
setCurrent(newNode);
#if SPEED_DEBUG < 2
if(!n->attached() && HTMLWidget)
n->attach();
#endif
} else {
#ifdef PARSER_DEBUG
kdDebug( 6035 ) << "adding style before to body failed!!!!" << endl;
#endif
discard_until = ID_STYLE + ID_CLOSE_TAG;
return false;
}
return true;
} else if(inBody) {
discard_until = ID_STYLE + ID_CLOSE_TAG;
return false;
}
break;
case ID_BODY:
if(inBody && doc()->body()) {
NamedAttrMapImpl *map = static_cast<ElementImpl*>(n)->attributes(true);
NamedAttrMapImpl *bmap = doc()->body()->attributes(false);
bool changed = false;
for (unsigned long l = 0; map && l < map->length(); ++l) {
AttributeImpl* it = map->attributeItem(l);
changed = !bmap->getAttributeItem(it->id());
bmap->insertAttribute(it->clone(false));
}
if ( changed )
doc()->recalcStyle( NodeImpl::Inherit );
} else if ( current->isDocumentNode() )
break;
return false;
break;
case ID_INPUT:
{
ElementImpl *e = static_cast<ElementImpl *>(n);
DOMString type = e->getAttribute(ATTR_TYPE);
if ( strcasecmp( type, "hidden" ) == 0 && form) {
form->addChild(n);
#if SPEED_DEBUG < 2
if(!n->attached() && HTMLWidget)
n->attach();
#endif
return true;
}
break;
}
case ID_TEXT:
switch(current->id())
{
case ID_SELECT:
return false;
default:
;
};
break;
case ID_DD:
case ID_DT:
e = new HTMLDListElementImpl(document);
if ( insertNode(e) ) {
insertNode(n);
return true;
}
break;
case ID_AREA:
{
if(map)
{
map->addChild(n);
#if SPEED_DEBUG < 2
if(!n->attached() && HTMLWidget)
n->attach();
#endif
handled = true;
}
else
return false;
return true;
}
case ID_CAPTION: {
switch (current->id()) {
case ID_THEAD:
case ID_TBODY:
case ID_TFOOT:
case ID_TR:
case ID_TH:
case ID_TD: {
NodeImpl* tsection = current;
if (current->id() == ID_TR)
tsection = current->parent();
else if (current->id() == ID_TD || current->id() == ID_TH)
tsection = current->parent()->parent();
NodeImpl* table = tsection->parent();
int exceptioncode = 0;
table->insertBefore(n, tsection, exceptioncode);
pushBlock(id, tagPriority(id));
setCurrent(n);
inStrayTableContent++;
blockStack->strayTableContent = true;
return true;
}
default:
break;
}
break;
}
case ID_THEAD:
case ID_TBODY:
case ID_TFOOT:
case ID_COLGROUP: {
if (isTableRelatedTag(current->id())) {
while (blockStack && current->id() != ID_TABLE && isTableRelatedTag(current->id()))
popOneBlock();
return insertNode(n);
}
}
default:
break;
}
switch(current->id())
{
case ID_HTML:
switch(id)
{
case ID_SCRIPT:
case ID_STYLE:
case ID_META:
case ID_LINK:
case ID_OBJECT:
case ID_EMBED:
case ID_TITLE:
case ID_ISINDEX:
case ID_BASE:
if(!head) {
head = new HTMLHeadElementImpl(document);
e = head;
insertNode(e);
handled = true;
}
break;
case ID_TEXT: {
TextImpl *t = static_cast<TextImpl *>(n);
if (t->containsOnlyWhitespace())
return false;
}
default:
if ( haveFrameSet ) break;
e = new HTMLBodyElementImpl(document);
startBody();
insertNode(e);
handled = true;
break;
}
break;
case ID_HEAD:
if (id == ID_HTML)
return false;
else {
if ( haveFrameSet ) break;
popBlock(ID_HEAD);
e = new HTMLBodyElementImpl(document);
startBody();
insertNode(e);
handled = true;
}
break;
case ID_BODY:
break;
case ID_CAPTION:
popBlock(ID_CAPTION);
switch( id ) {
case ID_THEAD:
case ID_TFOOT:
case ID_TBODY:
case ID_TR:
case ID_TD:
case ID_TH:
return insertNode(n, flat);
}
break;
case ID_TABLE:
case ID_THEAD:
case ID_TFOOT:
case ID_TBODY:
case ID_TR:
switch(id)
{
case ID_TABLE:
popBlock(ID_TABLE); handled = true; break;
case ID_TEXT:
{
TextImpl *t = static_cast<TextImpl *>(n);
if (t->containsOnlyWhitespace())
return false;
DOMStringImpl *i = t->string();
unsigned int pos = 0;
while(pos < i->l && ( *(i->s+pos) == QChar(' ') ||
*(i->s+pos) == QChar(0xa0))) pos++;
if(pos == i->l)
break;
}
default:
{
NodeImpl *node = current;
NodeImpl *parent = node->parentNode();
NodeImpl *parentparent = parent->parentNode();
if (n->isTextNode() ||
( node->id() == ID_TR &&
( parent->id() == ID_THEAD ||
parent->id() == ID_TBODY ||
parent->id() == ID_TFOOT ) && parentparent->id() == ID_TABLE ) ||
( !checkChild(ID_TR, id, !doc()->inCompatMode()) && ( node->id() == ID_THEAD || node->id() == ID_TBODY || node->id() == ID_TFOOT ) &&
parent->id() == ID_TABLE ))
{
node = (node->id() == ID_TABLE) ? node :
((node->id() == ID_TR) ? parentparent : parent);
NodeImpl *parent = node->parentNode();
int exceptioncode = 0;
parent->insertBefore( n, node, exceptioncode );
if ( exceptioncode ) {
#ifdef PARSER_DEBUG
kdDebug( 6035 ) << "adding content before table failed!" << endl;
#endif
break;
}
if (n->isElementNode() && tagPriority(id) != 0 &&
!flat && endTagRequirement(id) != DOM::FORBIDDEN)
{
pushBlock(id, tagPriority(id));
setCurrent(n);
inStrayTableContent++;
blockStack->strayTableContent = true;
}
return true;
}
if ( current->id() == ID_TR )
e = new HTMLTableCellElementImpl(document, ID_TD);
else if ( current->id() == ID_TABLE )
e = new HTMLTableSectionElementImpl( document, ID_TBODY, true );
else
e = new HTMLTableRowElementImpl( document );
insertNode(e);
handled = true;
break;
} } break;
case ID_OBJECT:
discard_until = ID_OBJECT + ID_CLOSE_TAG;
return false;
case ID_UL:
case ID_OL:
case ID_DIR:
case ID_MENU:
e = new HTMLDivElementImpl(document);
insertNode(e);
handled = true;
break;
case ID_DL:
popBlock(ID_DL);
handled = true;
break;
case ID_DT:
popBlock(ID_DT);
handled = true;
break;
case ID_SELECT:
if( n->isInline() )
return false;
break;
case ID_P:
case ID_H1:
case ID_H2:
case ID_H3:
case ID_H4:
case ID_H5:
case ID_H6:
if(!n->isInline())
{
popBlock(current->id());
handled = true;
}
break;
case ID_OPTION:
case ID_OPTGROUP:
if (id == ID_OPTGROUP)
{
popBlock(current->id());
handled = true;
}
else if(id == ID_SELECT)
{
popBlock( ID_SELECT );
break;
}
break;
case ID_ADDRESS:
popBlock(ID_ADDRESS);
handled = true;
break;
case ID_COLGROUP:
if (id != ID_TEXT) {
popBlock(ID_COLGROUP);
handled = true;
}
break;
case ID_FONT:
popBlock(ID_FONT);
handled = true;
break;
default:
if(current->isDocumentNode())
{
if(current->firstChild() == 0) {
e = new HTMLHtmlElementImpl(document);
insertNode(e);
handled = true;
}
}
else if(current->isInline())
{
popInlineBlocks();
handled = true;
}
}
if(!handled)
{
return false;
}
return insertNode(n);
}
}
NodeImpl *KHTMLParser::getElement(Token* t)
{
switch (t->id)
{
case ID_HEAD:
if (!head && current->id() == ID_HTML) {
head = new HTMLHeadElementImpl(document);
return head;
}
return 0;
case ID_BODY:
if (haveFrameSet)
return 0;
popBlock(ID_HEAD);
startBody();
return new HTMLBodyElementImpl(document);
case ID_FRAMESET:
popBlock(ID_HEAD);
if (inBody && !haveFrameSet && !haveContent) {
popBlock(ID_BODY);
if (doc()->body())
doc()->body()->setAttribute(ATTR_STYLE, "display:none");
inBody = false;
}
if ((haveContent || haveFrameSet) && current->id() == ID_HTML)
return 0;
haveFrameSet = true;
startBody();
return new HTMLFrameSetElementImpl(document);
case ID_IFRAME:
discard_until = ID_IFRAME + ID_CLOSE_TAG;
break;
case ID_FORM:
if (form)
return 0;
form = new HTMLFormElementImpl(document);
return form;
case ID_BUTTON:
return new HTMLButtonElementImpl(document, form);
case ID_FIELDSET:
return new HTMLFieldSetElementImpl(document, form);
case ID_INPUT:
return new HTMLInputElementImpl(document, form);
case ID_ISINDEX: {
NodeImpl *n = handleIsindex(t);
if (!inBody) {
if (isindex)
isindex->deref();
isindex = n;
isindex->ref();
return 0;
}
t->flat = true;
return n;
}
case ID_KEYGEN:
return new HTMLKeygenElementImpl(document, form);
case ID_LEGEND:
return new HTMLLegendElementImpl(document, form);
case ID_OPTGROUP:
return new HTMLOptGroupElementImpl(document, form);
case ID_OPTION:
return new HTMLOptionElementImpl(document, form);
case ID_SELECT:
inSelect = true;
return new HTMLSelectElementImpl(document, form);
case ID_TEXTAREA:
return new HTMLTextAreaElementImpl(document, form);
case ID_DD:
popBlock(ID_DT);
popBlock(ID_DD);
break;
case ID_DT:
popBlock(ID_DD);
popBlock(ID_DT);
break;
case ID_LI:
popBlock(ID_LI);
break;
case ID_A:
popBlock(ID_A);
break;
case ID_IMG:
return new HTMLImageElementImpl(document, form);
case ID_MAP:
map = new HTMLMapElementImpl(document);
return map;
case ID_TR:
popBlock(ID_TR);
break;
case ID_TD:
case ID_TH:
popBlock(ID_TH);
popBlock(ID_TD);
break;
case ID_TBODY:
case ID_THEAD:
case ID_TFOOT:
popBlock(ID_THEAD);
popBlock(ID_TBODY);
popBlock(ID_TFOOT);
break;
case ID_TT:
case ID_U:
case ID_B:
case ID_I:
case ID_S:
case ID_STRIKE:
case ID_BIG:
case ID_SMALL:
if (!allowNestedRedundantTag(t->id))
return 0;
break;
case ID_NOBR:
case ID_WBR:
popBlock(t->id); break;
case ID_NOEMBED:
discard_until = ID_NOEMBED + ID_CLOSE_TAG;
return 0;
case ID_NOFRAMES:
discard_until = ID_NOFRAMES + ID_CLOSE_TAG;
return 0;
case ID_NOSCRIPT:
if (HTMLWidget && HTMLWidget->part()->jScriptEnabled())
discard_until = ID_NOSCRIPT + ID_CLOSE_TAG;
return 0;
case ID_NOLAYER:
return 0;
case ID_TEXT:
return new TextImpl(document, t->text);
case ID_COMMENT:
if (!includesCommentsInDOM)
return 0;
break;
case ID_SCRIPT:
{
HTMLScriptElementImpl *scriptElement = new HTMLScriptElementImpl(document);
scriptElement->setCreatedByParser(true);
return scriptElement;
}
}
return document->document()->createHTMLElement(t->id);
}
#define MAX_REDUNDANT 20
bool KHTMLParser::allowNestedRedundantTag(int _id)
{
int i = 0;
for (HTMLStackElem* curr = blockStack;
i < MAX_REDUNDANT && curr && curr->id == _id;
curr = curr->next, i++);
return i != MAX_REDUNDANT;
}
void KHTMLParser::processCloseTag(Token *t)
{
switch(t->id)
{
case ID_HTML+ID_CLOSE_TAG:
case ID_BODY+ID_CLOSE_TAG:
return;
case ID_FORM+ID_CLOSE_TAG:
form = 0;
break;
case ID_MAP+ID_CLOSE_TAG:
map = 0;
break;
case ID_SELECT+ID_CLOSE_TAG:
inSelect = false;
break;
default:
break;
}
#ifdef PARSER_DEBUG
kdDebug( 6035 ) << "added the following childs to " << current->nodeName().string() << endl;
NodeImpl *child = current->firstChild();
while(child != 0)
{
kdDebug( 6035 ) << " " << child->nodeName().string() << endl;
child = child->nextSibling();
}
#endif
HTMLStackElem* oldElem = blockStack;
popBlock(t->id-ID_CLOSE_TAG);
if (oldElem == blockStack && t->id == ID_P+ID_CLOSE_TAG) {
t->id-=ID_CLOSE_TAG;
parseToken(t);
popBlock(ID_P);
}
#ifdef PARSER_DEBUG
kdDebug( 6035 ) << "closeTag --> current = " << current->nodeName().string() << endl;
#endif
}
bool KHTMLParser::isHeaderTag(int _id)
{
switch (_id) {
case ID_H1:
case ID_H2:
case ID_H3:
case ID_H4:
case ID_H5:
case ID_H6:
return true;
default:
return false;
}
}
void KHTMLParser::popNestedHeaderTag()
{
NodeImpl* currNode = current;
for (HTMLStackElem* curr = blockStack; curr; curr = curr->next) {
if (isHeaderTag(curr->id)) {
popBlock(curr->id);
return;
}
if (currNode && !currNode->isInline())
return;
currNode = curr->node;
}
}
bool KHTMLParser::isResidualStyleTag(int _id)
{
switch (_id) {
case ID_A:
case ID_FONT:
case ID_TT:
case ID_U:
case ID_B:
case ID_I:
case ID_S:
case ID_STRIKE:
case ID_BIG:
case ID_SMALL:
case ID_EM:
case ID_STRONG:
case ID_DFN:
case ID_CODE:
case ID_SAMP:
case ID_KBD:
case ID_VAR:
return true;
default:
return false;
}
}
bool KHTMLParser::isAffectedByResidualStyle(int _id)
{
if (isResidualStyleTag(_id))
return true;
switch (_id) {
case ID_P:
case ID_DIV:
case ID_BLOCKQUOTE:
case ID_ADDRESS:
case ID_H1:
case ID_H2:
case ID_H3:
case ID_H4:
case ID_H5:
case ID_H6:
case ID_CENTER:
case ID_UL:
case ID_OL:
case ID_LI:
case ID_DL:
case ID_DT:
case ID_DD:
case ID_PRE:
return true;
default:
return false;
}
}
void KHTMLParser::handleResidualStyleCloseTagAcrossBlocks(HTMLStackElem* elem)
{
int exceptionCode = 0;
HTMLStackElem* curr = blockStack;
HTMLStackElem* maxElem = 0;
HTMLStackElem* prev = 0;
HTMLStackElem* prevMaxElem = 0;
while (curr && curr != elem) {
if (curr->level > elem->level) {
if (maxElem)
return;
maxElem = curr;
prevMaxElem = prev;
}
prev = curr;
curr = curr->next;
}
if (!curr || !maxElem || !isAffectedByResidualStyle(maxElem->id)) return;
NodeImpl* residualElem = prev->node;
NodeImpl* blockElem = prevMaxElem ? prevMaxElem->node : current;
NodeImpl* parentElem = elem->node;
if (!parentElem->childAllowed(blockElem))
return;
if (maxElem->node->parentNode() != elem->node) {
HTMLStackElem* currElem = maxElem->next;
HTMLStackElem* prevElem = maxElem;
while (currElem != elem) {
HTMLStackElem* nextElem = currElem->next;
if (!isResidualStyleTag(currElem->id)) {
prevElem->next = nextElem;
prevElem->node = currElem->node;
delete currElem;
}
else
prevElem = currElem;
currElem = nextElem;
}
NodeImpl* prevNode = 0;
NodeImpl* currNode = 0;
currElem = maxElem;
while (currElem->node != residualElem) {
if (isResidualStyleTag(currElem->node->id())) {
currNode = currElem->node->cloneNode(false);
currElem->node = currNode;
if (prevNode)
currNode->appendChild(prevNode, exceptionCode);
else parentElem = currNode;
prevNode = currNode;
}
currElem = currElem->next;
}
if (prevNode)
elem->node->appendChild(prevNode, exceptionCode);
}
blockElem->parentNode()->removeChild(blockElem, exceptionCode);
NodeImpl* newNode = residualElem->cloneNode(false);
NodeImpl* currNode = blockElem->firstChild();
while (currNode) {
NodeImpl* nextNode = currNode->nextSibling();
blockElem->removeChild(currNode, exceptionCode);
newNode->appendChild(currNode, exceptionCode);
currNode = nextNode;
}
blockElem->appendChild(newNode, exceptionCode);
parentElem->appendChild(blockElem, exceptionCode);
HTMLStackElem* currElem = maxElem;
HTMLStackElem* prevElem = 0;
while (currElem != elem) {
prevElem = currElem;
currElem = currElem->next;
}
prevElem->next = elem->next;
prevElem->node = elem->node;
delete elem;
curr = blockStack;
HTMLStackElem* residualStyleStack = 0;
while (curr && curr != maxElem) {
NodeImpl* currNode = current;
if (isResidualStyleTag(curr->id)) {
popOneBlock(false);
curr->node = currNode;
curr->next = residualStyleStack;
residualStyleStack = curr;
}
else
popOneBlock();
curr = blockStack;
}
reopenResidualStyleTags(residualStyleStack, 0); }
void KHTMLParser::reopenResidualStyleTags(HTMLStackElem* elem, DOM::NodeImpl* malformedTableParent)
{
while (elem) {
NodeImpl* newNode = elem->node->cloneNode(false);
int exceptionCode = 0;
if (malformedTableParent)
malformedTableParent->insertBefore(newNode, malformedTableParent->lastChild(), exceptionCode);
else
current->appendChild(newNode, exceptionCode);
pushBlock(elem->id, elem->level);
blockStack->strayTableContent = malformedTableParent != 0;
if (blockStack->strayTableContent)
inStrayTableContent++;
malformedTableParent = 0;
setCurrent(newNode);
HTMLStackElem* next = elem->next;
delete elem;
elem = next;
}
}
void KHTMLParser::pushBlock(int _id, int _level)
{
HTMLStackElem *Elem = new HTMLStackElem(_id, _level, current, blockStack);
blockStack = Elem;
addForbidden(_id, forbiddenTag);
}
void KHTMLParser::popBlock( int _id )
{
HTMLStackElem *Elem = blockStack;
int maxLevel = 0;
#ifdef PARSER_DEBUG
kdDebug( 6035 ) << "popBlock(" << getTagName(_id).string() << ")" << endl;
while(Elem) {
kdDebug( 6035) << " > " << getTagName(Elem->id).string() << endl;
Elem = Elem->next;
}
Elem = blockStack;
#endif
while( Elem && (Elem->id != _id))
{
if (maxLevel < Elem->level)
{
maxLevel = Elem->level;
}
Elem = Elem->next;
}
if (!Elem)
return;
if (maxLevel > Elem->level) {
if (!isResidualStyleTag(_id))
return;
return handleResidualStyleCloseTagAcrossBlocks(Elem);
}
bool isAffectedByStyle = isAffectedByResidualStyle(Elem->id);
HTMLStackElem* residualStyleStack = 0;
NodeImpl* malformedTableParent = 0;
Elem = blockStack;
while (Elem)
{
if (Elem->id == _id)
{
int strayTable = inStrayTableContent;
popOneBlock();
Elem = 0;
if (strayTable && (inStrayTableContent < strayTable) && residualStyleStack) {
NodeImpl* curr = current;
while (curr && curr->id() != ID_TABLE)
curr = curr->parentNode();
malformedTableParent = curr ? curr->parentNode() : 0;
}
}
else
{
if (Elem->id == ID_FORM && form)
form->setMalformed(true);
NodeImpl* currNode = current;
if (isAffectedByStyle && isResidualStyleTag(Elem->id)) {
popOneBlock(false);
Elem->next = residualStyleStack;
Elem->node = currNode;
residualStyleStack = Elem;
}
else
popOneBlock();
Elem = blockStack;
}
}
reopenResidualStyleTags(residualStyleStack, malformedTableParent);
}
void KHTMLParser::popOneBlock(bool delBlock)
{
HTMLStackElem *Elem = blockStack;
#ifndef PARSER_DEBUG
if(!Elem) return;
#else
kdDebug( 6035 ) << "popping block: " << getTagName(Elem->id).string() << "(" << Elem->id << ")" << endl;
#endif
#if SPEED_DEBUG < 1
if((Elem->node != current)) {
if (current->maintainsState() && doc()){
doc()->registerMaintainsState(current);
QStringList &states = doc()->restoreState();
if (!states.isEmpty())
current->restoreState(states);
}
current->closeRenderer();
}
#endif
removeForbidden(Elem->id, forbiddenTag);
blockStack = Elem->next;
setCurrent(Elem->node);
if (Elem->strayTableContent)
inStrayTableContent--;
if (delBlock)
delete Elem;
}
void KHTMLParser::popInlineBlocks()
{
while (blockStack && current->isInline())
popOneBlock();
}
void KHTMLParser::freeBlock()
{
while (blockStack)
popOneBlock();
}
void KHTMLParser::createHead()
{
if(head || !doc()->firstChild())
return;
head = new HTMLHeadElementImpl(document);
HTMLElementImpl *body = doc()->body();
int exceptioncode = 0;
doc()->firstChild()->insertBefore(head, body, exceptioncode);
if ( exceptioncode ) {
#ifdef PARSER_DEBUG
kdDebug( 6035 ) << "creation of head failed!!!!" << endl;
#endif
head = 0;
}
}
NodeImpl *KHTMLParser::handleIsindex( Token *t )
{
NodeImpl *n;
HTMLFormElementImpl *myform = form;
if ( !myform ) {
myform = new HTMLFormElementImpl(document);
n = myform;
} else
n = new HTMLDivElementImpl( document );
NodeImpl *child = new HTMLHRElementImpl( document );
n->addChild( child );
AttributeImpl* a = t->attrs ? t->attrs->getAttributeItem(ATTR_PROMPT) : 0;
#if APPLE_CHANGES
DOMString text = searchableIndexIntroduction();
#else
DOMString text = i18n("This is a searchable index. Enter search keywords: ");
#endif
if (a)
text = DOMString(a->value()) + " ";
child = new TextImpl(document, text);
n->addChild( child );
child = new HTMLIsIndexElementImpl(document, myform);
static_cast<ElementImpl *>(child)->setAttribute(ATTR_TYPE, "khtml_isindex");
n->addChild( child );
child = new HTMLHRElementImpl( document );
n->addChild( child );
return n;
}
void KHTMLParser::startBody()
{
if(inBody) return;
inBody = true;
if( isindex ) {
insertNode( isindex, true );
isindex = 0;
}
}
void KHTMLParser::finished()
{
if (current && current->isDocumentNode() && current->firstChild() == 0) {
insertNode(new HTMLHtmlElementImpl(document));
}
freeBlock();
setCurrent(0);
}