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

QXmlStreamReader / QDomDocument does not process "white space handling" correctly

    XMLWordPrintable

Details

    Description

      In the XML specification, 2.10 White Space Handling https://www.w3.org/TR/xml/#sec-white-space the behavior of xml:space="preserve" is defined as

      In editing XML documents, it is often convenient to use "white space" (spaces, tabs, and blank lines) to set apart the markup for greater readability. Such white space is typically not intended for inclusion in the delivered version of the document. On the other hand, "significant" white space that should be preserved in the delivered version is common, for example in poetry and source code.

      An XML processor must always pass all characters in a document that are not markup through to the application. A validating XML processor must also inform the application which of these characters constitute white space appearing in element content.

      A special attribute named xml:space may be attached to an element to signal an intention that in that element, white space should be preserved by applications. In valid documents, this attribute, like any other, must be declared if it is used. When declared, it must be given as an enumerated type whose values are one or both of "default" and "preserve". For example:

      <!ATTLIST poem xml:space (default|preserve) 'preserve'>

      <!ATTLIST pre xml:space (preserve) #FIXED 'preserve'>
      The value "default" signals that applications' default white-space processing modes are acceptable for this element; the value "preserve" indicates the intent that applications preserve all the white space. This declared intent is considered to apply to all elements within the content of the element where it is specified, unless overridden with another instance of the xml:space attribute. This specification does not give meaning to any value of xml:space other than "default" and "preserve". It is an error for other values to be specified; the XML processor may report the error or may recover by ignoring the attribute specification or by reporting the (erroneous) value to the application. Applications may ignore or reject erroneous values.

      The root element of any document is considered to have signaled no intentions as regards application space handling, unless it provides a value for this attribute or the attribute is declared with a default value.

       

      Unfortunately, QXmlStreamReader with QDomDocument does not respect this MUST requirement. Using the attached test

      void QDomDocumentWithSpacesTest::entityWithSpaceAndPreserve()
      {
          // an XML document where <Description> has a whitespace as content and uses
          // xml:space="preserve"
          QString const xmlDocumentWithSpacePreserve = R"(<?xml version="1.0" encoding="UTF-8"?>
                  <Message  xmlns:xs="http://www.w3.org/2001/XMLSchema">
                     <Description xml:space="preserve"> </Description>
                  </Message>
              )";
          auto const doc = parse(xmlDocumentWithSpacePreserve);
          QCOMPARE(doc.firstChild().toElement().text(), " ");
      } 

       fails

      ********* Start testing of QDomDocumentWithSpaces *********
      Config: Using QtTest library 5.15.3, Qt 5.15.3 (x86_64-little_endian-llp64 shared (dynamic) debug build; by MSVC 2019), windows 10
      PASS   : QDomDocumentWithSpaces::initTestCase()
      PASS   : QDomDocumentWithSpaces::entityWithoutSpace()
      PASS   : QDomDocumentWithSpaces::entityWithSpace()
      FAIL!  : QDomDocumentWithSpaces::entityWithSpaceAndPreserver() Compared values are not the same
         Actual   (doc.firstChild().toElement().text()): ""
         Expected (" ")                                :
      C:\code\QDomDocument_and_spaces\QDomDocumentWithSpaces.cpp(65) : failure location
      PASS   : QDomDocumentWithSpaces::cleanupTestCase()
      Totals: 4 passed, 1 failed, 0 skipped, 0 blacklisted, 12ms
      ********* Finished testing of QDomDocumentWithSpaces *********

       

       

      Attachments

        Issue Links

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

          Activity

            People

              thiago Thiago Macieira
              ssproessig Sören Sprößig
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes