CSSKeywordValue.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "CSSKeywordValue.h"
  10. #include <LibGfx/Palette.h>
  11. #include <LibWeb/CSS/SystemColor.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/Layout/Node.h>
  14. #include <LibWeb/Page/Page.h>
  15. namespace Web::CSS {
  16. String CSSKeywordValue::to_string() const
  17. {
  18. return MUST(String::from_utf8(string_from_keyword(keyword())));
  19. }
  20. bool CSSKeywordValue::is_color(Keyword keyword)
  21. {
  22. switch (keyword) {
  23. case Keyword::Accentcolor:
  24. case Keyword::Accentcolortext:
  25. case Keyword::Activeborder:
  26. case Keyword::Activecaption:
  27. case Keyword::Activetext:
  28. case Keyword::Appworkspace:
  29. case Keyword::Background:
  30. case Keyword::Buttonborder:
  31. case Keyword::Buttonface:
  32. case Keyword::Buttonhighlight:
  33. case Keyword::Buttonshadow:
  34. case Keyword::Buttontext:
  35. case Keyword::Canvas:
  36. case Keyword::Canvastext:
  37. case Keyword::Captiontext:
  38. case Keyword::Currentcolor:
  39. case Keyword::Field:
  40. case Keyword::Fieldtext:
  41. case Keyword::Graytext:
  42. case Keyword::Highlight:
  43. case Keyword::Highlighttext:
  44. case Keyword::Inactiveborder:
  45. case Keyword::Inactivecaption:
  46. case Keyword::Inactivecaptiontext:
  47. case Keyword::Infobackground:
  48. case Keyword::Infotext:
  49. case Keyword::LibwebLink:
  50. case Keyword::LibwebPaletteActiveLink:
  51. case Keyword::LibwebPaletteActiveWindowBorder1:
  52. case Keyword::LibwebPaletteActiveWindowBorder2:
  53. case Keyword::LibwebPaletteActiveWindowTitle:
  54. case Keyword::LibwebPaletteBase:
  55. case Keyword::LibwebPaletteBaseText:
  56. case Keyword::LibwebPaletteButton:
  57. case Keyword::LibwebPaletteButtonText:
  58. case Keyword::LibwebPaletteDesktopBackground:
  59. case Keyword::LibwebPaletteFocusOutline:
  60. case Keyword::LibwebPaletteHighlightWindowBorder1:
  61. case Keyword::LibwebPaletteHighlightWindowBorder2:
  62. case Keyword::LibwebPaletteHighlightWindowTitle:
  63. case Keyword::LibwebPaletteHoverHighlight:
  64. case Keyword::LibwebPaletteInactiveSelection:
  65. case Keyword::LibwebPaletteInactiveSelectionText:
  66. case Keyword::LibwebPaletteInactiveWindowBorder1:
  67. case Keyword::LibwebPaletteInactiveWindowBorder2:
  68. case Keyword::LibwebPaletteInactiveWindowTitle:
  69. case Keyword::LibwebPaletteLink:
  70. case Keyword::LibwebPaletteMenuBase:
  71. case Keyword::LibwebPaletteMenuBaseText:
  72. case Keyword::LibwebPaletteMenuSelection:
  73. case Keyword::LibwebPaletteMenuSelectionText:
  74. case Keyword::LibwebPaletteMenuStripe:
  75. case Keyword::LibwebPaletteMovingWindowBorder1:
  76. case Keyword::LibwebPaletteMovingWindowBorder2:
  77. case Keyword::LibwebPaletteMovingWindowTitle:
  78. case Keyword::LibwebPaletteRubberBandBorder:
  79. case Keyword::LibwebPaletteRubberBandFill:
  80. case Keyword::LibwebPaletteRuler:
  81. case Keyword::LibwebPaletteRulerActiveText:
  82. case Keyword::LibwebPaletteRulerBorder:
  83. case Keyword::LibwebPaletteRulerInactiveText:
  84. case Keyword::LibwebPaletteSelection:
  85. case Keyword::LibwebPaletteSelectionText:
  86. case Keyword::LibwebPaletteSyntaxComment:
  87. case Keyword::LibwebPaletteSyntaxControlKeyword:
  88. case Keyword::LibwebPaletteSyntaxIdentifier:
  89. case Keyword::LibwebPaletteSyntaxKeyword:
  90. case Keyword::LibwebPaletteSyntaxNumber:
  91. case Keyword::LibwebPaletteSyntaxOperator:
  92. case Keyword::LibwebPaletteSyntaxPreprocessorStatement:
  93. case Keyword::LibwebPaletteSyntaxPreprocessorValue:
  94. case Keyword::LibwebPaletteSyntaxPunctuation:
  95. case Keyword::LibwebPaletteSyntaxString:
  96. case Keyword::LibwebPaletteSyntaxType:
  97. case Keyword::LibwebPaletteTextCursor:
  98. case Keyword::LibwebPaletteThreedHighlight:
  99. case Keyword::LibwebPaletteThreedShadow1:
  100. case Keyword::LibwebPaletteThreedShadow2:
  101. case Keyword::LibwebPaletteVisitedLink:
  102. case Keyword::LibwebPaletteWindow:
  103. case Keyword::LibwebPaletteWindowText:
  104. case Keyword::Linktext:
  105. case Keyword::Mark:
  106. case Keyword::Marktext:
  107. case Keyword::Menu:
  108. case Keyword::Menutext:
  109. case Keyword::Scrollbar:
  110. case Keyword::Selecteditem:
  111. case Keyword::Selecteditemtext:
  112. case Keyword::Threeddarkshadow:
  113. case Keyword::Threedface:
  114. case Keyword::Threedhighlight:
  115. case Keyword::Threedlightshadow:
  116. case Keyword::Threedshadow:
  117. case Keyword::Visitedtext:
  118. case Keyword::Window:
  119. case Keyword::Windowframe:
  120. case Keyword::Windowtext:
  121. return true;
  122. default:
  123. return false;
  124. }
  125. }
  126. bool CSSKeywordValue::has_color() const
  127. {
  128. return is_color(keyword());
  129. }
  130. Color CSSKeywordValue::to_color(Optional<Layout::NodeWithStyle const&> node) const
  131. {
  132. if (keyword() == Keyword::Currentcolor) {
  133. if (!node.has_value() || !node->has_style())
  134. return Color::Black;
  135. return node->computed_values().color();
  136. }
  137. // First, handle <system-color>s, since they don't require a node.
  138. // https://www.w3.org/TR/css-color-4/#css-system-colors
  139. // https://www.w3.org/TR/css-color-4/#deprecated-system-colors
  140. switch (keyword()) {
  141. case Keyword::Accentcolor:
  142. return SystemColor::accent_color();
  143. case Keyword::Accentcolortext:
  144. return SystemColor::accent_color_text();
  145. case Keyword::Activetext:
  146. return SystemColor::active_text();
  147. case Keyword::Buttonborder:
  148. case Keyword::Activeborder:
  149. case Keyword::Inactiveborder:
  150. case Keyword::Threeddarkshadow:
  151. case Keyword::Threedhighlight:
  152. case Keyword::Threedlightshadow:
  153. case Keyword::Threedshadow:
  154. case Keyword::Windowframe:
  155. return SystemColor::button_border();
  156. case Keyword::Buttonface:
  157. case Keyword::Buttonhighlight:
  158. case Keyword::Buttonshadow:
  159. case Keyword::Threedface:
  160. return SystemColor::button_face();
  161. case Keyword::Buttontext:
  162. return SystemColor::button_text();
  163. case Keyword::Canvas:
  164. case Keyword::Appworkspace:
  165. case Keyword::Background:
  166. case Keyword::Inactivecaption:
  167. case Keyword::Infobackground:
  168. case Keyword::Menu:
  169. case Keyword::Scrollbar:
  170. case Keyword::Window:
  171. return SystemColor::canvas();
  172. case Keyword::Canvastext:
  173. case Keyword::Activecaption:
  174. case Keyword::Captiontext:
  175. case Keyword::Infotext:
  176. case Keyword::Menutext:
  177. case Keyword::Windowtext:
  178. return SystemColor::canvas_text();
  179. case Keyword::Field:
  180. return SystemColor::field();
  181. case Keyword::Fieldtext:
  182. return SystemColor::field_text();
  183. case Keyword::Graytext:
  184. case Keyword::Inactivecaptiontext:
  185. return SystemColor::gray_text();
  186. case Keyword::Highlight:
  187. return SystemColor::highlight();
  188. case Keyword::Highlighttext:
  189. return SystemColor::highlight_text();
  190. case Keyword::Mark:
  191. return SystemColor::mark();
  192. case Keyword::Marktext:
  193. return SystemColor::mark_text();
  194. case Keyword::Selecteditem:
  195. return SystemColor::selected_item();
  196. case Keyword::Selecteditemtext:
  197. return SystemColor::selected_item_text();
  198. case Keyword::Visitedtext:
  199. return SystemColor::visited_text();
  200. default:
  201. break;
  202. }
  203. if (!node.has_value()) {
  204. // FIXME: Can't resolve palette colors without layout node.
  205. return Color::Black;
  206. }
  207. auto& document = node->document();
  208. if (keyword() == Keyword::LibwebLink || keyword() == Keyword::Linktext)
  209. return document.normal_link_color();
  210. auto palette = document.page().palette();
  211. switch (keyword()) {
  212. case Keyword::LibwebPaletteDesktopBackground:
  213. return palette.color(ColorRole::DesktopBackground);
  214. case Keyword::LibwebPaletteActiveWindowBorder1:
  215. return palette.color(ColorRole::ActiveWindowBorder1);
  216. case Keyword::LibwebPaletteActiveWindowBorder2:
  217. return palette.color(ColorRole::ActiveWindowBorder2);
  218. case Keyword::LibwebPaletteActiveWindowTitle:
  219. return palette.color(ColorRole::ActiveWindowTitle);
  220. case Keyword::LibwebPaletteInactiveWindowBorder1:
  221. return palette.color(ColorRole::InactiveWindowBorder1);
  222. case Keyword::LibwebPaletteInactiveWindowBorder2:
  223. return palette.color(ColorRole::InactiveWindowBorder2);
  224. case Keyword::LibwebPaletteInactiveWindowTitle:
  225. return palette.color(ColorRole::InactiveWindowTitle);
  226. case Keyword::LibwebPaletteMovingWindowBorder1:
  227. return palette.color(ColorRole::MovingWindowBorder1);
  228. case Keyword::LibwebPaletteMovingWindowBorder2:
  229. return palette.color(ColorRole::MovingWindowBorder2);
  230. case Keyword::LibwebPaletteMovingWindowTitle:
  231. return palette.color(ColorRole::MovingWindowTitle);
  232. case Keyword::LibwebPaletteHighlightWindowBorder1:
  233. return palette.color(ColorRole::HighlightWindowBorder1);
  234. case Keyword::LibwebPaletteHighlightWindowBorder2:
  235. return palette.color(ColorRole::HighlightWindowBorder2);
  236. case Keyword::LibwebPaletteHighlightWindowTitle:
  237. return palette.color(ColorRole::HighlightWindowTitle);
  238. case Keyword::LibwebPaletteMenuStripe:
  239. return palette.color(ColorRole::MenuStripe);
  240. case Keyword::LibwebPaletteMenuBase:
  241. return palette.color(ColorRole::MenuBase);
  242. case Keyword::LibwebPaletteMenuBaseText:
  243. return palette.color(ColorRole::MenuBaseText);
  244. case Keyword::LibwebPaletteMenuSelection:
  245. return palette.color(ColorRole::MenuSelection);
  246. case Keyword::LibwebPaletteMenuSelectionText:
  247. return palette.color(ColorRole::MenuSelectionText);
  248. case Keyword::LibwebPaletteWindow:
  249. return palette.color(ColorRole::Window);
  250. case Keyword::LibwebPaletteWindowText:
  251. return palette.color(ColorRole::WindowText);
  252. case Keyword::LibwebPaletteButton:
  253. return palette.color(ColorRole::Button);
  254. case Keyword::LibwebPaletteButtonText:
  255. return palette.color(ColorRole::ButtonText);
  256. case Keyword::LibwebPaletteBase:
  257. return palette.color(ColorRole::Base);
  258. case Keyword::LibwebPaletteBaseText:
  259. return palette.color(ColorRole::BaseText);
  260. case Keyword::LibwebPaletteThreedHighlight:
  261. return palette.color(ColorRole::ThreedHighlight);
  262. case Keyword::LibwebPaletteThreedShadow1:
  263. return palette.color(ColorRole::ThreedShadow1);
  264. case Keyword::LibwebPaletteThreedShadow2:
  265. return palette.color(ColorRole::ThreedShadow2);
  266. case Keyword::LibwebPaletteHoverHighlight:
  267. return palette.color(ColorRole::HoverHighlight);
  268. case Keyword::LibwebPaletteSelection:
  269. return palette.color(ColorRole::Selection);
  270. case Keyword::LibwebPaletteSelectionText:
  271. return palette.color(ColorRole::SelectionText);
  272. case Keyword::LibwebPaletteInactiveSelection:
  273. return palette.color(ColorRole::InactiveSelection);
  274. case Keyword::LibwebPaletteInactiveSelectionText:
  275. return palette.color(ColorRole::InactiveSelectionText);
  276. case Keyword::LibwebPaletteRubberBandFill:
  277. return palette.color(ColorRole::RubberBandFill);
  278. case Keyword::LibwebPaletteRubberBandBorder:
  279. return palette.color(ColorRole::RubberBandBorder);
  280. case Keyword::LibwebPaletteLink:
  281. return palette.color(ColorRole::Link);
  282. case Keyword::LibwebPaletteActiveLink:
  283. return palette.color(ColorRole::ActiveLink);
  284. case Keyword::LibwebPaletteVisitedLink:
  285. return palette.color(ColorRole::VisitedLink);
  286. case Keyword::LibwebPaletteRuler:
  287. return palette.color(ColorRole::Ruler);
  288. case Keyword::LibwebPaletteRulerBorder:
  289. return palette.color(ColorRole::RulerBorder);
  290. case Keyword::LibwebPaletteRulerActiveText:
  291. return palette.color(ColorRole::RulerActiveText);
  292. case Keyword::LibwebPaletteRulerInactiveText:
  293. return palette.color(ColorRole::RulerInactiveText);
  294. case Keyword::LibwebPaletteTextCursor:
  295. return palette.color(ColorRole::TextCursor);
  296. case Keyword::LibwebPaletteFocusOutline:
  297. return palette.color(ColorRole::FocusOutline);
  298. case Keyword::LibwebPaletteSyntaxComment:
  299. return palette.color(ColorRole::SyntaxComment);
  300. case Keyword::LibwebPaletteSyntaxNumber:
  301. return palette.color(ColorRole::SyntaxNumber);
  302. case Keyword::LibwebPaletteSyntaxString:
  303. return palette.color(ColorRole::SyntaxString);
  304. case Keyword::LibwebPaletteSyntaxType:
  305. return palette.color(ColorRole::SyntaxType);
  306. case Keyword::LibwebPaletteSyntaxPunctuation:
  307. return palette.color(ColorRole::SyntaxPunctuation);
  308. case Keyword::LibwebPaletteSyntaxOperator:
  309. return palette.color(ColorRole::SyntaxOperator);
  310. case Keyword::LibwebPaletteSyntaxKeyword:
  311. return palette.color(ColorRole::SyntaxKeyword);
  312. case Keyword::LibwebPaletteSyntaxControlKeyword:
  313. return palette.color(ColorRole::SyntaxControlKeyword);
  314. case Keyword::LibwebPaletteSyntaxIdentifier:
  315. return palette.color(ColorRole::SyntaxIdentifier);
  316. case Keyword::LibwebPaletteSyntaxPreprocessorStatement:
  317. return palette.color(ColorRole::SyntaxPreprocessorStatement);
  318. case Keyword::LibwebPaletteSyntaxPreprocessorValue:
  319. return palette.color(ColorRole::SyntaxPreprocessorValue);
  320. default:
  321. return {};
  322. }
  323. }
  324. }