StyleProperties.cpp 51 KB

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