StyleProperties.cpp 47 KB

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