StyleValue.h 9.1 KB

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