mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 14:02:46 +00:00 
			
		
		
		
	LibWeb: Add naive support for document.querySelectorAll()
This currently returns a JS::Array of elements matching a selector. The more correct behavior would be to return a static NodeList, but as we don't have NodeLists right now, that'll be a task for the future.
This commit is contained in:
		
							parent
							
								
									c56acba75e
								
							
						
					
					
						commit
						0f7bcd4111
					
				
					 11 changed files with 182 additions and 78 deletions
				
			
		|  | @ -26,6 +26,8 @@ | |||
| 
 | ||||
| #include <AK/FlyString.h> | ||||
| #include <LibJS/Interpreter.h> | ||||
| #include <LibJS/Runtime/Array.h> | ||||
| #include <LibJS/Runtime/Error.h> | ||||
| #include <LibJS/Runtime/PrimitiveString.h> | ||||
| #include <LibJS/Runtime/Value.h> | ||||
| #include <LibWeb/Bindings/DocumentWrapper.h> | ||||
|  | @ -39,6 +41,7 @@ DocumentWrapper::DocumentWrapper(Document& document) | |||
|     : NodeWrapper(document) | ||||
| { | ||||
|     put_native_function("getElementById", get_element_by_id); | ||||
|     put_native_function("querySelectorAll", query_selector_all); | ||||
| } | ||||
| 
 | ||||
| DocumentWrapper::~DocumentWrapper() | ||||
|  | @ -55,22 +58,50 @@ const Document& DocumentWrapper::node() const | |||
|     return static_cast<const Document&>(NodeWrapper::node()); | ||||
| } | ||||
| 
 | ||||
| JS::Value DocumentWrapper::get_element_by_id(JS::Interpreter& interpreter) | ||||
| static Document* document_from(JS::Interpreter& interpreter) | ||||
| { | ||||
|     auto* this_object = interpreter.this_value().to_object(interpreter.heap()); | ||||
|     if (!this_object) | ||||
|         return {}; | ||||
|     // FIXME: Verify that it's a DocumentWrapper somehow!
 | ||||
|     auto& node = static_cast<DocumentWrapper*>(this_object)->node(); | ||||
|     if (StringView("DocumentWrapper") != this_object->class_name()) { | ||||
|         interpreter.throw_exception<JS::Error>("TypeError", "That's not a DocumentWrapper, bro."); | ||||
|         return {}; | ||||
|     } | ||||
|     return &static_cast<DocumentWrapper*>(this_object)->node(); | ||||
| } | ||||
| 
 | ||||
| JS::Value DocumentWrapper::get_element_by_id(JS::Interpreter& interpreter) | ||||
| { | ||||
|     auto* document = document_from(interpreter); | ||||
|     if (!document) | ||||
|         return {}; | ||||
|     auto& arguments = interpreter.call_frame().arguments; | ||||
|     if (arguments.is_empty()) | ||||
|         return JS::js_null(); | ||||
|     auto id = arguments[0].to_string(); | ||||
|     auto* element = node.get_element_by_id(id); | ||||
|     auto* element = document->get_element_by_id(id); | ||||
|     if (!element) | ||||
|         return JS::js_null(); | ||||
|     return wrap(interpreter.heap(), const_cast<Element&>(*element)); | ||||
| } | ||||
| 
 | ||||
| JS::Value DocumentWrapper::query_selector_all(JS::Interpreter& interpreter) | ||||
| { | ||||
|     auto* document = document_from(interpreter); | ||||
|     if (!document) | ||||
|         return {}; | ||||
|     auto& arguments = interpreter.call_frame().arguments; | ||||
|     if (arguments.is_empty()) | ||||
|         return JS::js_null(); | ||||
|     auto selector = arguments[0].to_string(); | ||||
|     auto elements = document->query_selector_all(selector); | ||||
|     // FIXME: This should be a static NodeList, not a plain JS::Array.
 | ||||
|     auto* node_list = interpreter.heap().allocate<JS::Array>(); | ||||
|     for (auto& element : elements) { | ||||
|         node_list->push(wrap(interpreter.heap(), element)); | ||||
|     } | ||||
|     return node_list; | ||||
| } | ||||
| 
 | ||||
| } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling