StyleValue.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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. VERIFY(document.page());
  979. auto palette = document.page()->palette();
  980. switch (id()) {
  981. case CSS::ValueID::LibwebPaletteDesktopBackground:
  982. return palette.color(ColorRole::DesktopBackground);
  983. case CSS::ValueID::LibwebPaletteActiveWindowBorder1:
  984. return palette.color(ColorRole::ActiveWindowBorder1);
  985. case CSS::ValueID::LibwebPaletteActiveWindowBorder2:
  986. return palette.color(ColorRole::ActiveWindowBorder2);
  987. case CSS::ValueID::LibwebPaletteActiveWindowTitle:
  988. return palette.color(ColorRole::ActiveWindowTitle);
  989. case CSS::ValueID::LibwebPaletteInactiveWindowBorder1:
  990. return palette.color(ColorRole::InactiveWindowBorder1);
  991. case CSS::ValueID::LibwebPaletteInactiveWindowBorder2:
  992. return palette.color(ColorRole::InactiveWindowBorder2);
  993. case CSS::ValueID::LibwebPaletteInactiveWindowTitle:
  994. return palette.color(ColorRole::InactiveWindowTitle);
  995. case CSS::ValueID::LibwebPaletteMovingWindowBorder1:
  996. return palette.color(ColorRole::MovingWindowBorder1);
  997. case CSS::ValueID::LibwebPaletteMovingWindowBorder2:
  998. return palette.color(ColorRole::MovingWindowBorder2);
  999. case CSS::ValueID::LibwebPaletteMovingWindowTitle:
  1000. return palette.color(ColorRole::MovingWindowTitle);
  1001. case CSS::ValueID::LibwebPaletteHighlightWindowBorder1:
  1002. return palette.color(ColorRole::HighlightWindowBorder1);
  1003. case CSS::ValueID::LibwebPaletteHighlightWindowBorder2:
  1004. return palette.color(ColorRole::HighlightWindowBorder2);
  1005. case CSS::ValueID::LibwebPaletteHighlightWindowTitle:
  1006. return palette.color(ColorRole::HighlightWindowTitle);
  1007. case CSS::ValueID::LibwebPaletteMenuStripe:
  1008. return palette.color(ColorRole::MenuStripe);
  1009. case CSS::ValueID::LibwebPaletteMenuBase:
  1010. return palette.color(ColorRole::MenuBase);
  1011. case CSS::ValueID::LibwebPaletteMenuBaseText:
  1012. return palette.color(ColorRole::MenuBaseText);
  1013. case CSS::ValueID::LibwebPaletteMenuSelection:
  1014. return palette.color(ColorRole::MenuSelection);
  1015. case CSS::ValueID::LibwebPaletteMenuSelectionText:
  1016. return palette.color(ColorRole::MenuSelectionText);
  1017. case CSS::ValueID::LibwebPaletteWindow:
  1018. return palette.color(ColorRole::Window);
  1019. case CSS::ValueID::LibwebPaletteWindowText:
  1020. return palette.color(ColorRole::WindowText);
  1021. case CSS::ValueID::LibwebPaletteButton:
  1022. return palette.color(ColorRole::Button);
  1023. case CSS::ValueID::LibwebPaletteButtonText:
  1024. return palette.color(ColorRole::ButtonText);
  1025. case CSS::ValueID::LibwebPaletteBase:
  1026. return palette.color(ColorRole::Base);
  1027. case CSS::ValueID::LibwebPaletteBaseText:
  1028. return palette.color(ColorRole::BaseText);
  1029. case CSS::ValueID::LibwebPaletteThreedHighlight:
  1030. return palette.color(ColorRole::ThreedHighlight);
  1031. case CSS::ValueID::LibwebPaletteThreedShadow1:
  1032. return palette.color(ColorRole::ThreedShadow1);
  1033. case CSS::ValueID::LibwebPaletteThreedShadow2:
  1034. return palette.color(ColorRole::ThreedShadow2);
  1035. case CSS::ValueID::LibwebPaletteHoverHighlight:
  1036. return palette.color(ColorRole::HoverHighlight);
  1037. case CSS::ValueID::LibwebPaletteSelection:
  1038. return palette.color(ColorRole::Selection);
  1039. case CSS::ValueID::LibwebPaletteSelectionText:
  1040. return palette.color(ColorRole::SelectionText);
  1041. case CSS::ValueID::LibwebPaletteInactiveSelection:
  1042. return palette.color(ColorRole::InactiveSelection);
  1043. case CSS::ValueID::LibwebPaletteInactiveSelectionText:
  1044. return palette.color(ColorRole::InactiveSelectionText);
  1045. case CSS::ValueID::LibwebPaletteRubberBandFill:
  1046. return palette.color(ColorRole::RubberBandFill);
  1047. case CSS::ValueID::LibwebPaletteRubberBandBorder:
  1048. return palette.color(ColorRole::RubberBandBorder);
  1049. case CSS::ValueID::LibwebPaletteLink:
  1050. return palette.color(ColorRole::Link);
  1051. case CSS::ValueID::LibwebPaletteActiveLink:
  1052. return palette.color(ColorRole::ActiveLink);
  1053. case CSS::ValueID::LibwebPaletteVisitedLink:
  1054. return palette.color(ColorRole::VisitedLink);
  1055. case CSS::ValueID::LibwebPaletteRuler:
  1056. return palette.color(ColorRole::Ruler);
  1057. case CSS::ValueID::LibwebPaletteRulerBorder:
  1058. return palette.color(ColorRole::RulerBorder);
  1059. case CSS::ValueID::LibwebPaletteRulerActiveText:
  1060. return palette.color(ColorRole::RulerActiveText);
  1061. case CSS::ValueID::LibwebPaletteRulerInactiveText:
  1062. return palette.color(ColorRole::RulerInactiveText);
  1063. case CSS::ValueID::LibwebPaletteTextCursor:
  1064. return palette.color(ColorRole::TextCursor);
  1065. case CSS::ValueID::LibwebPaletteFocusOutline:
  1066. return palette.color(ColorRole::FocusOutline);
  1067. case CSS::ValueID::LibwebPaletteSyntaxComment:
  1068. return palette.color(ColorRole::SyntaxComment);
  1069. case CSS::ValueID::LibwebPaletteSyntaxNumber:
  1070. return palette.color(ColorRole::SyntaxNumber);
  1071. case CSS::ValueID::LibwebPaletteSyntaxString:
  1072. return palette.color(ColorRole::SyntaxString);
  1073. case CSS::ValueID::LibwebPaletteSyntaxType:
  1074. return palette.color(ColorRole::SyntaxType);
  1075. case CSS::ValueID::LibwebPaletteSyntaxPunctuation:
  1076. return palette.color(ColorRole::SyntaxPunctuation);
  1077. case CSS::ValueID::LibwebPaletteSyntaxOperator:
  1078. return palette.color(ColorRole::SyntaxOperator);
  1079. case CSS::ValueID::LibwebPaletteSyntaxKeyword:
  1080. return palette.color(ColorRole::SyntaxKeyword);
  1081. case CSS::ValueID::LibwebPaletteSyntaxControlKeyword:
  1082. return palette.color(ColorRole::SyntaxControlKeyword);
  1083. case CSS::ValueID::LibwebPaletteSyntaxIdentifier:
  1084. return palette.color(ColorRole::SyntaxIdentifier);
  1085. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  1086. return palette.color(ColorRole::SyntaxPreprocessorStatement);
  1087. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorValue:
  1088. return palette.color(ColorRole::SyntaxPreprocessorValue);
  1089. default:
  1090. return {};
  1091. }
  1092. }
  1093. ImageStyleValue::ImageStyleValue(AK::URL const& url)
  1094. : StyleValue(Type::Image)
  1095. , m_url(url)
  1096. {
  1097. }
  1098. void ImageStyleValue::load_bitmap(DOM::Document& document)
  1099. {
  1100. if (m_bitmap)
  1101. return;
  1102. m_document = &document;
  1103. auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
  1104. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  1105. }
  1106. void ImageStyleValue::resource_did_load()
  1107. {
  1108. if (!m_document)
  1109. return;
  1110. m_bitmap = resource()->bitmap();
  1111. // FIXME: Do less than a full repaint if possible?
  1112. if (m_document && m_document->browsing_context())
  1113. m_document->browsing_context()->set_needs_display({});
  1114. }
  1115. String ImageStyleValue::to_string() const
  1116. {
  1117. return serialize_a_url(m_url.to_string());
  1118. }
  1119. String ListStyleStyleValue::to_string() const
  1120. {
  1121. return String::formatted("{} {} {}", m_position->to_string(), m_image->to_string(), m_style_type->to_string());
  1122. }
  1123. String NumericStyleValue::to_string() const
  1124. {
  1125. return m_value.visit(
  1126. [](float value) {
  1127. return String::formatted("{}", value);
  1128. },
  1129. [](i64 value) {
  1130. return String::formatted("{}", value);
  1131. });
  1132. }
  1133. String OverflowStyleValue::to_string() const
  1134. {
  1135. return String::formatted("{} {}", m_overflow_x->to_string(), m_overflow_y->to_string());
  1136. }
  1137. String PercentageStyleValue::to_string() const
  1138. {
  1139. return m_percentage.to_string();
  1140. }
  1141. String PositionStyleValue::to_string() const
  1142. {
  1143. auto to_string = [](PositionEdge edge) {
  1144. switch (edge) {
  1145. case PositionEdge::Left:
  1146. return "left";
  1147. case PositionEdge::Right:
  1148. return "right";
  1149. case PositionEdge::Top:
  1150. return "top";
  1151. case PositionEdge::Bottom:
  1152. return "bottom";
  1153. }
  1154. VERIFY_NOT_REACHED();
  1155. };
  1156. return String::formatted("{} {} {} {}", to_string(m_edge_x), m_offset_x.to_string(), to_string(m_edge_y), m_offset_y.to_string());
  1157. }
  1158. String TextDecorationStyleValue::to_string() const
  1159. {
  1160. return String::formatted("{} {} {} {}", m_line->to_string(), m_thickness->to_string(), m_style->to_string(), m_color->to_string());
  1161. }
  1162. String TransformationStyleValue::to_string() const
  1163. {
  1164. StringBuilder builder;
  1165. switch (m_transform_function) {
  1166. case TransformFunction::TranslateY:
  1167. builder.append("translateY");
  1168. break;
  1169. default:
  1170. VERIFY_NOT_REACHED();
  1171. }
  1172. builder.append('(');
  1173. builder.join(", ", m_values);
  1174. builder.append(')');
  1175. return builder.to_string();
  1176. }
  1177. String UnresolvedStyleValue::to_string() const
  1178. {
  1179. StringBuilder builder;
  1180. for (auto& value : m_values)
  1181. builder.append(value.to_string());
  1182. return builder.to_string();
  1183. }
  1184. String StyleValueList::to_string() const
  1185. {
  1186. String separator = "";
  1187. switch (m_separator) {
  1188. case Separator::Space:
  1189. separator = " ";
  1190. break;
  1191. case Separator::Comma:
  1192. separator = ", ";
  1193. break;
  1194. default:
  1195. VERIFY_NOT_REACHED();
  1196. }
  1197. return String::join(separator, m_values);
  1198. }
  1199. NonnullRefPtr<ColorStyleValue> ColorStyleValue::create(Color color)
  1200. {
  1201. if (color.value() == 0) {
  1202. static auto transparent = adopt_ref(*new ColorStyleValue(color));
  1203. return transparent;
  1204. }
  1205. if (color == Color::from_rgb(0x000000)) {
  1206. static auto black = adopt_ref(*new ColorStyleValue(color));
  1207. return black;
  1208. }
  1209. if (color == Color::from_rgb(0xffffff)) {
  1210. static auto white = adopt_ref(*new ColorStyleValue(color));
  1211. return white;
  1212. }
  1213. return adopt_ref(*new ColorStyleValue(color));
  1214. }
  1215. NonnullRefPtr<LengthStyleValue> LengthStyleValue::create(Length const& length)
  1216. {
  1217. if (length.is_auto()) {
  1218. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_auto()));
  1219. return value;
  1220. }
  1221. if (length.is_px()) {
  1222. if (length.raw_value() == 0) {
  1223. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(0)));
  1224. return value;
  1225. }
  1226. if (length.raw_value() == 1) {
  1227. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(1)));
  1228. return value;
  1229. }
  1230. }
  1231. return adopt_ref(*new LengthStyleValue(length));
  1232. }
  1233. 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)
  1234. {
  1235. if (length.is_px())
  1236. return {};
  1237. if (length.is_absolute() || length.is_relative()) {
  1238. auto px = length.to_px(viewport_rect, font_metrics, font_size, root_font_size);
  1239. return CSS::Length::make_px(px);
  1240. }
  1241. return {};
  1242. }
  1243. NonnullRefPtr<StyleValue> StyleValue::absolutized(Gfx::IntRect const&, Gfx::FontMetrics const&, float, float) const
  1244. {
  1245. return *this;
  1246. }
  1247. NonnullRefPtr<StyleValue> LengthStyleValue::absolutized(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float font_size, float root_font_size) const
  1248. {
  1249. if (auto length = absolutized_length(m_length, viewport_rect, font_metrics, font_size, root_font_size); length.has_value())
  1250. return LengthStyleValue::create(length.release_value());
  1251. return *this;
  1252. }
  1253. NonnullRefPtr<StyleValue> BoxShadowStyleValue::absolutized(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float font_size, float root_font_size) const
  1254. {
  1255. auto absolutized_offset_x = absolutized_length(m_offset_x, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_offset_x);
  1256. auto absolutized_offset_y = absolutized_length(m_offset_y, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_offset_y);
  1257. auto absolutized_blur_radius = absolutized_length(m_blur_radius, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_blur_radius);
  1258. auto absolutized_spread_distance = absolutized_length(m_spread_distance, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_spread_distance);
  1259. return BoxShadowStyleValue::create(m_color, absolutized_offset_x, absolutized_offset_y, absolutized_blur_radius, absolutized_spread_distance, m_placement);
  1260. }
  1261. NonnullRefPtr<StyleValue> BorderRadiusStyleValue::absolutized(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float font_size, float root_font_size) const
  1262. {
  1263. if (m_horizontal_radius.is_percentage() && m_vertical_radius.is_percentage())
  1264. return *this;
  1265. auto absolutized_horizontal_radius = m_horizontal_radius;
  1266. auto absolutized_vertical_radius = m_vertical_radius;
  1267. if (!m_horizontal_radius.is_percentage())
  1268. absolutized_horizontal_radius = absolutized_length(m_horizontal_radius.length(), viewport_rect, font_metrics, font_size, root_font_size).value_or(m_horizontal_radius.length());
  1269. if (!m_vertical_radius.is_percentage())
  1270. absolutized_vertical_radius = absolutized_length(m_vertical_radius.length(), viewport_rect, font_metrics, font_size, root_font_size).value_or(m_vertical_radius.length());
  1271. return BorderRadiusStyleValue::create(absolutized_horizontal_radius, absolutized_vertical_radius);
  1272. }
  1273. }