void KHTMLParser::handleResidualStyleCloseTagAcrossBlocks(HTMLStackElem* elem) { // Find the element that crosses over to a higher level. For now, if there is more than // one, we will just give up and not attempt any sort of correction. It's highly unlikely that // there will be more than one, since <p> tags aren't allowed to be nested. 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; // Check to see if the reparenting that is going to occur is allowed according to the DOM. // FIXME: We should either always allow it or perform an additional fixup instead of // just bailing here. // Example: <p><font><center>blah</font></center></p> isn't doing a fixup right now. if (!parentElem->childAllowed(blockElem)) return; if (maxElem->node->parentNode() != elem->node) { // Walk the stack and remove any elements that aren't residual style tags. These // are basically just being closed up. Example: // <font><span>Moo<p>Goo</font></p>. // In the above example, the <span> doesn't need to be reopened. It can just close. 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; } // We have to reopen residual tags in between maxElem and elem. An example of this case is: // <font><i>Moo<p>Foo</font>. // In this case, we need to transform the part before the <p> into: // <font><i>Moo</i></font><i> // so that the <i> will remain open. This involves the modification of elements // in the block stack. // This will also affect how we ultimately reparent the block, since we want it to end up // under the reopened residual tags (e.g., the <i> in the above example.) NodeImpl* prevNode = 0; NodeImpl* currNode = 0; currElem = maxElem; while (currElem->node != residualElem) { if (isResidualStyleTag(currElem->node->id())) { // Create a clone of this element. currNode = currElem->node->cloneNode(false); // Change the stack element's node to point to the clone. currElem->node = currNode; // Attach the previous node as a child of this new node. if (prevNode) currNode->appendChild(prevNode, exceptionCode); else // The new parent for the block element is going to be the innermost clone. parentElem = currNode; prevNode = currNode; } currElem = currElem->next; } // Now append the chain of new residual style elements if one exists. if (prevNode) elem->node->appendChild(prevNode, exceptionCode); } // We need to make a clone of |residualElem| and place it just inside |blockElem|. // All content of |blockElem| is reparented to be under this clone. We then // reparent |blockElem| using real DOM calls so that attachment/detachment will // be performed to fix up the rendering tree. // So for this example: <b>...<p>Foo</b>Goo</p> // The end result will be: <b>...</b><p><b>Foo</b>Goo</p> // // Step 1: Remove |blockElem| from its parent, doing a batch detach of all the kids. blockElem->parentNode()->removeChild(blockElem, exceptionCode); // Step 2: Clone |residualElem|. NodeImpl* newNode = residualElem->cloneNode(false); // Shallow clone. We don't pick up the same kids. // Step 3: Place |blockElem|'s children under |newNode|. Remove all of the children of |blockElem| // before we've put |newElem| into the document. That way we'll only do one attachment of all // the new content (instead of a bunch of individual attachments). NodeImpl* currNode = blockElem->firstChild(); while (currNode) { NodeImpl* nextNode = currNode->nextSibling(); blockElem->removeChild(currNode, exceptionCode); newNode->appendChild(currNode, exceptionCode); currNode = nextNode; } // Step 4: Place |newNode| under |blockElem|. |blockElem| is still out of the document, so no // attachment can occur yet. blockElem->appendChild(newNode, exceptionCode); // Step 5: Reparent |blockElem|. Now the full attachment of the fixed up tree takes place. parentElem->appendChild(blockElem, exceptionCode); // Step 6: Elide |elem|, since it is effectively no longer open. Also update // the node associated with the previous stack element so that when it gets popped, // it doesn't make the residual element the next current node. HTMLStackElem* currElem = maxElem; HTMLStackElem* prevElem = 0; while (currElem != elem) { prevElem = currElem; currElem = currElem->next; } prevElem->next = elem->next; prevElem->node = elem->node; delete elem; // Step 7: Reopen intermediate inlines, e.g., <b><p><i>Foo</b>Goo</p>. // In the above example, Goo should stay italic. curr = blockStack; HTMLStackElem* residualStyleStack = 0; while (curr && curr != maxElem) { // We will actually schedule this tag for reopening // after we complete the close of this entire block. NodeImpl* currNode = current; if (isResidualStyleTag(curr->id)) { // We've overloaded the use of stack elements and are just reusing the // struct with a slightly different meaning to the variables. Instead of chaining // from innermost to outermost, we build up a list of all the tags we need to reopen // from the outermost to the innermost, i.e., residualStyleStack will end up pointing // to the outermost tag we need to reopen. // We also set curr->node to be the actual element that corresponds to the ID stored in // curr->id rather than the node that you should pop to when the element gets pulled off // the stack. popOneBlock(false); curr->node = currNode; curr->next = residualStyleStack; residualStyleStack = curr; } else popOneBlock(); curr = blockStack; } reopenResidualStyleTags(residualStyleStack, 0); // FIXME: Deal with stray table content some day // if it becomes necessary to do so. }
void XMLTokenizer::finish() { // parse xml file XMLHandler handler(m_doc,m_view); QXmlInputSource source; source.setData(m_xmlCode); QXmlSimpleReader reader; reader.setContentHandler( &handler ); reader.setLexicalHandler( &handler ); reader.setErrorHandler( &handler ); reader.setDeclHandler( &handler ); reader.setDTDHandler( &handler ); bool ok = reader.parse( source ); // ### handle exceptions inserting nodes if (!ok) { kdDebug(6036) << "Error during XML parsing: " << handler.errorProtocol() << endl; int exceptioncode; while (m_doc->hasChildNodes()) static_cast<NodeImpl*>(m_doc)->removeChild(m_doc->firstChild(),exceptioncode); // construct a HTML page giving the error message // ### for multiple error messages, display the code for each QTextIStream stream(&m_xmlCode); unsigned long lineno; for (lineno = 0; lineno < handler.errorLine-1; lineno++) stream.readLine(); QString line = stream.readLine(); m_doc->appendChild(m_doc->createElementNS("http://www.w3.org/1999/xhtml","html"),exceptioncode); NodeImpl *body = m_doc->createElementNS("http://www.w3.org/1999/xhtml","body"); m_doc->firstChild()->appendChild(body,exceptioncode); NodeImpl *h1 = m_doc->createElementNS("http://www.w3.org/1999/xhtml","h1"); body->appendChild(h1,exceptioncode); h1->appendChild(m_doc->createTextNode(i18n("XML parsing error")),exceptioncode); h1->renderer()->close(); body->appendChild(m_doc->createTextNode(handler.errorProtocol()),exceptioncode); body->appendChild(m_doc->createElementNS("http://www.w3.org/1999/xhtml","hr"),exceptioncode); NodeImpl *pre = m_doc->createElementNS("http://www.w3.org/1999/xhtml","pre"); body->appendChild(pre,exceptioncode); pre->appendChild(m_doc->createTextNode(line+"\n"),exceptioncode); unsigned long colno; QString indent = ""; for (colno = 0; colno < handler.errorCol-1; colno++) indent += " "; pre->appendChild(m_doc->createTextNode(indent+"^"),exceptioncode); pre->renderer()->close(); body->renderer()->close(); m_doc->applyChanges(); m_doc->updateRendering(); end(); } else { addScripts(m_doc); m_scriptsIt = new QListIterator<HTMLScriptElementImpl>(m_scripts); executeScripts(); } }
void XMLTokenizer::finish() { m_source.setFinished(true); if(!m_noErrors) { // An error occurred during parsing of the code. Display an error page to the user (the DOM // tree is created manually and includes an excerpt from the code where the error is located) // ### for multiple error messages, display the code for each (can this happen?) // Clear the document int exceptioncode = 0; while(m_doc->hasChildNodes()) static_cast< NodeImpl * >(m_doc)->removeChild(m_doc->firstChild(), exceptioncode); QString line, errorLocPtr; if(m_handler.errorLine) { QString xmlCode = m_source.data(); QTextIStream stream(&xmlCode); for(unsigned long lineno = 0; lineno < m_handler.errorLine - 1; lineno++) stream.readLine(); line = stream.readLine(); for(unsigned long colno = 0; colno < m_handler.errorCol - 1; colno++) errorLocPtr += " "; errorLocPtr += "^"; } // Create elements for display DocumentImpl *doc = m_doc; NodeImpl *html = doc->createElementNS(XHTML_NAMESPACE, "html"); NodeImpl *body = doc->createElementNS(XHTML_NAMESPACE, "body"); NodeImpl *h1 = doc->createElementNS(XHTML_NAMESPACE, "h1"); NodeImpl *headingText = doc->createTextNode(i18n("XML parsing error")); NodeImpl *errorText = doc->createTextNode(m_handler.errorProtocol()); NodeImpl *hr = 0; NodeImpl *pre = 0; NodeImpl *lineText = 0; NodeImpl *errorLocText = 0; if(!line.isNull()) { hr = doc->createElementNS(XHTML_NAMESPACE, "hr"); pre = doc->createElementNS(XHTML_NAMESPACE, "pre"); lineText = doc->createTextNode(line + "\n"); errorLocText = doc->createTextNode(errorLocPtr); } // Construct DOM tree. We ignore exceptions as we assume they will not be thrown here (due to the // fact we are using a known tag set) doc->appendChild(html, exceptioncode); html->appendChild(body, exceptioncode); if(body) body->appendChild(h1, exceptioncode); h1->appendChild(headingText, exceptioncode); body->appendChild(errorText, exceptioncode); body->appendChild(hr, exceptioncode); body->appendChild(pre, exceptioncode); if(pre) { pre->appendChild(lineText, exceptioncode); pre->appendChild(errorLocText, exceptioncode); } // Close the renderers so that they update their display correctly // ### this should not be necessary, but requires changes in the rendering code... h1->close(); if(pre) pre->close(); body->close(); m_doc->recalcStyle(NodeImpl::Inherit); m_doc->updateRendering(); end(); } else { // Parsing was successful. Now locate all html <script> tags in the document and execute them // one by one addScripts(m_doc); m_scriptsIt = new QPtrListIterator< HTMLScriptElementImpl >(m_scripts); executeScripts(); } }