Skip to content

Commit

Permalink
fix CSP warning, remove inline style from FontAwesome moinwiki#1816
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerHaase committed Feb 26, 2025
1 parent 2100f97 commit e6891d0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/moin/macros/FontAwesome.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"""


from moin.utils.tree import moin_page
from moin.utils.tree import html, moin_page
from moin.macros._base import MacroInlineBase, fail_message
from moin.i18n import _

Expand All @@ -39,22 +39,15 @@ def macro(self, content, arguments, page_url, alternative):
try:
int(color[1:], 16)
assert len(color) in (4, 7)
color = f"color: {color}; "
except (ValueError, AssertionError):
color = ""
else:
color = f"color: {color}; " if color.isalpha() else ""

if size:
try:
s = float(size)
assert s > 0.1
assert s < 99
size = f"font-size: {size}em;"
except (ValueError, AssertionError):
size = ""

style = color + size
classes = []
for font in fonts:
f = font if font.startswith("fa-") else "fa-" + font
Expand All @@ -66,6 +59,5 @@ def macro(self, content, arguments, page_url, alternative):
classes = " ".join(classes)

attrib = {moin_page.class_: classes}
if style:
attrib[moin_page.style] = style
attrib[html.data_style] = ",".join((color, size))
return moin_page.span(attrib=attrib)
14 changes: 14 additions & 0 deletions src/moin/static/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,20 @@ $(document).ready(function () {
alert(_("Your edit lock will expire in 1 minute: ") + $('#moin-item-name').val())}, (($("#moin-lock_duration").val() - 60) * 1000));
}

// convert FontAwesome data-style color & font-size attributes to css attr,
// cannot do style in macros/FontAwesome.py because CSP flags inline style attributes
var styl;
var elements = $('[data-style]');
elements.each(function() {
styl = $(this).data('style').split(",");
if (styl.length > 0) {
$(this).css('color', styl[0]);
}
if (styl.length > 1) {
$(this).css('font-size', styl[1] + "em");
}
});

moinFontChangeOnReady();

$('textarea.moin-autosize').autosize();
Expand Down

0 comments on commit e6891d0

Please sign in to comment.