Skip to content

Commit

Permalink
Merge pull request #386 from malthe/issue-385-auto-boolean-attributes
Browse files Browse the repository at this point in the history
Automatically configure boolean attributes for non-XML documents
  • Loading branch information
malthe committed Sep 22, 2023
2 parents a1fccb7 + 3b3625f commit 8d411b2
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 22 deletions.
10 changes: 9 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ Changes

In next release ...

-
- An XML document provided as a string (i.e. decoded) now correctly
has its content encoding parsed.

- Boolean attributes are now automatically configured for templates in
non-XML mode, presuming that we're being used to generate HTML.

This means that the same loading mechanism can be used for both XML-
and HTML-based templates.


4.1.0 (2023-08-29)
------------------
Expand Down
20 changes: 11 additions & 9 deletions src/chameleon/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
from .utils import DebuggingOutputStream
from .utils import Scope
from .utils import create_formatted_exception
from .utils import detect_encoding
from .utils import join
from .utils import mangle
from .utils import raise_with_traceback
from .utils import read_bytes
from .utils import read_xml_encoding
from .utils import value_repr


Expand Down Expand Up @@ -87,6 +89,7 @@ class BaseTemplate:
"""

default_encoding = "utf-8"
default_content_type = None

# This attribute is strictly informational in this template class
# and is used in exception formatting. It may be set on
Expand Down Expand Up @@ -224,11 +227,15 @@ def write(self, body):
body, encoding, content_type = read_bytes(
body, self.default_encoding
)
elif body.startswith('<?xml'):
content_type = 'text/xml'
encoding = read_xml_encoding(body.encode("utf-8"))
else:
content_type = body.startswith('<?xml')
encoding = None
content_type, encoding = detect_encoding(
body, self.default_encoding
)

self.content_type = content_type
self.content_type = content_type or self.default_content_type
self.content_encoding = encoding

self.cook(body)
Expand Down Expand Up @@ -344,12 +351,7 @@ def read(self):
data, self.default_encoding
)

# In non-XML mode, we support various platform-specific line
# endings and convert them to the UNIX newline character
if content_type != "text/xml" and '\r' in body:
body = body.replace('\r\n', '\n').replace('\r', '\n')

self.content_type = content_type
self.content_type = content_type or self.default_content_type
self.content_encoding = encoding

return body
Expand Down
4 changes: 2 additions & 2 deletions src/chameleon/tests/outputs/028.pt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div xmlns="http://www.w3.org/1999/xhtml">
<option selected="True"></option>
<option selected="False"></option>
<option selected="selected"></option>
<option></option>
<option></option>
</div>
4 changes: 2 additions & 2 deletions src/chameleon/tests/outputs/071.pt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<body>
<input type="input" checked="True" />
<input type="input" checked="False" />
<input type="input" checked="checked" />
<input type="input" />
<input type="input" />
<input type="input" checked="checked" />
<input type="input" checked />
Expand Down

0 comments on commit 8d411b2

Please sign in to comment.