StyleValue.cpp 55 KB

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