Skip to content

xml.dom.minidom toprettyxml: omit whitespace for text-only elements #48397

Closed

Description

@thomaslee
mannequin
BPO 4147
Nosy @birkenfeld, @pefu, @benjaminp, @ezio-melotti, @bitdancer, @dankenigsberg
Files
  • minidom-toprettyxml-01.patch: A patch implementing the proposed changes.
  • minidom-Text-toprettyxml.patch: toprettyxml() should conserve Text.data
  • minidom-Text-toprettyxml-02.patch: preserve only Text nodes.
  • issue4147.diff: patch against 2.7
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ezio-melotti'
    closed_at = <Date 2011-11-18.15:39:13.696>
    created_at = <Date 2008-10-19.15:32:24.037>
    labels = ['expert-XML', 'type-bug', 'library', 'release-blocker']
    title = 'xml.dom.minidom toprettyxml: omit whitespace for text-only elements'
    updated_at = <Date 2011-11-18.15:39:13.695>
    user = 'https://bugs.python.org/thomaslee'

    bugs.python.org fields:

    activity = <Date 2011-11-18.15:39:13.695>
    actor = 'ezio.melotti'
    assignee = 'ezio.melotti'
    closed = True
    closed_date = <Date 2011-11-18.15:39:13.696>
    closer = 'ezio.melotti'
    components = ['Library (Lib)', 'XML']
    creation = <Date 2008-10-19.15:32:24.037>
    creator = 'thomaslee'
    dependencies = []
    files = ['11832', '23286', '23294', '23692']
    hgrepos = []
    issue_num = 4147
    keywords = ['patch']
    message_count = 21.0
    messages = ['74978', '90546', '90574', '102247', '111604', '122289', '144745', '144746', '144747', '144748', '144755', '144770', '144771', '144778', '147655', '147659', '147662', '147685', '147848', '147879', '147880']
    nosy_count = 13.0
    nosy_names = ['georg.brandl', 'pefu', 'techtonik', 'thomaslee', 'benjamin.peterson', 'ezio.melotti', 'Arfrever', 'r.david.murray', 'jgarrison', 'm-samia', 'danken', 'tod', 'python-dev']
    pr_nums = []
    priority = 'release blocker'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue4147'
    versions = ['Python 2.7', 'Python 3.2', 'Python 3.3']

    Activity

    1. thomaslee commented on Oct 19, 2008

      thomasleemannequin
      MannequinAuthor

      For XML elements containing only text data, it would be nice if
      toprettyxml could omit the whitespace it normally injects before & after
      the text, e.g.

      <person>
      <first-name>
      Bob
      </first-name>
      </person>

      Becomes:

      <person>
      <first-name>Bob</first-name>
      </person>

      From what I understand the handling of whitespace within XML elements is
      application-defined, so I'm classifying this as a nice-to-have feature.
      However it should be noted that in our particular case, the existing
      behaviour caused a few problems with a third-party system which treated
      whitespace as being significant.

    2. added
      type-featureA feature request or enhancement
      stdlibStandard Library Python modules in the Lib/ directory
      on Oct 19, 2008
    3. jgarrison commented on Jul 15, 2009

      jgarrisonmannequin
      Mannequin

      Also needed here. While pretty-printing should be able to insert
      non-significant whitespace BETWEEN xml elements, it should never alter
      the content of (i.e. insert whitespace into) existing text elements.

    4. jgarrison commented on Jul 16, 2009

      jgarrisonmannequin
      Mannequin

      To clarify:

      ... it should never alter the content of (i.e. insert whitespace into)
      existing text elements that contain non-whitespace characters.

    5. m-samia commented on Apr 3, 2010

      m-samiamannequin
      Mannequin

      Could you please apply that patch? People are starting to use non-standard libraries to process xml files because of this issue

      for example this man is using lxml or pyxml:
      http://ronrothman.com/public/leftbraned/xml-dom-minidom-toprettyxml-and-silly-whitespace/

      Is there any problem with that patch?

    6. BreamoreBoy commented on Jul 26, 2010

      BreamoreBoymannequin
      Mannequin

      @thomas: could you provide a unit test to go with your patch.

    7. techtonik commented on Nov 24, 2010

      techtonikmannequin
      Mannequin

      This one is bug.

    8. dankenigsberg commented on Oct 1, 2011

      dankenigsbergmannequin
      Mannequin

      Here's another take on fixing this bug, with an accompanying unit test. Personally, I'm monkey-patching xml.dom.minidom in order to avoid it, but please consider fixing it properly upstream.

    9. python-dev commented on Oct 1, 2011

      python-devmannequin
      Mannequin

      New changeset 086ca132e161 by R David Murray in branch '3.2':
      bpo-4147: minidom's toprettyxml no longer adds whitespace to text nodes.
      http://hg.python.org/cpython/rev/086ca132e161

      New changeset fa0b1e50270f by R David Murray in branch 'default':
      merge bpo-4147: minidom's toprettyxml no longer adds whitespace to text nodes.
      http://hg.python.org/cpython/rev/fa0b1e50270f

    10. python-dev commented on Oct 1, 2011

      python-devmannequin
      Mannequin

      New changeset 406c5b69cb1b by R David Murray in branch '2.7':
      bpo-4147: minidom's toprettyxml no longer adds whitespace to text nodes.
      http://hg.python.org/cpython/rev/406c5b69cb1b

    11. bitdancer commented on Oct 1, 2011

      @bitdancer
      Member

      This looks correct to me, and it tested out fine on the test suite (and the provided test failed without the provided fix), so I committed it.

      I have a small concern that the change in output might be a bit radical for a bug fix release, but it does seem to me that this is clearly a bug. If people think it shouldn't go in the bug fix releases let me know and I'll back it out.

      Thanks for the patch, Dan.

    12. 2 remaining items

    13. dankenigsberg commented on Oct 2, 2011

      dankenigsbergmannequin
      Mannequin

      Oh dear.

      Thanks, Enzio, for pointing out that former patch is wrong. It is also quite naive, since the whole NATURE of toprettyprint() is to add whitespace to Text nodes. Tomas Lee's http://bugs.python.org/file11832/minidom-toprettyxml-01.patch made an effort to touch only "simple" Text nodes, that are confined within a single <elem></elem>.

      I did not expect http://bugs.python.org/file23286/minidom-Text-toprettyxml.patch to get in so quickly, after the former one spent several years on queue. However now is time to fix it, possible by my second patch.

    14. dankenigsberg commented on Oct 2, 2011

      dankenigsbergmannequin
      Mannequin

      btw, http://www.w3.org/TR/xml/#sec-white-space is a bit vague on how should a parser deal with whitespace, and seems to allow non-preservation of text nodes. Preserving "simple" text nodes is allowed, too, and is more polite to applications reading the prettyxml.

    15. bitdancer commented on Oct 2, 2011

      @bitdancer
      Member

      Heh, you happened to post your patch at a time when I wanted something to do as a break from something I didn't want to do...and I *thought* I understood the problem, after reading the various links. But clearly I didn't. We don't have someone who has stepped forward to be xml maintainer, so I just went ahead and committed it.

      I should find time to look at your new patch some time today, or perhaps Ezio will have time. (Clearly minidom doesn't have enough tests.)

    16. techtonik commented on Nov 15, 2011

      techtonikmannequin
      Mannequin

      I would revert this patch (leaving several test cases though) until a proper fix is available.

    17. bitdancer commented on Nov 15, 2011

      @bitdancer
      Member

      Yeah, I just haven't found time to do the revert yet (my first naive attempt using hg commands failed and I haven't found time to figure it out or do the reverse-patch method).

    18. self-assigned this
      on Nov 15, 2011
    19. ezio-melotti commented on Nov 15, 2011

      @ezio-melotti
      Member

      Here is a new patch based on Dan's last patch.
      Correct me if I'm wrong, but it seems to me that it's not possible for a node to have only text-nodes as children and yet have more than one child; i.e. you can't have two or more adjacent text nodes, because they would be considered as a single text node. I therefore changed the check with all() to check if there's only a child and if it's a text node.
      I also added a test that checks where the \n and \t are added, because testing only that the DOM is preserved is not enough.

    20. dankenigsberg commented on Nov 15, 2011

      dankenigsbergmannequin
      Mannequin

      Technically, adjacent Text nodes are not illegal, but preserving this oddity in pretty-print is impossible. It is perfectly fine to pretty-print only the simple case of len()==1.

    21. ezio-melotti commented on Nov 18, 2011

      @ezio-melotti
      Member

      I did some tests, creating an element ('elem') that contains two adjacent text nodes ('text'). With my latest patch the prettyprint is:
      <?xml version="1.0" ?>
      <elem>
      text
      text
      </elem>

      Here both the text nodes are printed on a newline and they are indented.

      With your patch it should be:
      <?xml version="1.0" ?>
      <elem>texttext</elem>

      I'm not sure there's any reason to prefer the second option though.

    22. python-dev commented on Nov 18, 2011

      python-devmannequin
      Mannequin

      New changeset 7262f8f276ff by Ezio Melotti in branch '2.7':
      bpo-4147: minidom's toprettyxml no longer adds whitespace around a text node when it is the only child of an element. Initial patch by Dan Kenigsberg.
      http://hg.python.org/cpython/rev/7262f8f276ff

      New changeset 5401daa96a21 by Ezio Melotti in branch '3.2':
      bpo-4147: minidom's toprettyxml no longer adds whitespace around a text node when it is the only child of an element. Initial patch by Dan Kenigsberg.
      http://hg.python.org/cpython/rev/5401daa96a21

      New changeset cb6614e3438b by Ezio Melotti in branch 'default':
      bpo-4147: merge with 3.2.
      http://hg.python.org/cpython/rev/cb6614e3438b

    23. ezio-melotti commented on Nov 18, 2011

      @ezio-melotti
      Member

      I committed my patch with a few more tests. This should be fixed now.
      Thanks for the report and the patch!

    24. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    release-blockerstdlibStandard Library Python modules in the Lib/ directorytopic-XMLtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions