mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Utility function to sanitize simple inline HTML markup
This commit is contained in:
parent
1aa59276bd
commit
c651219654
@ -198,6 +198,25 @@ def conditional_timeout(elem_id, timeout, func):
|
|||||||
func.call(elem)
|
func.call(elem)
|
||||||
window.setTimeout(ct_impl, timeout)
|
window.setTimeout(ct_impl, timeout)
|
||||||
|
|
||||||
|
|
||||||
|
def simple_markup(html):
|
||||||
|
html = (html or '').replace(/\u{ffff}/ug, '').replace(
|
||||||
|
/<\s*(\/?[a-zA-Z1-6]+)[^>]*>/g, def (match, tag):
|
||||||
|
tag = tag.toLowerCase()
|
||||||
|
is_closing = '/' if tag[0] is '/' else ''
|
||||||
|
if is_closing:
|
||||||
|
tag = tag[1:]
|
||||||
|
if simple_markup.allowed_tags.indexOf(tag) < 0:
|
||||||
|
tag = 'span'
|
||||||
|
return f'\uffff{is_closing}{tag}\uffff'
|
||||||
|
)
|
||||||
|
div = document.createElement('b')
|
||||||
|
div.textContent = html
|
||||||
|
html = div.innerHTML
|
||||||
|
return html.replace(/\u{ffff}(\/?[a-z1-6]+)\u{ffff}/ug, '<$1>')
|
||||||
|
simple_markup.allowed_tags = v"'b|i|br|h1|h2|h3|h4|h5|h6|div|em|strong|span'.split('|')"
|
||||||
|
|
||||||
|
|
||||||
if __name__ is '__main__':
|
if __name__ is '__main__':
|
||||||
from pythonize import strings
|
from pythonize import strings
|
||||||
strings()
|
strings()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user