Skip to content

Minidom should not pretty-print inside text elements #81623

Description

@mitar
mannequin
BPO 37442
Nosy @mitar

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 = None
closed_at = None
created_at = <Date 2019-06-28.18:04:51.410>
labels = ['expert-XML']
title = 'Minidom should not pretty-print inside text elements'
updated_at = <Date 2019-06-28.18:04:51.410>
user = 'https://github.com/mitar'

bugs.python.org fields:

activity = <Date 2019-06-28.18:04:51.410>
actor = 'mitar'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['XML']
creation = <Date 2019-06-28.18:04:51.410>
creator = 'mitar'
dependencies = []
files = []
hgrepos = []
issue_num = 37442
keywords = []
message_count = 1.0
messages = ['346846']
nosy_count = 1.0
nosy_names = ['mitar']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue37442'
versions = ['Python 3.6']

Linked PRs

Activity

  1. mitar commented on Jun 28, 2019

    mitarmannequin
    MannequinAuthor

    Inside text elements both in HTML and SVG white-space is significant and introduces differences in how things are rendered. By default in general all white-space is collapsed into one space and then this is rendered, adding additional text content.

    I observed this while working with SVG which can have content like:

    <text><tspan>foo</tspan><tspan>bar</tspan></text>

    After pretty-printing it with minidom, and white-space collapsing, the following is what is input to SVG rendering:

    <text> <tspan>foo</tspan> <tspan>bar</tspan> </text>

    And space between "foo" and "bar" is now visible and while before it was one word to the user, now it is shown as two.

    Related issue: mozman/svgwrite#58

    I think pretty-printing not add whitespace inside text elements.

  2. transferred this issue fromon Apr 10, 2022
  3. Andrej730 commented on Mar 16, 2023

    @Andrej730
    Contributor

    Still an issue for me with svgwrite because of that you basically either avoid pretty-printing or find some hack to remove the text inside tag - e.g. if you know font-size for children tags you can do: font-size: 0 on text.

  4. added a commit that references this issue on Aug 30, 2026
  5. serhiy-storchaka commented on Aug 30, 2026

    @serhiy-storchaka
    Member

    PR #156660 stops pretty-printing from adding whitespace inside an element which is marked with xml:space="preserve" or which contains text, in both xml.dom.minidom.toprettyxml() and xml.etree.ElementTree.indent().

    toprettyxml() also takes into account the content model declared in the DTD: only white space in element content is ignorable (XML 1.0, 3.2.1). With such declaration the reported case is no longer changed:

    >>> src = ("<!DOCTYPE text [<!ELEMENT text (#PCDATA|tspan)*><!ELEMENT tspan (#PCDATA)>]>"
    ...        "<text><tspan>foo</tspan><tspan>bar</tspan></text>")
    >>> parseString(src).documentElement.toprettyxml(indent="  ")
    '<text><tspan>foo</tspan><tspan>bar</tspan></text>\n'

    Without a DTD nothing in the document says that the whitespace between the tspan elements is significant, and xml:space="preserve" is the only way to express it. ElementTree has no such information at all.

  6. added 2 commits that reference this issue on Sep 13, 2026
  7. added a commit that references this issue on Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions