DocumentWrapper.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/GlobalObject.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. namespace Web {
  37. namespace Bindings {
  38. DocumentWrapper::DocumentWrapper(Document& document)
  39. : NodeWrapper(document)
  40. {
  41. define_native_function("getElementById", get_element_by_id, 1);
  42. define_native_function("querySelector", query_selector, 1);
  43. define_native_function("querySelectorAll", query_selector_all, 1);
  44. }
  45. DocumentWrapper::~DocumentWrapper()
  46. {
  47. }
  48. Document& DocumentWrapper::node()
  49. {
  50. return static_cast<Document&>(NodeWrapper::node());
  51. }
  52. const Document& DocumentWrapper::node() const
  53. {
  54. return static_cast<const Document&>(NodeWrapper::node());
  55. }
  56. static Document* document_from(JS::Interpreter& interpreter)
  57. {
  58. auto* this_object = interpreter.this_value(interpreter.global_object()).to_object(interpreter);
  59. if (!this_object)
  60. return {};
  61. if (StringView("DocumentWrapper") != this_object->class_name()) {
  62. interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, "DocumentWrapper");
  63. return {};
  64. }
  65. return &static_cast<DocumentWrapper*>(this_object)->node();
  66. }
  67. JS::Value DocumentWrapper::get_element_by_id(JS::Interpreter& interpreter)
  68. {
  69. auto* document = document_from(interpreter);
  70. if (!document)
  71. return {};
  72. if (!interpreter.argument_count())
  73. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "getElementById");
  74. auto id = interpreter.argument(0).to_string(interpreter);
  75. if (interpreter.exception())
  76. return {};
  77. auto* element = document->get_element_by_id(id);
  78. if (!element)
  79. return JS::js_null();
  80. return wrap(interpreter.heap(), const_cast<Element&>(*element));
  81. }
  82. JS::Value DocumentWrapper::query_selector(JS::Interpreter& interpreter)
  83. {
  84. auto* document = document_from(interpreter);
  85. if (!document)
  86. return {};
  87. if (!interpreter.argument_count())
  88. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "querySelector");
  89. auto selector = interpreter.argument(0).to_string(interpreter);
  90. if (interpreter.exception())
  91. return {};
  92. // FIXME: Throw if selector is invalid
  93. auto element = document->query_selector(selector);
  94. if (!element)
  95. return JS::js_null();
  96. return wrap(interpreter.heap(), *element);
  97. }
  98. JS::Value DocumentWrapper::query_selector_all(JS::Interpreter& interpreter)
  99. {
  100. auto* document = document_from(interpreter);
  101. if (!document)
  102. return {};
  103. if (!interpreter.argument_count())
  104. return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "querySelectorAll");
  105. auto selector = interpreter.argument(0).to_string(interpreter);
  106. if (interpreter.exception())
  107. return {};
  108. // FIXME: Throw if selector is invalid
  109. auto elements = document->query_selector_all(selector);
  110. // FIXME: This should be a static NodeList, not a plain JS::Array.
  111. auto* node_list = JS::Array::create(interpreter.global_object());
  112. for (auto& element : elements) {
  113. node_list->indexed_properties().append(wrap(interpreter.heap(), element));
  114. }
  115. return node_list;
  116. }
  117. }
  118. }