Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-126565

QTextDocument inserts underline tag for anchors when none was specified

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.7.0
    • GUI: Text handling
    • None
    • macOS

    Description

      In my app (get-notes.com), I work a lot with QTextDocument to transform the HTML of QML TextArea into Markdown and vice-versa. Whenever a user a creates a link, an underline is added (when non was specified and none is in the TextArea's HTML).

       

      To easily reproduce this problem:

       

      QString html = "<p>some <a href=\"google.com\">link</a></p>";
      QTextDocument doc;
      doc.setHtml(html);
      qDebug() << "HTML: " << doc.toHtml();

      This prints

       

      HTML:  "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n<html><head><meta name=\"qrichtext\" content=\"1\" /><meta charset=\"utf-8\" /><style type=\"text/css\">\np, li { white-space: pre-wrap; }\nhr { height: 1px; border-width: 0; }\nli.unchecked::marker { content: \"\\2610\"; }\nli.checked::marker { content: \"\\2612\"; }\n</style></head><body style=\" font-family:'.AppleSystemUIFont'; font-size:13pt; font-weight:400; font-style:normal;\">\n<p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">some <a href=\"google.com\"><span style=\" text-decoration: underline; color:#094fd1;\">link</span></a></p></body></html>"
       

      This HTML has the unspecified `text-decoration: underline;`  format. The problem is not just in `toHtml()`'s output, the problem is in the underline `QTextCharFormat`, which returns true for

       

       

      format.hasProperty(QTextFormat::FontUnderline) 
      format.fontUnderline()
      

      So I can't use a custom parser like [QBasicHtmlExporter](https://github.com/Open-App-Library/QBasicHtmlExporter), since it will falsely detect an underline when the given QString HTML doesn't have any.

       

      It seems like `doc.toMarkdown()` does return the right Markdown without an underline:

      some [link](google.com) 

      But since there are issues with toMarkdown (https://bugreports.qt.io/browse/QTBUG-123133) I can't use it, and have to resort to QBasicHtmlExporter and a different HTML to Markdown library (https://github.com/tim-gromeyer/html2md). 

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            esabraha Eskil Abrahamsen Blomfeldt
            rubymamis Ruby Mamistvalove
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes