mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 21:52:45 +00:00 
			
		
		
		
	LibHTML+IRCClient: Add an escape_html_entities() helper
This simple helper escapes '<', '>' and '&' so they can be used in HTML text without interfering with the parser. Use this in IRCClient to prevent incoming messages from messing with the DOM :^)
This commit is contained in:
		
							parent
							
								
									d17930d9e2
								
							
						
					
					
						commit
						a377e8d3f5
					
				
					 3 changed files with 19 additions and 2 deletions
				
			
		|  | @ -54,7 +54,7 @@ void IRCLogBuffer::add_message(char prefix, const String& name, const String& te | ||||||
|         color.to_string().characters(), |         color.to_string().characters(), | ||||||
|         timestamp_string().characters(), |         timestamp_string().characters(), | ||||||
|         nick_string.characters(), |         nick_string.characters(), | ||||||
|         text.characters()); |         escape_html_entities(text).characters()); | ||||||
|     auto fragment = parse_html_fragment(*m_document, html); |     auto fragment = parse_html_fragment(*m_document, html); | ||||||
|     m_container_element->append_child(fragment->remove_child(*fragment->first_child())); |     m_container_element->append_child(fragment->remove_child(*fragment->first_child())); | ||||||
|     m_document->force_layout(); |     m_document->force_layout(); | ||||||
|  | @ -69,7 +69,7 @@ void IRCLogBuffer::add_message(const String& text, Color color) | ||||||
|         "</div>", |         "</div>", | ||||||
|         color.to_string().characters(), |         color.to_string().characters(), | ||||||
|         timestamp_string().characters(), |         timestamp_string().characters(), | ||||||
|         text.characters()); |         escape_html_entities(text).characters()); | ||||||
|     auto fragment = parse_html_fragment(*m_document, html); |     auto fragment = parse_html_fragment(*m_document, html); | ||||||
|     m_container_element->append_child(fragment->remove_child(*fragment->first_child())); |     m_container_element->append_child(fragment->remove_child(*fragment->first_child())); | ||||||
|     m_document->force_layout(); |     m_document->force_layout(); | ||||||
|  |  | ||||||
|  | @ -339,3 +339,19 @@ RefPtr<Document> parse_html_document(const StringView& html, const URL& url) | ||||||
| 
 | 
 | ||||||
|     return document; |     return document; | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | String escape_html_entities(const StringView& html) | ||||||
|  | { | ||||||
|  |     StringBuilder builder; | ||||||
|  |     for (int i = 0; i < html.length(); ++i) { | ||||||
|  |         if (html[i] == '<') | ||||||
|  |             builder.append("<"); | ||||||
|  |         else if (html[i] == '>') | ||||||
|  |             builder.append(">"); | ||||||
|  |         else if (html[i] == '&') | ||||||
|  |             builder.append("&"); | ||||||
|  |         else | ||||||
|  |             builder.append(html[i]); | ||||||
|  |     } | ||||||
|  |     return builder.to_string(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -7,3 +7,4 @@ class DocumentFragment; | ||||||
| 
 | 
 | ||||||
| RefPtr<Document> parse_html_document(const StringView&, const URL& = URL()); | RefPtr<Document> parse_html_document(const StringView&, const URL& = URL()); | ||||||
| RefPtr<DocumentFragment> parse_html_fragment(Document&, const StringView&); | RefPtr<DocumentFragment> parse_html_fragment(Document&, const StringView&); | ||||||
|  | String escape_html_entities(const StringView&); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling