StyleValue.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ByteBuffer.h>
  8. #include <LibGfx/Palette.h>
  9. #include <LibWeb/CSS/Serialize.h>
  10. #include <LibWeb/CSS/StyleValue.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/HTML/BrowsingContext.h>
  13. #include <LibWeb/Loader/LoadRequest.h>
  14. #include <LibWeb/Loader/ResourceLoader.h>
  15. #include <LibWeb/Page/Page.h>
  16. namespace Web::CSS {
  17. StyleValue::StyleValue(Type type)
  18. : m_type(type)
  19. {
  20. }
  21. StyleValue::~StyleValue()
  22. {
  23. }
  24. BackgroundStyleValue const& StyleValue::as_background() const
  25. {
  26. VERIFY(is_background());
  27. return static_cast<BackgroundStyleValue const&>(*this);
  28. }
  29. BackgroundRepeatStyleValue const& StyleValue::as_background_repeat() const
  30. {
  31. VERIFY(is_background_repeat());
  32. return static_cast<BackgroundRepeatStyleValue const&>(*this);
  33. }
  34. BackgroundSizeStyleValue const& StyleValue::as_background_size() const
  35. {
  36. VERIFY(is_background_size());
  37. return static_cast<BackgroundSizeStyleValue const&>(*this);
  38. }
  39. BorderStyleValue const& StyleValue::as_border() const
  40. {
  41. VERIFY(is_border());
  42. return static_cast<BorderStyleValue const&>(*this);
  43. }
  44. BorderRadiusStyleValue const& StyleValue::as_border_radius() const
  45. {
  46. VERIFY(is_border_radius());
  47. return static_cast<BorderRadiusStyleValue const&>(*this);
  48. }
  49. BoxShadowStyleValue const& StyleValue::as_box_shadow() const
  50. {
  51. VERIFY(is_box_shadow());
  52. return static_cast<BoxShadowStyleValue const&>(*this);
  53. }
  54. CalculatedStyleValue const& StyleValue::as_calculated() const
  55. {
  56. VERIFY(is_calculated());
  57. return static_cast<CalculatedStyleValue const&>(*this);
  58. }
  59. ColorStyleValue const& StyleValue::as_color() const
  60. {
  61. VERIFY(is_color());
  62. return static_cast<ColorStyleValue const&>(*this);
  63. }
  64. FlexStyleValue const& StyleValue::as_flex() const
  65. {
  66. VERIFY(is_flex());
  67. return static_cast<FlexStyleValue const&>(*this);
  68. }
  69. FlexFlowStyleValue const& StyleValue::as_flex_flow() const
  70. {
  71. VERIFY(is_flex_flow());
  72. return static_cast<FlexFlowStyleValue const&>(*this);
  73. }
  74. FontStyleValue const& StyleValue::as_font() const
  75. {
  76. VERIFY(is_font());
  77. return static_cast<FontStyleValue const&>(*this);
  78. }
  79. IdentifierStyleValue const& StyleValue::as_identifier() const
  80. {
  81. VERIFY(is_identifier());
  82. return static_cast<IdentifierStyleValue const&>(*this);
  83. }
  84. ImageStyleValue const& StyleValue::as_image() const
  85. {
  86. VERIFY(is_image());
  87. return static_cast<ImageStyleValue const&>(*this);
  88. }
  89. InheritStyleValue const& StyleValue::as_inherit() const
  90. {
  91. VERIFY(is_inherit());
  92. return static_cast<InheritStyleValue const&>(*this);
  93. }
  94. InitialStyleValue const& StyleValue::as_initial() const
  95. {
  96. VERIFY(is_initial());
  97. return static_cast<InitialStyleValue const&>(*this);
  98. }
  99. LengthStyleValue const& StyleValue::as_length() const
  100. {
  101. VERIFY(is_length());
  102. return static_cast<LengthStyleValue const&>(*this);
  103. }
  104. ListStyleStyleValue const& StyleValue::as_list_style() const
  105. {
  106. VERIFY(is_list_style());
  107. return static_cast<ListStyleStyleValue const&>(*this);
  108. }
  109. NumericStyleValue const& StyleValue::as_numeric() const
  110. {
  111. VERIFY(is_numeric());
  112. return static_cast<NumericStyleValue const&>(*this);
  113. }
  114. OverflowStyleValue const& StyleValue::as_overflow() const
  115. {
  116. VERIFY(is_overflow());
  117. return static_cast<OverflowStyleValue const&>(*this);
  118. }
  119. PercentageStyleValue const& StyleValue::as_percentage() const
  120. {
  121. VERIFY(is_percentage());
  122. return static_cast<PercentageStyleValue const&>(*this);
  123. }
  124. PositionStyleValue const& StyleValue::as_position() const
  125. {
  126. VERIFY(is_position());
  127. return static_cast<PositionStyleValue const&>(*this);
  128. }
  129. StringStyleValue const& StyleValue::as_string() const
  130. {
  131. VERIFY(is_string());
  132. return static_cast<StringStyleValue const&>(*this);
  133. }
  134. TextDecorationStyleValue const& StyleValue::as_text_decoration() const
  135. {
  136. VERIFY(is_text_decoration());
  137. return static_cast<TextDecorationStyleValue const&>(*this);
  138. }
  139. TransformationStyleValue const& StyleValue::as_transformation() const
  140. {
  141. VERIFY(is_transformation());
  142. return static_cast<TransformationStyleValue const&>(*this);
  143. }
  144. UnresolvedStyleValue const& StyleValue::as_unresolved() const
  145. {
  146. VERIFY(is_unresolved());
  147. return static_cast<UnresolvedStyleValue const&>(*this);
  148. }
  149. UnsetStyleValue const& StyleValue::as_unset() const
  150. {
  151. VERIFY(is_unset());
  152. return static_cast<UnsetStyleValue const&>(*this);
  153. }
  154. StyleValueList const& StyleValue::as_value_list() const
  155. {
  156. VERIFY(is_value_list());
  157. return static_cast<StyleValueList const&>(*this);
  158. }
  159. BackgroundStyleValue::BackgroundStyleValue(
  160. NonnullRefPtr<StyleValue> color,
  161. NonnullRefPtr<StyleValue> image,
  162. NonnullRefPtr<StyleValue> position,
  163. NonnullRefPtr<StyleValue> size,
  164. NonnullRefPtr<StyleValue> repeat,
  165. NonnullRefPtr<StyleValue> attachment,
  166. NonnullRefPtr<StyleValue> origin,
  167. NonnullRefPtr<StyleValue> clip)
  168. : StyleValue(Type::Background)
  169. , m_color(color)
  170. , m_image(image)
  171. , m_position(position)
  172. , m_size(size)
  173. , m_repeat(repeat)
  174. , m_attachment(attachment)
  175. , m_origin(origin)
  176. , m_clip(clip)
  177. {
  178. auto layer_count = [](auto style_value) -> size_t {
  179. if (style_value->is_value_list())
  180. return style_value->as_value_list().size();
  181. else
  182. return 1;
  183. };
  184. m_layer_count = max(layer_count(m_image), layer_count(m_position));
  185. m_layer_count = max(m_layer_count, layer_count(m_size));
  186. m_layer_count = max(m_layer_count, layer_count(m_repeat));
  187. m_layer_count = max(m_layer_count, layer_count(m_attachment));
  188. m_layer_count = max(m_layer_count, layer_count(m_origin));
  189. m_layer_count = max(m_layer_count, layer_count(m_clip));
  190. VERIFY(!m_color->is_value_list());
  191. }
  192. String BackgroundStyleValue::to_string() const
  193. {
  194. if (m_layer_count == 1) {
  195. return String::formatted("{} {} {} {} {} {} {} {}", m_color->to_string(), m_image->to_string(), m_position->to_string(), m_size->to_string(), m_repeat->to_string(), m_attachment->to_string(), m_origin->to_string(), m_clip->to_string());
  196. }
  197. auto get_layer_value_string = [](NonnullRefPtr<StyleValue> const& style_value, size_t index) {
  198. if (style_value->is_value_list())
  199. return style_value->as_value_list().value_at(index, true)->to_string();
  200. return style_value->to_string();
  201. };
  202. StringBuilder builder;
  203. for (size_t i = 0; i < m_layer_count; i++) {
  204. if (i)
  205. builder.append(", ");
  206. if (i == m_layer_count - 1)
  207. builder.appendff("{} ", m_color->to_string());
  208. builder.appendff("{} {} {} {} {} {} {}", get_layer_value_string(m_image, i), get_layer_value_string(m_position, i), get_layer_value_string(m_size, i), get_layer_value_string(m_repeat, i), get_layer_value_string(m_attachment, i), get_layer_value_string(m_origin, i), get_layer_value_string(m_clip, i));
  209. }
  210. return builder.to_string();
  211. }
  212. String BackgroundRepeatStyleValue::to_string() const
  213. {
  214. return String::formatted("{} {}", CSS::to_string(m_repeat_x), CSS::to_string(m_repeat_y));
  215. }
  216. String BackgroundSizeStyleValue::to_string() const
  217. {
  218. return String::formatted("{} {}", m_size_x.to_string(), m_size_y.to_string());
  219. }
  220. String BorderStyleValue::to_string() const
  221. {
  222. return String::formatted("{} {} {}", m_border_width->to_string(), m_border_style->to_string(), m_border_color->to_string());
  223. }
  224. String BorderRadiusStyleValue::to_string() const
  225. {
  226. return String::formatted("{} / {}", m_horizontal_radius.to_string(), m_vertical_radius.to_string());
  227. }
  228. String BoxShadowStyleValue::to_string() const
  229. {
  230. return String::formatted("{} {} {} {}", m_offset_x.to_string(), m_offset_y.to_string(), m_blur_radius.to_string(), m_color.to_string());
  231. }
  232. // https://www.w3.org/TR/css-color-4/#serializing-sRGB-values
  233. String ColorStyleValue::to_string() const
  234. {
  235. if (m_color.alpha() == 1)
  236. return String::formatted("rgb({}, {}, {})", m_color.red(), m_color.green(), m_color.blue());
  237. return String::formatted("rgba({}, {}, {}, {})", m_color.red(), m_color.green(), m_color.blue(), (float)(m_color.alpha()) / 255.0f);
  238. }
  239. String CombinedBorderRadiusStyleValue::to_string() const
  240. {
  241. return String::formatted("{} {} {} {} / {} {} {} {}", m_top_left->horizontal_radius().to_string(), m_top_right->horizontal_radius().to_string(), m_bottom_right->horizontal_radius().to_string(), m_bottom_left->horizontal_radius().to_string(), m_top_left->vertical_radius().to_string(), m_top_right->vertical_radius().to_string(), m_bottom_right->vertical_radius().to_string(), m_bottom_left->vertical_radius().to_string());
  242. }
  243. String FlexStyleValue::to_string() const
  244. {
  245. return String::formatted("{} {} {}", m_grow->to_string(), m_shrink->to_string(), m_basis->to_string());
  246. }
  247. String FlexFlowStyleValue::to_string() const
  248. {
  249. return String::formatted("{} {}", m_flex_direction->to_string(), m_flex_wrap->to_string());
  250. }
  251. String FontStyleValue::to_string() const
  252. {
  253. return String::formatted("{} {} {} / {} {}", m_font_style->to_string(), m_font_weight->to_string(), m_font_size->to_string(), m_line_height->to_string(), m_font_families->to_string());
  254. }
  255. String IdentifierStyleValue::to_string() const
  256. {
  257. return CSS::string_from_value_id(m_id);
  258. }
  259. bool IdentifierStyleValue::has_color() const
  260. {
  261. switch (m_id) {
  262. case ValueID::Currentcolor:
  263. case ValueID::LibwebLink:
  264. case ValueID::LibwebPaletteActiveLink:
  265. case ValueID::LibwebPaletteActiveWindowBorder1:
  266. case ValueID::LibwebPaletteActiveWindowBorder2:
  267. case ValueID::LibwebPaletteActiveWindowTitle:
  268. case ValueID::LibwebPaletteBase:
  269. case ValueID::LibwebPaletteBaseText:
  270. case ValueID::LibwebPaletteButton:
  271. case ValueID::LibwebPaletteButtonText:
  272. case ValueID::LibwebPaletteDesktopBackground:
  273. case ValueID::LibwebPaletteFocusOutline:
  274. case ValueID::LibwebPaletteHighlightWindowBorder1:
  275. case ValueID::LibwebPaletteHighlightWindowBorder2:
  276. case ValueID::LibwebPaletteHighlightWindowTitle:
  277. case ValueID::LibwebPaletteHoverHighlight:
  278. case ValueID::LibwebPaletteInactiveSelection:
  279. case ValueID::LibwebPaletteInactiveSelectionText:
  280. case ValueID::LibwebPaletteInactiveWindowBorder1:
  281. case ValueID::LibwebPaletteInactiveWindowBorder2:
  282. case ValueID::LibwebPaletteInactiveWindowTitle:
  283. case ValueID::LibwebPaletteLink:
  284. case ValueID::LibwebPaletteMenuBase:
  285. case ValueID::LibwebPaletteMenuBaseText:
  286. case ValueID::LibwebPaletteMenuSelection:
  287. case ValueID::LibwebPaletteMenuSelectionText:
  288. case ValueID::LibwebPaletteMenuStripe:
  289. case ValueID::LibwebPaletteMovingWindowBorder1:
  290. case ValueID::LibwebPaletteMovingWindowBorder2:
  291. case ValueID::LibwebPaletteMovingWindowTitle:
  292. case ValueID::LibwebPaletteRubberBandBorder:
  293. case ValueID::LibwebPaletteRubberBandFill:
  294. case ValueID::LibwebPaletteRuler:
  295. case ValueID::LibwebPaletteRulerActiveText:
  296. case ValueID::LibwebPaletteRulerBorder:
  297. case ValueID::LibwebPaletteRulerInactiveText:
  298. case ValueID::LibwebPaletteSelection:
  299. case ValueID::LibwebPaletteSelectionText:
  300. case ValueID::LibwebPaletteSyntaxComment:
  301. case ValueID::LibwebPaletteSyntaxControlKeyword:
  302. case ValueID::LibwebPaletteSyntaxIdentifier:
  303. case ValueID::LibwebPaletteSyntaxKeyword:
  304. case ValueID::LibwebPaletteSyntaxNumber:
  305. case ValueID::LibwebPaletteSyntaxOperator:
  306. case ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  307. case ValueID::LibwebPaletteSyntaxPreprocessorValue:
  308. case ValueID::LibwebPaletteSyntaxPunctuation:
  309. case ValueID::LibwebPaletteSyntaxString:
  310. case ValueID::LibwebPaletteSyntaxType:
  311. case ValueID::LibwebPaletteTextCursor:
  312. case ValueID::LibwebPaletteThreedHighlight:
  313. case ValueID::LibwebPaletteThreedShadow1:
  314. case ValueID::LibwebPaletteThreedShadow2:
  315. case ValueID::LibwebPaletteVisitedLink:
  316. case ValueID::LibwebPaletteWindow:
  317. case ValueID::LibwebPaletteWindowText:
  318. return true;
  319. default:
  320. return false;
  321. }
  322. }
  323. Color IdentifierStyleValue::to_color(Layout::NodeWithStyle const& node) const
  324. {
  325. if (id() == CSS::ValueID::Currentcolor) {
  326. if (!node.has_style())
  327. return Color::Black;
  328. return node.computed_values().color();
  329. }
  330. auto& document = node.document();
  331. if (id() == CSS::ValueID::LibwebLink)
  332. return document.link_color();
  333. VERIFY(document.page());
  334. auto palette = document.page()->palette();
  335. switch (id()) {
  336. case CSS::ValueID::LibwebPaletteDesktopBackground:
  337. return palette.color(ColorRole::DesktopBackground);
  338. case CSS::ValueID::LibwebPaletteActiveWindowBorder1:
  339. return palette.color(ColorRole::ActiveWindowBorder1);
  340. case CSS::ValueID::LibwebPaletteActiveWindowBorder2:
  341. return palette.color(ColorRole::ActiveWindowBorder2);
  342. case CSS::ValueID::LibwebPaletteActiveWindowTitle:
  343. return palette.color(ColorRole::ActiveWindowTitle);
  344. case CSS::ValueID::LibwebPaletteInactiveWindowBorder1:
  345. return palette.color(ColorRole::InactiveWindowBorder1);
  346. case CSS::ValueID::LibwebPaletteInactiveWindowBorder2:
  347. return palette.color(ColorRole::InactiveWindowBorder2);
  348. case CSS::ValueID::LibwebPaletteInactiveWindowTitle:
  349. return palette.color(ColorRole::InactiveWindowTitle);
  350. case CSS::ValueID::LibwebPaletteMovingWindowBorder1:
  351. return palette.color(ColorRole::MovingWindowBorder1);
  352. case CSS::ValueID::LibwebPaletteMovingWindowBorder2:
  353. return palette.color(ColorRole::MovingWindowBorder2);
  354. case CSS::ValueID::LibwebPaletteMovingWindowTitle:
  355. return palette.color(ColorRole::MovingWindowTitle);
  356. case CSS::ValueID::LibwebPaletteHighlightWindowBorder1:
  357. return palette.color(ColorRole::HighlightWindowBorder1);
  358. case CSS::ValueID::LibwebPaletteHighlightWindowBorder2:
  359. return palette.color(ColorRole::HighlightWindowBorder2);
  360. case CSS::ValueID::LibwebPaletteHighlightWindowTitle:
  361. return palette.color(ColorRole::HighlightWindowTitle);
  362. case CSS::ValueID::LibwebPaletteMenuStripe:
  363. return palette.color(ColorRole::MenuStripe);
  364. case CSS::ValueID::LibwebPaletteMenuBase:
  365. return palette.color(ColorRole::MenuBase);
  366. case CSS::ValueID::LibwebPaletteMenuBaseText:
  367. return palette.color(ColorRole::MenuBaseText);
  368. case CSS::ValueID::LibwebPaletteMenuSelection:
  369. return palette.color(ColorRole::MenuSelection);
  370. case CSS::ValueID::LibwebPaletteMenuSelectionText:
  371. return palette.color(ColorRole::MenuSelectionText);
  372. case CSS::ValueID::LibwebPaletteWindow:
  373. return palette.color(ColorRole::Window);
  374. case CSS::ValueID::LibwebPaletteWindowText:
  375. return palette.color(ColorRole::WindowText);
  376. case CSS::ValueID::LibwebPaletteButton:
  377. return palette.color(ColorRole::Button);
  378. case CSS::ValueID::LibwebPaletteButtonText:
  379. return palette.color(ColorRole::ButtonText);
  380. case CSS::ValueID::LibwebPaletteBase:
  381. return palette.color(ColorRole::Base);
  382. case CSS::ValueID::LibwebPaletteBaseText:
  383. return palette.color(ColorRole::BaseText);
  384. case CSS::ValueID::LibwebPaletteThreedHighlight:
  385. return palette.color(ColorRole::ThreedHighlight);
  386. case CSS::ValueID::LibwebPaletteThreedShadow1:
  387. return palette.color(ColorRole::ThreedShadow1);
  388. case CSS::ValueID::LibwebPaletteThreedShadow2:
  389. return palette.color(ColorRole::ThreedShadow2);
  390. case CSS::ValueID::LibwebPaletteHoverHighlight:
  391. return palette.color(ColorRole::HoverHighlight);
  392. case CSS::ValueID::LibwebPaletteSelection:
  393. return palette.color(ColorRole::Selection);
  394. case CSS::ValueID::LibwebPaletteSelectionText:
  395. return palette.color(ColorRole::SelectionText);
  396. case CSS::ValueID::LibwebPaletteInactiveSelection:
  397. return palette.color(ColorRole::InactiveSelection);
  398. case CSS::ValueID::LibwebPaletteInactiveSelectionText:
  399. return palette.color(ColorRole::InactiveSelectionText);
  400. case CSS::ValueID::LibwebPaletteRubberBandFill:
  401. return palette.color(ColorRole::RubberBandFill);
  402. case CSS::ValueID::LibwebPaletteRubberBandBorder:
  403. return palette.color(ColorRole::RubberBandBorder);
  404. case CSS::ValueID::LibwebPaletteLink:
  405. return palette.color(ColorRole::Link);
  406. case CSS::ValueID::LibwebPaletteActiveLink:
  407. return palette.color(ColorRole::ActiveLink);
  408. case CSS::ValueID::LibwebPaletteVisitedLink:
  409. return palette.color(ColorRole::VisitedLink);
  410. case CSS::ValueID::LibwebPaletteRuler:
  411. return palette.color(ColorRole::Ruler);
  412. case CSS::ValueID::LibwebPaletteRulerBorder:
  413. return palette.color(ColorRole::RulerBorder);
  414. case CSS::ValueID::LibwebPaletteRulerActiveText:
  415. return palette.color(ColorRole::RulerActiveText);
  416. case CSS::ValueID::LibwebPaletteRulerInactiveText:
  417. return palette.color(ColorRole::RulerInactiveText);
  418. case CSS::ValueID::LibwebPaletteTextCursor:
  419. return palette.color(ColorRole::TextCursor);
  420. case CSS::ValueID::LibwebPaletteFocusOutline:
  421. return palette.color(ColorRole::FocusOutline);
  422. case CSS::ValueID::LibwebPaletteSyntaxComment:
  423. return palette.color(ColorRole::SyntaxComment);
  424. case CSS::ValueID::LibwebPaletteSyntaxNumber:
  425. return palette.color(ColorRole::SyntaxNumber);
  426. case CSS::ValueID::LibwebPaletteSyntaxString:
  427. return palette.color(ColorRole::SyntaxString);
  428. case CSS::ValueID::LibwebPaletteSyntaxType:
  429. return palette.color(ColorRole::SyntaxType);
  430. case CSS::ValueID::LibwebPaletteSyntaxPunctuation:
  431. return palette.color(ColorRole::SyntaxPunctuation);
  432. case CSS::ValueID::LibwebPaletteSyntaxOperator:
  433. return palette.color(ColorRole::SyntaxOperator);
  434. case CSS::ValueID::LibwebPaletteSyntaxKeyword:
  435. return palette.color(ColorRole::SyntaxKeyword);
  436. case CSS::ValueID::LibwebPaletteSyntaxControlKeyword:
  437. return palette.color(ColorRole::SyntaxControlKeyword);
  438. case CSS::ValueID::LibwebPaletteSyntaxIdentifier:
  439. return palette.color(ColorRole::SyntaxIdentifier);
  440. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  441. return palette.color(ColorRole::SyntaxPreprocessorStatement);
  442. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorValue:
  443. return palette.color(ColorRole::SyntaxPreprocessorValue);
  444. default:
  445. return {};
  446. }
  447. }
  448. ImageStyleValue::ImageStyleValue(AK::URL const& url)
  449. : StyleValue(Type::Image)
  450. , m_url(url)
  451. {
  452. }
  453. void ImageStyleValue::load_bitmap(DOM::Document& document)
  454. {
  455. if (m_bitmap)
  456. return;
  457. m_document = &document;
  458. auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
  459. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  460. }
  461. void ImageStyleValue::resource_did_load()
  462. {
  463. if (!m_document)
  464. return;
  465. m_bitmap = resource()->bitmap();
  466. // FIXME: Do less than a full repaint if possible?
  467. if (m_document && m_document->browsing_context())
  468. m_document->browsing_context()->set_needs_display({});
  469. }
  470. String ImageStyleValue::to_string() const
  471. {
  472. return serialize_a_url(m_url.to_string());
  473. }
  474. String ListStyleStyleValue::to_string() const
  475. {
  476. return String::formatted("{} {} {}", m_position->to_string(), m_image->to_string(), m_style_type->to_string());
  477. }
  478. String NumericStyleValue::to_string() const
  479. {
  480. return m_value.visit(
  481. [](float value) {
  482. return String::formatted("{}", value);
  483. },
  484. [](i64 value) {
  485. return String::formatted("{}", value);
  486. });
  487. }
  488. String OverflowStyleValue::to_string() const
  489. {
  490. return String::formatted("{} {}", m_overflow_x->to_string(), m_overflow_y->to_string());
  491. }
  492. String PercentageStyleValue::to_string() const
  493. {
  494. return m_percentage.to_string();
  495. }
  496. String PositionStyleValue::to_string() const
  497. {
  498. auto to_string = [](PositionEdge edge) {
  499. switch (edge) {
  500. case PositionEdge::Left:
  501. return "left";
  502. case PositionEdge::Right:
  503. return "right";
  504. case PositionEdge::Top:
  505. return "top";
  506. case PositionEdge::Bottom:
  507. return "bottom";
  508. }
  509. VERIFY_NOT_REACHED();
  510. };
  511. return String::formatted("{} {} {} {}", to_string(m_edge_x), m_offset_x.to_string(), to_string(m_edge_y), m_offset_y.to_string());
  512. }
  513. String TextDecorationStyleValue::to_string() const
  514. {
  515. return String::formatted("{} {} {}", m_line->to_string(), m_style->to_string(), m_color->to_string());
  516. }
  517. String TransformationStyleValue::to_string() const
  518. {
  519. StringBuilder builder;
  520. switch (m_transform_function) {
  521. case TransformFunction::TranslateY:
  522. builder.append("translateY");
  523. break;
  524. default:
  525. VERIFY_NOT_REACHED();
  526. }
  527. builder.append('(');
  528. builder.join(", ", m_values);
  529. builder.append(')');
  530. return builder.to_string();
  531. }
  532. String UnresolvedStyleValue::to_string() const
  533. {
  534. StringBuilder builder;
  535. for (auto& value : m_values)
  536. builder.append(value.to_string());
  537. return builder.to_string();
  538. }
  539. String StyleValueList::to_string() const
  540. {
  541. String separator = "";
  542. switch (m_separator) {
  543. case Separator::Space:
  544. separator = " ";
  545. break;
  546. case Separator::Comma:
  547. separator = ", ";
  548. break;
  549. default:
  550. VERIFY_NOT_REACHED();
  551. }
  552. return String::join(separator, m_values);
  553. }
  554. }