2017-10-30 21:11:34 +01:00
|
|
|
import lxml
|
2016-06-17 16:59:29 +02:00
|
|
|
|
2017-10-30 21:11:34 +01:00
|
|
|
from lxml.html.diff import htmldiff
|
|
|
|
from typing import Optional, Text
|
2013-10-10 21:37:26 +02:00
|
|
|
|
2017-10-30 21:11:34 +01:00
|
|
|
def highlight_with_class(text, klass):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text, Text) -> Text
|
2013-05-31 21:03:16 +02:00
|
|
|
return '<span class="%s">%s</span>' % (klass, text)
|
|
|
|
|
2017-10-12 08:38:02 +02:00
|
|
|
def highlight_html_differences(s1, s2, msg_id=None):
|
|
|
|
# type: (Text, Text, Optional[int]) -> Text
|
2017-10-30 21:11:34 +01:00
|
|
|
retval = htmldiff(s1, s2)
|
|
|
|
fragment = lxml.html.fromstring(retval) # type: ignore # https://github.com/python/typeshed/issues/525
|
2013-05-31 21:03:16 +02:00
|
|
|
|
2017-10-30 21:11:34 +01:00
|
|
|
for elem in fragment.cssselect('del'):
|
|
|
|
elem.tag = 'span'
|
|
|
|
elem.set('class', 'highlight_text_deleted')
|
2017-10-12 08:38:02 +02:00
|
|
|
|
2017-10-30 21:11:34 +01:00
|
|
|
for elem in fragment.cssselect('ins'):
|
|
|
|
elem.tag = 'span'
|
|
|
|
elem.set('class', 'highlight_text_inserted')
|
2013-05-31 21:03:16 +02:00
|
|
|
|
2017-10-30 21:11:34 +01:00
|
|
|
retval = lxml.html.tostring(fragment) # type: ignore # https://github.com/python/typeshed/issues/525
|
2013-05-31 21:03:16 +02:00
|
|
|
|
|
|
|
return retval
|