StyleProperties.cpp 47 KB

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