Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## 4.0.1 (unreleased)


- Nothing changed yet.
- Do not considere any attribute multiline,
so that we can preserve whitespaces if desired. @ale-rt, @thet


## 4.0.0 (2026-04-10)
Expand Down
18 changes: 14 additions & 4 deletions zpretty/prettifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ def __init__(self, filename="", text="", encoding="utf8"):

self.root = self.pretty_element(self.soup, -1)

def text2soup(self, text):
"""Build a BeautifulSoup object preserving raw attribute values.

In particular, avoid splitting multi-valued attributes like ``class``
into lists so original whitespace/newlines can be preserved.
"""
kwargs = {"multi_valued_attributes": None}
if self.builder is not None:
kwargs["builder"] = self.builder
return BeautifulSoup(text, self.parser, **kwargs)

def fix_rcdata_markup(self, soup):
"""Parse markup-like text inside RCDATA tags as child nodes.

Expand All @@ -87,9 +98,8 @@ def fix_rcdata_markup(self, soup):
raw_content = "".join(str(node) for node in tag.contents)

null_tag_name = self.pretty_element.null_tag_name
fragment_soup = BeautifulSoup(
fragment_soup = self.text2soup(
f"<{null_tag_name}>{raw_content}</{null_tag_name}>",
self.parser,
)
fragment_root = getattr(fragment_soup, null_tag_name, None)
if not fragment_root:
Expand Down Expand Up @@ -145,7 +155,7 @@ def get_soup(self, text):

If the text is not some xml like think a dummy element will be used to wrap it.
"""
original_soup = BeautifulSoup(text, self.parser)
original_soup = self.text2soup(text)
try:
first_el = next(original_soup.children)
except StopIteration:
Expand All @@ -156,7 +166,7 @@ def get_soup(self, text):
markup = "<{null}>{text}</{null}>".format(
null=self.pretty_element.null_tag_name, text=text
)
wrapped_soup = BeautifulSoup(markup, self.parser)
wrapped_soup = self.text2soup(markup)
return getattr(wrapped_soup, self.pretty_element.null_tag_name)

def pretty_print(self, el):
Expand Down
25 changes: 25 additions & 0 deletions zpretty/tests/original/sample_html.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,31 @@

<pre> </pre>

<section class="class-a">
<div class="
class-b
class-c

${'class-d' if True else ''}
button button-important
pil pil-${'blue' if True else ''}

pat-inject
"
data-pat-inject="
source: .main-content .important-stuff;
target: .class-b .target
&amp;&amp;
source: .header #logo;
target: .logo;
${'add: class-d' if True else ''}
history:;
"
>
okay
</div>
</section>

<tal:example>
<p>
Inside a tal!
Expand Down
24 changes: 24 additions & 0 deletions zpretty/tests/original/sample_pt.pt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@
Foo
Bar
</pre>
<section class="class-a">
<div class="
class-b
class-c

${'class-d' if True else ''}
button button-important
pil pil-${'blue' if True else ''}

pat-inject
"
data-pat-inject="
source: .main-content .important-stuff;
target: .class-b .target
&amp;&amp;
source: .header #logo;
target: .logo;
${'add: class-d' if True else ''}
history:;
"
>
okay
</div>
</section>
<form action="#"
method="post"
>
Expand Down
Loading