StyleValue.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/NonnullOwnPtr.h>
  10. #include <AK/NonnullOwnPtrVector.h>
  11. #include <AK/NonnullRefPtrVector.h>
  12. #include <AK/RefCounted.h>
  13. #include <AK/RefPtr.h>
  14. #include <AK/String.h>
  15. #include <AK/StringView.h>
  16. #include <AK/URL.h>
  17. #include <AK/Variant.h>
  18. #include <AK/Vector.h>
  19. #include <AK/WeakPtr.h>
  20. #include <LibGfx/Bitmap.h>
  21. #include <LibGfx/Color.h>
  22. #include <LibWeb/CSS/Length.h>
  23. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  24. #include <LibWeb/CSS/PropertyID.h>
  25. #include <LibWeb/CSS/ValueID.h>
  26. #include <LibWeb/Forward.h>
  27. #include <LibWeb/Loader/ImageResource.h>
  28. namespace Web::CSS {
  29. enum class Position {
  30. Static,
  31. Relative,
  32. Absolute,
  33. Fixed,
  34. Sticky,
  35. };
  36. enum class TextAlign {
  37. Left,
  38. Center,
  39. Right,
  40. Justify,
  41. LibwebCenter,
  42. };
  43. enum class TextDecorationLine {
  44. None,
  45. Underline,
  46. Overline,
  47. LineThrough,
  48. Blink,
  49. };
  50. enum class TextTransform {
  51. None,
  52. Capitalize,
  53. Uppercase,
  54. Lowercase,
  55. FullWidth,
  56. FullSizeKana,
  57. };
  58. enum class Display {
  59. None,
  60. Block,
  61. Inline,
  62. InlineBlock,
  63. ListItem,
  64. Table,
  65. TableRow,
  66. TableCell,
  67. TableHeaderGroup,
  68. TableRowGroup,
  69. TableFooterGroup,
  70. TableColumn,
  71. TableColumnGroup,
  72. TableCaption,
  73. Flex,
  74. };
  75. enum class FlexDirection {
  76. Row,
  77. RowReverse,
  78. Column,
  79. ColumnReverse,
  80. };
  81. enum class FlexWrap {
  82. Nowrap,
  83. Wrap,
  84. WrapReverse
  85. };
  86. enum class FlexBasis {
  87. Content,
  88. Length,
  89. Auto,
  90. };
  91. enum class WhiteSpace {
  92. Normal,
  93. Pre,
  94. Nowrap,
  95. PreLine,
  96. PreWrap,
  97. };
  98. enum class Float {
  99. None,
  100. Left,
  101. Right,
  102. };
  103. enum class Clear {
  104. None,
  105. Left,
  106. Right,
  107. Both,
  108. };
  109. enum class Cursor {
  110. Auto,
  111. Default,
  112. None,
  113. ContextMenu,
  114. Help,
  115. Pointer,
  116. Progress,
  117. Wait,
  118. Cell,
  119. Crosshair,
  120. Text,
  121. VerticalText,
  122. Alias,
  123. Copy,
  124. Move,
  125. NoDrop,
  126. NotAllowed,
  127. Grab,
  128. Grabbing,
  129. EResize,
  130. NResize,
  131. NeResize,
  132. NwResize,
  133. SResize,
  134. SeResize,
  135. SwResize,
  136. WResize,
  137. EwResize,
  138. NsResize,
  139. NeswResize,
  140. NwseResize,
  141. ColResize,
  142. RowResize,
  143. AllScroll,
  144. ZoomIn,
  145. ZoomOut,
  146. };
  147. enum class LineStyle {
  148. None,
  149. Hidden,
  150. Dotted,
  151. Dashed,
  152. Solid,
  153. Double,
  154. Groove,
  155. Ridge,
  156. Inset,
  157. Outset,
  158. };
  159. enum class ListStyleType {
  160. None,
  161. Disc,
  162. Circle,
  163. Square,
  164. Decimal,
  165. DecimalLeadingZero,
  166. LowerAlpha,
  167. LowerLatin,
  168. LowerRoman,
  169. UpperAlpha,
  170. UpperLatin,
  171. UpperRoman,
  172. };
  173. enum class Overflow : u8 {
  174. Auto,
  175. Clip,
  176. Hidden,
  177. Scroll,
  178. Visible,
  179. };
  180. enum class Repeat : u8 {
  181. NoRepeat,
  182. Repeat,
  183. Round,
  184. Space,
  185. };
  186. enum class JustifyContent {
  187. FlexStart,
  188. FlexEnd,
  189. Center,
  190. SpaceBetween,
  191. SpaceAround,
  192. };
  193. class StyleValue : public RefCounted<StyleValue> {
  194. public:
  195. virtual ~StyleValue();
  196. enum class Type {
  197. Invalid,
  198. Inherit,
  199. Initial,
  200. String,
  201. Length,
  202. Color,
  203. Identifier,
  204. Image,
  205. CustomProperty,
  206. Numeric,
  207. ValueList,
  208. Calculated,
  209. BoxShadow,
  210. };
  211. Type type() const { return m_type; }
  212. bool is_inherit() const { return type() == Type::Inherit; }
  213. bool is_initial() const { return type() == Type::Initial; }
  214. bool is_color() const { return type() == Type::Color; }
  215. bool is_identifier() const { return type() == Type::Identifier; }
  216. bool is_image() const { return type() == Type::Image; }
  217. bool is_string() const { return type() == Type::String; }
  218. virtual bool is_length() const { return type() == Type::Length; }
  219. bool is_custom_property() const { return type() == Type::CustomProperty; }
  220. bool is_numeric() const { return type() == Type::Numeric; }
  221. bool is_value_list() const { return type() == Type::ValueList; }
  222. bool is_box_shadow() const { return type() == Type::BoxShadow; }
  223. bool is_calculated() const { return type() == Type::Calculated; }
  224. bool is_builtin_or_dynamic() const
  225. {
  226. return is_inherit() || is_initial() || is_custom_property() || is_calculated();
  227. }
  228. virtual String to_string() const = 0;
  229. virtual Length to_length() const { return Length::make_auto(); }
  230. virtual Color to_color(const DOM::Document&) const { return {}; }
  231. CSS::ValueID to_identifier() const;
  232. virtual bool is_auto() const { return false; }
  233. bool operator==(const StyleValue& other) const { return equals(other); }
  234. bool operator!=(const StyleValue& other) const { return !(*this == other); }
  235. virtual bool equals(const StyleValue& other) const
  236. {
  237. if (type() != other.type())
  238. return false;
  239. return to_string() == other.to_string();
  240. }
  241. protected:
  242. explicit StyleValue(Type);
  243. private:
  244. Type m_type { Type::Invalid };
  245. };
  246. // FIXME: Allow for fallback
  247. class CustomStyleValue : public StyleValue {
  248. public:
  249. static NonnullRefPtr<CustomStyleValue> create(const String& custom_property_name)
  250. {
  251. return adopt_ref(*new CustomStyleValue(custom_property_name));
  252. }
  253. String custom_property_name() const { return m_custom_property_name; }
  254. String to_string() const override { return m_custom_property_name; }
  255. private:
  256. explicit CustomStyleValue(const String& custom_property_name)
  257. : StyleValue(Type::CustomProperty)
  258. , m_custom_property_name(custom_property_name)
  259. {
  260. }
  261. String m_custom_property_name {};
  262. };
  263. class NumericStyleValue : public StyleValue {
  264. public:
  265. static NonnullRefPtr<NumericStyleValue> create(float value)
  266. {
  267. return adopt_ref(*new NumericStyleValue(value));
  268. }
  269. virtual bool is_length() const override { return m_value == 0; }
  270. virtual Length to_length() const override { return Length(0, Length::Type::Px); }
  271. float value() const { return m_value; }
  272. String to_string() const override { return String::formatted("{}", m_value); }
  273. private:
  274. explicit NumericStyleValue(float value)
  275. : StyleValue(Type::Numeric)
  276. , m_value(value)
  277. {
  278. }
  279. float m_value { 0 };
  280. };
  281. class StringStyleValue : public StyleValue {
  282. public:
  283. static NonnullRefPtr<StringStyleValue> create(const String& string)
  284. {
  285. return adopt_ref(*new StringStyleValue(string));
  286. }
  287. virtual ~StringStyleValue() override { }
  288. String to_string() const override { return m_string; }
  289. private:
  290. explicit StringStyleValue(const String& string)
  291. : StyleValue(Type::String)
  292. , m_string(string)
  293. {
  294. }
  295. String m_string;
  296. };
  297. class BoxShadowStyleValue : public StyleValue {
  298. public:
  299. static NonnullRefPtr<BoxShadowStyleValue> create(Length const& offset_x, Length const& offset_y, Length const& blur_radius, Color const& color)
  300. {
  301. return adopt_ref(*new BoxShadowStyleValue(offset_x, offset_y, blur_radius, color));
  302. }
  303. virtual ~BoxShadowStyleValue() override { }
  304. Length const& offset_x() const { return m_offset_x; }
  305. Length const& offset_y() const { return m_offset_y; }
  306. Length const& blur_radius() const { return m_blur_radius; }
  307. Color const& color() const { return m_color; }
  308. String to_string() const override { return String::formatted("BoxShadow offset_x: {}, offset_y: {}, blur_radius: {}, color: {}",
  309. m_offset_x.to_string(), m_offset_y.to_string(), m_blur_radius.to_string(), m_color.to_string()); }
  310. private:
  311. explicit BoxShadowStyleValue(Length const& offset_x, Length const& offset_y, Length const& blur_radius, Color const& color)
  312. : StyleValue(Type::BoxShadow)
  313. , m_offset_x(offset_x)
  314. , m_offset_y(offset_y)
  315. , m_blur_radius(blur_radius)
  316. , m_color(color)
  317. {
  318. }
  319. Length m_offset_x;
  320. Length m_offset_y;
  321. Length m_blur_radius;
  322. Color m_color;
  323. };
  324. class LengthStyleValue : public StyleValue {
  325. public:
  326. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  327. {
  328. return adopt_ref(*new LengthStyleValue(length));
  329. }
  330. virtual ~LengthStyleValue() override { }
  331. virtual String to_string() const override { return m_length.to_string(); }
  332. virtual Length to_length() const override { return m_length; }
  333. const Length& length() const { return m_length; }
  334. virtual bool is_auto() const override { return m_length.is_auto(); }
  335. virtual bool equals(const StyleValue& other) const override
  336. {
  337. if (type() != other.type())
  338. return false;
  339. return m_length == static_cast<const LengthStyleValue&>(other).m_length;
  340. }
  341. private:
  342. explicit LengthStyleValue(const Length& length)
  343. : StyleValue(Type::Length)
  344. , m_length(length)
  345. {
  346. }
  347. Length m_length;
  348. };
  349. class CalculatedStyleValue : public StyleValue {
  350. public:
  351. struct CalcSum;
  352. struct CalcSumPartWithOperator;
  353. struct CalcProduct;
  354. struct CalcProductPartWithOperator;
  355. struct CalcNumberSum;
  356. struct CalcNumberSumPartWithOperator;
  357. struct CalcNumberProduct;
  358. struct CalcNumberProductPartWithOperator;
  359. using CalcNumberValue = Variant<float, NonnullOwnPtr<CalcNumberSum>>;
  360. using CalcValue = Variant<float, CSS::Length, NonnullOwnPtr<CalcSum>>;
  361. // This represents that: https://drafts.csswg.org/css-values-3/#calc-syntax
  362. struct CalcSum {
  363. CalcSum(NonnullOwnPtr<CalcProduct> first_calc_product, NonnullOwnPtrVector<CalcSumPartWithOperator> additional)
  364. : first_calc_product(move(first_calc_product))
  365. , zero_or_more_additional_calc_products(move(additional)) {};
  366. NonnullOwnPtr<CalcProduct> first_calc_product;
  367. NonnullOwnPtrVector<CalcSumPartWithOperator> zero_or_more_additional_calc_products;
  368. };
  369. struct CalcNumberSum {
  370. CalcNumberSum(NonnullOwnPtr<CalcNumberProduct> first_calc_number_product, NonnullOwnPtrVector<CalcNumberSumPartWithOperator> additional)
  371. : first_calc_number_product(move(first_calc_number_product))
  372. , zero_or_more_additional_calc_number_products(move(additional)) {};
  373. NonnullOwnPtr<CalcNumberProduct> first_calc_number_product;
  374. NonnullOwnPtrVector<CalcNumberSumPartWithOperator> zero_or_more_additional_calc_number_products;
  375. };
  376. struct CalcProduct {
  377. CalcValue first_calc_value;
  378. NonnullOwnPtrVector<CalcProductPartWithOperator> zero_or_more_additional_calc_values;
  379. };
  380. struct CalcSumPartWithOperator {
  381. enum Operation {
  382. Add,
  383. Subtract,
  384. };
  385. CalcSumPartWithOperator(Operation op, NonnullOwnPtr<CalcProduct> calc_product)
  386. : op(op)
  387. , calc_product(move(calc_product)) {};
  388. Operation op;
  389. NonnullOwnPtr<CalcProduct> calc_product;
  390. };
  391. struct CalcProductPartWithOperator {
  392. enum {
  393. Multiply,
  394. Divide,
  395. } op;
  396. Variant<CalcValue, CalcNumberValue> value;
  397. };
  398. struct CalcNumberProduct {
  399. CalcNumberValue first_calc_number_value;
  400. NonnullOwnPtrVector<CalcNumberProductPartWithOperator> zero_or_more_additional_calc_number_values;
  401. };
  402. struct CalcNumberProductPartWithOperator {
  403. enum {
  404. Multiply,
  405. Divide,
  406. } op;
  407. CalcNumberValue value;
  408. };
  409. struct CalcNumberSumPartWithOperator {
  410. enum Operation {
  411. Add,
  412. Subtract,
  413. };
  414. CalcNumberSumPartWithOperator(Operation op, NonnullOwnPtr<CalcNumberProduct> calc_number_product)
  415. : op(op)
  416. , calc_number_product(move(calc_number_product)) {};
  417. Operation op;
  418. NonnullOwnPtr<CalcNumberProduct> calc_number_product;
  419. };
  420. static NonnullRefPtr<CalculatedStyleValue> create(String const& expression_string, NonnullOwnPtr<CalcSum> calc_sum)
  421. {
  422. return adopt_ref(*new CalculatedStyleValue(expression_string, move(calc_sum)));
  423. }
  424. String to_string() const override { return m_expression_string; }
  425. NonnullOwnPtr<CalcSum> const& expression() const { return m_expression; }
  426. private:
  427. explicit CalculatedStyleValue(String const& expression_string, NonnullOwnPtr<CalcSum> calc_sum)
  428. : StyleValue(Type::Calculated)
  429. , m_expression_string(expression_string)
  430. , m_expression(move(calc_sum))
  431. {
  432. }
  433. String m_expression_string;
  434. NonnullOwnPtr<CalcSum> m_expression;
  435. };
  436. class InitialStyleValue final : public StyleValue {
  437. public:
  438. static NonnullRefPtr<InitialStyleValue> create() { return adopt_ref(*new InitialStyleValue); }
  439. virtual ~InitialStyleValue() override { }
  440. String to_string() const override { return "initial"; }
  441. private:
  442. InitialStyleValue()
  443. : StyleValue(Type::Initial)
  444. {
  445. }
  446. };
  447. class InheritStyleValue final : public StyleValue {
  448. public:
  449. static NonnullRefPtr<InheritStyleValue> create() { return adopt_ref(*new InheritStyleValue); }
  450. virtual ~InheritStyleValue() override { }
  451. String to_string() const override { return "inherit"; }
  452. private:
  453. InheritStyleValue()
  454. : StyleValue(Type::Inherit)
  455. {
  456. }
  457. };
  458. class ColorStyleValue : public StyleValue {
  459. public:
  460. static NonnullRefPtr<ColorStyleValue> create(Color color)
  461. {
  462. return adopt_ref(*new ColorStyleValue(color));
  463. }
  464. virtual ~ColorStyleValue() override { }
  465. Color color() const { return m_color; }
  466. String to_string() const override { return m_color.to_string(); }
  467. Color to_color(const DOM::Document&) const override { return m_color; }
  468. virtual bool equals(const StyleValue& other) const override
  469. {
  470. if (type() != other.type())
  471. return false;
  472. return m_color == static_cast<const ColorStyleValue&>(other).m_color;
  473. }
  474. private:
  475. explicit ColorStyleValue(Color color)
  476. : StyleValue(Type::Color)
  477. , m_color(color)
  478. {
  479. }
  480. Color m_color;
  481. };
  482. class IdentifierStyleValue final : public StyleValue {
  483. public:
  484. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  485. {
  486. return adopt_ref(*new IdentifierStyleValue(id));
  487. }
  488. virtual ~IdentifierStyleValue() override { }
  489. CSS::ValueID id() const { return m_id; }
  490. virtual String to_string() const override;
  491. virtual Color to_color(const DOM::Document&) const override;
  492. virtual bool equals(const StyleValue& other) const override
  493. {
  494. if (type() != other.type())
  495. return false;
  496. return m_id == static_cast<const IdentifierStyleValue&>(other).m_id;
  497. }
  498. private:
  499. explicit IdentifierStyleValue(CSS::ValueID id)
  500. : StyleValue(Type::Identifier)
  501. , m_id(id)
  502. {
  503. }
  504. CSS::ValueID m_id { CSS::ValueID::Invalid };
  505. };
  506. class ImageStyleValue final
  507. : public StyleValue
  508. , public ImageResourceClient {
  509. public:
  510. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt_ref(*new ImageStyleValue(url, document)); }
  511. virtual ~ImageStyleValue() override { }
  512. String to_string() const override { return String::formatted("Image({})", m_url.to_string()); }
  513. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  514. private:
  515. ImageStyleValue(const URL&, DOM::Document&);
  516. // ^ResourceClient
  517. virtual void resource_did_load() override;
  518. URL m_url;
  519. WeakPtr<DOM::Document> m_document;
  520. RefPtr<Gfx::Bitmap> m_bitmap;
  521. };
  522. class ValueListStyleValue final : public StyleValue {
  523. public:
  524. static NonnullRefPtr<ValueListStyleValue> create(Vector<StyleComponentValueRule>&& values) { return adopt_ref(*new ValueListStyleValue(move(values))); }
  525. virtual ~ValueListStyleValue() override { }
  526. virtual String to_string() const override;
  527. Vector<StyleComponentValueRule> const& values() const { return m_values; }
  528. private:
  529. ValueListStyleValue(Vector<StyleComponentValueRule>&&);
  530. Vector<StyleComponentValueRule> m_values;
  531. };
  532. inline CSS::ValueID StyleValue::to_identifier() const
  533. {
  534. if (is_identifier())
  535. return static_cast<const IdentifierStyleValue&>(*this).id();
  536. return CSS::ValueID::Invalid;
  537. }
  538. }