StyleProperties.cpp 51 KB

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