mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 05:32:44 +00:00 
			
		
		
		
	LibWeb: Support range delection accross nodes with shared parent.
This commit is contained in:
		
							parent
							
								
									43dc47a494
								
							
						
					
					
						commit
						e96faea1a2
					
				
					 2 changed files with 52 additions and 21 deletions
				
			
		|  | @ -31,6 +31,7 @@ | |||
| #include <LibWeb/Page/Frame.h> | ||||
| 
 | ||||
| #include <LibWeb/DOM/Document.h> | ||||
| #include <LibWeb/Dump.h> | ||||
| #include <LibWeb/Layout/InitialContainingBlockBox.h> | ||||
| 
 | ||||
| #include "EditEventHandler.h" | ||||
|  | @ -39,21 +40,55 @@ namespace Web { | |||
| 
 | ||||
| void EditEventHandler::handle_delete(DOM::Range range) | ||||
| { | ||||
|     if (range.start().node() != range.end().node()) | ||||
|     // FIXME: Find a better way of updating the selection and cursor position.
 | ||||
| 
 | ||||
|     if (range.start().node() != range.end().node()) { | ||||
|         if (range.start().node()->parent() == range.end().node()->parent()) { | ||||
|             m_frame.document()->layout_node()->set_selection({}); | ||||
|             m_frame.cursor_position().set_offset(range.start().offset()); | ||||
| 
 | ||||
|             // Remove all intermediate nodes.
 | ||||
|             auto* current = range.start().node()->next_sibling(); | ||||
|             while (current != range.end().node()) { | ||||
|                 auto* next = current->next_sibling(); | ||||
|                 current->parent()->remove_child(*current); | ||||
|                 current = next; | ||||
|             } | ||||
| 
 | ||||
|             if (!is<DOM::Text>(*range.start().node()) || !is<DOM::Text>(*range.end().node())) | ||||
|                 TODO(); | ||||
| 
 | ||||
|             // Join remaining text together.
 | ||||
|             StringBuilder builder; | ||||
|             builder.append(downcast<DOM::Text>(range.start().node())->data().substring_view(0, range.start().offset())); | ||||
|             builder.append(downcast<DOM::Text>(range.end().node())->data().substring_view(range.end().offset())); | ||||
| 
 | ||||
|             range.start().node()->parent()->remove_child(*range.end().node()); | ||||
|             downcast<DOM::Text>(range.start().node())->set_data(builder.to_string()); | ||||
| 
 | ||||
|             range.start().node()->invalidate_style(); | ||||
|         } else { | ||||
|             TODO(); | ||||
|         } | ||||
|     } else { | ||||
|         if (is<DOM::Text>(*range.start().node())) { | ||||
|             m_frame.document()->layout_node()->set_selection({}); | ||||
|             m_frame.cursor_position().set_offset(range.start().offset()); | ||||
| 
 | ||||
|             auto& node = downcast<DOM::Text>(*range.start().node()); | ||||
| 
 | ||||
|             StringBuilder builder; | ||||
| 
 | ||||
|             builder.append(node.data().substring_view(0, range.start().offset())); | ||||
|             builder.append(node.data().substring_view(range.end().offset())); | ||||
|             node.set_data(builder.to_string()); | ||||
| 
 | ||||
|         m_frame.document()->layout_node()->set_selection({}); | ||||
|             node.invalidate_style(); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     // FIXME: We need to remove stale layout nodes when nodes are removed from the DOM. Currently,
 | ||||
|     //        this is the only way to get these to disappear.
 | ||||
|     m_frame.document()->force_layout(); | ||||
| } | ||||
| 
 | ||||
| void EditEventHandler::handle_delete(DOM::Position position) | ||||
|  |  | |||
|  | @ -353,26 +353,22 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin | |||
|                 m_edit_event_handler->handle_delete(range); | ||||
|                 return true; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         // FIXME: Check if this code point is in the printable character range.
 | ||||
| 
 | ||||
|         } else { | ||||
|             m_edit_event_handler->handle_delete(range); | ||||
|             m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point); | ||||
|             return true; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     if (m_frame.cursor_position().is_valid() && m_frame.cursor_position().node()->is_editable()) { | ||||
|         if (key == KeyCode::Key_Backspace) { | ||||
|             m_edit_event_handler->handle_delete(m_frame.cursor_position()); | ||||
|             return true; | ||||
|         } | ||||
| 
 | ||||
|         // FIXME: Check if this code point is in the printable character range.
 | ||||
| 
 | ||||
|         } else { | ||||
|             m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point); | ||||
|             return true; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     return false; | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 asynts
						asynts