jinja2-htmlcompress
jinja2-htmlcompress copied to clipboard
Whitespace stripped from HTML attributes sometimes...
I haven't figured out exactly what the problem is, but I do have a reproducible test.
# in test_views/views.py
@test_views.route('/test_views/html_compress_bug1')
def html_compres_bug1():
return render_template('test_views/html_compress_bug1.html', broken='other')
<!-- Stored in test_views/html_compress_bug1.html -->
<html>
<head>
<title>Test</title>
<style type="text/css">
.alert-messageother {
background-color: #ff0000;
}
.other {
background-color: #00ff00;
}
</style>
</head>
<body>
<div class="alert-message{{' '}}{{broken}}">
<p>Forced a space, this works</p>
</div>
<div class="alert-message {{broken}}">
<p>Space nuked</p>
</div>
</body>
</html>
Here's another example that doesn't parse correctly:
<p>Please <a href="{{url_for('.login')}}">login</a> to
gets collapsed to:
<p>Please<a href="/login">login</a>to
I think nuking all but one space would be appropriate since no spaces does carry a different meaning to a web browser than one or more spaces.
you should use as explained in documentation {{ " " }} instead of spaces, if you need space before next node
it may also happen with tag attributes - there u also should use this {{ " " }} construct
This is not a smart solution. Is not possible to solve this bug?
EDITED: I edited the line 95: value = _ws_normalize_re.sub(' ', value.strip()) to value = _ws_normalize_re.sub(' ', value)
I've got a fork over in https://github.com/eli-collins/jinja2-htmlcompress which has a rewritten parser, that tries to be inline-block aware, and I've added a test for seans' <p>Please <a href="{{url_for('.login')}}">login</a> to
sample; and it seems to work correctly.