StyleProperties.cpp 52 KB

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