RangePrototype.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  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/Function.h>
  27. #include <LibJS/Runtime/GlobalObject.h>
  28. #include <LibWeb/Bindings/NodeWrapper.h>
  29. #include <LibWeb/Bindings/NodeWrapperFactory.h>
  30. #include <LibWeb/Bindings/RangePrototype.h>
  31. #include <LibWeb/Bindings/RangeWrapper.h>
  32. #include <LibWeb/DOM/Range.h>
  33. namespace Web::Bindings {
  34. RangePrototype::RangePrototype(JS::GlobalObject& global_object)
  35. : Object(*global_object.object_prototype())
  36. {
  37. }
  38. void RangePrototype::initialize(JS::GlobalObject& global_object)
  39. {
  40. auto default_attributes = JS::Attribute::Enumerable | JS::Attribute::Configurable;
  41. Object::initialize(global_object);
  42. define_native_function("setStart", set_start, 2);
  43. define_native_function("setEnd", set_end, 2);
  44. define_native_function("cloneRange", clone_range, 0);
  45. define_native_property("startContainer", start_container_getter, nullptr, default_attributes);
  46. define_native_property("endContainer", end_container_getter, nullptr, default_attributes);
  47. define_native_property("startOffset", start_offset_getter, nullptr, default_attributes);
  48. define_native_property("endOffset", end_offset_getter, nullptr, default_attributes);
  49. }
  50. static DOM::Range* impl_from(JS::VM& vm, JS::GlobalObject& global_object)
  51. {
  52. auto* this_object = vm.this_value(global_object).to_object(global_object);
  53. if (!this_object)
  54. return nullptr;
  55. if (StringView("RangeWrapper") != this_object->class_name()) {
  56. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "Range");
  57. return nullptr;
  58. }
  59. return &static_cast<RangeWrapper*>(this_object)->impl();
  60. }
  61. JS_DEFINE_NATIVE_FUNCTION(RangePrototype::set_start)
  62. {
  63. auto* impl = impl_from(vm, global_object);
  64. if (!impl)
  65. return {};
  66. auto arg0 = vm.argument(0).to_object(global_object);
  67. if (vm.exception())
  68. return {};
  69. auto arg1 = vm.argument(1).to_number(global_object);
  70. if (vm.exception())
  71. return {};
  72. if (!is<NodeWrapper>(arg0)) {
  73. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "Node");
  74. return {};
  75. }
  76. impl->set_start(static_cast<NodeWrapper*>(arg0)->impl(), arg1.as_i32());
  77. return JS::js_undefined();
  78. }
  79. JS_DEFINE_NATIVE_FUNCTION(RangePrototype::set_end)
  80. {
  81. auto* impl = impl_from(vm, global_object);
  82. if (!impl)
  83. return {};
  84. auto arg0 = vm.argument(0).to_object(global_object);
  85. if (vm.exception())
  86. return {};
  87. auto arg1 = vm.argument(1).to_number(global_object);
  88. if (vm.exception())
  89. return {};
  90. if (!is<NodeWrapper>(arg0)) {
  91. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "Node");
  92. return {};
  93. }
  94. impl->set_end(static_cast<NodeWrapper*>(arg0)->impl(), arg1.as_i32());
  95. return JS::js_undefined();
  96. }
  97. JS_DEFINE_NATIVE_FUNCTION(RangePrototype::clone_range)
  98. {
  99. auto* impl = impl_from(vm, global_object);
  100. if (!impl)
  101. return {};
  102. return wrap(global_object, *impl->clone_range());
  103. }
  104. JS_DEFINE_NATIVE_GETTER(RangePrototype::start_container_getter)
  105. {
  106. auto* impl = impl_from(vm, global_object);
  107. if (!impl)
  108. return {};
  109. return wrap(global_object, *impl->start_container());
  110. }
  111. JS_DEFINE_NATIVE_GETTER(RangePrototype::end_container_getter)
  112. {
  113. auto* impl = impl_from(vm, global_object);
  114. if (!impl)
  115. return {};
  116. return wrap(global_object, *impl->end_container());
  117. }
  118. JS_DEFINE_NATIVE_GETTER(RangePrototype::start_offset_getter)
  119. {
  120. auto* impl = impl_from(vm, global_object);
  121. if (!impl)
  122. return {};
  123. return JS::Value(impl->start_offset());
  124. }
  125. JS_DEFINE_NATIVE_GETTER(RangePrototype::end_offset_getter)
  126. {
  127. auto* impl = impl_from(vm, global_object);
  128. if (!impl)
  129. return {};
  130. return JS::Value(impl->end_offset());
  131. }
  132. }