2023-03-04 02:19:34 +01:00
|
|
|
import lxml.html
|
2017-10-30 21:11:34 +01:00
|
|
|
from lxml.html.diff import htmldiff
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2013-10-10 21:37:26 +02:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def highlight_html_differences(s1: str, s2: str, msg_id: int | None = None) -> str:
|
2017-10-30 21:11:34 +01:00
|
|
|
retval = htmldiff(s1, s2)
|
2021-10-01 00:14:28 +02:00
|
|
|
fragment = lxml.html.fragment_fromstring(retval, create_parent=True)
|
2013-05-31 21:03:16 +02:00
|
|
|
|
2021-02-12 08:20:45 +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
|
|
|
|
2021-02-12 08:20:45 +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
|
|
|
|
2020-08-07 06:57:18 +02:00
|
|
|
retval = lxml.html.tostring(fragment, encoding="unicode")
|
2013-05-31 21:03:16 +02:00
|
|
|
|
|
|
|
return retval
|