]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Fix broken nl2br filter example 929/head
authorMark Amery <markamery@btinternet.com>
Sun, 25 Nov 2018 17:49:22 +0000 (17:49 +0000)
committerMark Amery <markamery@btinternet.com>
Sun, 25 Nov 2018 17:49:22 +0000 (17:49 +0000)
This example attempts to handle both Windows-style and Unix-style line endings, but does it wrong; as noted at https://stackoverflow.com/q/12521127/1709587, the regex here will match a single \r\n as if it were a double line-break, which results in all single Windows-style line-breaks getting converted into <p> breaks instead of <br> breaks as intended.

This patch fixes that by applying Alan Moore's proposed fix from https://stackoverflow.com/a/12521544/1709587 of using a negative lookahead so that a \r cannot be counted as a line break if it is followed by a \n (while still allowing the \r\n combination to count as a line break).

docs/api.rst

index 6db7507f088229c72c8c622cfaccf1ee16d3b027..47760e365f18e459dc718dd63e7bdb04c978d9c1 100644 (file)
@@ -740,7 +740,7 @@ enabled::
     import re
     from jinja2 import evalcontextfilter, Markup, escape
 
-    _paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')
+    _paragraph_re = re.compile(r'(?:\r\n|\r(?!\n)|\n){2,}')
 
     @evalcontextfilter
     def nl2br(eval_ctx, value):