DocumentWrapper.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include <AK/FlyString.h>
  27. #include <LibJS/Interpreter.h>
  28. #include <LibJS/Runtime/Array.h>
  29. #include <LibJS/Runtime/Error.h>
  30. #include <LibJS/Runtime/PrimitiveString.h>
  31. #include <LibJS/Runtime/Value.h>
  32. #include <LibWeb/Bindings/DocumentWrapper.h>
  33. #include <LibWeb/DOM/Document.h>
  34. #include <LibWeb/DOM/Element.h>
  35. namespace Web {
  36. namespace Bindings {
  37. DocumentWrapper::DocumentWrapper(Document& document)
  38. : NodeWrapper(document)
  39. {
  40. put_native_function("getElementById", get_element_by_id, 1);
  41. put_native_function("querySelectorAll", query_selector_all, 1);
  42. }
  43. DocumentWrapper::~DocumentWrapper()
  44. {
  45. }
  46. Document& DocumentWrapper::node()
  47. {
  48. return static_cast<Document&>(NodeWrapper::node());
  49. }
  50. const Document& DocumentWrapper::node() const
  51. {
  52. return static_cast<const Document&>(NodeWrapper::node());
  53. }
  54. static Document* document_from(JS::Interpreter& interpreter)
  55. {
  56. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  57. if (!this_object)
  58. return {};
  59. if (StringView("DocumentWrapper") != this_object->class_name()) {
  60. interpreter.throw_exception<JS::Error>("TypeError", "That's not a DocumentWrapper, bro.");
  61. return {};
  62. }
  63. return &static_cast<DocumentWrapper*>(this_object)->node();
  64. }
  65. JS::Value DocumentWrapper::get_element_by_id(JS::Interpreter& interpreter)
  66. {
  67. auto* document = document_from(interpreter);
  68. if (!document)
  69. return {};
  70. auto& arguments = interpreter.call_frame().arguments;
  71. if (arguments.is_empty())
  72. return JS::js_null();
  73. auto id = arguments[0].to_string();
  74. auto* element = document->get_element_by_id(id);
  75. if (!element)
  76. return JS::js_null();
  77. return wrap(interpreter.heap(), const_cast<Element&>(*element));
  78. }
  79. JS::Value DocumentWrapper::query_selector_all(JS::Interpreter& interpreter)
  80. {
  81. auto* document = document_from(interpreter);
  82. if (!document)
  83. return {};
  84. auto& arguments = interpreter.call_frame().arguments;
  85. if (arguments.is_empty())
  86. return JS::js_null();
  87. auto selector = arguments[0].to_string();
  88. auto elements = document->query_selector_all(selector);
  89. // FIXME: This should be a static NodeList, not a plain JS::Array.
  90. auto* node_list = interpreter.heap().allocate<JS::Array>();
  91. for (auto& element : elements) {
  92. node_list->push(wrap(interpreter.heap(), element));
  93. }
  94. return node_list;
  95. }
  96. }
  97. }