HTMLEmbedElement.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/ComputedProperties.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. bool HTMLEmbedElement::is_presentational_hint(FlyString const& name) const
  26. {
  27. if (Base::is_presentational_hint(name))
  28. return true;
  29. return first_is_one_of(name,
  30. HTML::AttributeNames::align,
  31. HTML::AttributeNames::height,
  32. HTML::AttributeNames::hspace,
  33. HTML::AttributeNames::vspace,
  34. HTML::AttributeNames::width);
  35. }
  36. void HTMLEmbedElement::apply_presentational_hints(GC::Ref<CSS::CascadedProperties> cascaded_properties) const
  37. {
  38. for_each_attribute([&](auto& name, auto& value) {
  39. if (name == HTML::AttributeNames::align) {
  40. if (value.equals_ignoring_ascii_case("center"sv))
  41. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::TextAlign, CSS::CSSKeywordValue::create(CSS::Keyword::Center));
  42. else if (value.equals_ignoring_ascii_case("middle"sv))
  43. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::TextAlign, CSS::CSSKeywordValue::create(CSS::Keyword::Middle));
  44. } else if (name == HTML::AttributeNames::height) {
  45. if (auto parsed_value = parse_dimension_value(value))
  46. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::Height, *parsed_value);
  47. }
  48. // https://html.spec.whatwg.org/multipage/rendering.html#attributes-for-embedded-content-and-images:maps-to-the-dimension-property
  49. else if (name == HTML::AttributeNames::hspace) {
  50. if (auto parsed_value = parse_dimension_value(value)) {
  51. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::MarginLeft, *parsed_value);
  52. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::MarginRight, *parsed_value);
  53. }
  54. } else if (name == HTML::AttributeNames::vspace) {
  55. if (auto parsed_value = parse_dimension_value(value)) {
  56. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::MarginTop, *parsed_value);
  57. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::MarginBottom, *parsed_value);
  58. }
  59. } else if (name == HTML::AttributeNames::width) {
  60. if (auto parsed_value = parse_dimension_value(value)) {
  61. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::Width, *parsed_value);
  62. }
  63. }
  64. });
  65. }
  66. void HTMLEmbedElement::adjust_computed_style(CSS::ComputedProperties& style)
  67. {
  68. // https://drafts.csswg.org/css-display-3/#unbox
  69. if (style.display().is_contents())
  70. style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::None)));
  71. }
  72. }