html_filter: don't remove \n in <pre> content

This commit is contained in:
miruka 2019-10-24 08:06:50 -04:00
parent c4b05befa4
commit 5522de5165
2 changed files with 16 additions and 7 deletions

View File

@ -45,8 +45,6 @@
- Fixes
- Newlines and quote after newline in room subtitle
- Newlines in code blocks
- Sending a message with `<thing>`
- Remove usage of `splice()`
- Event delegates changing height don't scroll the list
- When selecting text and scrolling up, selection stops working after a while

View File

@ -77,10 +77,6 @@ class HtmlFilter:
def filter_inline(self, html: str, outgoing: bool = False) -> str:
html = self._inline_sanitizer.sanitize(html)
# Remove excess newlines to avoid additional blank lines with
# HTML/CSS using `white-space: pre`
html = self.newlines_regex.sub(r"\1", html)
if outgoing:
return html
@ -92,7 +88,6 @@ class HtmlFilter:
def filter(self, html: str, outgoing: bool = False) -> str:
html = self._sanitizer.sanitize(html).rstrip("\n")
html = self.newlines_regex.sub(r"\1", html)
if outgoing:
return html
@ -149,6 +144,7 @@ class HtmlFilter:
sanitizer.target_blank_noopener,
self._process_span_font,
self._img_to_a,
self._remove_extra_newlines,
],
"element_postprocessors": [],
"is_mergeable": lambda e1, e2: e1.attrib == e2.attrib,
@ -178,4 +174,19 @@ class HtmlFilter:
return el
def _remove_extra_newlines(self, el: HtmlElement) -> HtmlElement:
# Remove excess \n characters to avoid additional blank lines with
# HTML/CSS using `white-space: pre`, except in <pre> content.
pre_parent = any(parent.tag == "pre" for parent in el.iterancestors())
if el.tag != "pre" and not pre_parent:
if el.text:
el.text = self.newlines_regex.sub(r"\1", el.text)
if el.tail:
el.tail = self.newlines_regex.sub(r"\1", el.tail)
return el
HTML_FILTER = HtmlFilter()