Skip to content

Commit 4757c2a

Browse files
authored
Merge pull request #407 from ocrybit/add-code-block
エディターのコードブロック追加に伴って<pre>と<code>タグを許可
2 parents 0924c11 + b5c7baf commit 4757c2a

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/common/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@
276276
html_allowed_tags = ['a', 'b', 'blockquote', 'br', 'h2', 'h3', 'i', 'p', 'u', 'img', 'hr',
277277
'div', 'figure', 'figcaption']
278278
html_allowed_tags_v2 = ['a', 'strong', 'blockquote', 'br', 'h2', 'h3', 'i', 'p', 'img', 'hr',
279-
'figure', 'figcaption', 'oembed']
279+
'figure', 'figcaption', 'oembed', 'pre', 'code']
280280

281281
ng_user_name = [
282282
'about', 'account', 'activity', 'add', 'admin', 'all', 'alpha', 'analysis',

src/common/text_sanitizer.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import settings
22
import bleach
33
import os
4+
import re
45
from urllib.parse import urlparse
56
from jsonschema import ValidationError
67

@@ -106,6 +107,13 @@ def allow_oembed_v2(tag, name, value):
106107
return is_url
107108
return False
108109

110+
@staticmethod
111+
def allow_code_v2(tag, name, value):
112+
if name == 'class':
113+
if re.match("language-", value):
114+
return True
115+
return False
116+
109117
@staticmethod
110118
def sanitize_article_body_v2(text):
111119
if text is None:
@@ -118,7 +126,8 @@ def sanitize_article_body_v2(text):
118126
'a': ['href'],
119127
'img': TextSanitizer.allow_img_v2,
120128
'figure': TextSanitizer.allow_figure_v2,
121-
'oembed': TextSanitizer.allow_oembed_v2
129+
'oembed': TextSanitizer.allow_oembed_v2,
130+
'code': TextSanitizer.allow_code_v2
122131
}
123132
)
124133

tests/common/test_text_sanitizer.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ def test_sanitize_article_body_v2(self):
257257
<figcaption>hoge</figcaption>
258258
</figure>
259259
<p>shift+enter<br>test</p>
260+
<pre><code class="language-javascript">const ALIS = "cool"</code></pre>
260261
'''.format(domain=os.environ['DOMAIN'])
261262

262263
result = TextSanitizer.sanitize_article_body_v2(target_html)
@@ -353,6 +354,19 @@ def test_sanitize_article_body_with_a_unauthorized_class(self):
353354

354355
self.assertEqual(result, expected_html)
355356

357+
def test_sanitize_article_body_with_code_unauthorized_class(self):
358+
target_html = '''
359+
<pre><code class='image hogehoge' data='aaa'></code></pre>
360+
'''
361+
362+
expected_html = '''
363+
<pre><code></code></pre>
364+
'''
365+
366+
result = TextSanitizer.sanitize_article_body_v2(target_html)
367+
368+
self.assertEqual(result, expected_html)
369+
356370
def test_validate_img_url_ok(self):
357371
img_url = 'https://' + os.environ['DOMAIN'] + '/img/test.jpg'
358372
result = TextSanitizer.validate_img_url(img_url)

0 commit comments

Comments
 (0)