StyleProperties.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*
  2. * Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021-2024, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/TypeCasts.h>
  8. #include <LibCore/DirIterator.h>
  9. #include <LibWeb/CSS/Clip.h>
  10. #include <LibWeb/CSS/StyleProperties.h>
  11. #include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
  12. #include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
  13. #include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/CounterDefinitionsStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/CounterStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/GridAutoFlowStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/MathDepthStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  28. #include <LibWeb/CSS/StyleValues/ScrollbarGutterStyleValue.h>
  29. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  30. #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
  31. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  32. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  33. #include <LibWeb/Layout/BlockContainer.h>
  34. #include <LibWeb/Layout/Node.h>
  35. #include <LibWeb/Platform/FontPlugin.h>
  36. namespace Web::CSS {
  37. NonnullRefPtr<StyleProperties::Data> StyleProperties::Data::clone() const
  38. {
  39. auto clone = adopt_ref(*new StyleProperties::Data);
  40. clone->m_animation_name_source = m_animation_name_source;
  41. clone->m_transition_property_source = m_transition_property_source;
  42. clone->m_property_values = m_property_values;
  43. clone->m_property_important = m_property_important;
  44. clone->m_property_inherited = m_property_inherited;
  45. clone->m_animated_property_values = m_animated_property_values;
  46. clone->m_math_depth = m_math_depth;
  47. clone->m_font_list = m_font_list;
  48. clone->m_line_height = m_line_height;
  49. return clone;
  50. }
  51. NonnullRefPtr<StyleProperties> StyleProperties::clone() const
  52. {
  53. auto cloned = adopt_ref(*new StyleProperties);
  54. cloned->m_data = m_data;
  55. return cloned;
  56. }
  57. bool StyleProperties::is_property_important(CSS::PropertyID property_id) const
  58. {
  59. size_t n = to_underlying(property_id);
  60. return m_data->m_property_important[n / 8] & (1 << (n % 8));
  61. }
  62. void StyleProperties::set_property_important(CSS::PropertyID property_id, Important important)
  63. {
  64. size_t n = to_underlying(property_id);
  65. if (important == Important::Yes)
  66. m_data->m_property_important[n / 8] |= (1 << (n % 8));
  67. else
  68. m_data->m_property_important[n / 8] &= ~(1 << (n % 8));
  69. }
  70. bool StyleProperties::is_property_inherited(CSS::PropertyID property_id) const
  71. {
  72. size_t n = to_underlying(property_id);
  73. return m_data->m_property_inherited[n / 8] & (1 << (n % 8));
  74. }
  75. void StyleProperties::set_property_inherited(CSS::PropertyID property_id, Inherited inherited)
  76. {
  77. size_t n = to_underlying(property_id);
  78. if (inherited == Inherited::Yes)
  79. m_data->m_property_inherited[n / 8] |= (1 << (n % 8));
  80. else
  81. m_data->m_property_inherited[n / 8] &= ~(1 << (n % 8));
  82. }
  83. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<CSSStyleValue const> value, Inherited inherited, Important important)
  84. {
  85. m_data->m_property_values[to_underlying(id)] = move(value);
  86. set_property_important(id, important);
  87. set_property_inherited(id, inherited);
  88. }
  89. void StyleProperties::revert_property(CSS::PropertyID id, StyleProperties const& style_for_revert)
  90. {
  91. m_data->m_property_values[to_underlying(id)] = style_for_revert.m_data->m_property_values[to_underlying(id)];
  92. set_property_important(id, style_for_revert.is_property_important(id) ? Important::Yes : Important::No);
  93. set_property_inherited(id, style_for_revert.is_property_inherited(id) ? Inherited::Yes : Inherited::No);
  94. }
  95. void StyleProperties::set_animated_property(CSS::PropertyID id, NonnullRefPtr<CSSStyleValue const> value)
  96. {
  97. m_data->m_animated_property_values.set(id, move(value));
  98. }
  99. void StyleProperties::reset_animated_properties()
  100. {
  101. m_data->m_animated_property_values.clear();
  102. }
  103. NonnullRefPtr<CSSStyleValue const> StyleProperties::property(CSS::PropertyID property_id, WithAnimationsApplied return_animated_value) const
  104. {
  105. if (return_animated_value == WithAnimationsApplied::Yes) {
  106. if (auto animated_value = m_data->m_animated_property_values.get(property_id).value_or(nullptr))
  107. return *animated_value;
  108. }
  109. // By the time we call this method, all properties have values assigned.
  110. return *m_data->m_property_values[to_underlying(property_id)];
  111. }
  112. RefPtr<CSSStyleValue const> StyleProperties::maybe_null_property(CSS::PropertyID property_id) const
  113. {
  114. if (auto animated_value = m_data->m_animated_property_values.get(property_id).value_or(nullptr))
  115. return *animated_value;
  116. return m_data->m_property_values[to_underlying(property_id)];
  117. }
  118. CSS::Size StyleProperties::size_value(CSS::PropertyID id) const
  119. {
  120. auto value = property(id);
  121. if (value->is_keyword()) {
  122. switch (value->to_keyword()) {
  123. case Keyword::Auto:
  124. return CSS::Size::make_auto();
  125. case Keyword::MinContent:
  126. return CSS::Size::make_min_content();
  127. case Keyword::MaxContent:
  128. return CSS::Size::make_max_content();
  129. case Keyword::FitContent:
  130. return CSS::Size::make_fit_content();
  131. case Keyword::None:
  132. return CSS::Size::make_none();
  133. default:
  134. VERIFY_NOT_REACHED();
  135. }
  136. }
  137. if (value->is_math())
  138. return CSS::Size::make_calculated(const_cast<CSSMathValue&>(value->as_math()));
  139. if (value->is_percentage())
  140. return CSS::Size::make_percentage(value->as_percentage().percentage());
  141. if (value->is_length()) {
  142. auto length = value->as_length().length();
  143. if (length.is_auto())
  144. return CSS::Size::make_auto();
  145. return CSS::Size::make_length(length);
  146. }
  147. // FIXME: Support `fit-content(<length>)`
  148. dbgln("FIXME: Unsupported size value: `{}`, treating as `auto`", value->to_string());
  149. return CSS::Size::make_auto();
  150. }
  151. LengthPercentage StyleProperties::length_percentage_or_fallback(CSS::PropertyID id, LengthPercentage const& fallback) const
  152. {
  153. return length_percentage(id).value_or(fallback);
  154. }
  155. Optional<LengthPercentage> StyleProperties::length_percentage(CSS::PropertyID id) const
  156. {
  157. auto value = property(id);
  158. if (value->is_math())
  159. return LengthPercentage { const_cast<CSSMathValue&>(value->as_math()) };
  160. if (value->is_percentage())
  161. return value->as_percentage().percentage();
  162. if (value->is_length())
  163. return value->as_length().length();
  164. if (value->has_auto())
  165. return LengthPercentage { Length::make_auto() };
  166. return {};
  167. }
  168. LengthBox StyleProperties::length_box(CSS::PropertyID left_id, CSS::PropertyID top_id, CSS::PropertyID right_id, CSS::PropertyID bottom_id, const CSS::Length& default_value) const
  169. {
  170. LengthBox box;
  171. box.left() = length_percentage_or_fallback(left_id, default_value);
  172. box.top() = length_percentage_or_fallback(top_id, default_value);
  173. box.right() = length_percentage_or_fallback(right_id, default_value);
  174. box.bottom() = length_percentage_or_fallback(bottom_id, default_value);
  175. return box;
  176. }
  177. Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithStyle const& node, Color fallback) const
  178. {
  179. auto value = property(id);
  180. if (!value->has_color())
  181. return fallback;
  182. return value->to_color(node);
  183. }
  184. NonnullRefPtr<Gfx::Font const> StyleProperties::font_fallback(bool monospace, bool bold)
  185. {
  186. if (monospace && bold)
  187. return Platform::FontPlugin::the().default_fixed_width_font().bold_variant();
  188. if (monospace)
  189. return Platform::FontPlugin::the().default_fixed_width_font();
  190. if (bold)
  191. return Platform::FontPlugin::the().default_font().bold_variant();
  192. return Platform::FontPlugin::the().default_font();
  193. }
  194. CSSPixels StyleProperties::compute_line_height(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const
  195. {
  196. auto line_height = property(CSS::PropertyID::LineHeight);
  197. if (line_height->is_keyword() && line_height->to_keyword() == Keyword::Normal)
  198. return font_metrics.line_height;
  199. if (line_height->is_length()) {
  200. auto line_height_length = line_height->as_length().length();
  201. if (!line_height_length.is_auto())
  202. return line_height_length.to_px(viewport_rect, font_metrics, root_font_metrics);
  203. }
  204. if (line_height->is_number())
  205. return Length(line_height->as_number().number(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  206. if (line_height->is_percentage()) {
  207. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  208. auto& percentage = line_height->as_percentage().percentage();
  209. return Length(percentage.as_fraction(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  210. }
  211. if (line_height->is_math()) {
  212. if (line_height->as_math().resolves_to_number()) {
  213. auto resolved = line_height->as_math().resolve_number();
  214. if (!resolved.has_value()) {
  215. dbgln("FIXME: Failed to resolve calc() line-height (number): {}", line_height->as_math().to_string());
  216. return CSSPixels::nearest_value_for(m_data->m_font_list->first().pixel_metrics().line_spacing());
  217. }
  218. return Length(resolved.value(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  219. }
  220. auto resolved = line_height->as_math().resolve_length(Length::ResolutionContext { viewport_rect, font_metrics, root_font_metrics });
  221. if (!resolved.has_value()) {
  222. dbgln("FIXME: Failed to resolve calc() line-height: {}", line_height->as_math().to_string());
  223. return CSSPixels::nearest_value_for(m_data->m_font_list->first().pixel_metrics().line_spacing());
  224. }
  225. return resolved->to_px(viewport_rect, font_metrics, root_font_metrics);
  226. }
  227. return font_metrics.line_height;
  228. }
  229. Optional<int> StyleProperties::z_index() const
  230. {
  231. auto value = property(CSS::PropertyID::ZIndex);
  232. if (value->has_auto())
  233. return {};
  234. if (value->is_integer()) {
  235. // Clamp z-index to the range of a signed 32-bit integer for consistency with other engines.
  236. auto integer = value->as_integer().integer();
  237. if (integer >= NumericLimits<int>::max())
  238. return NumericLimits<int>::max();
  239. if (integer <= NumericLimits<int>::min())
  240. return NumericLimits<int>::min();
  241. return static_cast<int>(integer);
  242. }
  243. return {};
  244. }
  245. float StyleProperties::resolve_opacity_value(CSSStyleValue const& value)
  246. {
  247. float unclamped_opacity = 1.0f;
  248. if (value.is_number()) {
  249. unclamped_opacity = value.as_number().number();
  250. } else if (value.is_math()) {
  251. auto& calculated = value.as_math();
  252. if (calculated.resolves_to_percentage()) {
  253. auto maybe_percentage = value.as_math().resolve_percentage();
  254. if (maybe_percentage.has_value())
  255. unclamped_opacity = maybe_percentage->as_fraction();
  256. else
  257. dbgln("Unable to resolve calc() as opacity (percentage): {}", value.to_string());
  258. } else if (calculated.resolves_to_number()) {
  259. auto maybe_number = const_cast<CSSMathValue&>(value.as_math()).resolve_number();
  260. if (maybe_number.has_value())
  261. unclamped_opacity = maybe_number.value();
  262. else
  263. dbgln("Unable to resolve calc() as opacity (number): {}", value.to_string());
  264. }
  265. } else if (value.is_percentage()) {
  266. unclamped_opacity = value.as_percentage().percentage().as_fraction();
  267. }
  268. return clamp(unclamped_opacity, 0.0f, 1.0f);
  269. }
  270. float StyleProperties::opacity() const
  271. {
  272. auto value = property(CSS::PropertyID::Opacity);
  273. return resolve_opacity_value(*value);
  274. }
  275. float StyleProperties::fill_opacity() const
  276. {
  277. auto value = property(CSS::PropertyID::FillOpacity);
  278. return resolve_opacity_value(*value);
  279. }
  280. float StyleProperties::stroke_opacity() const
  281. {
  282. auto value = property(CSS::PropertyID::StrokeOpacity);
  283. return resolve_opacity_value(*value);
  284. }
  285. float StyleProperties::stop_opacity() const
  286. {
  287. auto value = property(CSS::PropertyID::StopOpacity);
  288. return resolve_opacity_value(*value);
  289. }
  290. Optional<CSS::FillRule> StyleProperties::fill_rule() const
  291. {
  292. auto value = property(CSS::PropertyID::FillRule);
  293. return keyword_to_fill_rule(value->to_keyword());
  294. }
  295. Optional<CSS::ClipRule> StyleProperties::clip_rule() const
  296. {
  297. auto value = property(CSS::PropertyID::ClipRule);
  298. return keyword_to_fill_rule(value->to_keyword());
  299. }
  300. Optional<CSS::FlexDirection> StyleProperties::flex_direction() const
  301. {
  302. auto value = property(CSS::PropertyID::FlexDirection);
  303. return keyword_to_flex_direction(value->to_keyword());
  304. }
  305. Optional<CSS::FlexWrap> StyleProperties::flex_wrap() const
  306. {
  307. auto value = property(CSS::PropertyID::FlexWrap);
  308. return keyword_to_flex_wrap(value->to_keyword());
  309. }
  310. Optional<CSS::FlexBasis> StyleProperties::flex_basis() const
  311. {
  312. auto value = property(CSS::PropertyID::FlexBasis);
  313. if (value->is_keyword() && value->to_keyword() == CSS::Keyword::Content)
  314. return CSS::FlexBasisContent {};
  315. return size_value(CSS::PropertyID::FlexBasis);
  316. }
  317. float StyleProperties::flex_grow() const
  318. {
  319. auto value = property(CSS::PropertyID::FlexGrow);
  320. if (!value->is_number())
  321. return 0;
  322. return value->as_number().number();
  323. }
  324. float StyleProperties::flex_shrink() const
  325. {
  326. auto value = property(CSS::PropertyID::FlexShrink);
  327. if (!value->is_number())
  328. return 1;
  329. return value->as_number().number();
  330. }
  331. int StyleProperties::order() const
  332. {
  333. auto value = property(CSS::PropertyID::Order);
  334. if (!value->is_integer())
  335. return 0;
  336. return value->as_integer().integer();
  337. }
  338. Optional<CSS::ImageRendering> StyleProperties::image_rendering() const
  339. {
  340. auto value = property(CSS::PropertyID::ImageRendering);
  341. return keyword_to_image_rendering(value->to_keyword());
  342. }
  343. CSS::Length StyleProperties::border_spacing_horizontal() const
  344. {
  345. auto value = property(CSS::PropertyID::BorderSpacing);
  346. if (value->is_length())
  347. return value->as_length().length();
  348. auto const& list = value->as_value_list();
  349. return list.value_at(0, false)->as_length().length();
  350. }
  351. CSS::Length StyleProperties::border_spacing_vertical() const
  352. {
  353. auto value = property(CSS::PropertyID::BorderSpacing);
  354. if (value->is_length())
  355. return value->as_length().length();
  356. auto const& list = value->as_value_list();
  357. return list.value_at(1, false)->as_length().length();
  358. }
  359. Optional<CSS::CaptionSide> StyleProperties::caption_side() const
  360. {
  361. auto value = property(CSS::PropertyID::CaptionSide);
  362. return keyword_to_caption_side(value->to_keyword());
  363. }
  364. CSS::Clip StyleProperties::clip() const
  365. {
  366. auto value = property(CSS::PropertyID::Clip);
  367. if (!value->is_rect())
  368. return CSS::Clip::make_auto();
  369. return CSS::Clip(value->as_rect().rect());
  370. }
  371. Optional<CSS::JustifyContent> StyleProperties::justify_content() const
  372. {
  373. auto value = property(CSS::PropertyID::JustifyContent);
  374. return keyword_to_justify_content(value->to_keyword());
  375. }
  376. Optional<CSS::JustifyItems> StyleProperties::justify_items() const
  377. {
  378. auto value = property(CSS::PropertyID::JustifyItems);
  379. return keyword_to_justify_items(value->to_keyword());
  380. }
  381. Optional<CSS::JustifySelf> StyleProperties::justify_self() const
  382. {
  383. auto value = property(CSS::PropertyID::JustifySelf);
  384. return keyword_to_justify_self(value->to_keyword());
  385. }
  386. Vector<CSS::Transformation> StyleProperties::transformations_for_style_value(CSSStyleValue const& value)
  387. {
  388. if (value.is_keyword() && value.to_keyword() == CSS::Keyword::None)
  389. return {};
  390. if (!value.is_value_list())
  391. return {};
  392. auto& list = value.as_value_list();
  393. Vector<CSS::Transformation> transformations;
  394. for (auto& it : list.values()) {
  395. if (!it->is_transformation())
  396. return {};
  397. auto& transformation_style_value = it->as_transformation();
  398. auto function = transformation_style_value.transform_function();
  399. auto function_metadata = transform_function_metadata(function);
  400. Vector<TransformValue> values;
  401. size_t argument_index = 0;
  402. for (auto& transformation_value : transformation_style_value.values()) {
  403. if (transformation_value->is_math()) {
  404. auto& calculated = transformation_value->as_math();
  405. if (calculated.resolves_to_length_percentage()) {
  406. values.append(CSS::LengthPercentage { calculated });
  407. } else if (calculated.resolves_to_percentage()) {
  408. // FIXME: Maybe transform this for loop to always check the metadata for the correct types
  409. if (function_metadata.parameters[argument_index].type == TransformFunctionParameterType::NumberPercentage) {
  410. values.append(NumberPercentage { calculated.resolve_percentage().value() });
  411. } else {
  412. values.append(LengthPercentage { calculated.resolve_percentage().value() });
  413. }
  414. } else if (calculated.resolves_to_number()) {
  415. values.append({ Number(Number::Type::Number, calculated.resolve_number().value()) });
  416. } else if (calculated.resolves_to_angle()) {
  417. values.append({ calculated.resolve_angle().value() });
  418. } else {
  419. dbgln("FIXME: Unsupported calc value in transform! {}", calculated.to_string());
  420. }
  421. } else if (transformation_value->is_length()) {
  422. values.append({ transformation_value->as_length().length() });
  423. } else if (transformation_value->is_percentage()) {
  424. if (function_metadata.parameters[argument_index].type == TransformFunctionParameterType::NumberPercentage) {
  425. values.append(NumberPercentage { transformation_value->as_percentage().percentage() });
  426. } else {
  427. values.append(LengthPercentage { transformation_value->as_percentage().percentage() });
  428. }
  429. } else if (transformation_value->is_number()) {
  430. values.append({ Number(Number::Type::Number, transformation_value->as_number().number()) });
  431. } else if (transformation_value->is_angle()) {
  432. values.append({ transformation_value->as_angle().angle() });
  433. } else {
  434. dbgln("FIXME: Unsupported value in transform! {}", transformation_value->to_string());
  435. }
  436. argument_index++;
  437. }
  438. transformations.empend(function, move(values));
  439. }
  440. return transformations;
  441. }
  442. Vector<CSS::Transformation> StyleProperties::transformations() const
  443. {
  444. return transformations_for_style_value(property(CSS::PropertyID::Transform));
  445. }
  446. static Optional<LengthPercentage> length_percentage_for_style_value(CSSStyleValue const& value)
  447. {
  448. if (value.is_length())
  449. return value.as_length().length();
  450. if (value.is_percentage())
  451. return value.as_percentage().percentage();
  452. return {};
  453. }
  454. Optional<CSS::TransformBox> StyleProperties::transform_box() const
  455. {
  456. auto value = property(CSS::PropertyID::TransformBox);
  457. return keyword_to_transform_box(value->to_keyword());
  458. }
  459. CSS::TransformOrigin StyleProperties::transform_origin() const
  460. {
  461. auto value = property(CSS::PropertyID::TransformOrigin);
  462. if (!value->is_value_list() || value->as_value_list().size() != 2)
  463. return {};
  464. auto const& list = value->as_value_list();
  465. auto x_value = length_percentage_for_style_value(list.values()[0]);
  466. auto y_value = length_percentage_for_style_value(list.values()[1]);
  467. if (!x_value.has_value() || !y_value.has_value()) {
  468. return {};
  469. }
  470. return { x_value.value(), y_value.value() };
  471. }
  472. Optional<Color> StyleProperties::accent_color(Layout::NodeWithStyle const& node) const
  473. {
  474. auto value = property(CSS::PropertyID::AccentColor);
  475. if (value->has_color())
  476. return value->to_color(node);
  477. return {};
  478. }
  479. Optional<CSS::AlignContent> StyleProperties::align_content() const
  480. {
  481. auto value = property(CSS::PropertyID::AlignContent);
  482. return keyword_to_align_content(value->to_keyword());
  483. }
  484. Optional<CSS::AlignItems> StyleProperties::align_items() const
  485. {
  486. auto value = property(CSS::PropertyID::AlignItems);
  487. return keyword_to_align_items(value->to_keyword());
  488. }
  489. Optional<CSS::AlignSelf> StyleProperties::align_self() const
  490. {
  491. auto value = property(CSS::PropertyID::AlignSelf);
  492. return keyword_to_align_self(value->to_keyword());
  493. }
  494. Optional<CSS::Appearance> StyleProperties::appearance() const
  495. {
  496. auto value = property(CSS::PropertyID::Appearance);
  497. auto appearance = keyword_to_appearance(value->to_keyword());
  498. if (appearance.has_value()) {
  499. switch (*appearance) {
  500. // Note: All these compatibility values can be treated as 'auto'
  501. case CSS::Appearance::Textfield:
  502. case CSS::Appearance::MenulistButton:
  503. case CSS::Appearance::Searchfield:
  504. case CSS::Appearance::Textarea:
  505. case CSS::Appearance::PushButton:
  506. case CSS::Appearance::SliderHorizontal:
  507. case CSS::Appearance::Checkbox:
  508. case CSS::Appearance::Radio:
  509. case CSS::Appearance::SquareButton:
  510. case CSS::Appearance::Menulist:
  511. case CSS::Appearance::Listbox:
  512. case CSS::Appearance::Meter:
  513. case CSS::Appearance::ProgressBar:
  514. case CSS::Appearance::Button:
  515. appearance = CSS::Appearance::Auto;
  516. break;
  517. default:
  518. break;
  519. }
  520. }
  521. return appearance;
  522. }
  523. CSS::BackdropFilter StyleProperties::backdrop_filter() const
  524. {
  525. auto value = property(CSS::PropertyID::BackdropFilter);
  526. if (value->is_filter_value_list())
  527. return BackdropFilter(value->as_filter_value_list());
  528. return BackdropFilter::make_none();
  529. }
  530. Optional<CSS::Positioning> StyleProperties::position() const
  531. {
  532. auto value = property(CSS::PropertyID::Position);
  533. return keyword_to_positioning(value->to_keyword());
  534. }
  535. bool StyleProperties::operator==(StyleProperties const& other) const
  536. {
  537. if (m_data->m_property_values.size() != other.m_data->m_property_values.size())
  538. return false;
  539. for (size_t i = 0; i < m_data->m_property_values.size(); ++i) {
  540. auto const& my_style = m_data->m_property_values[i];
  541. auto const& other_style = other.m_data->m_property_values[i];
  542. if (!my_style) {
  543. if (other_style)
  544. return false;
  545. continue;
  546. }
  547. if (!other_style)
  548. return false;
  549. auto const& my_value = *my_style;
  550. auto const& other_value = *other_style;
  551. if (my_value.type() != other_value.type())
  552. return false;
  553. if (my_value != other_value)
  554. return false;
  555. }
  556. return true;
  557. }
  558. Optional<CSS::TextAnchor> StyleProperties::text_anchor() const
  559. {
  560. auto value = property(CSS::PropertyID::TextAnchor);
  561. return keyword_to_text_anchor(value->to_keyword());
  562. }
  563. Optional<CSS::TextAlign> StyleProperties::text_align() const
  564. {
  565. auto value = property(CSS::PropertyID::TextAlign);
  566. return keyword_to_text_align(value->to_keyword());
  567. }
  568. Optional<CSS::TextJustify> StyleProperties::text_justify() const
  569. {
  570. auto value = property(CSS::PropertyID::TextJustify);
  571. return keyword_to_text_justify(value->to_keyword());
  572. }
  573. Optional<CSS::TextOverflow> StyleProperties::text_overflow() const
  574. {
  575. auto value = property(CSS::PropertyID::TextOverflow);
  576. return keyword_to_text_overflow(value->to_keyword());
  577. }
  578. Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
  579. {
  580. auto value = property(CSS::PropertyID::PointerEvents);
  581. return keyword_to_pointer_events(value->to_keyword());
  582. }
  583. Variant<LengthOrCalculated, NumberOrCalculated> StyleProperties::tab_size() const
  584. {
  585. auto value = property(CSS::PropertyID::TabSize);
  586. if (value->is_math()) {
  587. auto& math_value = value->as_math();
  588. if (math_value.resolves_to_length()) {
  589. return LengthOrCalculated { math_value };
  590. }
  591. if (math_value.resolves_to_number()) {
  592. return NumberOrCalculated { math_value };
  593. }
  594. }
  595. if (value->is_length())
  596. return LengthOrCalculated { value->as_length().length() };
  597. return NumberOrCalculated { value->as_number().number() };
  598. }
  599. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  600. {
  601. auto value = property(CSS::PropertyID::WhiteSpace);
  602. return keyword_to_white_space(value->to_keyword());
  603. }
  604. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  605. {
  606. auto value = property(property_id);
  607. return keyword_to_line_style(value->to_keyword());
  608. }
  609. Optional<CSS::OutlineStyle> StyleProperties::outline_style() const
  610. {
  611. auto value = property(CSS::PropertyID::OutlineStyle);
  612. return keyword_to_outline_style(value->to_keyword());
  613. }
  614. Optional<CSS::Float> StyleProperties::float_() const
  615. {
  616. auto value = property(CSS::PropertyID::Float);
  617. return keyword_to_float(value->to_keyword());
  618. }
  619. Optional<CSS::Clear> StyleProperties::clear() const
  620. {
  621. auto value = property(CSS::PropertyID::Clear);
  622. return keyword_to_clear(value->to_keyword());
  623. }
  624. Optional<CSS::ColumnSpan> StyleProperties::column_span() const
  625. {
  626. auto value = property(CSS::PropertyID::ColumnSpan);
  627. return keyword_to_column_span(value->to_keyword());
  628. }
  629. StyleProperties::ContentDataAndQuoteNestingLevel StyleProperties::content(DOM::Element& element, u32 initial_quote_nesting_level) const
  630. {
  631. auto value = property(CSS::PropertyID::Content);
  632. auto quotes_data = quotes();
  633. auto quote_nesting_level = initial_quote_nesting_level;
  634. auto get_quote_string = [&](bool open, auto depth) {
  635. switch (quotes_data.type) {
  636. case QuotesData::Type::None:
  637. return FlyString {};
  638. case QuotesData::Type::Auto:
  639. // FIXME: "A typographically appropriate used value for quotes is automatically chosen by the UA
  640. // based on the content language of the element and/or its parent."
  641. if (open)
  642. return depth == 0 ? "“"_fly_string : "‘"_fly_string;
  643. return depth == 0 ? "”"_fly_string : "’"_fly_string;
  644. case QuotesData::Type::Specified:
  645. // If the depth is greater than the number of pairs, the last pair is repeated.
  646. auto& level = quotes_data.strings[min(depth, quotes_data.strings.size() - 1)];
  647. return open ? level[0] : level[1];
  648. }
  649. VERIFY_NOT_REACHED();
  650. };
  651. if (value->is_content()) {
  652. auto& content_style_value = value->as_content();
  653. CSS::ContentData content_data;
  654. // FIXME: The content is a list of things: strings, identifiers or functions that return strings, and images.
  655. // So it can't always be represented as a single String, but may have to be multiple boxes.
  656. // For now, we'll just assume strings since that is easiest.
  657. StringBuilder builder;
  658. for (auto const& item : content_style_value.content().values()) {
  659. if (item->is_string()) {
  660. builder.append(item->as_string().string_value());
  661. } else if (item->is_keyword()) {
  662. switch (item->to_keyword()) {
  663. case Keyword::OpenQuote:
  664. builder.append(get_quote_string(true, quote_nesting_level++));
  665. break;
  666. case Keyword::CloseQuote:
  667. // A 'close-quote' or 'no-close-quote' that would make the depth negative is in error and is ignored
  668. // (at rendering time): the depth stays at 0 and no quote mark is rendered (although the rest of the
  669. // 'content' property's value is still inserted).
  670. // - https://www.w3.org/TR/CSS21/generate.html#quotes-insert
  671. // (This is missing from the CONTENT-3 spec.)
  672. if (quote_nesting_level > 0)
  673. builder.append(get_quote_string(false, --quote_nesting_level));
  674. break;
  675. case Keyword::NoOpenQuote:
  676. quote_nesting_level++;
  677. break;
  678. case Keyword::NoCloseQuote:
  679. // NOTE: See CloseQuote
  680. if (quote_nesting_level > 0)
  681. quote_nesting_level--;
  682. break;
  683. default:
  684. dbgln("`{}` is not supported in `content` (yet?)", item->to_string());
  685. break;
  686. }
  687. } else if (item->is_counter()) {
  688. builder.append(item->as_counter().resolve(element));
  689. } else {
  690. // TODO: Implement images, and other things.
  691. dbgln("`{}` is not supported in `content` (yet?)", item->to_string());
  692. }
  693. }
  694. content_data.type = ContentData::Type::String;
  695. content_data.data = MUST(builder.to_string());
  696. if (content_style_value.has_alt_text()) {
  697. StringBuilder alt_text_builder;
  698. for (auto const& item : content_style_value.alt_text()->values()) {
  699. if (item->is_string()) {
  700. alt_text_builder.append(item->as_string().string_value());
  701. } else if (item->is_counter()) {
  702. alt_text_builder.append(item->as_counter().resolve(element));
  703. } else {
  704. dbgln("`{}` is not supported in `content` alt-text (yet?)", item->to_string());
  705. }
  706. }
  707. content_data.alt_text = MUST(alt_text_builder.to_string());
  708. }
  709. return { content_data, quote_nesting_level };
  710. }
  711. switch (value->to_keyword()) {
  712. case Keyword::None:
  713. return { { ContentData::Type::None }, quote_nesting_level };
  714. case Keyword::Normal:
  715. return { { ContentData::Type::Normal }, quote_nesting_level };
  716. default:
  717. break;
  718. }
  719. return { {}, quote_nesting_level };
  720. }
  721. Optional<CSS::ContentVisibility> StyleProperties::content_visibility() const
  722. {
  723. auto value = property(CSS::PropertyID::ContentVisibility);
  724. return keyword_to_content_visibility(value->to_keyword());
  725. }
  726. Optional<CSS::Cursor> StyleProperties::cursor() const
  727. {
  728. auto value = property(CSS::PropertyID::Cursor);
  729. return keyword_to_cursor(value->to_keyword());
  730. }
  731. Optional<CSS::Visibility> StyleProperties::visibility() const
  732. {
  733. auto value = property(CSS::PropertyID::Visibility);
  734. if (!value->is_keyword())
  735. return {};
  736. return keyword_to_visibility(value->to_keyword());
  737. }
  738. Display StyleProperties::display() const
  739. {
  740. auto value = property(PropertyID::Display);
  741. if (value->is_display()) {
  742. return value->as_display().display();
  743. }
  744. return Display::from_short(Display::Short::Inline);
  745. }
  746. Vector<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  747. {
  748. auto value = property(CSS::PropertyID::TextDecorationLine);
  749. if (value->is_value_list()) {
  750. Vector<CSS::TextDecorationLine> lines;
  751. auto& values = value->as_value_list().values();
  752. for (auto const& item : values) {
  753. lines.append(keyword_to_text_decoration_line(item->to_keyword()).value());
  754. }
  755. return lines;
  756. }
  757. if (value->is_keyword() && value->to_keyword() == Keyword::None)
  758. return {};
  759. dbgln("FIXME: Unsupported value for text-decoration-line: {}", value->to_string());
  760. return {};
  761. }
  762. Optional<CSS::TextDecorationStyle> StyleProperties::text_decoration_style() const
  763. {
  764. auto value = property(CSS::PropertyID::TextDecorationStyle);
  765. return keyword_to_text_decoration_style(value->to_keyword());
  766. }
  767. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  768. {
  769. auto value = property(CSS::PropertyID::TextTransform);
  770. return keyword_to_text_transform(value->to_keyword());
  771. }
  772. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  773. {
  774. auto value = property(CSS::PropertyID::ListStyleType);
  775. return keyword_to_list_style_type(value->to_keyword());
  776. }
  777. Optional<CSS::ListStylePosition> StyleProperties::list_style_position() const
  778. {
  779. auto value = property(CSS::PropertyID::ListStylePosition);
  780. return keyword_to_list_style_position(value->to_keyword());
  781. }
  782. Optional<CSS::Overflow> StyleProperties::overflow_x() const
  783. {
  784. return overflow(CSS::PropertyID::OverflowX);
  785. }
  786. Optional<CSS::Overflow> StyleProperties::overflow_y() const
  787. {
  788. return overflow(CSS::PropertyID::OverflowY);
  789. }
  790. Optional<CSS::Overflow> StyleProperties::overflow(CSS::PropertyID property_id) const
  791. {
  792. auto value = property(property_id);
  793. return keyword_to_overflow(value->to_keyword());
  794. }
  795. Vector<ShadowData> StyleProperties::shadow(PropertyID property_id, Layout::Node const& layout_node) const
  796. {
  797. auto value = property(property_id);
  798. auto resolve_to_length = [&layout_node](NonnullRefPtr<CSSStyleValue const> const& value) -> Optional<Length> {
  799. if (value->is_length())
  800. return value->as_length().length();
  801. if (value->is_math())
  802. return value->as_math().resolve_length(layout_node);
  803. return {};
  804. };
  805. auto make_shadow_data = [resolve_to_length, &layout_node](ShadowStyleValue const& value) -> Optional<ShadowData> {
  806. auto maybe_offset_x = resolve_to_length(value.offset_x());
  807. if (!maybe_offset_x.has_value())
  808. return {};
  809. auto maybe_offset_y = resolve_to_length(value.offset_y());
  810. if (!maybe_offset_y.has_value())
  811. return {};
  812. auto maybe_blur_radius = resolve_to_length(value.blur_radius());
  813. if (!maybe_blur_radius.has_value())
  814. return {};
  815. auto maybe_spread_distance = resolve_to_length(value.spread_distance());
  816. if (!maybe_spread_distance.has_value())
  817. return {};
  818. return ShadowData {
  819. value.color()->to_color(verify_cast<Layout::NodeWithStyle>(layout_node)),
  820. maybe_offset_x.release_value(),
  821. maybe_offset_y.release_value(),
  822. maybe_blur_radius.release_value(),
  823. maybe_spread_distance.release_value(),
  824. value.placement()
  825. };
  826. };
  827. if (value->is_value_list()) {
  828. auto const& value_list = value->as_value_list();
  829. Vector<ShadowData> shadow_data;
  830. shadow_data.ensure_capacity(value_list.size());
  831. for (auto const& layer_value : value_list.values()) {
  832. auto maybe_shadow_data = make_shadow_data(layer_value->as_shadow());
  833. if (!maybe_shadow_data.has_value())
  834. return {};
  835. shadow_data.append(maybe_shadow_data.release_value());
  836. }
  837. return shadow_data;
  838. }
  839. if (value->is_shadow()) {
  840. auto maybe_shadow_data = make_shadow_data(value->as_shadow());
  841. if (!maybe_shadow_data.has_value())
  842. return {};
  843. return { maybe_shadow_data.release_value() };
  844. }
  845. return {};
  846. }
  847. Vector<ShadowData> StyleProperties::box_shadow(Layout::Node const& layout_node) const
  848. {
  849. return shadow(PropertyID::BoxShadow, layout_node);
  850. }
  851. Vector<ShadowData> StyleProperties::text_shadow(Layout::Node const& layout_node) const
  852. {
  853. return shadow(PropertyID::TextShadow, layout_node);
  854. }
  855. Optional<CSS::BoxSizing> StyleProperties::box_sizing() const
  856. {
  857. auto value = property(CSS::PropertyID::BoxSizing);
  858. return keyword_to_box_sizing(value->to_keyword());
  859. }
  860. Variant<CSS::VerticalAlign, CSS::LengthPercentage> StyleProperties::vertical_align() const
  861. {
  862. auto value = property(CSS::PropertyID::VerticalAlign);
  863. if (value->is_keyword())
  864. return keyword_to_vertical_align(value->to_keyword()).release_value();
  865. if (value->is_length())
  866. return CSS::LengthPercentage(value->as_length().length());
  867. if (value->is_percentage())
  868. return CSS::LengthPercentage(value->as_percentage().percentage());
  869. if (value->is_math())
  870. return LengthPercentage { const_cast<CSSMathValue&>(value->as_math()) };
  871. VERIFY_NOT_REACHED();
  872. }
  873. Optional<CSS::FontVariant> StyleProperties::font_variant() const
  874. {
  875. auto value = property(CSS::PropertyID::FontVariant);
  876. return keyword_to_font_variant(value->to_keyword());
  877. }
  878. Optional<FlyString> StyleProperties::font_language_override() const
  879. {
  880. auto value = property(CSS::PropertyID::FontLanguageOverride);
  881. if (value->is_string())
  882. return value->as_string().string_value();
  883. return {};
  884. }
  885. CSS::GridTrackSizeList StyleProperties::grid_auto_columns() const
  886. {
  887. auto value = property(CSS::PropertyID::GridAutoColumns);
  888. return value->as_grid_track_size_list().grid_track_size_list();
  889. }
  890. CSS::GridTrackSizeList StyleProperties::grid_auto_rows() const
  891. {
  892. auto value = property(CSS::PropertyID::GridAutoRows);
  893. return value->as_grid_track_size_list().grid_track_size_list();
  894. }
  895. CSS::GridTrackSizeList StyleProperties::grid_template_columns() const
  896. {
  897. auto value = property(CSS::PropertyID::GridTemplateColumns);
  898. return value->as_grid_track_size_list().grid_track_size_list();
  899. }
  900. CSS::GridTrackSizeList StyleProperties::grid_template_rows() const
  901. {
  902. auto value = property(CSS::PropertyID::GridTemplateRows);
  903. return value->as_grid_track_size_list().grid_track_size_list();
  904. }
  905. CSS::GridAutoFlow StyleProperties::grid_auto_flow() const
  906. {
  907. auto value = property(CSS::PropertyID::GridAutoFlow);
  908. if (!value->is_grid_auto_flow())
  909. return CSS::GridAutoFlow {};
  910. auto& grid_auto_flow_value = value->as_grid_auto_flow();
  911. return CSS::GridAutoFlow { .row = grid_auto_flow_value.is_row(), .dense = grid_auto_flow_value.is_dense() };
  912. }
  913. CSS::GridTrackPlacement StyleProperties::grid_column_end() const
  914. {
  915. auto value = property(CSS::PropertyID::GridColumnEnd);
  916. return value->as_grid_track_placement().grid_track_placement();
  917. }
  918. CSS::GridTrackPlacement StyleProperties::grid_column_start() const
  919. {
  920. auto value = property(CSS::PropertyID::GridColumnStart);
  921. return value->as_grid_track_placement().grid_track_placement();
  922. }
  923. CSS::GridTrackPlacement StyleProperties::grid_row_end() const
  924. {
  925. auto value = property(CSS::PropertyID::GridRowEnd);
  926. return value->as_grid_track_placement().grid_track_placement();
  927. }
  928. CSS::GridTrackPlacement StyleProperties::grid_row_start() const
  929. {
  930. auto value = property(CSS::PropertyID::GridRowStart);
  931. return value->as_grid_track_placement().grid_track_placement();
  932. }
  933. Optional<CSS::BorderCollapse> StyleProperties::border_collapse() const
  934. {
  935. auto value = property(CSS::PropertyID::BorderCollapse);
  936. return keyword_to_border_collapse(value->to_keyword());
  937. }
  938. Vector<Vector<String>> StyleProperties::grid_template_areas() const
  939. {
  940. auto value = property(CSS::PropertyID::GridTemplateAreas);
  941. return value->as_grid_template_area().grid_template_area();
  942. }
  943. Optional<CSS::ObjectFit> StyleProperties::object_fit() const
  944. {
  945. auto value = property(CSS::PropertyID::ObjectFit);
  946. return keyword_to_object_fit(value->to_keyword());
  947. }
  948. CSS::ObjectPosition StyleProperties::object_position() const
  949. {
  950. auto value = property(CSS::PropertyID::ObjectPosition);
  951. auto const& position = value->as_position();
  952. CSS::ObjectPosition object_position;
  953. auto const& edge_x = position.edge_x();
  954. auto const& edge_y = position.edge_y();
  955. if (edge_x->is_edge()) {
  956. auto const& edge = edge_x->as_edge();
  957. object_position.edge_x = edge.edge();
  958. object_position.offset_x = edge.offset();
  959. }
  960. if (edge_y->is_edge()) {
  961. auto const& edge = edge_y->as_edge();
  962. object_position.edge_y = edge.edge();
  963. object_position.offset_y = edge.offset();
  964. }
  965. return object_position;
  966. }
  967. Optional<CSS::TableLayout> StyleProperties::table_layout() const
  968. {
  969. auto value = property(CSS::PropertyID::TableLayout);
  970. return keyword_to_table_layout(value->to_keyword());
  971. }
  972. Optional<CSS::Direction> StyleProperties::direction() const
  973. {
  974. auto value = property(CSS::PropertyID::Direction);
  975. return keyword_to_direction(value->to_keyword());
  976. }
  977. Optional<CSS::MaskType> StyleProperties::mask_type() const
  978. {
  979. auto value = property(CSS::PropertyID::MaskType);
  980. return keyword_to_mask_type(value->to_keyword());
  981. }
  982. Color StyleProperties::stop_color() const
  983. {
  984. auto value = property(CSS::PropertyID::StopColor);
  985. if (value->is_keyword()) {
  986. // Workaround lack of layout node to resolve current color.
  987. auto& keyword = value->as_keyword();
  988. if (keyword.keyword() == CSS::Keyword::Currentcolor)
  989. value = property(CSS::PropertyID::Color);
  990. }
  991. if (value->has_color()) {
  992. // FIXME: This is used by the SVGStopElement, which does not participate in layout,
  993. // so can't pass a layout node (so can't resolve some colors, e.g. palette ones)
  994. return value->to_color({});
  995. }
  996. return Color::Black;
  997. }
  998. void StyleProperties::set_math_depth(int math_depth)
  999. {
  1000. m_data->m_math_depth = math_depth;
  1001. // Make our children inherit our computed value, not our specified value.
  1002. set_property(PropertyID::MathDepth, MathDepthStyleValue::create_integer(IntegerStyleValue::create(math_depth)));
  1003. }
  1004. QuotesData StyleProperties::quotes() const
  1005. {
  1006. auto value = property(CSS::PropertyID::Quotes);
  1007. if (value->is_keyword()) {
  1008. switch (value->to_keyword()) {
  1009. case Keyword::Auto:
  1010. return QuotesData { .type = QuotesData::Type::Auto };
  1011. case Keyword::None:
  1012. return QuotesData { .type = QuotesData::Type::None };
  1013. default:
  1014. break;
  1015. }
  1016. }
  1017. if (value->is_value_list()) {
  1018. auto& value_list = value->as_value_list();
  1019. QuotesData quotes_data { .type = QuotesData::Type::Specified };
  1020. VERIFY(value_list.size() % 2 == 0);
  1021. for (auto i = 0u; i < value_list.size(); i += 2) {
  1022. quotes_data.strings.empend(
  1023. value_list.value_at(i, false)->as_string().string_value(),
  1024. value_list.value_at(i + 1, false)->as_string().string_value());
  1025. }
  1026. return quotes_data;
  1027. }
  1028. return InitialValues::quotes();
  1029. }
  1030. Vector<CounterData> StyleProperties::counter_data(PropertyID property_id) const
  1031. {
  1032. auto value = property(property_id);
  1033. if (value->is_counter_definitions()) {
  1034. auto& counter_definitions = value->as_counter_definitions().counter_definitions();
  1035. Vector<CounterData> result;
  1036. for (auto& counter : counter_definitions) {
  1037. CounterData data {
  1038. .name = counter.name,
  1039. .is_reversed = counter.is_reversed,
  1040. .value = {},
  1041. };
  1042. if (counter.value) {
  1043. if (counter.value->is_integer()) {
  1044. data.value = AK::clamp_to<i32>(counter.value->as_integer().integer());
  1045. } else if (counter.value->is_math()) {
  1046. auto maybe_int = counter.value->as_math().resolve_integer();
  1047. if (maybe_int.has_value())
  1048. data.value = AK::clamp_to<i32>(*maybe_int);
  1049. } else {
  1050. dbgln("Unimplemented type for {} integer value: '{}'", string_from_property_id(property_id), counter.value->to_string());
  1051. }
  1052. }
  1053. result.append(move(data));
  1054. }
  1055. return result;
  1056. }
  1057. if (value->to_keyword() == Keyword::None)
  1058. return {};
  1059. dbgln("Unhandled type for {} value: '{}'", string_from_property_id(property_id), value->to_string());
  1060. return {};
  1061. }
  1062. Optional<CSS::ScrollbarWidth> StyleProperties::scrollbar_width() const
  1063. {
  1064. auto value = property(CSS::PropertyID::ScrollbarWidth);
  1065. return keyword_to_scrollbar_width(value->to_keyword());
  1066. }
  1067. }