escape title correctly for use in css content property
This commit is contained in:
parent
769ad0a3b8
commit
896e7fd7d5
4 changed files with 22 additions and 2 deletions
0
schickmacher/renderer/templatetags/__init__.py
Normal file
0
schickmacher/renderer/templatetags/__init__.py
Normal file
8
schickmacher/renderer/templatetags/extra_filters.py
Normal file
8
schickmacher/renderer/templatetags/extra_filters.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
from django import template
|
||||
import re
|
||||
|
||||
register = template.Library()
|
||||
|
||||
@register.filter
|
||||
def escape_css_content(value):
|
||||
return re.sub(r'''['"\n\\]''', lambda m: '\{:06X}'.format(ord(m.group())), value)
|
12
schickmacher/renderer/tests/test_extra_filters.py
Normal file
12
schickmacher/renderer/tests/test_extra_filters.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
from schickmacher.renderer.templatetags.extra_filters import escape_css_content
|
||||
|
||||
# we use the padded 6 digit css escape sequences
|
||||
# https://www.w3.org/International/questions/qa-escapes
|
||||
def test_css_content():
|
||||
assert escape_css_content("head\"tail") == "head\\000022tail"
|
||||
assert escape_css_content("head\'tail") == "head\\000027tail"
|
||||
assert escape_css_content("head\\tail") == "head\\00005Ctail"
|
||||
assert escape_css_content("head\ntail") == "head\\00000Atail"
|
||||
assert escape_css_content("\"\"") == "\\000022\\000022"
|
||||
assert escape_css_content("\\\\") == "\\00005C\\00005C"
|
||||
assert escape_css_content("ABCD1234<li>") == "ABCD1234<li>"
|
|
@ -1,4 +1,4 @@
|
|||
{% load static compress tz %}<!DOCTYPE html>
|
||||
{% load extra_filters %}{% load static compress tz %}<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
|
@ -16,7 +16,7 @@
|
|||
content: "{{ date | date:'d.m.Y' }}";
|
||||
}
|
||||
@top-left {
|
||||
content: "{{ title }}"
|
||||
content: "{{ title | escape_css_content | safe }}"
|
||||
}
|
||||
@top-right-corner {
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue