HTMLEmbedElement.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/HTMLEmbedElementPrototype.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/CSS/StyleProperties.h>
  9. #include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
  10. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  11. #include <LibWeb/HTML/HTMLEmbedElement.h>
  12. #include <LibWeb/HTML/Parser/HTMLParser.h>
  13. namespace Web::HTML {
  14. GC_DEFINE_ALLOCATOR(HTMLEmbedElement);
  15. HTMLEmbedElement::HTMLEmbedElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  16. : HTMLElement(document, move(qualified_name))
  17. {
  18. }
  19. HTMLEmbedElement::~HTMLEmbedElement() = default;
  20. void HTMLEmbedElement::initialize(JS::Realm& realm)
  21. {
  22. Base::initialize(realm);
  23. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLEmbedElement);
  24. }
  25. void HTMLEmbedElement::apply_presentational_hints(CSS::StyleProperties& style) const
  26. {
  27. for_each_attribute([&](auto& name, auto& value) {
  28. if (name == HTML::AttributeNames::align) {
  29. if (value.equals_ignoring_ascii_case("center"sv))
  30. style.set_property(CSS::PropertyID::TextAlign, CSS::CSSKeywordValue::create(CSS::Keyword::Center));
  31. else if (value.equals_ignoring_ascii_case("middle"sv))
  32. style.set_property(CSS::PropertyID::TextAlign, CSS::CSSKeywordValue::create(CSS::Keyword::Middle));
  33. } else if (name == HTML::AttributeNames::height) {
  34. if (auto parsed_value = parse_dimension_value(value))
  35. style.set_property(CSS::PropertyID::Height, *parsed_value);
  36. }
  37. // https://html.spec.whatwg.org/multipage/rendering.html#attributes-for-embedded-content-and-images:maps-to-the-dimension-property
  38. else if (name == HTML::AttributeNames::hspace) {
  39. if (auto parsed_value = parse_dimension_value(value)) {
  40. style.set_property(CSS::PropertyID::MarginLeft, *parsed_value);
  41. style.set_property(CSS::PropertyID::MarginRight, *parsed_value);
  42. }
  43. } else if (name == HTML::AttributeNames::vspace) {
  44. if (auto parsed_value = parse_dimension_value(value)) {
  45. style.set_property(CSS::PropertyID::MarginTop, *parsed_value);
  46. style.set_property(CSS::PropertyID::MarginBottom, *parsed_value);
  47. }
  48. } else if (name == HTML::AttributeNames::width) {
  49. if (auto parsed_value = parse_dimension_value(value)) {
  50. style.set_property(CSS::PropertyID::Width, *parsed_value);
  51. }
  52. }
  53. });
  54. }
  55. void HTMLEmbedElement::adjust_computed_style(CSS::StyleProperties& style)
  56. {
  57. // https://drafts.csswg.org/css-display-3/#unbox
  58. if (style.display().is_contents())
  59. style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::None)));
  60. }
  61. }