StyleProperties.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/TypeCasts.h>
  8. #include <LibCore/DirIterator.h>
  9. #include <LibWeb/CSS/Clip.h>
  10. #include <LibWeb/CSS/StyleProperties.h>
  11. #include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
  12. #include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
  13. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/GridAutoFlowStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/MathDepthStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
  28. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  29. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  30. #include <LibWeb/Layout/BlockContainer.h>
  31. #include <LibWeb/Layout/Node.h>
  32. #include <LibWeb/Platform/FontPlugin.h>
  33. namespace Web::CSS {
  34. bool StyleProperties::is_property_important(CSS::PropertyID property_id) const
  35. {
  36. return m_property_values[to_underlying(property_id)].style && m_property_values[to_underlying(property_id)].important == Important::Yes;
  37. }
  38. bool StyleProperties::is_property_inherited(CSS::PropertyID property_id) const
  39. {
  40. return m_property_values[to_underlying(property_id)].style && m_property_values[to_underlying(property_id)].inherited == Inherited::Yes;
  41. }
  42. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue const> value, CSS::CSSStyleDeclaration const* source_declaration, Inherited inherited, Important important)
  43. {
  44. m_property_values[to_underlying(id)] = StyleAndSourceDeclaration { move(value), source_declaration, important, inherited };
  45. }
  46. void StyleProperties::set_animated_property(CSS::PropertyID id, NonnullRefPtr<StyleValue const> value)
  47. {
  48. m_animated_property_values.set(id, move(value));
  49. }
  50. void StyleProperties::reset_animated_properties()
  51. {
  52. m_animated_property_values.clear();
  53. }
  54. NonnullRefPtr<StyleValue const> StyleProperties::property(CSS::PropertyID property_id) const
  55. {
  56. if (auto animated_value = m_animated_property_values.get(property_id).value_or(nullptr))
  57. return *animated_value;
  58. // By the time we call this method, all properties have values assigned.
  59. return *m_property_values[to_underlying(property_id)].style;
  60. }
  61. RefPtr<StyleValue const> StyleProperties::maybe_null_property(CSS::PropertyID property_id) const
  62. {
  63. if (auto animated_value = m_animated_property_values.get(property_id).value_or(nullptr))
  64. return *animated_value;
  65. return m_property_values[to_underlying(property_id)].style;
  66. }
  67. CSS::CSSStyleDeclaration const* StyleProperties::property_source_declaration(CSS::PropertyID property_id) const
  68. {
  69. return m_property_values[to_underlying(property_id)].declaration;
  70. }
  71. CSS::Size StyleProperties::size_value(CSS::PropertyID id) const
  72. {
  73. auto value = property(id);
  74. if (value->is_identifier()) {
  75. switch (value->to_identifier()) {
  76. case ValueID::Auto:
  77. return CSS::Size::make_auto();
  78. case ValueID::MinContent:
  79. return CSS::Size::make_min_content();
  80. case ValueID::MaxContent:
  81. return CSS::Size::make_max_content();
  82. case ValueID::FitContent:
  83. return CSS::Size::make_fit_content();
  84. case ValueID::None:
  85. return CSS::Size::make_none();
  86. default:
  87. VERIFY_NOT_REACHED();
  88. }
  89. }
  90. if (value->is_calculated())
  91. return CSS::Size::make_calculated(const_cast<CalculatedStyleValue&>(value->as_calculated()));
  92. if (value->is_percentage())
  93. return CSS::Size::make_percentage(value->as_percentage().percentage());
  94. if (value->is_length()) {
  95. auto length = value->as_length().length();
  96. if (length.is_auto())
  97. return CSS::Size::make_auto();
  98. return CSS::Size::make_length(length);
  99. }
  100. // FIXME: Support `fit-content(<length>)`
  101. dbgln("FIXME: Unsupported size value: `{}`, treating as `auto`", value->to_string());
  102. return CSS::Size::make_auto();
  103. }
  104. LengthPercentage StyleProperties::length_percentage_or_fallback(CSS::PropertyID id, LengthPercentage const& fallback) const
  105. {
  106. return length_percentage(id).value_or(fallback);
  107. }
  108. Optional<LengthPercentage> StyleProperties::length_percentage(CSS::PropertyID id) const
  109. {
  110. auto value = property(id);
  111. if (value->is_calculated())
  112. return LengthPercentage { const_cast<CalculatedStyleValue&>(value->as_calculated()) };
  113. if (value->is_percentage())
  114. return value->as_percentage().percentage();
  115. if (value->is_length())
  116. return value->as_length().length();
  117. if (value->has_auto())
  118. return LengthPercentage { Length::make_auto() };
  119. return {};
  120. }
  121. 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
  122. {
  123. LengthBox box;
  124. box.left() = length_percentage_or_fallback(left_id, default_value);
  125. box.top() = length_percentage_or_fallback(top_id, default_value);
  126. box.right() = length_percentage_or_fallback(right_id, default_value);
  127. box.bottom() = length_percentage_or_fallback(bottom_id, default_value);
  128. return box;
  129. }
  130. Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithStyle const& node, Color fallback) const
  131. {
  132. auto value = property(id);
  133. if (!value->has_color())
  134. return fallback;
  135. return value->to_color(node);
  136. }
  137. NonnullRefPtr<Gfx::Font const> StyleProperties::font_fallback(bool monospace, bool bold)
  138. {
  139. if (monospace && bold)
  140. return Platform::FontPlugin::the().default_fixed_width_font().bold_variant();
  141. if (monospace)
  142. return Platform::FontPlugin::the().default_fixed_width_font();
  143. if (bold)
  144. return Platform::FontPlugin::the().default_font().bold_variant();
  145. return Platform::FontPlugin::the().default_font();
  146. }
  147. // FIXME: This implementation is almost identical to compute_line_height(Layout::Node) below. Maybe they can be combined somehow.
  148. CSSPixels StyleProperties::compute_line_height(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const
  149. {
  150. auto line_height = property(CSS::PropertyID::LineHeight);
  151. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  152. return font_metrics.line_height;
  153. if (line_height->is_length()) {
  154. auto line_height_length = line_height->as_length().length();
  155. if (!line_height_length.is_auto())
  156. return line_height_length.to_px(viewport_rect, font_metrics, root_font_metrics);
  157. }
  158. if (line_height->is_number())
  159. return Length(line_height->as_number().number(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  160. if (line_height->is_percentage()) {
  161. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  162. auto& percentage = line_height->as_percentage().percentage();
  163. return Length(percentage.as_fraction(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  164. }
  165. if (line_height->is_calculated()) {
  166. if (line_height->as_calculated().resolves_to_number()) {
  167. auto resolved = line_height->as_calculated().resolve_number();
  168. if (!resolved.has_value()) {
  169. dbgln("FIXME: Failed to resolve calc() line-height (number): {}", line_height->as_calculated().to_string());
  170. return CSSPixels::nearest_value_for(m_font_list->first().pixel_metrics().line_spacing());
  171. }
  172. return Length(resolved.value(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
  173. }
  174. auto resolved = line_height->as_calculated().resolve_length(Length::ResolutionContext { viewport_rect, font_metrics, root_font_metrics });
  175. if (!resolved.has_value()) {
  176. dbgln("FIXME: Failed to resolve calc() line-height: {}", line_height->as_calculated().to_string());
  177. return CSSPixels::nearest_value_for(m_font_list->first().pixel_metrics().line_spacing());
  178. }
  179. return resolved->to_px(viewport_rect, font_metrics, root_font_metrics);
  180. }
  181. return font_metrics.line_height;
  182. }
  183. CSSPixels StyleProperties::compute_line_height(Layout::Node const& layout_node) const
  184. {
  185. auto line_height = property(CSS::PropertyID::LineHeight);
  186. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  187. return CSSPixels::nearest_value_for(layout_node.first_available_font().pixel_metrics().line_spacing());
  188. if (line_height->is_length()) {
  189. auto line_height_length = line_height->as_length().length();
  190. if (!line_height_length.is_auto())
  191. return line_height_length.to_px(layout_node);
  192. }
  193. if (line_height->is_number())
  194. return Length(line_height->as_number().number(), Length::Type::Em).to_px(layout_node);
  195. if (line_height->is_percentage()) {
  196. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  197. auto& percentage = line_height->as_percentage().percentage();
  198. return Length(percentage.as_fraction(), Length::Type::Em).to_px(layout_node);
  199. }
  200. if (line_height->is_calculated()) {
  201. if (line_height->as_calculated().resolves_to_number()) {
  202. auto resolved = line_height->as_calculated().resolve_number();
  203. if (!resolved.has_value()) {
  204. dbgln("FIXME: Failed to resolve calc() line-height (number): {}", line_height->as_calculated().to_string());
  205. return CSSPixels::nearest_value_for(layout_node.first_available_font().pixel_metrics().line_spacing());
  206. }
  207. return Length(resolved.value(), Length::Type::Em).to_px(layout_node);
  208. }
  209. auto resolved = line_height->as_calculated().resolve_length(layout_node);
  210. if (!resolved.has_value()) {
  211. dbgln("FIXME: Failed to resolve calc() line-height: {}", line_height->as_calculated().to_string());
  212. return CSSPixels::nearest_value_for(layout_node.first_available_font().pixel_metrics().line_spacing());
  213. }
  214. return resolved->to_px(layout_node);
  215. }
  216. return CSSPixels::nearest_value_for(layout_node.first_available_font().pixel_metrics().line_spacing());
  217. }
  218. Optional<int> StyleProperties::z_index() const
  219. {
  220. auto value = property(CSS::PropertyID::ZIndex);
  221. if (value->has_auto())
  222. return {};
  223. if (value->is_integer()) {
  224. // Clamp z-index to the range of a signed 32-bit integer for consistency with other engines.
  225. auto integer = value->as_integer().integer();
  226. if (integer >= NumericLimits<int>::max())
  227. return NumericLimits<int>::max();
  228. if (integer <= NumericLimits<int>::min())
  229. return NumericLimits<int>::min();
  230. return static_cast<int>(integer);
  231. }
  232. return {};
  233. }
  234. float StyleProperties::resolve_opacity_value(CSS::StyleValue const& value)
  235. {
  236. float unclamped_opacity = 1.0f;
  237. if (value.is_number()) {
  238. unclamped_opacity = value.as_number().number();
  239. } else if (value.is_calculated()) {
  240. auto& calculated = value.as_calculated();
  241. if (calculated.resolves_to_percentage()) {
  242. auto maybe_percentage = value.as_calculated().resolve_percentage();
  243. if (maybe_percentage.has_value())
  244. unclamped_opacity = maybe_percentage->as_fraction();
  245. else
  246. dbgln("Unable to resolve calc() as opacity (percentage): {}", value.to_string());
  247. } else if (calculated.resolves_to_number()) {
  248. auto maybe_number = const_cast<CalculatedStyleValue&>(value.as_calculated()).resolve_number();
  249. if (maybe_number.has_value())
  250. unclamped_opacity = maybe_number.value();
  251. else
  252. dbgln("Unable to resolve calc() as opacity (number): {}", value.to_string());
  253. }
  254. } else if (value.is_percentage()) {
  255. unclamped_opacity = value.as_percentage().percentage().as_fraction();
  256. }
  257. return clamp(unclamped_opacity, 0.0f, 1.0f);
  258. }
  259. float StyleProperties::opacity() const
  260. {
  261. auto value = property(CSS::PropertyID::Opacity);
  262. return resolve_opacity_value(*value);
  263. }
  264. float StyleProperties::fill_opacity() const
  265. {
  266. auto value = property(CSS::PropertyID::FillOpacity);
  267. return resolve_opacity_value(*value);
  268. }
  269. float StyleProperties::stroke_opacity() const
  270. {
  271. auto value = property(CSS::PropertyID::StrokeOpacity);
  272. return resolve_opacity_value(*value);
  273. }
  274. float StyleProperties::stop_opacity() const
  275. {
  276. auto value = property(CSS::PropertyID::StopOpacity);
  277. return resolve_opacity_value(*value);
  278. }
  279. Optional<CSS::FillRule> StyleProperties::fill_rule() const
  280. {
  281. auto value = property(CSS::PropertyID::FillRule);
  282. return value_id_to_fill_rule(value->to_identifier());
  283. }
  284. Optional<CSS::ClipRule> StyleProperties::clip_rule() const
  285. {
  286. auto value = property(CSS::PropertyID::ClipRule);
  287. return value_id_to_fill_rule(value->to_identifier());
  288. }
  289. Optional<CSS::FlexDirection> StyleProperties::flex_direction() const
  290. {
  291. auto value = property(CSS::PropertyID::FlexDirection);
  292. return value_id_to_flex_direction(value->to_identifier());
  293. }
  294. Optional<CSS::FlexWrap> StyleProperties::flex_wrap() const
  295. {
  296. auto value = property(CSS::PropertyID::FlexWrap);
  297. return value_id_to_flex_wrap(value->to_identifier());
  298. }
  299. Optional<CSS::FlexBasis> StyleProperties::flex_basis() const
  300. {
  301. auto value = property(CSS::PropertyID::FlexBasis);
  302. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::Content)
  303. return CSS::FlexBasisContent {};
  304. return size_value(CSS::PropertyID::FlexBasis);
  305. }
  306. float StyleProperties::flex_grow() const
  307. {
  308. auto value = property(CSS::PropertyID::FlexGrow);
  309. if (!value->is_number())
  310. return 0;
  311. return value->as_number().number();
  312. }
  313. float StyleProperties::flex_shrink() const
  314. {
  315. auto value = property(CSS::PropertyID::FlexShrink);
  316. if (!value->is_number())
  317. return 1;
  318. return value->as_number().number();
  319. }
  320. int StyleProperties::order() const
  321. {
  322. auto value = property(CSS::PropertyID::Order);
  323. if (!value->is_integer())
  324. return 0;
  325. return value->as_integer().integer();
  326. }
  327. Optional<CSS::ImageRendering> StyleProperties::image_rendering() const
  328. {
  329. auto value = property(CSS::PropertyID::ImageRendering);
  330. return value_id_to_image_rendering(value->to_identifier());
  331. }
  332. CSS::Length StyleProperties::border_spacing_horizontal() const
  333. {
  334. auto value = property(CSS::PropertyID::BorderSpacing);
  335. if (value->is_length())
  336. return value->as_length().length();
  337. auto const& list = value->as_value_list();
  338. return list.value_at(0, false)->as_length().length();
  339. }
  340. CSS::Length StyleProperties::border_spacing_vertical() const
  341. {
  342. auto value = property(CSS::PropertyID::BorderSpacing);
  343. if (value->is_length())
  344. return value->as_length().length();
  345. auto const& list = value->as_value_list();
  346. return list.value_at(1, false)->as_length().length();
  347. }
  348. Optional<CSS::CaptionSide> StyleProperties::caption_side() const
  349. {
  350. auto value = property(CSS::PropertyID::CaptionSide);
  351. return value_id_to_caption_side(value->to_identifier());
  352. }
  353. CSS::Clip StyleProperties::clip() const
  354. {
  355. auto value = property(CSS::PropertyID::Clip);
  356. if (!value->is_rect())
  357. return CSS::Clip::make_auto();
  358. return CSS::Clip(value->as_rect().rect());
  359. }
  360. Optional<CSS::JustifyContent> StyleProperties::justify_content() const
  361. {
  362. auto value = property(CSS::PropertyID::JustifyContent);
  363. return value_id_to_justify_content(value->to_identifier());
  364. }
  365. Optional<CSS::JustifyItems> StyleProperties::justify_items() const
  366. {
  367. auto value = property(CSS::PropertyID::JustifyItems);
  368. return value_id_to_justify_items(value->to_identifier());
  369. }
  370. Optional<CSS::JustifySelf> StyleProperties::justify_self() const
  371. {
  372. auto value = property(CSS::PropertyID::JustifySelf);
  373. return value_id_to_justify_self(value->to_identifier());
  374. }
  375. Vector<CSS::Transformation> StyleProperties::transformations_for_style_value(StyleValue const& value)
  376. {
  377. if (value.is_identifier() && value.to_identifier() == CSS::ValueID::None)
  378. return {};
  379. if (!value.is_value_list())
  380. return {};
  381. auto& list = value.as_value_list();
  382. Vector<CSS::Transformation> transformations;
  383. for (auto& it : list.values()) {
  384. if (!it->is_transformation())
  385. return {};
  386. auto& transformation_style_value = it->as_transformation();
  387. auto function = transformation_style_value.transform_function();
  388. auto function_metadata = transform_function_metadata(function);
  389. Vector<TransformValue> values;
  390. size_t argument_index = 0;
  391. for (auto& transformation_value : transformation_style_value.values()) {
  392. if (transformation_value->is_calculated()) {
  393. auto& calculated = transformation_value->as_calculated();
  394. if (calculated.resolves_to_length_percentage()) {
  395. values.append(CSS::LengthPercentage { calculated });
  396. } else if (calculated.resolves_to_percentage()) {
  397. // FIXME: Maybe transform this for loop to always check the metadata for the correct types
  398. if (function_metadata.parameters[argument_index].type == TransformFunctionParameterType::NumberPercentage) {
  399. values.append(NumberPercentage { calculated.resolve_percentage().value() });
  400. } else {
  401. values.append(LengthPercentage { calculated.resolve_percentage().value() });
  402. }
  403. } else if (calculated.resolves_to_number()) {
  404. values.append({ Number(Number::Type::Number, calculated.resolve_number().value()) });
  405. } else if (calculated.resolves_to_angle()) {
  406. values.append({ calculated.resolve_angle().value() });
  407. } else {
  408. dbgln("FIXME: Unsupported calc value in transform! {}", calculated.to_string());
  409. }
  410. } else if (transformation_value->is_length()) {
  411. values.append({ transformation_value->as_length().length() });
  412. } else if (transformation_value->is_percentage()) {
  413. if (function_metadata.parameters[argument_index].type == TransformFunctionParameterType::NumberPercentage) {
  414. values.append(NumberPercentage { transformation_value->as_percentage().percentage() });
  415. } else {
  416. values.append(LengthPercentage { transformation_value->as_percentage().percentage() });
  417. }
  418. } else if (transformation_value->is_number()) {
  419. values.append({ Number(Number::Type::Number, transformation_value->as_number().number()) });
  420. } else if (transformation_value->is_angle()) {
  421. values.append({ transformation_value->as_angle().angle() });
  422. } else {
  423. dbgln("FIXME: Unsupported value in transform! {}", transformation_value->to_string());
  424. }
  425. argument_index++;
  426. }
  427. transformations.empend(function, move(values));
  428. }
  429. return transformations;
  430. }
  431. Vector<CSS::Transformation> StyleProperties::transformations() const
  432. {
  433. return transformations_for_style_value(property(CSS::PropertyID::Transform));
  434. }
  435. static Optional<LengthPercentage> length_percentage_for_style_value(StyleValue const& value)
  436. {
  437. if (value.is_length())
  438. return value.as_length().length();
  439. if (value.is_percentage())
  440. return value.as_percentage().percentage();
  441. return {};
  442. }
  443. Optional<CSS::TransformBox> StyleProperties::transform_box() const
  444. {
  445. auto value = property(CSS::PropertyID::TransformBox);
  446. return value_id_to_transform_box(value->to_identifier());
  447. }
  448. CSS::TransformOrigin StyleProperties::transform_origin() const
  449. {
  450. auto value = property(CSS::PropertyID::TransformOrigin);
  451. if (!value->is_value_list() || value->as_value_list().size() != 2)
  452. return {};
  453. auto const& list = value->as_value_list();
  454. auto x_value = length_percentage_for_style_value(list.values()[0]);
  455. auto y_value = length_percentage_for_style_value(list.values()[1]);
  456. if (!x_value.has_value() || !y_value.has_value()) {
  457. return {};
  458. }
  459. return { x_value.value(), y_value.value() };
  460. }
  461. Optional<Color> StyleProperties::accent_color(Layout::NodeWithStyle const& node) const
  462. {
  463. auto value = property(CSS::PropertyID::AccentColor);
  464. if (value->has_color())
  465. return value->to_color(node);
  466. return {};
  467. }
  468. Optional<CSS::AlignContent> StyleProperties::align_content() const
  469. {
  470. auto value = property(CSS::PropertyID::AlignContent);
  471. return value_id_to_align_content(value->to_identifier());
  472. }
  473. Optional<CSS::AlignItems> StyleProperties::align_items() const
  474. {
  475. auto value = property(CSS::PropertyID::AlignItems);
  476. return value_id_to_align_items(value->to_identifier());
  477. }
  478. Optional<CSS::AlignSelf> StyleProperties::align_self() const
  479. {
  480. auto value = property(CSS::PropertyID::AlignSelf);
  481. return value_id_to_align_self(value->to_identifier());
  482. }
  483. Optional<CSS::Appearance> StyleProperties::appearance() const
  484. {
  485. auto value = property(CSS::PropertyID::Appearance);
  486. auto appearance = value_id_to_appearance(value->to_identifier());
  487. if (appearance.has_value()) {
  488. switch (*appearance) {
  489. // Note: All these compatibility values can be treated as 'auto'
  490. case CSS::Appearance::Textfield:
  491. case CSS::Appearance::MenulistButton:
  492. case CSS::Appearance::Searchfield:
  493. case CSS::Appearance::Textarea:
  494. case CSS::Appearance::PushButton:
  495. case CSS::Appearance::SliderHorizontal:
  496. case CSS::Appearance::Checkbox:
  497. case CSS::Appearance::Radio:
  498. case CSS::Appearance::SquareButton:
  499. case CSS::Appearance::Menulist:
  500. case CSS::Appearance::Listbox:
  501. case CSS::Appearance::Meter:
  502. case CSS::Appearance::ProgressBar:
  503. case CSS::Appearance::Button:
  504. appearance = CSS::Appearance::Auto;
  505. break;
  506. default:
  507. break;
  508. }
  509. }
  510. return appearance;
  511. }
  512. CSS::BackdropFilter StyleProperties::backdrop_filter() const
  513. {
  514. auto value = property(CSS::PropertyID::BackdropFilter);
  515. if (value->is_filter_value_list())
  516. return BackdropFilter(value->as_filter_value_list());
  517. return BackdropFilter::make_none();
  518. }
  519. Optional<CSS::Positioning> StyleProperties::position() const
  520. {
  521. auto value = property(CSS::PropertyID::Position);
  522. return value_id_to_positioning(value->to_identifier());
  523. }
  524. bool StyleProperties::operator==(StyleProperties const& other) const
  525. {
  526. if (m_property_values.size() != other.m_property_values.size())
  527. return false;
  528. for (size_t i = 0; i < m_property_values.size(); ++i) {
  529. auto const& my_style = m_property_values[i];
  530. auto const& other_style = other.m_property_values[i];
  531. if (!my_style.style) {
  532. if (other_style.style)
  533. return false;
  534. continue;
  535. }
  536. if (!other_style.style)
  537. return false;
  538. auto const& my_value = *my_style.style;
  539. auto const& other_value = *other_style.style;
  540. if (my_value.type() != other_value.type())
  541. return false;
  542. if (my_value != other_value)
  543. return false;
  544. }
  545. return true;
  546. }
  547. Optional<CSS::TextAnchor> StyleProperties::text_anchor() const
  548. {
  549. auto value = property(CSS::PropertyID::TextAnchor);
  550. return value_id_to_text_anchor(value->to_identifier());
  551. }
  552. Optional<CSS::TextAlign> StyleProperties::text_align() const
  553. {
  554. auto value = property(CSS::PropertyID::TextAlign);
  555. return value_id_to_text_align(value->to_identifier());
  556. }
  557. Optional<CSS::TextJustify> StyleProperties::text_justify() const
  558. {
  559. auto value = property(CSS::PropertyID::TextJustify);
  560. return value_id_to_text_justify(value->to_identifier());
  561. }
  562. Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
  563. {
  564. auto value = property(CSS::PropertyID::PointerEvents);
  565. return value_id_to_pointer_events(value->to_identifier());
  566. }
  567. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  568. {
  569. auto value = property(CSS::PropertyID::WhiteSpace);
  570. return value_id_to_white_space(value->to_identifier());
  571. }
  572. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  573. {
  574. auto value = property(property_id);
  575. return value_id_to_line_style(value->to_identifier());
  576. }
  577. Optional<CSS::OutlineStyle> StyleProperties::outline_style() const
  578. {
  579. auto value = property(CSS::PropertyID::OutlineStyle);
  580. return value_id_to_outline_style(value->to_identifier());
  581. }
  582. Optional<CSS::Float> StyleProperties::float_() const
  583. {
  584. auto value = property(CSS::PropertyID::Float);
  585. return value_id_to_float(value->to_identifier());
  586. }
  587. Optional<CSS::Clear> StyleProperties::clear() const
  588. {
  589. auto value = property(CSS::PropertyID::Clear);
  590. return value_id_to_clear(value->to_identifier());
  591. }
  592. StyleProperties::ContentDataAndQuoteNestingLevel StyleProperties::content(u32 initial_quote_nesting_level) const
  593. {
  594. auto value = property(CSS::PropertyID::Content);
  595. auto quotes_data = quotes();
  596. auto quote_nesting_level = initial_quote_nesting_level;
  597. auto get_quote_string = [&](bool open, auto depth) {
  598. switch (quotes_data.type) {
  599. case QuotesData::Type::None:
  600. return String {};
  601. case QuotesData::Type::Auto:
  602. // FIXME: "A typographically appropriate used value for quotes is automatically chosen by the UA
  603. // based on the content language of the element and/or its parent."
  604. if (open)
  605. return depth == 0 ? "“"_string : "‘"_string;
  606. return depth == 0 ? "”"_string : "’"_string;
  607. case QuotesData::Type::Specified:
  608. // If the depth is greater than the number of pairs, the last pair is repeated.
  609. auto& level = quotes_data.strings[min(depth, quotes_data.strings.size() - 1)];
  610. return open ? level[0] : level[1];
  611. }
  612. VERIFY_NOT_REACHED();
  613. };
  614. if (value->is_content()) {
  615. auto& content_style_value = value->as_content();
  616. CSS::ContentData content_data;
  617. // FIXME: The content is a list of things: strings, identifiers or functions that return strings, and images.
  618. // So it can't always be represented as a single String, but may have to be multiple boxes.
  619. // For now, we'll just assume strings since that is easiest.
  620. StringBuilder builder;
  621. for (auto const& item : content_style_value.content().values()) {
  622. if (item->is_string()) {
  623. builder.append(item->as_string().string_value());
  624. } else if (item->is_identifier()) {
  625. switch (item->to_identifier()) {
  626. case ValueID::OpenQuote:
  627. builder.append(get_quote_string(true, quote_nesting_level++));
  628. break;
  629. case ValueID::CloseQuote:
  630. // A 'close-quote' or 'no-close-quote' that would make the depth negative is in error and is ignored
  631. // (at rendering time): the depth stays at 0 and no quote mark is rendered (although the rest of the
  632. // 'content' property's value is still inserted).
  633. // - https://www.w3.org/TR/CSS21/generate.html#quotes-insert
  634. // (This is missing from the CONTENT-3 spec.)
  635. if (quote_nesting_level > 0)
  636. builder.append(get_quote_string(false, --quote_nesting_level));
  637. break;
  638. case ValueID::NoOpenQuote:
  639. quote_nesting_level++;
  640. break;
  641. case ValueID::NoCloseQuote:
  642. // NOTE: See CloseQuote
  643. if (quote_nesting_level > 0)
  644. quote_nesting_level--;
  645. break;
  646. default:
  647. dbgln("`{}` is not supported in `content` (yet?)", item->to_string());
  648. break;
  649. }
  650. } else {
  651. // TODO: Implement counters, images, and other things.
  652. dbgln("`{}` is not supported in `content` (yet?)", item->to_string());
  653. }
  654. }
  655. content_data.type = ContentData::Type::String;
  656. content_data.data = MUST(builder.to_string());
  657. if (content_style_value.has_alt_text()) {
  658. StringBuilder alt_text_builder;
  659. for (auto const& item : content_style_value.alt_text()->values()) {
  660. if (item->is_string()) {
  661. alt_text_builder.append(item->as_string().string_value());
  662. } else {
  663. // TODO: Implement counters
  664. }
  665. }
  666. content_data.alt_text = MUST(alt_text_builder.to_string());
  667. }
  668. return { content_data, quote_nesting_level };
  669. }
  670. switch (value->to_identifier()) {
  671. case ValueID::None:
  672. return { { ContentData::Type::None }, quote_nesting_level };
  673. case ValueID::Normal:
  674. return { { ContentData::Type::Normal }, quote_nesting_level };
  675. default:
  676. break;
  677. }
  678. return { {}, quote_nesting_level };
  679. }
  680. Optional<CSS::Cursor> StyleProperties::cursor() const
  681. {
  682. auto value = property(CSS::PropertyID::Cursor);
  683. return value_id_to_cursor(value->to_identifier());
  684. }
  685. Optional<CSS::Visibility> StyleProperties::visibility() const
  686. {
  687. auto value = property(CSS::PropertyID::Visibility);
  688. if (!value->is_identifier())
  689. return {};
  690. return value_id_to_visibility(value->to_identifier());
  691. }
  692. Display StyleProperties::display() const
  693. {
  694. auto value = property(PropertyID::Display);
  695. if (value->is_display()) {
  696. return value->as_display().display();
  697. }
  698. return Display::from_short(Display::Short::Inline);
  699. }
  700. Vector<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  701. {
  702. auto value = property(CSS::PropertyID::TextDecorationLine);
  703. if (value->is_value_list()) {
  704. Vector<CSS::TextDecorationLine> lines;
  705. auto& values = value->as_value_list().values();
  706. for (auto const& item : values) {
  707. lines.append(value_id_to_text_decoration_line(item->to_identifier()).value());
  708. }
  709. return lines;
  710. }
  711. if (value->is_identifier() && value->to_identifier() == ValueID::None)
  712. return {};
  713. dbgln("FIXME: Unsupported value for text-decoration-line: {}", value->to_string());
  714. return {};
  715. }
  716. Optional<CSS::TextDecorationStyle> StyleProperties::text_decoration_style() const
  717. {
  718. auto value = property(CSS::PropertyID::TextDecorationStyle);
  719. return value_id_to_text_decoration_style(value->to_identifier());
  720. }
  721. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  722. {
  723. auto value = property(CSS::PropertyID::TextTransform);
  724. return value_id_to_text_transform(value->to_identifier());
  725. }
  726. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  727. {
  728. auto value = property(CSS::PropertyID::ListStyleType);
  729. return value_id_to_list_style_type(value->to_identifier());
  730. }
  731. Optional<CSS::ListStylePosition> StyleProperties::list_style_position() const
  732. {
  733. auto value = property(CSS::PropertyID::ListStylePosition);
  734. return value_id_to_list_style_position(value->to_identifier());
  735. }
  736. Optional<CSS::Overflow> StyleProperties::overflow_x() const
  737. {
  738. return overflow(CSS::PropertyID::OverflowX);
  739. }
  740. Optional<CSS::Overflow> StyleProperties::overflow_y() const
  741. {
  742. return overflow(CSS::PropertyID::OverflowY);
  743. }
  744. Optional<CSS::Overflow> StyleProperties::overflow(CSS::PropertyID property_id) const
  745. {
  746. auto value = property(property_id);
  747. return value_id_to_overflow(value->to_identifier());
  748. }
  749. Vector<ShadowData> StyleProperties::shadow(PropertyID property_id, Layout::Node const& layout_node) const
  750. {
  751. auto value = property(property_id);
  752. auto resolve_to_length = [&layout_node](NonnullRefPtr<StyleValue const> const& value) -> Optional<Length> {
  753. if (value->is_length())
  754. return value->as_length().length();
  755. if (value->is_calculated())
  756. return value->as_calculated().resolve_length(layout_node);
  757. return {};
  758. };
  759. auto make_shadow_data = [resolve_to_length](ShadowStyleValue const& value) -> Optional<ShadowData> {
  760. auto maybe_offset_x = resolve_to_length(value.offset_x());
  761. if (!maybe_offset_x.has_value())
  762. return {};
  763. auto maybe_offset_y = resolve_to_length(value.offset_y());
  764. if (!maybe_offset_y.has_value())
  765. return {};
  766. auto maybe_blur_radius = resolve_to_length(value.blur_radius());
  767. if (!maybe_blur_radius.has_value())
  768. return {};
  769. auto maybe_spread_distance = resolve_to_length(value.spread_distance());
  770. if (!maybe_spread_distance.has_value())
  771. return {};
  772. return ShadowData {
  773. value.color(),
  774. maybe_offset_x.release_value(),
  775. maybe_offset_y.release_value(),
  776. maybe_blur_radius.release_value(),
  777. maybe_spread_distance.release_value(),
  778. value.placement()
  779. };
  780. };
  781. if (value->is_value_list()) {
  782. auto const& value_list = value->as_value_list();
  783. Vector<ShadowData> shadow_data;
  784. shadow_data.ensure_capacity(value_list.size());
  785. for (auto const& layer_value : value_list.values()) {
  786. auto maybe_shadow_data = make_shadow_data(layer_value->as_shadow());
  787. if (!maybe_shadow_data.has_value())
  788. return {};
  789. shadow_data.append(maybe_shadow_data.release_value());
  790. }
  791. return shadow_data;
  792. }
  793. if (value->is_shadow()) {
  794. auto maybe_shadow_data = make_shadow_data(value->as_shadow());
  795. if (!maybe_shadow_data.has_value())
  796. return {};
  797. return { maybe_shadow_data.release_value() };
  798. }
  799. return {};
  800. }
  801. Vector<ShadowData> StyleProperties::box_shadow(Layout::Node const& layout_node) const
  802. {
  803. return shadow(PropertyID::BoxShadow, layout_node);
  804. }
  805. Vector<ShadowData> StyleProperties::text_shadow(Layout::Node const& layout_node) const
  806. {
  807. return shadow(PropertyID::TextShadow, layout_node);
  808. }
  809. Optional<CSS::BoxSizing> StyleProperties::box_sizing() const
  810. {
  811. auto value = property(CSS::PropertyID::BoxSizing);
  812. return value_id_to_box_sizing(value->to_identifier());
  813. }
  814. Variant<CSS::VerticalAlign, CSS::LengthPercentage> StyleProperties::vertical_align() const
  815. {
  816. auto value = property(CSS::PropertyID::VerticalAlign);
  817. if (value->is_identifier())
  818. return value_id_to_vertical_align(value->to_identifier()).release_value();
  819. if (value->is_length())
  820. return CSS::LengthPercentage(value->as_length().length());
  821. if (value->is_percentage())
  822. return CSS::LengthPercentage(value->as_percentage().percentage());
  823. if (value->is_calculated())
  824. return LengthPercentage { const_cast<CalculatedStyleValue&>(value->as_calculated()) };
  825. VERIFY_NOT_REACHED();
  826. }
  827. Optional<CSS::FontVariant> StyleProperties::font_variant() const
  828. {
  829. auto value = property(CSS::PropertyID::FontVariant);
  830. return value_id_to_font_variant(value->to_identifier());
  831. }
  832. CSS::GridTrackSizeList StyleProperties::grid_auto_columns() const
  833. {
  834. auto value = property(CSS::PropertyID::GridAutoColumns);
  835. return value->as_grid_track_size_list().grid_track_size_list();
  836. }
  837. CSS::GridTrackSizeList StyleProperties::grid_auto_rows() const
  838. {
  839. auto value = property(CSS::PropertyID::GridAutoRows);
  840. return value->as_grid_track_size_list().grid_track_size_list();
  841. }
  842. CSS::GridTrackSizeList StyleProperties::grid_template_columns() const
  843. {
  844. auto value = property(CSS::PropertyID::GridTemplateColumns);
  845. return value->as_grid_track_size_list().grid_track_size_list();
  846. }
  847. CSS::GridTrackSizeList StyleProperties::grid_template_rows() const
  848. {
  849. auto value = property(CSS::PropertyID::GridTemplateRows);
  850. return value->as_grid_track_size_list().grid_track_size_list();
  851. }
  852. CSS::GridAutoFlow StyleProperties::grid_auto_flow() const
  853. {
  854. auto value = property(CSS::PropertyID::GridAutoFlow);
  855. if (!value->is_grid_auto_flow())
  856. return CSS::GridAutoFlow {};
  857. auto& grid_auto_flow_value = value->as_grid_auto_flow();
  858. return CSS::GridAutoFlow { .row = grid_auto_flow_value.is_row(), .dense = grid_auto_flow_value.is_dense() };
  859. }
  860. CSS::GridTrackPlacement StyleProperties::grid_column_end() const
  861. {
  862. auto value = property(CSS::PropertyID::GridColumnEnd);
  863. return value->as_grid_track_placement().grid_track_placement();
  864. }
  865. CSS::GridTrackPlacement StyleProperties::grid_column_start() const
  866. {
  867. auto value = property(CSS::PropertyID::GridColumnStart);
  868. return value->as_grid_track_placement().grid_track_placement();
  869. }
  870. CSS::GridTrackPlacement StyleProperties::grid_row_end() const
  871. {
  872. auto value = property(CSS::PropertyID::GridRowEnd);
  873. return value->as_grid_track_placement().grid_track_placement();
  874. }
  875. CSS::GridTrackPlacement StyleProperties::grid_row_start() const
  876. {
  877. auto value = property(CSS::PropertyID::GridRowStart);
  878. return value->as_grid_track_placement().grid_track_placement();
  879. }
  880. Optional<CSS::BorderCollapse> StyleProperties::border_collapse() const
  881. {
  882. auto value = property(CSS::PropertyID::BorderCollapse);
  883. return value_id_to_border_collapse(value->to_identifier());
  884. }
  885. Vector<Vector<String>> StyleProperties::grid_template_areas() const
  886. {
  887. auto value = property(CSS::PropertyID::GridTemplateAreas);
  888. return value->as_grid_template_area().grid_template_area();
  889. }
  890. String StyleProperties::grid_area() const
  891. {
  892. auto value = property(CSS::PropertyID::GridArea);
  893. return value->as_string().string_value();
  894. }
  895. Optional<CSS::ObjectFit> StyleProperties::object_fit() const
  896. {
  897. auto value = property(CSS::PropertyID::ObjectFit);
  898. return value_id_to_object_fit(value->to_identifier());
  899. }
  900. CSS::ObjectPosition StyleProperties::object_position() const
  901. {
  902. auto value = property(CSS::PropertyID::ObjectPosition);
  903. auto const& position = value->as_position();
  904. CSS::ObjectPosition object_position;
  905. auto const& edge_x = position.edge_x();
  906. auto const& edge_y = position.edge_y();
  907. if (edge_x->is_edge()) {
  908. auto const& edge = edge_x->as_edge();
  909. object_position.edge_x = edge.edge();
  910. object_position.offset_x = edge.offset();
  911. }
  912. if (edge_y->is_edge()) {
  913. auto const& edge = edge_y->as_edge();
  914. object_position.edge_y = edge.edge();
  915. object_position.offset_y = edge.offset();
  916. }
  917. return object_position;
  918. }
  919. Optional<CSS::TableLayout> StyleProperties::table_layout() const
  920. {
  921. auto value = property(CSS::PropertyID::TableLayout);
  922. return value_id_to_table_layout(value->to_identifier());
  923. }
  924. Optional<CSS::MaskType> StyleProperties::mask_type() const
  925. {
  926. auto value = property(CSS::PropertyID::MaskType);
  927. return value_id_to_mask_type(value->to_identifier());
  928. }
  929. Color StyleProperties::stop_color() const
  930. {
  931. auto value = property(CSS::PropertyID::StopColor);
  932. if (value->is_identifier()) {
  933. // Workaround lack of layout node to resolve current color.
  934. auto& ident = value->as_identifier();
  935. if (ident.id() == CSS::ValueID::Currentcolor)
  936. value = property(CSS::PropertyID::Color);
  937. }
  938. if (value->has_color()) {
  939. // FIXME: This is used by the SVGStopElement, which does not participate in layout,
  940. // so can't pass a layout node (so can't resolve some colors, e.g. palette ones)
  941. return value->to_color({});
  942. }
  943. return Color::Black;
  944. }
  945. void StyleProperties::set_math_depth(int math_depth)
  946. {
  947. m_math_depth = math_depth;
  948. // Make our children inherit our computed value, not our specified value.
  949. set_property(PropertyID::MathDepth, MathDepthStyleValue::create_integer(IntegerStyleValue::create(math_depth)));
  950. }
  951. QuotesData StyleProperties::quotes() const
  952. {
  953. auto value = property(CSS::PropertyID::Quotes);
  954. if (value->is_identifier()) {
  955. switch (value->to_identifier()) {
  956. case ValueID::Auto:
  957. return QuotesData { .type = QuotesData::Type::Auto };
  958. case ValueID::None:
  959. return QuotesData { .type = QuotesData::Type::None };
  960. default:
  961. break;
  962. }
  963. }
  964. if (value->is_value_list()) {
  965. auto& value_list = value->as_value_list();
  966. QuotesData quotes_data { .type = QuotesData::Type::Specified };
  967. VERIFY(value_list.size() % 2 == 0);
  968. for (auto i = 0u; i < value_list.size(); i += 2) {
  969. quotes_data.strings.empend(
  970. value_list.value_at(i, false)->as_string().string_value(),
  971. value_list.value_at(i + 1, false)->as_string().string_value());
  972. }
  973. return quotes_data;
  974. }
  975. return InitialValues::quotes();
  976. }
  977. Optional<CSS::ScrollbarWidth> StyleProperties::scrollbar_width() const
  978. {
  979. auto value = property(CSS::PropertyID::ScrollbarWidth);
  980. return value_id_to_scrollbar_width(value->to_identifier());
  981. }
  982. }