HTMLPreElement.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/CSS/StyleProperties.h>
  8. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  9. #include <LibWeb/HTML/HTMLPreElement.h>
  10. #include <LibWeb/HTML/Numbers.h>
  11. namespace Web::HTML {
  12. JS_DEFINE_ALLOCATOR(HTMLPreElement);
  13. HTMLPreElement::HTMLPreElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  14. : HTMLElement(document, move(qualified_name))
  15. {
  16. }
  17. HTMLPreElement::~HTMLPreElement() = default;
  18. void HTMLPreElement::initialize(JS::Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLPreElement);
  22. }
  23. void HTMLPreElement::apply_presentational_hints(CSS::StyleProperties& style) const
  24. {
  25. HTMLElement::apply_presentational_hints(style);
  26. for_each_attribute([&](auto const& name, auto const&) {
  27. if (name.equals_ignoring_ascii_case(HTML::AttributeNames::wrap))
  28. style.set_property(CSS::PropertyID::WhiteSpace, CSS::IdentifierStyleValue::create(CSS::ValueID::PreWrap));
  29. });
  30. }
  31. // https://html.spec.whatwg.org/multipage/obsolete.html#dom-pre-width
  32. WebIDL::Long HTMLPreElement::width() const
  33. {
  34. // The width IDL attribute of the pre element must reflect the content attribute of the same name.
  35. if (auto width_string = get_attribute(HTML::AttributeNames::width); width_string.has_value()) {
  36. if (auto width = parse_integer(*width_string); width.has_value())
  37. return *width;
  38. }
  39. return 0;
  40. }
  41. WebIDL::ExceptionOr<void> HTMLPreElement::set_width(WebIDL::Long width)
  42. {
  43. return set_attribute(HTML::AttributeNames::width, MUST(String::number(width)));
  44. }
  45. }