mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	`rendered_content` in historical messages may be empty; examining the history of them may thus require diff'ing two empty strings, which itself produces an empty string. Use `lxml.html.fragment_fromstring` to be able to successfully parse these, rather than 500. Part of #19559.
		
			
				
	
	
		
			22 lines
		
	
	
		
			596 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			22 lines
		
	
	
		
			596 B
		
	
	
	
		
			Python
		
	
	
	
	
	
from typing import Optional
 | 
						|
 | 
						|
import lxml
 | 
						|
from lxml.html.diff import htmldiff
 | 
						|
 | 
						|
 | 
						|
def highlight_html_differences(s1: str, s2: str, msg_id: Optional[int] = None) -> str:
 | 
						|
    retval = htmldiff(s1, s2)
 | 
						|
    fragment = lxml.html.fragment_fromstring(retval, create_parent=True)
 | 
						|
 | 
						|
    for elem in fragment.cssselect("del"):
 | 
						|
        elem.tag = "span"
 | 
						|
        elem.set("class", "highlight_text_deleted")
 | 
						|
 | 
						|
    for elem in fragment.cssselect("ins"):
 | 
						|
        elem.tag = "span"
 | 
						|
        elem.set("class", "highlight_text_inserted")
 | 
						|
 | 
						|
    retval = lxml.html.tostring(fragment, encoding="unicode")
 | 
						|
 | 
						|
    return retval
 |