StyleValue.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. class StyleValue : public RefCounted<StyleValue> {
  179. public:
  180. virtual ~StyleValue();
  181. enum class Type {
  182. Invalid,
  183. Inherit,
  184. Initial,
  185. String,
  186. Length,
  187. Color,
  188. Identifier,
  189. Image,
  190. Position,
  191. CustomProperty,
  192. Numeric,
  193. };
  194. Type type() const { return m_type; }
  195. bool is_inherit() const { return type() == Type::Inherit; }
  196. bool is_initial() const { return type() == Type::Initial; }
  197. bool is_color() const { return type() == Type::Color; }
  198. bool is_identifier() const { return type() == Type::Identifier; }
  199. bool is_image() const { return type() == Type::Image; }
  200. bool is_string() const { return type() == Type::String; }
  201. bool is_length() const { return type() == Type::Length; }
  202. bool is_position() const { return type() == Type::Position; }
  203. bool is_custom_property() const { return type() == Type::CustomProperty; }
  204. bool is_numeric() const { return type() == Type::Numeric; }
  205. virtual String to_string() const = 0;
  206. virtual Length to_length() const { return Length::make_auto(); }
  207. virtual Color to_color(const DOM::Document&) const { return {}; }
  208. CSS::ValueID to_identifier() const;
  209. virtual bool is_auto() const { return false; }
  210. bool operator==(const StyleValue& other) const { return equals(other); }
  211. bool operator!=(const StyleValue& other) const { return !(*this == other); }
  212. virtual bool equals(const StyleValue& other) const
  213. {
  214. if (type() != other.type())
  215. return false;
  216. return to_string() == other.to_string();
  217. }
  218. protected:
  219. explicit StyleValue(Type);
  220. private:
  221. Type m_type { Type::Invalid };
  222. };
  223. // FIXME: Allow for fallback
  224. class CustomStyleValue : public StyleValue {
  225. public:
  226. static NonnullRefPtr<CustomStyleValue> create(const String& custom_property_name)
  227. {
  228. return adopt_ref(*new CustomStyleValue(custom_property_name));
  229. }
  230. String custom_property_name() const { return m_custom_property_name; }
  231. String to_string() const override { return m_custom_property_name; }
  232. private:
  233. explicit CustomStyleValue(const String& custom_property_name)
  234. : StyleValue(Type::CustomProperty)
  235. , m_custom_property_name(custom_property_name)
  236. {
  237. }
  238. String m_custom_property_name {};
  239. };
  240. class NumericStyleValue : public StyleValue {
  241. public:
  242. static NonnullRefPtr<NumericStyleValue> create(float value)
  243. {
  244. return adopt_ref(*new NumericStyleValue(value));
  245. }
  246. float value() const { return m_value; }
  247. String to_string() const override { return String::formatted("{}", m_value); }
  248. private:
  249. explicit NumericStyleValue(float value)
  250. : StyleValue(Type::Numeric)
  251. , m_value(value)
  252. {
  253. }
  254. float m_value { 0 };
  255. };
  256. class StringStyleValue : public StyleValue {
  257. public:
  258. static NonnullRefPtr<StringStyleValue> create(const String& string)
  259. {
  260. return adopt_ref(*new StringStyleValue(string));
  261. }
  262. virtual ~StringStyleValue() override { }
  263. String to_string() const override { return m_string; }
  264. private:
  265. explicit StringStyleValue(const String& string)
  266. : StyleValue(Type::String)
  267. , m_string(string)
  268. {
  269. }
  270. String m_string;
  271. };
  272. class LengthStyleValue : public StyleValue {
  273. public:
  274. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  275. {
  276. return adopt_ref(*new LengthStyleValue(length));
  277. }
  278. virtual ~LengthStyleValue() override { }
  279. virtual String to_string() const override { return m_length.to_string(); }
  280. virtual Length to_length() const override { return m_length; }
  281. const Length& length() const { return m_length; }
  282. virtual bool is_auto() const override { return m_length.is_auto(); }
  283. virtual bool equals(const StyleValue& other) const override
  284. {
  285. if (type() != other.type())
  286. return false;
  287. return m_length == static_cast<const LengthStyleValue&>(other).m_length;
  288. }
  289. private:
  290. explicit LengthStyleValue(const Length& length)
  291. : StyleValue(Type::Length)
  292. , m_length(length)
  293. {
  294. }
  295. Length m_length;
  296. };
  297. class InitialStyleValue final : public StyleValue {
  298. public:
  299. static NonnullRefPtr<InitialStyleValue> create() { return adopt_ref(*new InitialStyleValue); }
  300. virtual ~InitialStyleValue() override { }
  301. String to_string() const override { return "initial"; }
  302. private:
  303. InitialStyleValue()
  304. : StyleValue(Type::Initial)
  305. {
  306. }
  307. };
  308. class InheritStyleValue final : public StyleValue {
  309. public:
  310. static NonnullRefPtr<InheritStyleValue> create() { return adopt_ref(*new InheritStyleValue); }
  311. virtual ~InheritStyleValue() override { }
  312. String to_string() const override { return "inherit"; }
  313. private:
  314. InheritStyleValue()
  315. : StyleValue(Type::Inherit)
  316. {
  317. }
  318. };
  319. class ColorStyleValue : public StyleValue {
  320. public:
  321. static NonnullRefPtr<ColorStyleValue> create(Color color)
  322. {
  323. return adopt_ref(*new ColorStyleValue(color));
  324. }
  325. virtual ~ColorStyleValue() override { }
  326. Color color() const { return m_color; }
  327. String to_string() const override { return m_color.to_string(); }
  328. Color to_color(const DOM::Document&) const override { return m_color; }
  329. virtual bool equals(const StyleValue& other) const override
  330. {
  331. if (type() != other.type())
  332. return false;
  333. return m_color == static_cast<const ColorStyleValue&>(other).m_color;
  334. }
  335. private:
  336. explicit ColorStyleValue(Color color)
  337. : StyleValue(Type::Color)
  338. , m_color(color)
  339. {
  340. }
  341. Color m_color;
  342. };
  343. class IdentifierStyleValue final : public StyleValue {
  344. public:
  345. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  346. {
  347. return adopt_ref(*new IdentifierStyleValue(id));
  348. }
  349. virtual ~IdentifierStyleValue() override { }
  350. CSS::ValueID id() const { return m_id; }
  351. virtual String to_string() const override;
  352. virtual Color to_color(const DOM::Document&) const override;
  353. virtual bool equals(const StyleValue& other) const override
  354. {
  355. if (type() != other.type())
  356. return false;
  357. return m_id == static_cast<const IdentifierStyleValue&>(other).m_id;
  358. }
  359. private:
  360. explicit IdentifierStyleValue(CSS::ValueID id)
  361. : StyleValue(Type::Identifier)
  362. , m_id(id)
  363. {
  364. }
  365. CSS::ValueID m_id { CSS::ValueID::Invalid };
  366. };
  367. class ImageStyleValue final
  368. : public StyleValue
  369. , public ImageResourceClient {
  370. public:
  371. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt_ref(*new ImageStyleValue(url, document)); }
  372. virtual ~ImageStyleValue() override { }
  373. String to_string() const override { return String::formatted("Image({})", m_url.to_string()); }
  374. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  375. private:
  376. ImageStyleValue(const URL&, DOM::Document&);
  377. // ^ResourceClient
  378. virtual void resource_did_load() override;
  379. URL m_url;
  380. WeakPtr<DOM::Document> m_document;
  381. RefPtr<Gfx::Bitmap> m_bitmap;
  382. };
  383. inline CSS::ValueID StyleValue::to_identifier() const
  384. {
  385. if (is_identifier())
  386. return static_cast<const IdentifierStyleValue&>(*this).id();
  387. return CSS::ValueID::Invalid;
  388. }
  389. }