Serenity Operating System
at hosted 107 lines 3.8 kB view raw
1/* 2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/FlyString.h> 28#include <LibJS/Interpreter.h> 29#include <LibJS/Runtime/Array.h> 30#include <LibJS/Runtime/Error.h> 31#include <LibJS/Runtime/PrimitiveString.h> 32#include <LibJS/Runtime/Value.h> 33#include <LibWeb/Bindings/DocumentWrapper.h> 34#include <LibWeb/DOM/Document.h> 35#include <LibWeb/DOM/Element.h> 36 37namespace Web { 38namespace Bindings { 39 40DocumentWrapper::DocumentWrapper(Document& document) 41 : NodeWrapper(document) 42{ 43 put_native_function("getElementById", get_element_by_id, 1); 44 put_native_function("querySelectorAll", query_selector_all, 1); 45} 46 47DocumentWrapper::~DocumentWrapper() 48{ 49} 50 51Document& DocumentWrapper::node() 52{ 53 return static_cast<Document&>(NodeWrapper::node()); 54} 55 56const Document& DocumentWrapper::node() const 57{ 58 return static_cast<const Document&>(NodeWrapper::node()); 59} 60 61static Document* document_from(JS::Interpreter& interpreter) 62{ 63 auto* this_object = interpreter.this_value().to_object(interpreter.heap()); 64 if (!this_object) 65 return {}; 66 if (StringView("DocumentWrapper") != this_object->class_name()) { 67 interpreter.throw_exception<JS::Error>("TypeError", "That's not a DocumentWrapper, bro."); 68 return {}; 69 } 70 return &static_cast<DocumentWrapper*>(this_object)->node(); 71} 72 73JS::Value DocumentWrapper::get_element_by_id(JS::Interpreter& interpreter) 74{ 75 auto* document = document_from(interpreter); 76 if (!document) 77 return {}; 78 auto& arguments = interpreter.call_frame().arguments; 79 if (arguments.is_empty()) 80 return JS::js_null(); 81 auto id = arguments[0].to_string(); 82 auto* element = document->get_element_by_id(id); 83 if (!element) 84 return JS::js_null(); 85 return wrap(interpreter.heap(), const_cast<Element&>(*element)); 86} 87 88JS::Value DocumentWrapper::query_selector_all(JS::Interpreter& interpreter) 89{ 90 auto* document = document_from(interpreter); 91 if (!document) 92 return {}; 93 auto& arguments = interpreter.call_frame().arguments; 94 if (arguments.is_empty()) 95 return JS::js_null(); 96 auto selector = arguments[0].to_string(); 97 auto elements = document->query_selector_all(selector); 98 // FIXME: This should be a static NodeList, not a plain JS::Array. 99 auto* node_list = interpreter.heap().allocate<JS::Array>(); 100 for (auto& element : elements) { 101 node_list->push(wrap(interpreter.heap(), element)); 102 } 103 return node_list; 104} 105 106} 107}