StyleValue.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 LineStyle {
  112. None,
  113. Hidden,
  114. Dotted,
  115. Dashed,
  116. Solid,
  117. Double,
  118. Groove,
  119. Ridge,
  120. Inset,
  121. Outset,
  122. };
  123. enum class ListStyleType {
  124. None,
  125. Disc,
  126. Circle,
  127. Square,
  128. Decimal,
  129. };
  130. class StyleValue : public RefCounted<StyleValue> {
  131. public:
  132. virtual ~StyleValue();
  133. enum class Type {
  134. Invalid,
  135. Inherit,
  136. Initial,
  137. String,
  138. Length,
  139. Color,
  140. Identifier,
  141. Image,
  142. Position,
  143. };
  144. Type type() const { return m_type; }
  145. bool is_inherit() const { return type() == Type::Inherit; }
  146. bool is_initial() const { return type() == Type::Initial; }
  147. bool is_color() const { return type() == Type::Color; }
  148. bool is_identifier() const { return type() == Type::Identifier; }
  149. bool is_image() const { return type() == Type::Image; }
  150. bool is_string() const { return type() == Type::String; }
  151. bool is_length() const { return type() == Type::Length; }
  152. bool is_position() const { return type() == Type::Position; }
  153. virtual String to_string() const = 0;
  154. virtual Length to_length() const { return Length::make_auto(); }
  155. virtual Color to_color(const DOM::Document&) const { return {}; }
  156. CSS::ValueID to_identifier() const;
  157. virtual bool is_auto() const { return false; }
  158. bool operator==(const StyleValue& other) const { return equals(other); }
  159. bool operator!=(const StyleValue& other) const { return !(*this == other); }
  160. virtual bool equals(const StyleValue& other) const
  161. {
  162. if (type() != other.type())
  163. return false;
  164. return to_string() == other.to_string();
  165. }
  166. protected:
  167. explicit StyleValue(Type);
  168. private:
  169. Type m_type { Type::Invalid };
  170. };
  171. class StringStyleValue : public StyleValue {
  172. public:
  173. static NonnullRefPtr<StringStyleValue> create(const String& string)
  174. {
  175. return adopt(*new StringStyleValue(string));
  176. }
  177. virtual ~StringStyleValue() override { }
  178. String to_string() const override { return m_string; }
  179. private:
  180. explicit StringStyleValue(const String& string)
  181. : StyleValue(Type::String)
  182. , m_string(string)
  183. {
  184. }
  185. String m_string;
  186. };
  187. class LengthStyleValue : public StyleValue {
  188. public:
  189. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  190. {
  191. return adopt(*new LengthStyleValue(length));
  192. }
  193. virtual ~LengthStyleValue() override { }
  194. virtual String to_string() const override { return m_length.to_string(); }
  195. virtual Length to_length() const override { return m_length; }
  196. const Length& length() const { return m_length; }
  197. virtual bool is_auto() const override { return m_length.is_auto(); }
  198. virtual bool equals(const StyleValue& other) const override
  199. {
  200. if (type() != other.type())
  201. return false;
  202. return m_length == static_cast<const LengthStyleValue&>(other).m_length;
  203. }
  204. private:
  205. explicit LengthStyleValue(const Length& length)
  206. : StyleValue(Type::Length)
  207. , m_length(length)
  208. {
  209. }
  210. Length m_length;
  211. };
  212. class InitialStyleValue final : public StyleValue {
  213. public:
  214. static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }
  215. virtual ~InitialStyleValue() override { }
  216. String to_string() const override { return "initial"; }
  217. private:
  218. InitialStyleValue()
  219. : StyleValue(Type::Initial)
  220. {
  221. }
  222. };
  223. class InheritStyleValue final : public StyleValue {
  224. public:
  225. static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); }
  226. virtual ~InheritStyleValue() override { }
  227. String to_string() const override { return "inherit"; }
  228. private:
  229. InheritStyleValue()
  230. : StyleValue(Type::Inherit)
  231. {
  232. }
  233. };
  234. class ColorStyleValue : public StyleValue {
  235. public:
  236. static NonnullRefPtr<ColorStyleValue> create(Color color)
  237. {
  238. return adopt(*new ColorStyleValue(color));
  239. }
  240. virtual ~ColorStyleValue() override { }
  241. Color color() const { return m_color; }
  242. String to_string() const override { return m_color.to_string(); }
  243. Color to_color(const DOM::Document&) const override { return m_color; }
  244. virtual bool equals(const StyleValue& other) const override
  245. {
  246. if (type() != other.type())
  247. return false;
  248. return m_color == static_cast<const ColorStyleValue&>(other).m_color;
  249. }
  250. private:
  251. explicit ColorStyleValue(Color color)
  252. : StyleValue(Type::Color)
  253. , m_color(color)
  254. {
  255. }
  256. Color m_color;
  257. };
  258. class IdentifierStyleValue final : public StyleValue {
  259. public:
  260. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  261. {
  262. return adopt(*new IdentifierStyleValue(id));
  263. }
  264. virtual ~IdentifierStyleValue() override { }
  265. CSS::ValueID id() const { return m_id; }
  266. virtual String to_string() const override;
  267. virtual Color to_color(const DOM::Document&) const override;
  268. virtual bool equals(const StyleValue& other) const override
  269. {
  270. if (type() != other.type())
  271. return false;
  272. return m_id == static_cast<const IdentifierStyleValue&>(other).m_id;
  273. }
  274. private:
  275. explicit IdentifierStyleValue(CSS::ValueID id)
  276. : StyleValue(Type::Identifier)
  277. , m_id(id)
  278. {
  279. }
  280. CSS::ValueID m_id { CSS::ValueID::Invalid };
  281. };
  282. class ImageStyleValue final
  283. : public StyleValue
  284. , public ImageResourceClient {
  285. public:
  286. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt(*new ImageStyleValue(url, document)); }
  287. virtual ~ImageStyleValue() override { }
  288. String to_string() const override { return String::formatted("Image({})", m_url.to_string()); }
  289. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  290. private:
  291. ImageStyleValue(const URL&, DOM::Document&);
  292. // ^ResourceClient
  293. virtual void resource_did_load() override;
  294. URL m_url;
  295. WeakPtr<DOM::Document> m_document;
  296. RefPtr<Gfx::Bitmap> m_bitmap;
  297. };
  298. inline CSS::ValueID StyleValue::to_identifier() const
  299. {
  300. if (is_identifier())
  301. return static_cast<const IdentifierStyleValue&>(*this).id();
  302. return CSS::ValueID::Invalid;
  303. }
  304. }