HTML link, style, and script elements are erroneously removed by QWebKit from the head element



  • When I inspect the HTML loaded into webkit using the toHtml method (or the toOuterXml method) I find that the Qt webkit methods produce erroneous results for elements in the head element.

    For example, I used QWebView to display the following HTML:
    @
    <!DOCTYPE html>
    <html>
    <head>
    <title>head test</title>
    <meta charset="utf-8" />.
    <link href="sample.css" rel="stylesheet" />
    <script></script>
    <style>p{font-family:"Times New Roman";font-size:20px;}</style>
    </head>
    <body>
    <p>test paragraph</p>
    </body>
    </html>
    @

    When I try to inspect the loaded HTML using;
    @qDebug()<<ui->webView->page()->mainFrame()->toHtml();@

    I get the following result:
    @
    "<!DOCTYPE html><html><head>
    <title>head test</title>
    <meta charset="utf-8"></head><body>.
    <link href="sample.css" rel="stylesheet">
    <script></script>
    <style>p{font-family:"Times New Roman";font-size:20px;}</style>

    <p>test paragraph</p>

    </body></html>"
    @

    Notice that the <style> , <link> , and <<script> elements are all erroneously moved into the body element from the head element.

    Has anyone else encountered this. If so, do they have a solution?


  • Moderators

    maybe because there is a point after the meta-element which causes a parsing error and webkit tries to repair this.



  • After doing some more investigation of the file I'd copied to form my test case I found some unexpected Unicode control characters after the meta tag that displayed as a dot(point). After removing these characters everything worked ok.

    Since I am constrained to using HTML generated elsewhere I'll have to preprocess the source HTML but I am a little surprised that WebKit didn't throw an error. In fact I'm now curious as to why WebKit processed a link element it thought was a child of the body element in violation of the specifications!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.