jinja2-htmlcompress icon indicating copy to clipboard operation
jinja2-htmlcompress copied to clipboard

Whitespace stripped from HTML attributes sometimes...

Open sean- opened this issue 13 years ago • 4 comments

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>

sean- avatar Sep 27 '11 01:09 sean-

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.

sean- avatar Oct 01 '11 01:10 sean-

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

test157 avatar Dec 12 '11 19:12 test157

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)

tupy avatar Dec 26 '11 20:12 tupy

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.

eli-collins avatar May 22 '17 19:05 eli-collins