DocumentWrapper.cpp 4.6 KB

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