CSSParser.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include <LibHTML/CSS/StyleSheet.h>
  2. #include <LibHTML/Parser/CSSParser.h>
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #define PARSE_ASSERT(x) \
  6. if (!(x)) { \
  7. dbg() << "CSS PARSER ASSERTION FAILED: " << #x; \
  8. dbg() << "At character# " << index << " in CSS: _" << css << "_"; \
  9. ASSERT_NOT_REACHED(); \
  10. }
  11. static Optional<Color> parse_css_color(const StringView& view)
  12. {
  13. auto color = Color::from_string(view);
  14. if (color.has_value())
  15. return color;
  16. // FIXME: Parse all valid color strings :^)
  17. return {};
  18. }
  19. NonnullRefPtr<StyleValue> parse_css_value(const StringView& view)
  20. {
  21. String string(view);
  22. bool ok;
  23. int as_int = string.to_int(ok);
  24. if (ok)
  25. return LengthStyleValue::create(Length(as_int, Length::Type::Absolute));
  26. unsigned as_uint = string.to_uint(ok);
  27. if (ok)
  28. return LengthStyleValue::create(Length(as_uint, Length::Type::Absolute));
  29. if (string == "inherit")
  30. return InheritStyleValue::create();
  31. if (string == "initial")
  32. return InitialStyleValue::create();
  33. if (string == "auto")
  34. return LengthStyleValue::create(Length());
  35. auto color = parse_css_color(view);
  36. if (color.has_value())
  37. return ColorStyleValue::create(color.value());
  38. return StringStyleValue::create(string);
  39. }
  40. class CSSParser {
  41. public:
  42. CSSParser(const StringView& input)
  43. : css(input)
  44. {
  45. }
  46. char peek() const
  47. {
  48. if (index < css.length())
  49. return css[index];
  50. return 0;
  51. }
  52. char consume_specific(char ch)
  53. {
  54. PARSE_ASSERT(peek() == ch);
  55. ++index;
  56. return ch;
  57. }
  58. char consume_one()
  59. {
  60. return css[index++];
  61. };
  62. void consume_whitespace()
  63. {
  64. while (isspace(peek()))
  65. ++index;
  66. }
  67. bool is_valid_selector_char(char ch) const
  68. {
  69. return isalnum(ch) || ch == '-' || ch == '_' || ch == '(' || ch == ')' || ch == '@';
  70. }
  71. Optional<Selector::Component> parse_selector_component()
  72. {
  73. consume_whitespace();
  74. Selector::Component::Type type;
  75. Selector::Component::Relation relation = Selector::Component::Relation::None;
  76. if (peek() == '{')
  77. return {};
  78. if (peek() == '>') {
  79. relation = Selector::Component::Relation::ImmediateChild;
  80. consume_one();
  81. consume_whitespace();
  82. }
  83. if (peek() == '.') {
  84. type = Selector::Component::Type::Class;
  85. consume_one();
  86. } else if (peek() == '#') {
  87. type = Selector::Component::Type::Id;
  88. consume_one();
  89. } else {
  90. type = Selector::Component::Type::TagName;
  91. }
  92. while (is_valid_selector_char(peek()))
  93. buffer.append(consume_one());
  94. PARSE_ASSERT(!buffer.is_null());
  95. Selector::Component component { type, relation, String::copy(buffer) };
  96. buffer.clear();
  97. if (peek() == '[') {
  98. // FIXME: Implement attribute selectors.
  99. while (peek() != ']') {
  100. consume_one();
  101. }
  102. consume_one();
  103. }
  104. if (peek() == ':') {
  105. // FIXME: Implement pseudo stuff.
  106. consume_one();
  107. if (peek() == ':')
  108. consume_one();
  109. while (is_valid_selector_char(peek()))
  110. consume_one();
  111. }
  112. return component;
  113. }
  114. void parse_selector()
  115. {
  116. Vector<Selector::Component> components;
  117. for (;;) {
  118. auto component = parse_selector_component();
  119. if (component.has_value())
  120. components.append(component.value());
  121. consume_whitespace();
  122. if (peek() == ',' || peek() == '{')
  123. break;
  124. }
  125. current_rule.selectors.append(Selector(move(components)));
  126. };
  127. void parse_selector_list()
  128. {
  129. for (;;) {
  130. parse_selector();
  131. consume_whitespace();
  132. if (peek() == ',') {
  133. consume_one();
  134. continue;
  135. }
  136. if (peek() == '{')
  137. break;
  138. }
  139. }
  140. bool is_valid_property_name_char(char ch) const
  141. {
  142. return !isspace(ch) && ch != ':';
  143. }
  144. bool is_valid_property_value_char(char ch) const
  145. {
  146. return ch != '!' && ch != ';';
  147. }
  148. Optional<StyleProperty> parse_property()
  149. {
  150. consume_whitespace();
  151. if (peek() == ';') {
  152. consume_one();
  153. return {};
  154. }
  155. buffer.clear();
  156. while (is_valid_property_name_char(peek()))
  157. buffer.append(consume_one());
  158. auto property_name = String::copy(buffer);
  159. buffer.clear();
  160. consume_whitespace();
  161. consume_specific(':');
  162. consume_whitespace();
  163. while (is_valid_property_value_char(peek()))
  164. buffer.append(consume_one());
  165. auto property_value = String::copy(buffer);
  166. buffer.clear();
  167. consume_whitespace();
  168. bool is_important = false;
  169. if (peek() == '!') {
  170. consume_specific('!');
  171. consume_specific('i');
  172. consume_specific('m');
  173. consume_specific('p');
  174. consume_specific('o');
  175. consume_specific('r');
  176. consume_specific('t');
  177. consume_specific('a');
  178. consume_specific('n');
  179. consume_specific('t');
  180. consume_whitespace();
  181. is_important = true;
  182. }
  183. consume_specific(';');
  184. return StyleProperty { property_name, parse_css_value(property_value), is_important };
  185. }
  186. void parse_declaration()
  187. {
  188. for (;;) {
  189. auto property = parse_property();
  190. if (property.has_value())
  191. current_rule.properties.append(property.value());
  192. consume_whitespace();
  193. if (peek() == '}')
  194. break;
  195. }
  196. }
  197. void parse_rule()
  198. {
  199. parse_selector_list();
  200. consume_specific('{');
  201. parse_declaration();
  202. consume_specific('}');
  203. rules.append(StyleRule::create(move(current_rule.selectors), StyleDeclaration::create(move(current_rule.properties))));
  204. consume_whitespace();
  205. }
  206. NonnullRefPtr<StyleSheet> parse_sheet()
  207. {
  208. while (index < css.length()) {
  209. parse_rule();
  210. }
  211. return StyleSheet::create(move(rules));
  212. }
  213. NonnullRefPtr<StyleDeclaration> parse_standalone_declaration()
  214. {
  215. consume_whitespace();
  216. for (;;) {
  217. parse_property();
  218. consume_whitespace();
  219. if (!peek())
  220. break;
  221. }
  222. return StyleDeclaration::create(move(current_rule.properties));
  223. }
  224. private:
  225. NonnullRefPtrVector<StyleRule> rules;
  226. enum class State {
  227. Free,
  228. InSelectorComponent,
  229. InPropertyName,
  230. InPropertyValue,
  231. };
  232. struct CurrentRule {
  233. Vector<Selector> selectors;
  234. Vector<StyleProperty> properties;
  235. };
  236. CurrentRule current_rule;
  237. Vector<char> buffer;
  238. int index = 0;
  239. String css;
  240. };
  241. NonnullRefPtr<StyleSheet> parse_css(const String& css)
  242. {
  243. CSSParser parser(css);
  244. return parser.parse_sheet();
  245. }
  246. NonnullRefPtr<StyleDeclaration> parse_css_declaration(const String& css)
  247. {
  248. CSSParser parser(css);
  249. return parser.parse_standalone_declaration();
  250. }