Skip to content

Commit 76e58c4

Browse files
sanitizer: allow style for description
1 parent dfef91b commit 76e58c4

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

  • cds/modules/records/serializers

cds/modules/records/serializers/json.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@
3232
has_read_record_permission,
3333
)
3434
from ..utils import HTMLTagRemover, parse_video_chapters, remove_html_tags
35-
from marshmallow_utils.html import sanitize_html
35+
from marshmallow_utils.html import sanitize_html, ALLOWED_HTML_ATTRS, ALLOWED_CSS_STYLES
36+
37+
CUSTOM_ALLOWED_ATTRS = {
38+
**ALLOWED_HTML_ATTRS,
39+
"p": ALLOWED_HTML_ATTRS.get("p", []) + ["style"],
40+
"span": ALLOWED_HTML_ATTRS.get("span", []) + ["style"],
41+
}
42+
43+
CUSTOM_ALLOWED_CSS = ALLOWED_CSS_STYLES + ["color"]
3644

3745

3846
class CDSJSONSerializer(JSONSerializer):
@@ -60,7 +68,11 @@ def _sanitize_metadata(self, metadata):
6068
if "description" in metadata:
6169
description = metadata["description"]
6270
description = self.html_tag_remover.unescape(description)
63-
metadata["description"] = sanitize_html(description)
71+
metadata["description"] = sanitize_html(
72+
description,
73+
attrs=CUSTOM_ALLOWED_ATTRS,
74+
css_styles=CUSTOM_ALLOWED_CSS,
75+
)
6476

6577
if "translations" in metadata:
6678
for t in metadata["translations"]:
@@ -108,12 +120,12 @@ def preprocess_record(self, pid, record, links_factory=None):
108120
except KeyError:
109121
# ignore error if keys are missing in the metadata
110122
pass
111-
112-
description = metadata.get('description', '')
123+
124+
description = metadata.get("description", "")
113125
if description:
114-
metadata['chapters'] = parse_video_chapters(description)
126+
metadata["chapters"] = parse_video_chapters(description)
115127
else:
116-
metadata['chapters'] = []
128+
metadata["chapters"] = []
117129

118130
return result
119131

0 commit comments

Comments
 (0)