HTMLTableColElement.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/HTMLTableColElementPrototype.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/CSS/StyleProperties.h>
  9. #include <LibWeb/HTML/HTMLTableColElement.h>
  10. #include <LibWeb/HTML/Numbers.h>
  11. #include <LibWeb/HTML/Parser/HTMLParser.h>
  12. namespace Web::HTML {
  13. GC_DEFINE_ALLOCATOR(HTMLTableColElement);
  14. HTMLTableColElement::HTMLTableColElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  15. : HTMLElement(document, move(qualified_name))
  16. {
  17. }
  18. HTMLTableColElement::~HTMLTableColElement() = default;
  19. void HTMLTableColElement::initialize(JS::Realm& realm)
  20. {
  21. Base::initialize(realm);
  22. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLTableColElement);
  23. }
  24. // https://html.spec.whatwg.org/multipage/tables.html#dom-colgroup-span
  25. WebIDL::UnsignedLong HTMLTableColElement::span() const
  26. {
  27. // The span IDL attribute must reflect the content attribute of the same name. It is clamped to the range [1, 1000], and its default value is 1.
  28. if (auto span_string = get_attribute(HTML::AttributeNames::span); span_string.has_value()) {
  29. if (auto span_digits = parse_non_negative_integer_digits(*span_string); span_digits.has_value()) {
  30. auto span = AK::StringUtils::convert_to_int<i64>(*span_digits);
  31. // NOTE: If span has no value at this point, the value must be larger than NumericLimits<i64>::max(), so return the maximum value of 1000.
  32. if (!span.has_value())
  33. return 1000;
  34. return clamp(*span, 1, 1000);
  35. }
  36. }
  37. return 1;
  38. }
  39. WebIDL::ExceptionOr<void> HTMLTableColElement::set_span(unsigned int value)
  40. {
  41. if (value > 2147483647)
  42. value = 1;
  43. return set_attribute(HTML::AttributeNames::span, String::number(value));
  44. }
  45. void HTMLTableColElement::apply_presentational_hints(CSS::StyleProperties& style) const
  46. {
  47. for_each_attribute([&](auto& name, auto& value) {
  48. // https://html.spec.whatwg.org/multipage/rendering.html#tables-2:maps-to-the-dimension-property-2
  49. if (name == HTML::AttributeNames::width) {
  50. if (auto parsed_value = parse_dimension_value(value)) {
  51. style.set_property(CSS::PropertyID::Width, *parsed_value);
  52. }
  53. }
  54. });
  55. }
  56. }