StyleValue.cpp 55 KB

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