StyleValue.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/RefCounted.h>
  28. #include <AK/RefPtr.h>
  29. #include <AK/String.h>
  30. #include <AK/StringView.h>
  31. #include <AK/URL.h>
  32. #include <AK/WeakPtr.h>
  33. #include <LibGfx/Bitmap.h>
  34. #include <LibGfx/Color.h>
  35. #include <LibWeb/CSS/Length.h>
  36. #include <LibWeb/CSS/PropertyID.h>
  37. #include <LibWeb/CSS/ValueID.h>
  38. #include <LibWeb/Forward.h>
  39. #include <LibWeb/Loader/ImageResource.h>
  40. namespace Web::CSS {
  41. enum class Position {
  42. Static,
  43. Relative,
  44. Absolute,
  45. Fixed,
  46. Sticky,
  47. };
  48. enum class TextAlign {
  49. Left,
  50. Center,
  51. Right,
  52. Justify,
  53. LibwebCenter,
  54. };
  55. enum class TextDecorationLine {
  56. None,
  57. Underline,
  58. Overline,
  59. LineThrough,
  60. Blink,
  61. };
  62. enum class TextTransform {
  63. None,
  64. Capitalize,
  65. Uppercase,
  66. Lowercase,
  67. FullWidth,
  68. FullSizeKana,
  69. };
  70. enum class Display {
  71. None,
  72. Block,
  73. Inline,
  74. InlineBlock,
  75. ListItem,
  76. Table,
  77. TableRow,
  78. TableCell,
  79. TableHeaderGroup,
  80. TableRowGroup,
  81. TableFooterGroup,
  82. TableColumn,
  83. TableColumnGroup,
  84. TableCaption,
  85. Flex,
  86. };
  87. enum class FlexDirection {
  88. Row,
  89. RowReverse,
  90. Column,
  91. ColumnReverse,
  92. };
  93. enum class WhiteSpace {
  94. Normal,
  95. Pre,
  96. Nowrap,
  97. PreLine,
  98. PreWrap,
  99. };
  100. enum class Float {
  101. None,
  102. Left,
  103. Right,
  104. };
  105. enum class Clear {
  106. None,
  107. Left,
  108. Right,
  109. Both,
  110. };
  111. enum class Cursor {
  112. Auto,
  113. Default,
  114. None,
  115. ContextMenu,
  116. Help,
  117. Pointer,
  118. Progress,
  119. Wait,
  120. Cell,
  121. Crosshair,
  122. Text,
  123. VerticalText,
  124. Alias,
  125. Copy,
  126. Move,
  127. NoDrop,
  128. NotAllowed,
  129. Grab,
  130. Grabbing,
  131. EResize,
  132. NResize,
  133. NeResize,
  134. NwResize,
  135. SResize,
  136. SeResize,
  137. SwResize,
  138. WResize,
  139. EwResize,
  140. NsResize,
  141. NeswResize,
  142. NwseResize,
  143. ColResize,
  144. RowResize,
  145. AllScroll,
  146. ZoomIn,
  147. ZoomOut,
  148. };
  149. enum class LineStyle {
  150. None,
  151. Hidden,
  152. Dotted,
  153. Dashed,
  154. Solid,
  155. Double,
  156. Groove,
  157. Ridge,
  158. Inset,
  159. Outset,
  160. };
  161. enum class ListStyleType {
  162. None,
  163. Disc,
  164. Circle,
  165. Square,
  166. Decimal,
  167. };
  168. enum class Overflow : u8 {
  169. Auto,
  170. Clip,
  171. Hidden,
  172. Scroll,
  173. Visible,
  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. };
  189. Type type() const { return m_type; }
  190. bool is_inherit() const { return type() == Type::Inherit; }
  191. bool is_initial() const { return type() == Type::Initial; }
  192. bool is_color() const { return type() == Type::Color; }
  193. bool is_identifier() const { return type() == Type::Identifier; }
  194. bool is_image() const { return type() == Type::Image; }
  195. bool is_string() const { return type() == Type::String; }
  196. bool is_length() const { return type() == Type::Length; }
  197. bool is_position() const { return type() == Type::Position; }
  198. virtual String to_string() const = 0;
  199. virtual Length to_length() const { return Length::make_auto(); }
  200. virtual Color to_color(const DOM::Document&) const { return {}; }
  201. CSS::ValueID to_identifier() const;
  202. virtual bool is_auto() const { return false; }
  203. bool operator==(const StyleValue& other) const { return equals(other); }
  204. bool operator!=(const StyleValue& other) const { return !(*this == other); }
  205. virtual bool equals(const StyleValue& other) const
  206. {
  207. if (type() != other.type())
  208. return false;
  209. return to_string() == other.to_string();
  210. }
  211. protected:
  212. explicit StyleValue(Type);
  213. private:
  214. Type m_type { Type::Invalid };
  215. };
  216. class StringStyleValue : public StyleValue {
  217. public:
  218. static NonnullRefPtr<StringStyleValue> create(const String& string)
  219. {
  220. return adopt(*new StringStyleValue(string));
  221. }
  222. virtual ~StringStyleValue() override { }
  223. String to_string() const override { return m_string; }
  224. private:
  225. explicit StringStyleValue(const String& string)
  226. : StyleValue(Type::String)
  227. , m_string(string)
  228. {
  229. }
  230. String m_string;
  231. };
  232. class LengthStyleValue : public StyleValue {
  233. public:
  234. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  235. {
  236. return adopt(*new LengthStyleValue(length));
  237. }
  238. virtual ~LengthStyleValue() override { }
  239. virtual String to_string() const override { return m_length.to_string(); }
  240. virtual Length to_length() const override { return m_length; }
  241. const Length& length() const { return m_length; }
  242. virtual bool is_auto() const override { return m_length.is_auto(); }
  243. virtual bool equals(const StyleValue& other) const override
  244. {
  245. if (type() != other.type())
  246. return false;
  247. return m_length == static_cast<const LengthStyleValue&>(other).m_length;
  248. }
  249. private:
  250. explicit LengthStyleValue(const Length& length)
  251. : StyleValue(Type::Length)
  252. , m_length(length)
  253. {
  254. }
  255. Length m_length;
  256. };
  257. class InitialStyleValue final : public StyleValue {
  258. public:
  259. static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }
  260. virtual ~InitialStyleValue() override { }
  261. String to_string() const override { return "initial"; }
  262. private:
  263. InitialStyleValue()
  264. : StyleValue(Type::Initial)
  265. {
  266. }
  267. };
  268. class InheritStyleValue final : public StyleValue {
  269. public:
  270. static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); }
  271. virtual ~InheritStyleValue() override { }
  272. String to_string() const override { return "inherit"; }
  273. private:
  274. InheritStyleValue()
  275. : StyleValue(Type::Inherit)
  276. {
  277. }
  278. };
  279. class ColorStyleValue : public StyleValue {
  280. public:
  281. static NonnullRefPtr<ColorStyleValue> create(Color color)
  282. {
  283. return adopt(*new ColorStyleValue(color));
  284. }
  285. virtual ~ColorStyleValue() override { }
  286. Color color() const { return m_color; }
  287. String to_string() const override { return m_color.to_string(); }
  288. Color to_color(const DOM::Document&) const override { return m_color; }
  289. virtual bool equals(const StyleValue& other) const override
  290. {
  291. if (type() != other.type())
  292. return false;
  293. return m_color == static_cast<const ColorStyleValue&>(other).m_color;
  294. }
  295. private:
  296. explicit ColorStyleValue(Color color)
  297. : StyleValue(Type::Color)
  298. , m_color(color)
  299. {
  300. }
  301. Color m_color;
  302. };
  303. class IdentifierStyleValue final : public StyleValue {
  304. public:
  305. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  306. {
  307. return adopt(*new IdentifierStyleValue(id));
  308. }
  309. virtual ~IdentifierStyleValue() override { }
  310. CSS::ValueID id() const { return m_id; }
  311. virtual String to_string() const override;
  312. virtual Color to_color(const DOM::Document&) const override;
  313. virtual bool equals(const StyleValue& other) const override
  314. {
  315. if (type() != other.type())
  316. return false;
  317. return m_id == static_cast<const IdentifierStyleValue&>(other).m_id;
  318. }
  319. private:
  320. explicit IdentifierStyleValue(CSS::ValueID id)
  321. : StyleValue(Type::Identifier)
  322. , m_id(id)
  323. {
  324. }
  325. CSS::ValueID m_id { CSS::ValueID::Invalid };
  326. };
  327. class ImageStyleValue final
  328. : public StyleValue
  329. , public ImageResourceClient {
  330. public:
  331. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt(*new ImageStyleValue(url, document)); }
  332. virtual ~ImageStyleValue() override { }
  333. String to_string() const override { return String::formatted("Image({})", m_url.to_string()); }
  334. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  335. private:
  336. ImageStyleValue(const URL&, DOM::Document&);
  337. // ^ResourceClient
  338. virtual void resource_did_load() override;
  339. URL m_url;
  340. WeakPtr<DOM::Document> m_document;
  341. RefPtr<Gfx::Bitmap> m_bitmap;
  342. };
  343. inline CSS::ValueID StyleValue::to_identifier() const
  344. {
  345. if (is_identifier())
  346. return static_cast<const IdentifierStyleValue&>(*this).id();
  347. return CSS::ValueID::Invalid;
  348. }
  349. }