StyleValue.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/Vector.h>
  9. #include <LibGfx/Palette.h>
  10. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  11. #include <LibWeb/CSS/StyleValue.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/InProcessWebView.h>
  14. #include <LibWeb/Loader/LoadRequest.h>
  15. #include <LibWeb/Loader/ResourceLoader.h>
  16. #include <LibWeb/Page/BrowsingContext.h>
  17. namespace Web::CSS {
  18. StyleValue::StyleValue(Type type)
  19. : m_type(type)
  20. {
  21. }
  22. StyleValue::~StyleValue()
  23. {
  24. }
  25. String IdentifierStyleValue::to_string() const
  26. {
  27. return CSS::string_from_value_id(m_id);
  28. }
  29. Color IdentifierStyleValue::to_color(const DOM::Document& document) const
  30. {
  31. if (id() == CSS::ValueID::LibwebLink)
  32. return document.link_color();
  33. VERIFY(document.page());
  34. auto palette = document.page()->palette();
  35. switch (id()) {
  36. case CSS::ValueID::LibwebPaletteDesktopBackground:
  37. return palette.color(ColorRole::DesktopBackground);
  38. case CSS::ValueID::LibwebPaletteActiveWindowBorder1:
  39. return palette.color(ColorRole::ActiveWindowBorder1);
  40. case CSS::ValueID::LibwebPaletteActiveWindowBorder2:
  41. return palette.color(ColorRole::ActiveWindowBorder2);
  42. case CSS::ValueID::LibwebPaletteActiveWindowTitle:
  43. return palette.color(ColorRole::ActiveWindowTitle);
  44. case CSS::ValueID::LibwebPaletteInactiveWindowBorder1:
  45. return palette.color(ColorRole::InactiveWindowBorder1);
  46. case CSS::ValueID::LibwebPaletteInactiveWindowBorder2:
  47. return palette.color(ColorRole::InactiveWindowBorder2);
  48. case CSS::ValueID::LibwebPaletteInactiveWindowTitle:
  49. return palette.color(ColorRole::InactiveWindowTitle);
  50. case CSS::ValueID::LibwebPaletteMovingWindowBorder1:
  51. return palette.color(ColorRole::MovingWindowBorder1);
  52. case CSS::ValueID::LibwebPaletteMovingWindowBorder2:
  53. return palette.color(ColorRole::MovingWindowBorder2);
  54. case CSS::ValueID::LibwebPaletteMovingWindowTitle:
  55. return palette.color(ColorRole::MovingWindowTitle);
  56. case CSS::ValueID::LibwebPaletteHighlightWindowBorder1:
  57. return palette.color(ColorRole::HighlightWindowBorder1);
  58. case CSS::ValueID::LibwebPaletteHighlightWindowBorder2:
  59. return palette.color(ColorRole::HighlightWindowBorder2);
  60. case CSS::ValueID::LibwebPaletteHighlightWindowTitle:
  61. return palette.color(ColorRole::HighlightWindowTitle);
  62. case CSS::ValueID::LibwebPaletteMenuStripe:
  63. return palette.color(ColorRole::MenuStripe);
  64. case CSS::ValueID::LibwebPaletteMenuBase:
  65. return palette.color(ColorRole::MenuBase);
  66. case CSS::ValueID::LibwebPaletteMenuBaseText:
  67. return palette.color(ColorRole::MenuBaseText);
  68. case CSS::ValueID::LibwebPaletteMenuSelection:
  69. return palette.color(ColorRole::MenuSelection);
  70. case CSS::ValueID::LibwebPaletteMenuSelectionText:
  71. return palette.color(ColorRole::MenuSelectionText);
  72. case CSS::ValueID::LibwebPaletteWindow:
  73. return palette.color(ColorRole::Window);
  74. case CSS::ValueID::LibwebPaletteWindowText:
  75. return palette.color(ColorRole::WindowText);
  76. case CSS::ValueID::LibwebPaletteButton:
  77. return palette.color(ColorRole::Button);
  78. case CSS::ValueID::LibwebPaletteButtonText:
  79. return palette.color(ColorRole::ButtonText);
  80. case CSS::ValueID::LibwebPaletteBase:
  81. return palette.color(ColorRole::Base);
  82. case CSS::ValueID::LibwebPaletteBaseText:
  83. return palette.color(ColorRole::BaseText);
  84. case CSS::ValueID::LibwebPaletteThreedHighlight:
  85. return palette.color(ColorRole::ThreedHighlight);
  86. case CSS::ValueID::LibwebPaletteThreedShadow1:
  87. return palette.color(ColorRole::ThreedShadow1);
  88. case CSS::ValueID::LibwebPaletteThreedShadow2:
  89. return palette.color(ColorRole::ThreedShadow2);
  90. case CSS::ValueID::LibwebPaletteHoverHighlight:
  91. return palette.color(ColorRole::HoverHighlight);
  92. case CSS::ValueID::LibwebPaletteSelection:
  93. return palette.color(ColorRole::Selection);
  94. case CSS::ValueID::LibwebPaletteSelectionText:
  95. return palette.color(ColorRole::SelectionText);
  96. case CSS::ValueID::LibwebPaletteInactiveSelection:
  97. return palette.color(ColorRole::InactiveSelection);
  98. case CSS::ValueID::LibwebPaletteInactiveSelectionText:
  99. return palette.color(ColorRole::InactiveSelectionText);
  100. case CSS::ValueID::LibwebPaletteRubberBandFill:
  101. return palette.color(ColorRole::RubberBandFill);
  102. case CSS::ValueID::LibwebPaletteRubberBandBorder:
  103. return palette.color(ColorRole::RubberBandBorder);
  104. case CSS::ValueID::LibwebPaletteLink:
  105. return palette.color(ColorRole::Link);
  106. case CSS::ValueID::LibwebPaletteActiveLink:
  107. return palette.color(ColorRole::ActiveLink);
  108. case CSS::ValueID::LibwebPaletteVisitedLink:
  109. return palette.color(ColorRole::VisitedLink);
  110. case CSS::ValueID::LibwebPaletteRuler:
  111. return palette.color(ColorRole::Ruler);
  112. case CSS::ValueID::LibwebPaletteRulerBorder:
  113. return palette.color(ColorRole::RulerBorder);
  114. case CSS::ValueID::LibwebPaletteRulerActiveText:
  115. return palette.color(ColorRole::RulerActiveText);
  116. case CSS::ValueID::LibwebPaletteRulerInactiveText:
  117. return palette.color(ColorRole::RulerInactiveText);
  118. case CSS::ValueID::LibwebPaletteTextCursor:
  119. return palette.color(ColorRole::TextCursor);
  120. case CSS::ValueID::LibwebPaletteFocusOutline:
  121. return palette.color(ColorRole::FocusOutline);
  122. case CSS::ValueID::LibwebPaletteSyntaxComment:
  123. return palette.color(ColorRole::SyntaxComment);
  124. case CSS::ValueID::LibwebPaletteSyntaxNumber:
  125. return palette.color(ColorRole::SyntaxNumber);
  126. case CSS::ValueID::LibwebPaletteSyntaxString:
  127. return palette.color(ColorRole::SyntaxString);
  128. case CSS::ValueID::LibwebPaletteSyntaxType:
  129. return palette.color(ColorRole::SyntaxType);
  130. case CSS::ValueID::LibwebPaletteSyntaxPunctuation:
  131. return palette.color(ColorRole::SyntaxPunctuation);
  132. case CSS::ValueID::LibwebPaletteSyntaxOperator:
  133. return palette.color(ColorRole::SyntaxOperator);
  134. case CSS::ValueID::LibwebPaletteSyntaxKeyword:
  135. return palette.color(ColorRole::SyntaxKeyword);
  136. case CSS::ValueID::LibwebPaletteSyntaxControlKeyword:
  137. return palette.color(ColorRole::SyntaxControlKeyword);
  138. case CSS::ValueID::LibwebPaletteSyntaxIdentifier:
  139. return palette.color(ColorRole::SyntaxIdentifier);
  140. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  141. return palette.color(ColorRole::SyntaxPreprocessorStatement);
  142. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorValue:
  143. return palette.color(ColorRole::SyntaxPreprocessorValue);
  144. default:
  145. return {};
  146. }
  147. }
  148. ImageStyleValue::ImageStyleValue(const URL& url, DOM::Document& document)
  149. : StyleValue(Type::Image)
  150. , m_url(url)
  151. , m_document(document)
  152. {
  153. auto request = LoadRequest::create_for_url_on_page(url, document.page());
  154. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  155. }
  156. void ImageStyleValue::resource_did_load()
  157. {
  158. if (!m_document)
  159. return;
  160. m_bitmap = resource()->bitmap();
  161. // FIXME: Do less than a full repaint if possible?
  162. if (m_document->browsing_context())
  163. m_document->browsing_context()->set_needs_display({});
  164. }
  165. ValueListStyleValue::ValueListStyleValue(Vector<StyleComponentValueRule>&& values)
  166. : StyleValue(Type::ValueList)
  167. , m_values(move(values))
  168. {
  169. }
  170. String ValueListStyleValue::to_string() const
  171. {
  172. StringBuilder builder;
  173. builder.appendff("List[{}](", m_values.size());
  174. for (auto& value : m_values) {
  175. builder.append(value.to_debug_string());
  176. builder.append(",");
  177. }
  178. builder.append(")");
  179. return builder.to_string();
  180. }
  181. }