StyleValue.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobi@tobyase.de>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/RefCounted.h>
  9. #include <AK/RefPtr.h>
  10. #include <AK/String.h>
  11. #include <AK/StringView.h>
  12. #include <AK/URL.h>
  13. #include <AK/WeakPtr.h>
  14. #include <LibGfx/Bitmap.h>
  15. #include <LibGfx/Color.h>
  16. #include <LibWeb/CSS/Length.h>
  17. #include <LibWeb/CSS/PropertyID.h>
  18. #include <LibWeb/CSS/ValueID.h>
  19. #include <LibWeb/Forward.h>
  20. #include <LibWeb/Loader/ImageResource.h>
  21. namespace Web::CSS {
  22. enum class Position {
  23. Static,
  24. Relative,
  25. Absolute,
  26. Fixed,
  27. Sticky,
  28. };
  29. enum class TextAlign {
  30. Left,
  31. Center,
  32. Right,
  33. Justify,
  34. LibwebCenter,
  35. };
  36. enum class TextDecorationLine {
  37. None,
  38. Underline,
  39. Overline,
  40. LineThrough,
  41. Blink,
  42. };
  43. enum class TextTransform {
  44. None,
  45. Capitalize,
  46. Uppercase,
  47. Lowercase,
  48. FullWidth,
  49. FullSizeKana,
  50. };
  51. enum class Display {
  52. None,
  53. Block,
  54. Inline,
  55. InlineBlock,
  56. ListItem,
  57. Table,
  58. TableRow,
  59. TableCell,
  60. TableHeaderGroup,
  61. TableRowGroup,
  62. TableFooterGroup,
  63. TableColumn,
  64. TableColumnGroup,
  65. TableCaption,
  66. Flex,
  67. };
  68. enum class FlexDirection {
  69. Row,
  70. RowReverse,
  71. Column,
  72. ColumnReverse,
  73. };
  74. enum class FlexWrap {
  75. Nowrap,
  76. Wrap,
  77. WrapReverse
  78. };
  79. enum class FlexBasis {
  80. Content,
  81. Length
  82. };
  83. enum class WhiteSpace {
  84. Normal,
  85. Pre,
  86. Nowrap,
  87. PreLine,
  88. PreWrap,
  89. };
  90. enum class Float {
  91. None,
  92. Left,
  93. Right,
  94. };
  95. enum class Clear {
  96. None,
  97. Left,
  98. Right,
  99. Both,
  100. };
  101. enum class Cursor {
  102. Auto,
  103. Default,
  104. None,
  105. ContextMenu,
  106. Help,
  107. Pointer,
  108. Progress,
  109. Wait,
  110. Cell,
  111. Crosshair,
  112. Text,
  113. VerticalText,
  114. Alias,
  115. Copy,
  116. Move,
  117. NoDrop,
  118. NotAllowed,
  119. Grab,
  120. Grabbing,
  121. EResize,
  122. NResize,
  123. NeResize,
  124. NwResize,
  125. SResize,
  126. SeResize,
  127. SwResize,
  128. WResize,
  129. EwResize,
  130. NsResize,
  131. NeswResize,
  132. NwseResize,
  133. ColResize,
  134. RowResize,
  135. AllScroll,
  136. ZoomIn,
  137. ZoomOut,
  138. };
  139. enum class LineStyle {
  140. None,
  141. Hidden,
  142. Dotted,
  143. Dashed,
  144. Solid,
  145. Double,
  146. Groove,
  147. Ridge,
  148. Inset,
  149. Outset,
  150. };
  151. enum class ListStyleType {
  152. None,
  153. Disc,
  154. Circle,
  155. Square,
  156. Decimal,
  157. DecimalLeadingZero,
  158. LowerAlpha,
  159. LowerLatin,
  160. LowerRoman,
  161. UpperAlpha,
  162. UpperLatin,
  163. UpperRoman,
  164. };
  165. enum class Overflow : u8 {
  166. Auto,
  167. Clip,
  168. Hidden,
  169. Scroll,
  170. Visible,
  171. };
  172. enum class Repeat : u8 {
  173. NoRepeat,
  174. Repeat,
  175. Round,
  176. Space,
  177. };
  178. enum class JustifyContent {
  179. FlexStart,
  180. FlexEnd,
  181. Center,
  182. SpaceBetween,
  183. SpaceAround,
  184. };
  185. class StyleValue : public RefCounted<StyleValue> {
  186. public:
  187. virtual ~StyleValue();
  188. enum class Type {
  189. Invalid,
  190. Inherit,
  191. Initial,
  192. String,
  193. Length,
  194. Color,
  195. Identifier,
  196. Image,
  197. Position,
  198. CustomProperty,
  199. Numeric,
  200. };
  201. Type type() const { return m_type; }
  202. bool is_inherit() const { return type() == Type::Inherit; }
  203. bool is_initial() const { return type() == Type::Initial; }
  204. bool is_color() const { return type() == Type::Color; }
  205. bool is_identifier() const { return type() == Type::Identifier; }
  206. bool is_image() const { return type() == Type::Image; }
  207. bool is_string() const { return type() == Type::String; }
  208. bool is_length() const { return type() == Type::Length; }
  209. bool is_position() const { return type() == Type::Position; }
  210. bool is_custom_property() const { return type() == Type::CustomProperty; }
  211. bool is_numeric() const { return type() == Type::Numeric; }
  212. virtual String to_string() const = 0;
  213. virtual Length to_length() const { return Length::make_auto(); }
  214. virtual Color to_color(const DOM::Document&) const { return {}; }
  215. CSS::ValueID to_identifier() const;
  216. virtual bool is_auto() const { return false; }
  217. bool operator==(const StyleValue& other) const { return equals(other); }
  218. bool operator!=(const StyleValue& other) const { return !(*this == other); }
  219. virtual bool equals(const StyleValue& other) const
  220. {
  221. if (type() != other.type())
  222. return false;
  223. return to_string() == other.to_string();
  224. }
  225. protected:
  226. explicit StyleValue(Type);
  227. private:
  228. Type m_type { Type::Invalid };
  229. };
  230. // FIXME: Allow for fallback
  231. class CustomStyleValue : public StyleValue {
  232. public:
  233. static NonnullRefPtr<CustomStyleValue> create(const String& custom_property_name)
  234. {
  235. return adopt_ref(*new CustomStyleValue(custom_property_name));
  236. }
  237. String custom_property_name() const { return m_custom_property_name; }
  238. String to_string() const override { return m_custom_property_name; }
  239. private:
  240. explicit CustomStyleValue(const String& custom_property_name)
  241. : StyleValue(Type::CustomProperty)
  242. , m_custom_property_name(custom_property_name)
  243. {
  244. }
  245. String m_custom_property_name {};
  246. };
  247. class NumericStyleValue : public StyleValue {
  248. public:
  249. static NonnullRefPtr<NumericStyleValue> create(float value)
  250. {
  251. return adopt_ref(*new NumericStyleValue(value));
  252. }
  253. float value() const { return m_value; }
  254. String to_string() const override { return String::formatted("{}", m_value); }
  255. private:
  256. explicit NumericStyleValue(float value)
  257. : StyleValue(Type::Numeric)
  258. , m_value(value)
  259. {
  260. }
  261. float m_value { 0 };
  262. };
  263. class StringStyleValue : public StyleValue {
  264. public:
  265. static NonnullRefPtr<StringStyleValue> create(const String& string)
  266. {
  267. return adopt_ref(*new StringStyleValue(string));
  268. }
  269. virtual ~StringStyleValue() override { }
  270. String to_string() const override { return m_string; }
  271. private:
  272. explicit StringStyleValue(const String& string)
  273. : StyleValue(Type::String)
  274. , m_string(string)
  275. {
  276. }
  277. String m_string;
  278. };
  279. class LengthStyleValue : public StyleValue {
  280. public:
  281. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  282. {
  283. return adopt_ref(*new LengthStyleValue(length));
  284. }
  285. virtual ~LengthStyleValue() override { }
  286. virtual String to_string() const override { return m_length.to_string(); }
  287. virtual Length to_length() const override { return m_length; }
  288. const Length& length() const { return m_length; }
  289. virtual bool is_auto() const override { return m_length.is_auto(); }
  290. virtual bool equals(const StyleValue& other) const override
  291. {
  292. if (type() != other.type())
  293. return false;
  294. return m_length == static_cast<const LengthStyleValue&>(other).m_length;
  295. }
  296. private:
  297. explicit LengthStyleValue(const Length& length)
  298. : StyleValue(Type::Length)
  299. , m_length(length)
  300. {
  301. }
  302. Length m_length;
  303. };
  304. class InitialStyleValue final : public StyleValue {
  305. public:
  306. static NonnullRefPtr<InitialStyleValue> create() { return adopt_ref(*new InitialStyleValue); }
  307. virtual ~InitialStyleValue() override { }
  308. String to_string() const override { return "initial"; }
  309. private:
  310. InitialStyleValue()
  311. : StyleValue(Type::Initial)
  312. {
  313. }
  314. };
  315. class InheritStyleValue final : public StyleValue {
  316. public:
  317. static NonnullRefPtr<InheritStyleValue> create() { return adopt_ref(*new InheritStyleValue); }
  318. virtual ~InheritStyleValue() override { }
  319. String to_string() const override { return "inherit"; }
  320. private:
  321. InheritStyleValue()
  322. : StyleValue(Type::Inherit)
  323. {
  324. }
  325. };
  326. class ColorStyleValue : public StyleValue {
  327. public:
  328. static NonnullRefPtr<ColorStyleValue> create(Color color)
  329. {
  330. return adopt_ref(*new ColorStyleValue(color));
  331. }
  332. virtual ~ColorStyleValue() override { }
  333. Color color() const { return m_color; }
  334. String to_string() const override { return m_color.to_string(); }
  335. Color to_color(const DOM::Document&) const override { return m_color; }
  336. virtual bool equals(const StyleValue& other) const override
  337. {
  338. if (type() != other.type())
  339. return false;
  340. return m_color == static_cast<const ColorStyleValue&>(other).m_color;
  341. }
  342. private:
  343. explicit ColorStyleValue(Color color)
  344. : StyleValue(Type::Color)
  345. , m_color(color)
  346. {
  347. }
  348. Color m_color;
  349. };
  350. class IdentifierStyleValue final : public StyleValue {
  351. public:
  352. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  353. {
  354. return adopt_ref(*new IdentifierStyleValue(id));
  355. }
  356. virtual ~IdentifierStyleValue() override { }
  357. CSS::ValueID id() const { return m_id; }
  358. virtual String to_string() const override;
  359. virtual Color to_color(const DOM::Document&) const override;
  360. virtual bool equals(const StyleValue& other) const override
  361. {
  362. if (type() != other.type())
  363. return false;
  364. return m_id == static_cast<const IdentifierStyleValue&>(other).m_id;
  365. }
  366. private:
  367. explicit IdentifierStyleValue(CSS::ValueID id)
  368. : StyleValue(Type::Identifier)
  369. , m_id(id)
  370. {
  371. }
  372. CSS::ValueID m_id { CSS::ValueID::Invalid };
  373. };
  374. class ImageStyleValue final
  375. : public StyleValue
  376. , public ImageResourceClient {
  377. public:
  378. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt_ref(*new ImageStyleValue(url, document)); }
  379. virtual ~ImageStyleValue() override { }
  380. String to_string() const override { return String::formatted("Image({})", m_url.to_string()); }
  381. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  382. private:
  383. ImageStyleValue(const URL&, DOM::Document&);
  384. // ^ResourceClient
  385. virtual void resource_did_load() override;
  386. URL m_url;
  387. WeakPtr<DOM::Document> m_document;
  388. RefPtr<Gfx::Bitmap> m_bitmap;
  389. };
  390. inline CSS::ValueID StyleValue::to_identifier() const
  391. {
  392. if (is_identifier())
  393. return static_cast<const IdentifierStyleValue&>(*this).id();
  394. return CSS::ValueID::Invalid;
  395. }
  396. }