StyleValue.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/ByteBuffer.h>
  9. #include <LibGfx/Palette.h>
  10. #include <LibWeb/CSS/Serialize.h>
  11. #include <LibWeb/CSS/StyleValue.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/HTML/BrowsingContext.h>
  14. #include <LibWeb/Loader/LoadRequest.h>
  15. #include <LibWeb/Loader/ResourceLoader.h>
  16. #include <LibWeb/Page/Page.h>
  17. namespace Web::CSS {
  18. StyleValue::StyleValue(Type type)
  19. : m_type(type)
  20. {
  21. }
  22. AngleStyleValue const& StyleValue::as_angle() const
  23. {
  24. VERIFY(is_angle());
  25. return static_cast<AngleStyleValue const&>(*this);
  26. }
  27. BackgroundStyleValue const& StyleValue::as_background() const
  28. {
  29. VERIFY(is_background());
  30. return static_cast<BackgroundStyleValue const&>(*this);
  31. }
  32. BackgroundRepeatStyleValue const& StyleValue::as_background_repeat() const
  33. {
  34. VERIFY(is_background_repeat());
  35. return static_cast<BackgroundRepeatStyleValue const&>(*this);
  36. }
  37. BackgroundSizeStyleValue const& StyleValue::as_background_size() const
  38. {
  39. VERIFY(is_background_size());
  40. return static_cast<BackgroundSizeStyleValue const&>(*this);
  41. }
  42. BorderStyleValue const& StyleValue::as_border() const
  43. {
  44. VERIFY(is_border());
  45. return static_cast<BorderStyleValue const&>(*this);
  46. }
  47. BorderRadiusStyleValue const& StyleValue::as_border_radius() const
  48. {
  49. VERIFY(is_border_radius());
  50. return static_cast<BorderRadiusStyleValue const&>(*this);
  51. }
  52. BorderRadiusShorthandStyleValue const& StyleValue::as_border_radius_shorthand() const
  53. {
  54. VERIFY(is_border_radius_shorthand());
  55. return static_cast<BorderRadiusShorthandStyleValue const&>(*this);
  56. }
  57. ShadowStyleValue const& StyleValue::as_shadow() const
  58. {
  59. VERIFY(is_shadow());
  60. return static_cast<ShadowStyleValue const&>(*this);
  61. }
  62. CalculatedStyleValue const& StyleValue::as_calculated() const
  63. {
  64. VERIFY(is_calculated());
  65. return static_cast<CalculatedStyleValue const&>(*this);
  66. }
  67. ColorStyleValue const& StyleValue::as_color() const
  68. {
  69. VERIFY(is_color());
  70. return static_cast<ColorStyleValue const&>(*this);
  71. }
  72. ContentStyleValue const& StyleValue::as_content() const
  73. {
  74. VERIFY(is_content());
  75. return static_cast<ContentStyleValue const&>(*this);
  76. }
  77. FlexStyleValue const& StyleValue::as_flex() const
  78. {
  79. VERIFY(is_flex());
  80. return static_cast<FlexStyleValue const&>(*this);
  81. }
  82. FlexFlowStyleValue const& StyleValue::as_flex_flow() const
  83. {
  84. VERIFY(is_flex_flow());
  85. return static_cast<FlexFlowStyleValue const&>(*this);
  86. }
  87. FontStyleValue const& StyleValue::as_font() const
  88. {
  89. VERIFY(is_font());
  90. return static_cast<FontStyleValue const&>(*this);
  91. }
  92. FrequencyStyleValue const& StyleValue::as_frequency() const
  93. {
  94. VERIFY(is_frequency());
  95. return static_cast<FrequencyStyleValue const&>(*this);
  96. }
  97. IdentifierStyleValue const& StyleValue::as_identifier() const
  98. {
  99. VERIFY(is_identifier());
  100. return static_cast<IdentifierStyleValue const&>(*this);
  101. }
  102. ImageStyleValue const& StyleValue::as_image() const
  103. {
  104. VERIFY(is_image());
  105. return static_cast<ImageStyleValue const&>(*this);
  106. }
  107. InheritStyleValue const& StyleValue::as_inherit() const
  108. {
  109. VERIFY(is_inherit());
  110. return static_cast<InheritStyleValue const&>(*this);
  111. }
  112. InitialStyleValue const& StyleValue::as_initial() const
  113. {
  114. VERIFY(is_initial());
  115. return static_cast<InitialStyleValue const&>(*this);
  116. }
  117. LengthStyleValue const& StyleValue::as_length() const
  118. {
  119. VERIFY(is_length());
  120. return static_cast<LengthStyleValue const&>(*this);
  121. }
  122. ListStyleStyleValue const& StyleValue::as_list_style() const
  123. {
  124. VERIFY(is_list_style());
  125. return static_cast<ListStyleStyleValue const&>(*this);
  126. }
  127. NumericStyleValue const& StyleValue::as_numeric() const
  128. {
  129. VERIFY(is_numeric());
  130. return static_cast<NumericStyleValue const&>(*this);
  131. }
  132. OverflowStyleValue const& StyleValue::as_overflow() const
  133. {
  134. VERIFY(is_overflow());
  135. return static_cast<OverflowStyleValue const&>(*this);
  136. }
  137. PercentageStyleValue const& StyleValue::as_percentage() const
  138. {
  139. VERIFY(is_percentage());
  140. return static_cast<PercentageStyleValue const&>(*this);
  141. }
  142. PositionStyleValue const& StyleValue::as_position() const
  143. {
  144. VERIFY(is_position());
  145. return static_cast<PositionStyleValue const&>(*this);
  146. }
  147. ResolutionStyleValue const& StyleValue::as_resolution() const
  148. {
  149. VERIFY(is_resolution());
  150. return static_cast<ResolutionStyleValue const&>(*this);
  151. }
  152. StringStyleValue const& StyleValue::as_string() const
  153. {
  154. VERIFY(is_string());
  155. return static_cast<StringStyleValue const&>(*this);
  156. }
  157. TextDecorationStyleValue const& StyleValue::as_text_decoration() const
  158. {
  159. VERIFY(is_text_decoration());
  160. return static_cast<TextDecorationStyleValue const&>(*this);
  161. }
  162. TimeStyleValue const& StyleValue::as_time() const
  163. {
  164. VERIFY(is_time());
  165. return static_cast<TimeStyleValue const&>(*this);
  166. }
  167. TransformationStyleValue const& StyleValue::as_transformation() const
  168. {
  169. VERIFY(is_transformation());
  170. return static_cast<TransformationStyleValue const&>(*this);
  171. }
  172. UnresolvedStyleValue const& StyleValue::as_unresolved() const
  173. {
  174. VERIFY(is_unresolved());
  175. return static_cast<UnresolvedStyleValue const&>(*this);
  176. }
  177. UnsetStyleValue const& StyleValue::as_unset() const
  178. {
  179. VERIFY(is_unset());
  180. return static_cast<UnsetStyleValue const&>(*this);
  181. }
  182. StyleValueList const& StyleValue::as_value_list() const
  183. {
  184. VERIFY(is_value_list());
  185. return static_cast<StyleValueList const&>(*this);
  186. }
  187. BackgroundStyleValue::BackgroundStyleValue(
  188. NonnullRefPtr<StyleValue> color,
  189. NonnullRefPtr<StyleValue> image,
  190. NonnullRefPtr<StyleValue> position,
  191. NonnullRefPtr<StyleValue> size,
  192. NonnullRefPtr<StyleValue> repeat,
  193. NonnullRefPtr<StyleValue> attachment,
  194. NonnullRefPtr<StyleValue> origin,
  195. NonnullRefPtr<StyleValue> clip)
  196. : StyleValue(Type::Background)
  197. , m_color(color)
  198. , m_image(image)
  199. , m_position(position)
  200. , m_size(size)
  201. , m_repeat(repeat)
  202. , m_attachment(attachment)
  203. , m_origin(origin)
  204. , m_clip(clip)
  205. {
  206. auto layer_count = [](auto style_value) -> size_t {
  207. if (style_value->is_value_list())
  208. return style_value->as_value_list().size();
  209. else
  210. return 1;
  211. };
  212. m_layer_count = max(layer_count(m_image), layer_count(m_position));
  213. m_layer_count = max(m_layer_count, layer_count(m_size));
  214. m_layer_count = max(m_layer_count, layer_count(m_repeat));
  215. m_layer_count = max(m_layer_count, layer_count(m_attachment));
  216. m_layer_count = max(m_layer_count, layer_count(m_origin));
  217. m_layer_count = max(m_layer_count, layer_count(m_clip));
  218. VERIFY(!m_color->is_value_list());
  219. }
  220. String BackgroundStyleValue::to_string() const
  221. {
  222. if (m_layer_count == 1) {
  223. return String::formatted("{} {} {} {} {} {} {} {}", m_color->to_string(), m_image->to_string(), m_position->to_string(), m_size->to_string(), m_repeat->to_string(), m_attachment->to_string(), m_origin->to_string(), m_clip->to_string());
  224. }
  225. auto get_layer_value_string = [](NonnullRefPtr<StyleValue> const& style_value, size_t index) {
  226. if (style_value->is_value_list())
  227. return style_value->as_value_list().value_at(index, true)->to_string();
  228. return style_value->to_string();
  229. };
  230. StringBuilder builder;
  231. for (size_t i = 0; i < m_layer_count; i++) {
  232. if (i)
  233. builder.append(", ");
  234. if (i == m_layer_count - 1)
  235. builder.appendff("{} ", m_color->to_string());
  236. builder.appendff("{} {} {} {} {} {} {}", get_layer_value_string(m_image, i), get_layer_value_string(m_position, i), get_layer_value_string(m_size, i), get_layer_value_string(m_repeat, i), get_layer_value_string(m_attachment, i), get_layer_value_string(m_origin, i), get_layer_value_string(m_clip, i));
  237. }
  238. return builder.to_string();
  239. }
  240. bool BackgroundStyleValue::equals(StyleValue const& other) const
  241. {
  242. if (type() != other.type())
  243. return false;
  244. auto const& typed_other = other.as_background();
  245. return m_color->equals(typed_other.m_color)
  246. && m_image->equals(typed_other.m_image)
  247. && m_position->equals(typed_other.m_position)
  248. && m_size->equals(typed_other.m_size)
  249. && m_repeat->equals(typed_other.m_repeat)
  250. && m_attachment->equals(typed_other.m_attachment)
  251. && m_origin->equals(typed_other.m_origin)
  252. && m_clip->equals(typed_other.m_clip);
  253. }
  254. String BackgroundRepeatStyleValue::to_string() const
  255. {
  256. return String::formatted("{} {}", CSS::to_string(m_repeat_x), CSS::to_string(m_repeat_y));
  257. }
  258. bool BackgroundRepeatStyleValue::equals(StyleValue const& other) const
  259. {
  260. if (type() != other.type())
  261. return false;
  262. auto const& typed_other = other.as_background_repeat();
  263. return m_repeat_x == typed_other.m_repeat_x && m_repeat_y == typed_other.m_repeat_y;
  264. }
  265. String BackgroundSizeStyleValue::to_string() const
  266. {
  267. return String::formatted("{} {}", m_size_x.to_string(), m_size_y.to_string());
  268. }
  269. bool BackgroundSizeStyleValue::equals(StyleValue const& other) const
  270. {
  271. if (type() != other.type())
  272. return false;
  273. auto const& typed_other = other.as_background_size();
  274. return m_size_x == typed_other.m_size_x && m_size_y == typed_other.m_size_y;
  275. }
  276. String BorderStyleValue::to_string() const
  277. {
  278. return String::formatted("{} {} {}", m_border_width->to_string(), m_border_style->to_string(), m_border_color->to_string());
  279. }
  280. bool BorderStyleValue::equals(StyleValue const& other) const
  281. {
  282. if (type() != other.type())
  283. return false;
  284. auto const& typed_other = other.as_border();
  285. return m_border_width->equals(typed_other.m_border_width)
  286. && m_border_style->equals(typed_other.m_border_style)
  287. && m_border_color->equals(typed_other.m_border_color);
  288. }
  289. String BorderRadiusStyleValue::to_string() const
  290. {
  291. if (m_horizontal_radius == m_vertical_radius)
  292. return m_horizontal_radius.to_string();
  293. return String::formatted("{} / {}", m_horizontal_radius.to_string(), m_vertical_radius.to_string());
  294. }
  295. bool BorderRadiusStyleValue::equals(StyleValue const& other) const
  296. {
  297. if (type() != other.type())
  298. return false;
  299. auto const& typed_other = other.as_border_radius();
  300. return m_is_elliptical == typed_other.m_is_elliptical
  301. && m_horizontal_radius == typed_other.m_horizontal_radius
  302. && m_vertical_radius == typed_other.m_vertical_radius;
  303. }
  304. String BorderRadiusShorthandStyleValue::to_string() const
  305. {
  306. return String::formatted("{} {} {} {} / {} {} {} {}", m_top_left->horizontal_radius().to_string(), m_top_right->horizontal_radius().to_string(), m_bottom_right->horizontal_radius().to_string(), m_bottom_left->horizontal_radius().to_string(), m_top_left->vertical_radius().to_string(), m_top_right->vertical_radius().to_string(), m_bottom_right->vertical_radius().to_string(), m_bottom_left->vertical_radius().to_string());
  307. }
  308. bool BorderRadiusShorthandStyleValue::equals(StyleValue const& other) const
  309. {
  310. if (type() != other.type())
  311. return false;
  312. auto const& typed_other = other.as_border_radius_shorthand();
  313. return m_top_left->equals(typed_other.m_top_left)
  314. && m_top_right->equals(typed_other.m_top_right)
  315. && m_bottom_right->equals(typed_other.m_bottom_right)
  316. && m_bottom_left->equals(typed_other.m_bottom_left);
  317. }
  318. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  319. {
  320. add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis);
  321. }
  322. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  323. {
  324. add_or_subtract_internal(SumOperation::Subtract, other, layout_node, percentage_basis);
  325. }
  326. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  327. {
  328. // We know from validation when resolving the type, that "both sides have the same type, or that one side is a <number> and the other is an <integer>".
  329. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  330. // Note: This is almost identical to ::add()
  331. m_value.visit(
  332. [&](Number const& number) {
  333. auto other_number = other.m_value.get<Number>();
  334. if (op == SumOperation::Add) {
  335. m_value = number + other_number;
  336. } else {
  337. m_value = number - other_number;
  338. }
  339. },
  340. [&](Angle const& angle) {
  341. auto this_degrees = angle.to_degrees();
  342. if (other.m_value.has<Angle>()) {
  343. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  344. if (op == SumOperation::Add)
  345. m_value = Angle::make_degrees(this_degrees + other_degrees);
  346. else
  347. m_value = Angle::make_degrees(this_degrees - other_degrees);
  348. } else {
  349. VERIFY(percentage_basis.has<Angle>());
  350. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  351. if (op == SumOperation::Add)
  352. m_value = Angle::make_degrees(this_degrees + other_degrees);
  353. else
  354. m_value = Angle::make_degrees(this_degrees - other_degrees);
  355. }
  356. },
  357. [&](Frequency const& frequency) {
  358. auto this_hertz = frequency.to_hertz();
  359. if (other.m_value.has<Frequency>()) {
  360. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  361. if (op == SumOperation::Add)
  362. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  363. else
  364. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  365. } else {
  366. VERIFY(percentage_basis.has<Frequency>());
  367. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  368. if (op == SumOperation::Add)
  369. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  370. else
  371. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  372. }
  373. },
  374. [&](Length const& length) {
  375. auto this_px = length.to_px(*layout_node);
  376. if (other.m_value.has<Length>()) {
  377. auto other_px = other.m_value.get<Length>().to_px(*layout_node);
  378. if (op == SumOperation::Add)
  379. m_value = Length::make_px(this_px + other_px);
  380. else
  381. m_value = Length::make_px(this_px - other_px);
  382. } else {
  383. VERIFY(percentage_basis.has<Length>());
  384. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*layout_node);
  385. if (op == SumOperation::Add)
  386. m_value = Length::make_px(this_px + other_px);
  387. else
  388. m_value = Length::make_px(this_px - other_px);
  389. }
  390. },
  391. [&](Time const& time) {
  392. auto this_seconds = time.to_seconds();
  393. if (other.m_value.has<Time>()) {
  394. auto other_seconds = other.m_value.get<Time>().to_seconds();
  395. if (op == SumOperation::Add)
  396. m_value = Time::make_seconds(this_seconds + other_seconds);
  397. else
  398. m_value = Time::make_seconds(this_seconds - other_seconds);
  399. } else {
  400. VERIFY(percentage_basis.has<Time>());
  401. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  402. if (op == SumOperation::Add)
  403. m_value = Time::make_seconds(this_seconds + other_seconds);
  404. else
  405. m_value = Time::make_seconds(this_seconds - other_seconds);
  406. }
  407. },
  408. [&](Percentage const& percentage) {
  409. if (other.m_value.has<Percentage>()) {
  410. if (op == SumOperation::Add)
  411. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  412. else
  413. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  414. return;
  415. }
  416. // Other side isn't a percentage, so the easiest way to handle it without duplicating all the logic, is just to swap `this` and `other`.
  417. CalculationResult new_value = other;
  418. if (op == SumOperation::Add)
  419. new_value.add(*this, layout_node, percentage_basis);
  420. else
  421. new_value.subtract(*this, layout_node, percentage_basis);
  422. *this = new_value;
  423. });
  424. }
  425. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Layout::Node const* layout_node)
  426. {
  427. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  428. // Both of these are represented as a float.
  429. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  430. bool other_is_number = other.m_value.has<Number>();
  431. m_value.visit(
  432. [&](Number const& number) {
  433. if (other_is_number) {
  434. m_value = number * other.m_value.get<Number>();
  435. } else {
  436. // Avoid duplicating all the logic by swapping `this` and `other`.
  437. CalculationResult new_value = other;
  438. new_value.multiply_by(*this, layout_node);
  439. *this = new_value;
  440. }
  441. },
  442. [&](Angle const& angle) {
  443. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  444. },
  445. [&](Frequency const& frequency) {
  446. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  447. },
  448. [&](Length const& length) {
  449. VERIFY(layout_node);
  450. m_value = Length::make_px(length.to_px(*layout_node) * other.m_value.get<Number>().value());
  451. },
  452. [&](Time const& time) {
  453. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  454. },
  455. [&](Percentage const& percentage) {
  456. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  457. });
  458. }
  459. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Layout::Node const* layout_node)
  460. {
  461. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  462. // Both of these are represented as a Number.
  463. auto denominator = other.m_value.get<Number>().value();
  464. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  465. VERIFY(denominator != 0.0f);
  466. m_value.visit(
  467. [&](Number const& number) {
  468. m_value = Number {
  469. Number::Type::Number,
  470. number.value() / denominator
  471. };
  472. },
  473. [&](Angle const& angle) {
  474. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  475. },
  476. [&](Frequency const& frequency) {
  477. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  478. },
  479. [&](Length const& length) {
  480. VERIFY(layout_node);
  481. m_value = Length::make_px(length.to_px(*layout_node) / denominator);
  482. },
  483. [&](Time const& time) {
  484. m_value = Time::make_seconds(time.to_seconds() / denominator);
  485. },
  486. [&](Percentage const& percentage) {
  487. m_value = Percentage { percentage.value() / denominator };
  488. });
  489. }
  490. String CalculatedStyleValue::to_string() const
  491. {
  492. return String::formatted("calc({})", m_expression->to_string());
  493. }
  494. bool CalculatedStyleValue::equals(StyleValue const& other) const
  495. {
  496. if (type() != other.type())
  497. return false;
  498. // This is a case where comparing the strings actually makes sense.
  499. return to_string() == other.to_string();
  500. }
  501. String CalculatedStyleValue::CalcNumberValue::to_string() const
  502. {
  503. return value.visit(
  504. [](Number const& number) { return String::number(number.value()); },
  505. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return String::formatted("({})", sum->to_string()); });
  506. }
  507. String CalculatedStyleValue::CalcValue::to_string() const
  508. {
  509. return value.visit(
  510. [](Number const& number) { return String::number(number.value()); },
  511. [](NonnullOwnPtr<CalcSum> const& sum) { return String::formatted("({})", sum->to_string()); },
  512. [](auto const& v) { return v.to_string(); });
  513. }
  514. String CalculatedStyleValue::CalcSum::to_string() const
  515. {
  516. StringBuilder builder;
  517. builder.append(first_calc_product->to_string());
  518. for (auto const& item : zero_or_more_additional_calc_products)
  519. builder.append(item.to_string());
  520. return builder.to_string();
  521. }
  522. String CalculatedStyleValue::CalcNumberSum::to_string() const
  523. {
  524. StringBuilder builder;
  525. builder.append(first_calc_number_product->to_string());
  526. for (auto const& item : zero_or_more_additional_calc_number_products)
  527. builder.append(item.to_string());
  528. return builder.to_string();
  529. }
  530. String CalculatedStyleValue::CalcProduct::to_string() const
  531. {
  532. StringBuilder builder;
  533. builder.append(first_calc_value.to_string());
  534. for (auto const& item : zero_or_more_additional_calc_values)
  535. builder.append(item.to_string());
  536. return builder.to_string();
  537. }
  538. String CalculatedStyleValue::CalcSumPartWithOperator::to_string() const
  539. {
  540. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, value->to_string());
  541. }
  542. String CalculatedStyleValue::CalcProductPartWithOperator::to_string() const
  543. {
  544. auto value_string = value.visit(
  545. [](CalcValue const& v) { return v.to_string(); },
  546. [](CalcNumberValue const& v) { return v.to_string(); });
  547. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value_string);
  548. }
  549. String CalculatedStyleValue::CalcNumberProduct::to_string() const
  550. {
  551. StringBuilder builder;
  552. builder.append(first_calc_number_value.to_string());
  553. for (auto const& item : zero_or_more_additional_calc_number_values)
  554. builder.append(item.to_string());
  555. return builder.to_string();
  556. }
  557. String CalculatedStyleValue::CalcNumberProductPartWithOperator::to_string() const
  558. {
  559. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value.to_string());
  560. }
  561. String CalculatedStyleValue::CalcNumberSumPartWithOperator::to_string() const
  562. {
  563. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, value->to_string());
  564. }
  565. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  566. {
  567. auto result = m_expression->resolve(nullptr, {});
  568. if (result.value().has<Angle>())
  569. return result.value().get<Angle>();
  570. return {};
  571. }
  572. Optional<AnglePercentage> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  573. {
  574. auto result = m_expression->resolve(nullptr, percentage_basis);
  575. return result.value().visit(
  576. [&](Angle const& angle) -> Optional<AnglePercentage> {
  577. return angle;
  578. },
  579. [&](Percentage const& percentage) -> Optional<AnglePercentage> {
  580. return percentage;
  581. },
  582. [&](auto const&) -> Optional<AnglePercentage> {
  583. return {};
  584. });
  585. }
  586. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  587. {
  588. auto result = m_expression->resolve(nullptr, {});
  589. if (result.value().has<Frequency>())
  590. return result.value().get<Frequency>();
  591. return {};
  592. }
  593. Optional<FrequencyPercentage> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  594. {
  595. auto result = m_expression->resolve(nullptr, percentage_basis);
  596. return result.value().visit(
  597. [&](Frequency const& frequency) -> Optional<FrequencyPercentage> {
  598. return frequency;
  599. },
  600. [&](Percentage const& percentage) -> Optional<FrequencyPercentage> {
  601. return percentage;
  602. },
  603. [&](auto const&) -> Optional<FrequencyPercentage> {
  604. return {};
  605. });
  606. }
  607. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  608. {
  609. auto result = m_expression->resolve(&layout_node, {});
  610. if (result.value().has<Length>())
  611. return result.value().get<Length>();
  612. return {};
  613. }
  614. Optional<LengthPercentage> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  615. {
  616. auto result = m_expression->resolve(&layout_node, percentage_basis);
  617. return result.value().visit(
  618. [&](Length const& length) -> Optional<LengthPercentage> {
  619. return length;
  620. },
  621. [&](Percentage const& percentage) -> Optional<LengthPercentage> {
  622. return percentage;
  623. },
  624. [&](auto const&) -> Optional<LengthPercentage> {
  625. return {};
  626. });
  627. }
  628. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  629. {
  630. auto result = m_expression->resolve(nullptr, {});
  631. if (result.value().has<Percentage>())
  632. return result.value().get<Percentage>();
  633. return {};
  634. }
  635. Optional<Time> CalculatedStyleValue::resolve_time() const
  636. {
  637. auto result = m_expression->resolve(nullptr, {});
  638. if (result.value().has<Time>())
  639. return result.value().get<Time>();
  640. return {};
  641. }
  642. Optional<TimePercentage> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  643. {
  644. auto result = m_expression->resolve(nullptr, percentage_basis);
  645. return result.value().visit(
  646. [&](Time const& time) -> Optional<TimePercentage> {
  647. return time;
  648. },
  649. [&](Percentage const& percentage) -> Optional<TimePercentage> {
  650. return percentage;
  651. },
  652. [&](auto const&) -> Optional<TimePercentage> {
  653. return {};
  654. });
  655. }
  656. Optional<float> CalculatedStyleValue::resolve_number()
  657. {
  658. auto result = m_expression->resolve(nullptr, {});
  659. if (result.value().has<Number>())
  660. return result.value().get<Number>().value();
  661. return {};
  662. }
  663. Optional<i64> CalculatedStyleValue::resolve_integer()
  664. {
  665. auto result = m_expression->resolve(nullptr, {});
  666. if (result.value().has<Number>())
  667. return result.value().get<Number>().integer_value();
  668. return {};
  669. }
  670. static bool is_number(CalculatedStyleValue::ResolvedType type)
  671. {
  672. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  673. }
  674. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  675. {
  676. return type != CalculatedStyleValue::ResolvedType::Number
  677. && type != CalculatedStyleValue::ResolvedType::Integer
  678. && type != CalculatedStyleValue::ResolvedType::Percentage;
  679. }
  680. template<typename SumWithOperator>
  681. static Optional<CalculatedStyleValue::ResolvedType> resolve_sum_type(CalculatedStyleValue::ResolvedType first_type, NonnullOwnPtrVector<SumWithOperator> const& zero_or_more_additional_products)
  682. {
  683. auto type = first_type;
  684. for (auto const& product : zero_or_more_additional_products) {
  685. auto maybe_product_type = product.resolved_type();
  686. if (!maybe_product_type.has_value())
  687. return {};
  688. auto product_type = maybe_product_type.value();
  689. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  690. // If both sides are the same type, resolve to that type.
  691. if (product_type == type)
  692. continue;
  693. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  694. if (is_number(type) && is_number(product_type)) {
  695. type = CalculatedStyleValue::ResolvedType::Number;
  696. continue;
  697. }
  698. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  699. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  700. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(product_type)) {
  701. type = product_type;
  702. continue;
  703. }
  704. if (is_dimension(type) && product_type == CalculatedStyleValue::ResolvedType::Percentage)
  705. continue;
  706. return {};
  707. }
  708. return type;
  709. }
  710. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSum::resolved_type() const
  711. {
  712. auto maybe_type = first_calc_product->resolved_type();
  713. if (!maybe_type.has_value())
  714. return {};
  715. auto type = maybe_type.value();
  716. return resolve_sum_type(type, zero_or_more_additional_calc_products);
  717. }
  718. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSum::resolved_type() const
  719. {
  720. auto maybe_type = first_calc_number_product->resolved_type();
  721. if (!maybe_type.has_value())
  722. return {};
  723. auto type = maybe_type.value();
  724. return resolve_sum_type(type, zero_or_more_additional_calc_number_products);
  725. }
  726. template<typename ProductWithOperator>
  727. static Optional<CalculatedStyleValue::ResolvedType> resolve_product_type(CalculatedStyleValue::ResolvedType first_type, NonnullOwnPtrVector<ProductWithOperator> const& zero_or_more_additional_values)
  728. {
  729. auto type = first_type;
  730. for (auto const& value : zero_or_more_additional_values) {
  731. auto maybe_value_type = value.resolved_type();
  732. if (!maybe_value_type.has_value())
  733. return {};
  734. auto value_type = maybe_value_type.value();
  735. if (value.op == CalculatedStyleValue::ProductOperation::Multiply) {
  736. // At *, check that at least one side is <number>.
  737. if (!(is_number(type) || is_number(value_type)))
  738. return {};
  739. // If both sides are <integer>, resolve to <integer>.
  740. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  741. type = CalculatedStyleValue::ResolvedType::Integer;
  742. } else {
  743. // Otherwise, resolve to the type of the other side.
  744. if (is_number(type))
  745. type = value_type;
  746. }
  747. continue;
  748. } else {
  749. VERIFY(value.op == CalculatedStyleValue::ProductOperation::Divide);
  750. // At /, check that the right side is <number>.
  751. if (!is_number(value_type))
  752. return {};
  753. // If the left side is <integer>, resolve to <number>.
  754. if (type == CalculatedStyleValue::ResolvedType::Integer) {
  755. type = CalculatedStyleValue::ResolvedType::Number;
  756. } else {
  757. // Otherwise, resolve to the type of the left side.
  758. }
  759. // FIXME: Division by zero makes the whole calc() expression invalid.
  760. }
  761. }
  762. return type;
  763. }
  764. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProduct::resolved_type() const
  765. {
  766. auto maybe_type = first_calc_value.resolved_type();
  767. if (!maybe_type.has_value())
  768. return {};
  769. auto type = maybe_type.value();
  770. return resolve_product_type(type, zero_or_more_additional_calc_values);
  771. }
  772. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSumPartWithOperator::resolved_type() const
  773. {
  774. return value->resolved_type();
  775. }
  776. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProduct::resolved_type() const
  777. {
  778. auto maybe_type = first_calc_number_value.resolved_type();
  779. if (!maybe_type.has_value())
  780. return {};
  781. auto type = maybe_type.value();
  782. return resolve_product_type(type, zero_or_more_additional_calc_number_values);
  783. }
  784. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProductPartWithOperator::resolved_type() const
  785. {
  786. return value.resolved_type();
  787. }
  788. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSumPartWithOperator::resolved_type() const
  789. {
  790. return value->resolved_type();
  791. }
  792. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProductPartWithOperator::resolved_type() const
  793. {
  794. return value.visit(
  795. [](CalcValue const& calc_value) {
  796. return calc_value.resolved_type();
  797. },
  798. [](CalcNumberValue const& calc_number_value) {
  799. return calc_number_value.resolved_type();
  800. });
  801. }
  802. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcValue::resolved_type() const
  803. {
  804. return value.visit(
  805. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  806. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  807. },
  808. [](Angle const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Angle }; },
  809. [](Frequency const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Frequency }; },
  810. [](Length const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Length }; },
  811. [](Percentage const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Percentage }; },
  812. [](Time const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Time }; },
  813. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->resolved_type(); });
  814. }
  815. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberValue::resolved_type() const
  816. {
  817. return value.visit(
  818. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  819. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  820. },
  821. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return sum->resolved_type(); });
  822. }
  823. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  824. {
  825. return value.visit(
  826. [&](Number const& number) -> CalculatedStyleValue::CalculationResult {
  827. return CalculatedStyleValue::CalculationResult { number };
  828. },
  829. [&](NonnullOwnPtr<CalcNumberSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  830. return sum->resolve(layout_node, percentage_basis);
  831. });
  832. }
  833. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  834. {
  835. return value.visit(
  836. [&](NonnullOwnPtr<CalcSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  837. return sum->resolve(layout_node, percentage_basis);
  838. },
  839. [&](auto const& v) -> CalculatedStyleValue::CalculationResult {
  840. return CalculatedStyleValue::CalculationResult { v };
  841. });
  842. }
  843. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  844. {
  845. auto value = first_calc_product->resolve(layout_node, percentage_basis);
  846. for (auto& additional_product : zero_or_more_additional_calc_products) {
  847. auto additional_value = additional_product.resolve(layout_node, percentage_basis);
  848. if (additional_product.op == CalculatedStyleValue::SumOperation::Add)
  849. value.add(additional_value, layout_node, percentage_basis);
  850. else if (additional_product.op == CalculatedStyleValue::SumOperation::Subtract)
  851. value.subtract(additional_value, layout_node, percentage_basis);
  852. else
  853. VERIFY_NOT_REACHED();
  854. }
  855. return value;
  856. }
  857. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  858. {
  859. auto value = first_calc_number_product->resolve(layout_node, percentage_basis);
  860. for (auto& additional_product : zero_or_more_additional_calc_number_products) {
  861. auto additional_value = additional_product.resolve(layout_node, percentage_basis);
  862. if (additional_product.op == CSS::CalculatedStyleValue::SumOperation::Add)
  863. value.add(additional_value, layout_node, percentage_basis);
  864. else if (additional_product.op == CalculatedStyleValue::SumOperation::Subtract)
  865. value.subtract(additional_value, layout_node, percentage_basis);
  866. else
  867. VERIFY_NOT_REACHED();
  868. }
  869. return value;
  870. }
  871. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  872. {
  873. auto value = first_calc_value.resolve(layout_node, percentage_basis);
  874. for (auto& additional_value : zero_or_more_additional_calc_values) {
  875. additional_value.value.visit(
  876. [&](CalculatedStyleValue::CalcValue const& calc_value) {
  877. VERIFY(additional_value.op == CalculatedStyleValue::ProductOperation::Multiply);
  878. auto resolved_value = calc_value.resolve(layout_node, percentage_basis);
  879. value.multiply_by(resolved_value, layout_node);
  880. },
  881. [&](CalculatedStyleValue::CalcNumberValue const& calc_number_value) {
  882. VERIFY(additional_value.op == CalculatedStyleValue::ProductOperation::Divide);
  883. auto resolved_calc_number_value = calc_number_value.resolve(layout_node, percentage_basis);
  884. // FIXME: Checking for division by 0 should happen during parsing.
  885. VERIFY(resolved_calc_number_value.value().get<Number>().value() != 0.0f);
  886. value.divide_by(resolved_calc_number_value, layout_node);
  887. });
  888. }
  889. return value;
  890. }
  891. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  892. {
  893. auto value = first_calc_number_value.resolve(layout_node, percentage_basis);
  894. for (auto& additional_number_value : zero_or_more_additional_calc_number_values) {
  895. auto additional_value = additional_number_value.resolve(layout_node, percentage_basis);
  896. if (additional_number_value.op == CalculatedStyleValue::ProductOperation::Multiply)
  897. value.multiply_by(additional_value, layout_node);
  898. else if (additional_number_value.op == CalculatedStyleValue::ProductOperation::Divide)
  899. value.divide_by(additional_value, layout_node);
  900. else
  901. VERIFY_NOT_REACHED();
  902. }
  903. return value;
  904. }
  905. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  906. {
  907. return value.visit(
  908. [&](CalcValue const& calc_value) {
  909. return calc_value.resolve(layout_node, percentage_basis);
  910. },
  911. [&](CalcNumberValue const& calc_number_value) {
  912. return calc_number_value.resolve(layout_node, percentage_basis);
  913. });
  914. }
  915. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  916. {
  917. return value->resolve(layout_node, percentage_basis);
  918. }
  919. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  920. {
  921. return value.resolve(layout_node, percentage_basis);
  922. }
  923. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  924. {
  925. return value->resolve(layout_node, percentage_basis);
  926. }
  927. // https://www.w3.org/TR/css-color-4/#serializing-sRGB-values
  928. String ColorStyleValue::to_string() const
  929. {
  930. if (m_color.alpha() == 1)
  931. return String::formatted("rgb({}, {}, {})", m_color.red(), m_color.green(), m_color.blue());
  932. return String::formatted("rgba({}, {}, {}, {})", m_color.red(), m_color.green(), m_color.blue(), (float)(m_color.alpha()) / 255.0f);
  933. }
  934. bool ColorStyleValue::equals(StyleValue const& other) const
  935. {
  936. if (type() != other.type())
  937. return false;
  938. return m_color == other.as_color().m_color;
  939. }
  940. String ContentStyleValue::to_string() const
  941. {
  942. if (has_alt_text())
  943. return String::formatted("{} / {}", m_content->to_string(), m_alt_text->to_string());
  944. return m_content->to_string();
  945. }
  946. bool ContentStyleValue::equals(StyleValue const& other) const
  947. {
  948. if (type() != other.type())
  949. return false;
  950. auto const& typed_other = other.as_content();
  951. if (!m_content->equals(typed_other.m_content))
  952. return false;
  953. if (m_alt_text.is_null() != typed_other.m_alt_text.is_null())
  954. return false;
  955. if (!m_alt_text.is_null())
  956. return m_alt_text->equals(*typed_other.m_alt_text);
  957. return true;
  958. }
  959. String FlexStyleValue::to_string() const
  960. {
  961. return String::formatted("{} {} {}", m_grow->to_string(), m_shrink->to_string(), m_basis->to_string());
  962. }
  963. bool FlexStyleValue::equals(StyleValue const& other) const
  964. {
  965. if (type() != other.type())
  966. return false;
  967. auto const& typed_other = other.as_flex();
  968. return m_grow->equals(typed_other.m_grow)
  969. && m_shrink->equals(typed_other.m_shrink)
  970. && m_basis->equals(typed_other.m_basis);
  971. }
  972. String FlexFlowStyleValue::to_string() const
  973. {
  974. return String::formatted("{} {}", m_flex_direction->to_string(), m_flex_wrap->to_string());
  975. }
  976. bool FlexFlowStyleValue::equals(StyleValue const& other) const
  977. {
  978. if (type() != other.type())
  979. return false;
  980. auto const& typed_other = other.as_flex_flow();
  981. return m_flex_direction->equals(typed_other.m_flex_direction)
  982. && m_flex_wrap->equals(typed_other.m_flex_wrap);
  983. }
  984. String FontStyleValue::to_string() const
  985. {
  986. return String::formatted("{} {} {} / {} {}", m_font_style->to_string(), m_font_weight->to_string(), m_font_size->to_string(), m_line_height->to_string(), m_font_families->to_string());
  987. }
  988. bool FontStyleValue::equals(StyleValue const& other) const
  989. {
  990. if (type() != other.type())
  991. return false;
  992. auto const& typed_other = other.as_font();
  993. return m_font_style->equals(typed_other.m_font_style)
  994. && m_font_weight->equals(typed_other.m_font_weight)
  995. && m_font_size->equals(typed_other.m_font_size)
  996. && m_line_height->equals(typed_other.m_line_height)
  997. && m_font_families->equals(typed_other.m_font_families);
  998. }
  999. bool FrequencyStyleValue::equals(StyleValue const& other) const
  1000. {
  1001. if (type() != other.type())
  1002. return false;
  1003. return m_frequency == other.as_frequency().m_frequency;
  1004. }
  1005. String IdentifierStyleValue::to_string() const
  1006. {
  1007. return CSS::string_from_value_id(m_id);
  1008. }
  1009. bool IdentifierStyleValue::equals(StyleValue const& other) const
  1010. {
  1011. if (type() != other.type())
  1012. return false;
  1013. return m_id == other.as_identifier().m_id;
  1014. }
  1015. bool IdentifierStyleValue::has_color() const
  1016. {
  1017. switch (m_id) {
  1018. case ValueID::Currentcolor:
  1019. case ValueID::LibwebLink:
  1020. case ValueID::LibwebPaletteActiveLink:
  1021. case ValueID::LibwebPaletteActiveWindowBorder1:
  1022. case ValueID::LibwebPaletteActiveWindowBorder2:
  1023. case ValueID::LibwebPaletteActiveWindowTitle:
  1024. case ValueID::LibwebPaletteBase:
  1025. case ValueID::LibwebPaletteBaseText:
  1026. case ValueID::LibwebPaletteButton:
  1027. case ValueID::LibwebPaletteButtonText:
  1028. case ValueID::LibwebPaletteDesktopBackground:
  1029. case ValueID::LibwebPaletteFocusOutline:
  1030. case ValueID::LibwebPaletteHighlightWindowBorder1:
  1031. case ValueID::LibwebPaletteHighlightWindowBorder2:
  1032. case ValueID::LibwebPaletteHighlightWindowTitle:
  1033. case ValueID::LibwebPaletteHoverHighlight:
  1034. case ValueID::LibwebPaletteInactiveSelection:
  1035. case ValueID::LibwebPaletteInactiveSelectionText:
  1036. case ValueID::LibwebPaletteInactiveWindowBorder1:
  1037. case ValueID::LibwebPaletteInactiveWindowBorder2:
  1038. case ValueID::LibwebPaletteInactiveWindowTitle:
  1039. case ValueID::LibwebPaletteLink:
  1040. case ValueID::LibwebPaletteMenuBase:
  1041. case ValueID::LibwebPaletteMenuBaseText:
  1042. case ValueID::LibwebPaletteMenuSelection:
  1043. case ValueID::LibwebPaletteMenuSelectionText:
  1044. case ValueID::LibwebPaletteMenuStripe:
  1045. case ValueID::LibwebPaletteMovingWindowBorder1:
  1046. case ValueID::LibwebPaletteMovingWindowBorder2:
  1047. case ValueID::LibwebPaletteMovingWindowTitle:
  1048. case ValueID::LibwebPaletteRubberBandBorder:
  1049. case ValueID::LibwebPaletteRubberBandFill:
  1050. case ValueID::LibwebPaletteRuler:
  1051. case ValueID::LibwebPaletteRulerActiveText:
  1052. case ValueID::LibwebPaletteRulerBorder:
  1053. case ValueID::LibwebPaletteRulerInactiveText:
  1054. case ValueID::LibwebPaletteSelection:
  1055. case ValueID::LibwebPaletteSelectionText:
  1056. case ValueID::LibwebPaletteSyntaxComment:
  1057. case ValueID::LibwebPaletteSyntaxControlKeyword:
  1058. case ValueID::LibwebPaletteSyntaxIdentifier:
  1059. case ValueID::LibwebPaletteSyntaxKeyword:
  1060. case ValueID::LibwebPaletteSyntaxNumber:
  1061. case ValueID::LibwebPaletteSyntaxOperator:
  1062. case ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  1063. case ValueID::LibwebPaletteSyntaxPreprocessorValue:
  1064. case ValueID::LibwebPaletteSyntaxPunctuation:
  1065. case ValueID::LibwebPaletteSyntaxString:
  1066. case ValueID::LibwebPaletteSyntaxType:
  1067. case ValueID::LibwebPaletteTextCursor:
  1068. case ValueID::LibwebPaletteThreedHighlight:
  1069. case ValueID::LibwebPaletteThreedShadow1:
  1070. case ValueID::LibwebPaletteThreedShadow2:
  1071. case ValueID::LibwebPaletteVisitedLink:
  1072. case ValueID::LibwebPaletteWindow:
  1073. case ValueID::LibwebPaletteWindowText:
  1074. return true;
  1075. default:
  1076. return false;
  1077. }
  1078. }
  1079. Color IdentifierStyleValue::to_color(Layout::NodeWithStyle const& node) const
  1080. {
  1081. if (id() == CSS::ValueID::Currentcolor) {
  1082. if (!node.has_style())
  1083. return Color::Black;
  1084. return node.computed_values().color();
  1085. }
  1086. auto& document = node.document();
  1087. if (id() == CSS::ValueID::LibwebLink)
  1088. return document.link_color();
  1089. if (!document.page())
  1090. return {};
  1091. auto palette = document.page()->palette();
  1092. switch (id()) {
  1093. case CSS::ValueID::LibwebPaletteDesktopBackground:
  1094. return palette.color(ColorRole::DesktopBackground);
  1095. case CSS::ValueID::LibwebPaletteActiveWindowBorder1:
  1096. return palette.color(ColorRole::ActiveWindowBorder1);
  1097. case CSS::ValueID::LibwebPaletteActiveWindowBorder2:
  1098. return palette.color(ColorRole::ActiveWindowBorder2);
  1099. case CSS::ValueID::LibwebPaletteActiveWindowTitle:
  1100. return palette.color(ColorRole::ActiveWindowTitle);
  1101. case CSS::ValueID::LibwebPaletteInactiveWindowBorder1:
  1102. return palette.color(ColorRole::InactiveWindowBorder1);
  1103. case CSS::ValueID::LibwebPaletteInactiveWindowBorder2:
  1104. return palette.color(ColorRole::InactiveWindowBorder2);
  1105. case CSS::ValueID::LibwebPaletteInactiveWindowTitle:
  1106. return palette.color(ColorRole::InactiveWindowTitle);
  1107. case CSS::ValueID::LibwebPaletteMovingWindowBorder1:
  1108. return palette.color(ColorRole::MovingWindowBorder1);
  1109. case CSS::ValueID::LibwebPaletteMovingWindowBorder2:
  1110. return palette.color(ColorRole::MovingWindowBorder2);
  1111. case CSS::ValueID::LibwebPaletteMovingWindowTitle:
  1112. return palette.color(ColorRole::MovingWindowTitle);
  1113. case CSS::ValueID::LibwebPaletteHighlightWindowBorder1:
  1114. return palette.color(ColorRole::HighlightWindowBorder1);
  1115. case CSS::ValueID::LibwebPaletteHighlightWindowBorder2:
  1116. return palette.color(ColorRole::HighlightWindowBorder2);
  1117. case CSS::ValueID::LibwebPaletteHighlightWindowTitle:
  1118. return palette.color(ColorRole::HighlightWindowTitle);
  1119. case CSS::ValueID::LibwebPaletteMenuStripe:
  1120. return palette.color(ColorRole::MenuStripe);
  1121. case CSS::ValueID::LibwebPaletteMenuBase:
  1122. return palette.color(ColorRole::MenuBase);
  1123. case CSS::ValueID::LibwebPaletteMenuBaseText:
  1124. return palette.color(ColorRole::MenuBaseText);
  1125. case CSS::ValueID::LibwebPaletteMenuSelection:
  1126. return palette.color(ColorRole::MenuSelection);
  1127. case CSS::ValueID::LibwebPaletteMenuSelectionText:
  1128. return palette.color(ColorRole::MenuSelectionText);
  1129. case CSS::ValueID::LibwebPaletteWindow:
  1130. return palette.color(ColorRole::Window);
  1131. case CSS::ValueID::LibwebPaletteWindowText:
  1132. return palette.color(ColorRole::WindowText);
  1133. case CSS::ValueID::LibwebPaletteButton:
  1134. return palette.color(ColorRole::Button);
  1135. case CSS::ValueID::LibwebPaletteButtonText:
  1136. return palette.color(ColorRole::ButtonText);
  1137. case CSS::ValueID::LibwebPaletteBase:
  1138. return palette.color(ColorRole::Base);
  1139. case CSS::ValueID::LibwebPaletteBaseText:
  1140. return palette.color(ColorRole::BaseText);
  1141. case CSS::ValueID::LibwebPaletteThreedHighlight:
  1142. return palette.color(ColorRole::ThreedHighlight);
  1143. case CSS::ValueID::LibwebPaletteThreedShadow1:
  1144. return palette.color(ColorRole::ThreedShadow1);
  1145. case CSS::ValueID::LibwebPaletteThreedShadow2:
  1146. return palette.color(ColorRole::ThreedShadow2);
  1147. case CSS::ValueID::LibwebPaletteHoverHighlight:
  1148. return palette.color(ColorRole::HoverHighlight);
  1149. case CSS::ValueID::LibwebPaletteSelection:
  1150. return palette.color(ColorRole::Selection);
  1151. case CSS::ValueID::LibwebPaletteSelectionText:
  1152. return palette.color(ColorRole::SelectionText);
  1153. case CSS::ValueID::LibwebPaletteInactiveSelection:
  1154. return palette.color(ColorRole::InactiveSelection);
  1155. case CSS::ValueID::LibwebPaletteInactiveSelectionText:
  1156. return palette.color(ColorRole::InactiveSelectionText);
  1157. case CSS::ValueID::LibwebPaletteRubberBandFill:
  1158. return palette.color(ColorRole::RubberBandFill);
  1159. case CSS::ValueID::LibwebPaletteRubberBandBorder:
  1160. return palette.color(ColorRole::RubberBandBorder);
  1161. case CSS::ValueID::LibwebPaletteLink:
  1162. return palette.color(ColorRole::Link);
  1163. case CSS::ValueID::LibwebPaletteActiveLink:
  1164. return palette.color(ColorRole::ActiveLink);
  1165. case CSS::ValueID::LibwebPaletteVisitedLink:
  1166. return palette.color(ColorRole::VisitedLink);
  1167. case CSS::ValueID::LibwebPaletteRuler:
  1168. return palette.color(ColorRole::Ruler);
  1169. case CSS::ValueID::LibwebPaletteRulerBorder:
  1170. return palette.color(ColorRole::RulerBorder);
  1171. case CSS::ValueID::LibwebPaletteRulerActiveText:
  1172. return palette.color(ColorRole::RulerActiveText);
  1173. case CSS::ValueID::LibwebPaletteRulerInactiveText:
  1174. return palette.color(ColorRole::RulerInactiveText);
  1175. case CSS::ValueID::LibwebPaletteTextCursor:
  1176. return palette.color(ColorRole::TextCursor);
  1177. case CSS::ValueID::LibwebPaletteFocusOutline:
  1178. return palette.color(ColorRole::FocusOutline);
  1179. case CSS::ValueID::LibwebPaletteSyntaxComment:
  1180. return palette.color(ColorRole::SyntaxComment);
  1181. case CSS::ValueID::LibwebPaletteSyntaxNumber:
  1182. return palette.color(ColorRole::SyntaxNumber);
  1183. case CSS::ValueID::LibwebPaletteSyntaxString:
  1184. return palette.color(ColorRole::SyntaxString);
  1185. case CSS::ValueID::LibwebPaletteSyntaxType:
  1186. return palette.color(ColorRole::SyntaxType);
  1187. case CSS::ValueID::LibwebPaletteSyntaxPunctuation:
  1188. return palette.color(ColorRole::SyntaxPunctuation);
  1189. case CSS::ValueID::LibwebPaletteSyntaxOperator:
  1190. return palette.color(ColorRole::SyntaxOperator);
  1191. case CSS::ValueID::LibwebPaletteSyntaxKeyword:
  1192. return palette.color(ColorRole::SyntaxKeyword);
  1193. case CSS::ValueID::LibwebPaletteSyntaxControlKeyword:
  1194. return palette.color(ColorRole::SyntaxControlKeyword);
  1195. case CSS::ValueID::LibwebPaletteSyntaxIdentifier:
  1196. return palette.color(ColorRole::SyntaxIdentifier);
  1197. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  1198. return palette.color(ColorRole::SyntaxPreprocessorStatement);
  1199. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorValue:
  1200. return palette.color(ColorRole::SyntaxPreprocessorValue);
  1201. default:
  1202. return {};
  1203. }
  1204. }
  1205. ImageStyleValue::ImageStyleValue(AK::URL const& url)
  1206. : StyleValue(Type::Image)
  1207. , m_url(url)
  1208. {
  1209. }
  1210. void ImageStyleValue::load_bitmap(DOM::Document& document)
  1211. {
  1212. if (m_bitmap)
  1213. return;
  1214. m_document = &document;
  1215. auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
  1216. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  1217. }
  1218. void ImageStyleValue::resource_did_load()
  1219. {
  1220. if (!m_document)
  1221. return;
  1222. m_bitmap = resource()->bitmap();
  1223. // FIXME: Do less than a full repaint if possible?
  1224. if (m_document && m_document->browsing_context())
  1225. m_document->browsing_context()->set_needs_display({});
  1226. }
  1227. String ImageStyleValue::to_string() const
  1228. {
  1229. return serialize_a_url(m_url.to_string());
  1230. }
  1231. bool ImageStyleValue::equals(StyleValue const& other) const
  1232. {
  1233. if (type() != other.type())
  1234. return false;
  1235. return m_url == other.as_image().m_url;
  1236. }
  1237. bool InheritStyleValue::equals(StyleValue const& other) const
  1238. {
  1239. return type() == other.type();
  1240. }
  1241. bool InitialStyleValue::equals(StyleValue const& other) const
  1242. {
  1243. return type() == other.type();
  1244. }
  1245. bool LengthStyleValue::equals(StyleValue const& other) const
  1246. {
  1247. if (type() != other.type())
  1248. return false;
  1249. return m_length == other.as_length().m_length;
  1250. }
  1251. String ListStyleStyleValue::to_string() const
  1252. {
  1253. return String::formatted("{} {} {}", m_position->to_string(), m_image->to_string(), m_style_type->to_string());
  1254. }
  1255. bool ListStyleStyleValue::equals(StyleValue const& other) const
  1256. {
  1257. if (type() != other.type())
  1258. return false;
  1259. auto const& typed_other = other.as_list_style();
  1260. return m_position->equals(typed_other.m_position)
  1261. && m_image->equals(typed_other.m_image)
  1262. && m_style_type->equals(typed_other.m_style_type);
  1263. }
  1264. String NumericStyleValue::to_string() const
  1265. {
  1266. return m_value.visit(
  1267. [](float value) {
  1268. return String::formatted("{}", value);
  1269. },
  1270. [](i64 value) {
  1271. return String::formatted("{}", value);
  1272. });
  1273. }
  1274. bool NumericStyleValue::equals(StyleValue const& other) const
  1275. {
  1276. if (type() != other.type())
  1277. return false;
  1278. if (has_integer() != other.has_integer())
  1279. return false;
  1280. if (has_integer())
  1281. return m_value.get<i64>() == other.as_numeric().m_value.get<i64>();
  1282. return m_value.get<float>() == other.as_numeric().m_value.get<float>();
  1283. }
  1284. String OverflowStyleValue::to_string() const
  1285. {
  1286. return String::formatted("{} {}", m_overflow_x->to_string(), m_overflow_y->to_string());
  1287. }
  1288. bool OverflowStyleValue::equals(StyleValue const& other) const
  1289. {
  1290. if (type() != other.type())
  1291. return false;
  1292. auto const& typed_other = other.as_overflow();
  1293. return m_overflow_x->equals(typed_other.m_overflow_x)
  1294. && m_overflow_y->equals(typed_other.m_overflow_y);
  1295. }
  1296. String PercentageStyleValue::to_string() const
  1297. {
  1298. return m_percentage.to_string();
  1299. }
  1300. bool PercentageStyleValue::equals(StyleValue const& other) const
  1301. {
  1302. if (type() != other.type())
  1303. return false;
  1304. return m_percentage == other.as_percentage().m_percentage;
  1305. }
  1306. String PositionStyleValue::to_string() const
  1307. {
  1308. auto to_string = [](PositionEdge edge) {
  1309. switch (edge) {
  1310. case PositionEdge::Left:
  1311. return "left";
  1312. case PositionEdge::Right:
  1313. return "right";
  1314. case PositionEdge::Top:
  1315. return "top";
  1316. case PositionEdge::Bottom:
  1317. return "bottom";
  1318. }
  1319. VERIFY_NOT_REACHED();
  1320. };
  1321. return String::formatted("{} {} {} {}", to_string(m_edge_x), m_offset_x.to_string(), to_string(m_edge_y), m_offset_y.to_string());
  1322. }
  1323. bool PositionStyleValue::equals(StyleValue const& other) const
  1324. {
  1325. if (type() != other.type())
  1326. return false;
  1327. auto const& typed_other = other.as_position();
  1328. return m_edge_x == typed_other.m_edge_x
  1329. && m_offset_x == typed_other.m_offset_x
  1330. && m_edge_y == typed_other.m_edge_y
  1331. && m_offset_y == typed_other.m_offset_y;
  1332. }
  1333. bool ResolutionStyleValue::equals(StyleValue const& other) const
  1334. {
  1335. if (type() != other.type())
  1336. return false;
  1337. return m_resolution == other.as_resolution().m_resolution;
  1338. }
  1339. String ShadowStyleValue::to_string() const
  1340. {
  1341. StringBuilder builder;
  1342. builder.appendff("{} {} {} {} {}", m_color.to_string(), m_offset_x.to_string(), m_offset_y.to_string(), m_blur_radius.to_string(), m_spread_distance.to_string());
  1343. if (m_placement == ShadowPlacement::Inner)
  1344. builder.append(" inset");
  1345. return builder.to_string();
  1346. }
  1347. bool ShadowStyleValue::equals(StyleValue const& other) const
  1348. {
  1349. if (type() != other.type())
  1350. return false;
  1351. auto const& typed_other = other.as_shadow();
  1352. return m_color == typed_other.m_color
  1353. && m_offset_x == typed_other.m_offset_x
  1354. && m_offset_y == typed_other.m_offset_y
  1355. && m_blur_radius == typed_other.m_blur_radius
  1356. && m_spread_distance == typed_other.m_spread_distance
  1357. && m_placement == typed_other.m_placement;
  1358. }
  1359. bool StringStyleValue::equals(StyleValue const& other) const
  1360. {
  1361. if (type() != other.type())
  1362. return false;
  1363. return m_string == other.as_string().m_string;
  1364. }
  1365. String TextDecorationStyleValue::to_string() const
  1366. {
  1367. return String::formatted("{} {} {} {}", m_line->to_string(), m_thickness->to_string(), m_style->to_string(), m_color->to_string());
  1368. }
  1369. bool TextDecorationStyleValue::equals(StyleValue const& other) const
  1370. {
  1371. if (type() != other.type())
  1372. return false;
  1373. auto const& typed_other = other.as_text_decoration();
  1374. return m_line->equals(typed_other.m_line)
  1375. && m_thickness->equals(typed_other.m_thickness)
  1376. && m_style->equals(typed_other.m_style)
  1377. && m_color->equals(typed_other.m_color);
  1378. }
  1379. bool TimeStyleValue::equals(StyleValue const& other) const
  1380. {
  1381. if (type() != other.type())
  1382. return false;
  1383. return m_time == other.as_time().m_time;
  1384. }
  1385. String TransformationStyleValue::to_string() const
  1386. {
  1387. StringBuilder builder;
  1388. builder.append(CSS::to_string(m_transform_function));
  1389. builder.append('(');
  1390. builder.join(", ", m_values);
  1391. builder.append(')');
  1392. return builder.to_string();
  1393. }
  1394. bool TransformationStyleValue::equals(StyleValue const& other) const
  1395. {
  1396. if (type() != other.type())
  1397. return false;
  1398. auto const& typed_other = other.as_transformation();
  1399. if (m_transform_function != typed_other.m_transform_function)
  1400. return false;
  1401. if (m_values.size() != typed_other.m_values.size())
  1402. return false;
  1403. for (size_t i = 0; i < m_values.size(); ++i) {
  1404. if (!m_values[i].equals(typed_other.m_values[i]))
  1405. return false;
  1406. }
  1407. return true;
  1408. }
  1409. String UnresolvedStyleValue::to_string() const
  1410. {
  1411. StringBuilder builder;
  1412. for (auto& value : m_values)
  1413. builder.append(value.to_string());
  1414. return builder.to_string();
  1415. }
  1416. bool UnresolvedStyleValue::equals(StyleValue const& other) const
  1417. {
  1418. if (type() != other.type())
  1419. return false;
  1420. // This is a case where comparing the strings actually makes sense.
  1421. return to_string() == other.to_string();
  1422. }
  1423. bool UnsetStyleValue::equals(StyleValue const& other) const
  1424. {
  1425. return type() == other.type();
  1426. }
  1427. String StyleValueList::to_string() const
  1428. {
  1429. String separator = "";
  1430. switch (m_separator) {
  1431. case Separator::Space:
  1432. separator = " ";
  1433. break;
  1434. case Separator::Comma:
  1435. separator = ", ";
  1436. break;
  1437. default:
  1438. VERIFY_NOT_REACHED();
  1439. }
  1440. return String::join(separator, m_values);
  1441. }
  1442. bool StyleValueList::equals(StyleValue const& other) const
  1443. {
  1444. if (type() != other.type())
  1445. return false;
  1446. auto const& typed_other = other.as_value_list();
  1447. if (m_separator != typed_other.m_separator)
  1448. return false;
  1449. if (m_values.size() != typed_other.m_values.size())
  1450. return false;
  1451. for (size_t i = 0; i < m_values.size(); ++i) {
  1452. if (!m_values[i].equals(typed_other.m_values[i]))
  1453. return false;
  1454. }
  1455. return true;
  1456. }
  1457. NonnullRefPtr<ColorStyleValue> ColorStyleValue::create(Color color)
  1458. {
  1459. if (color.value() == 0) {
  1460. static auto transparent = adopt_ref(*new ColorStyleValue(color));
  1461. return transparent;
  1462. }
  1463. if (color == Color::from_rgb(0x000000)) {
  1464. static auto black = adopt_ref(*new ColorStyleValue(color));
  1465. return black;
  1466. }
  1467. if (color == Color::from_rgb(0xffffff)) {
  1468. static auto white = adopt_ref(*new ColorStyleValue(color));
  1469. return white;
  1470. }
  1471. return adopt_ref(*new ColorStyleValue(color));
  1472. }
  1473. NonnullRefPtr<LengthStyleValue> LengthStyleValue::create(Length const& length)
  1474. {
  1475. if (length.is_auto()) {
  1476. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_auto()));
  1477. return value;
  1478. }
  1479. if (length.is_px()) {
  1480. if (length.raw_value() == 0) {
  1481. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(0)));
  1482. return value;
  1483. }
  1484. if (length.raw_value() == 1) {
  1485. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(1)));
  1486. return value;
  1487. }
  1488. }
  1489. return adopt_ref(*new LengthStyleValue(length));
  1490. }
  1491. static Optional<CSS::Length> absolutized_length(CSS::Length const& length, Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size)
  1492. {
  1493. if (length.is_px())
  1494. return {};
  1495. if (length.is_absolute() || length.is_relative()) {
  1496. auto px = length.to_px(viewport_rect, font_metrics, font_size, root_font_size);
  1497. return CSS::Length::make_px(px);
  1498. }
  1499. return {};
  1500. }
  1501. NonnullRefPtr<StyleValue> StyleValue::absolutized(Gfx::IntRect const&, Gfx::FontPixelMetrics const&, float, float) const
  1502. {
  1503. return *this;
  1504. }
  1505. NonnullRefPtr<StyleValue> LengthStyleValue::absolutized(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const
  1506. {
  1507. if (auto length = absolutized_length(m_length, viewport_rect, font_metrics, font_size, root_font_size); length.has_value())
  1508. return LengthStyleValue::create(length.release_value());
  1509. return *this;
  1510. }
  1511. NonnullRefPtr<StyleValue> ShadowStyleValue::absolutized(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const
  1512. {
  1513. auto absolutized_offset_x = absolutized_length(m_offset_x, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_offset_x);
  1514. auto absolutized_offset_y = absolutized_length(m_offset_y, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_offset_y);
  1515. auto absolutized_blur_radius = absolutized_length(m_blur_radius, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_blur_radius);
  1516. auto absolutized_spread_distance = absolutized_length(m_spread_distance, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_spread_distance);
  1517. return ShadowStyleValue::create(m_color, absolutized_offset_x, absolutized_offset_y, absolutized_blur_radius, absolutized_spread_distance, m_placement);
  1518. }
  1519. NonnullRefPtr<StyleValue> BorderRadiusStyleValue::absolutized(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const
  1520. {
  1521. if (m_horizontal_radius.is_percentage() && m_vertical_radius.is_percentage())
  1522. return *this;
  1523. auto absolutized_horizontal_radius = m_horizontal_radius;
  1524. auto absolutized_vertical_radius = m_vertical_radius;
  1525. if (!m_horizontal_radius.is_percentage())
  1526. absolutized_horizontal_radius = absolutized_length(m_horizontal_radius.length(), viewport_rect, font_metrics, font_size, root_font_size).value_or(m_horizontal_radius.length());
  1527. if (!m_vertical_radius.is_percentage())
  1528. absolutized_vertical_radius = absolutized_length(m_vertical_radius.length(), viewport_rect, font_metrics, font_size, root_font_size).value_or(m_vertical_radius.length());
  1529. return BorderRadiusStyleValue::create(absolutized_horizontal_radius, absolutized_vertical_radius);
  1530. }
  1531. }