CSSParser.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. if (string == "-libhtml-link")
  39. return IdentifierStyleValue::create(CSS::ValueID::VendorSpecificLink);
  40. return StringStyleValue::create(string);
  41. }
  42. class CSSParser {
  43. public:
  44. CSSParser(const StringView& input)
  45. : css(input)
  46. {
  47. }
  48. char peek() const
  49. {
  50. if (index < css.length())
  51. return css[index];
  52. return 0;
  53. }
  54. char consume_specific(char ch)
  55. {
  56. PARSE_ASSERT(peek() == ch);
  57. ++index;
  58. return ch;
  59. }
  60. char consume_one()
  61. {
  62. return css[index++];
  63. };
  64. void consume_whitespace()
  65. {
  66. while (isspace(peek()))
  67. ++index;
  68. }
  69. bool is_valid_selector_char(char ch) const
  70. {
  71. return isalnum(ch) || ch == '-' || ch == '_' || ch == '(' || ch == ')' || ch == '@';
  72. }
  73. Optional<Selector::Component> parse_selector_component()
  74. {
  75. consume_whitespace();
  76. Selector::Component::Type type;
  77. Selector::Component::Relation relation = Selector::Component::Relation::Descendant;
  78. if (peek() == '{')
  79. return {};
  80. if (peek() == '>') {
  81. relation = Selector::Component::Relation::ImmediateChild;
  82. consume_one();
  83. consume_whitespace();
  84. }
  85. if (peek() == '.') {
  86. type = Selector::Component::Type::Class;
  87. consume_one();
  88. } else if (peek() == '#') {
  89. type = Selector::Component::Type::Id;
  90. consume_one();
  91. } else {
  92. type = Selector::Component::Type::TagName;
  93. }
  94. while (is_valid_selector_char(peek()))
  95. buffer.append(consume_one());
  96. PARSE_ASSERT(!buffer.is_null());
  97. Selector::Component component { type, relation, String::copy(buffer) };
  98. buffer.clear();
  99. if (peek() == '[') {
  100. // FIXME: Implement attribute selectors.
  101. while (peek() != ']') {
  102. consume_one();
  103. }
  104. consume_one();
  105. }
  106. if (peek() == ':') {
  107. // FIXME: Implement pseudo stuff.
  108. consume_one();
  109. if (peek() == ':')
  110. consume_one();
  111. while (is_valid_selector_char(peek()))
  112. consume_one();
  113. }
  114. return component;
  115. }
  116. void parse_selector()
  117. {
  118. Vector<Selector::Component> components;
  119. for (;;) {
  120. auto component = parse_selector_component();
  121. if (component.has_value())
  122. components.append(component.value());
  123. consume_whitespace();
  124. if (peek() == ',' || peek() == '{')
  125. break;
  126. }
  127. if (components.is_empty())
  128. return;
  129. components.first().relation = Selector::Component::Relation::None;
  130. current_rule.selectors.append(Selector(move(components)));
  131. };
  132. void parse_selector_list()
  133. {
  134. for (;;) {
  135. parse_selector();
  136. consume_whitespace();
  137. if (peek() == ',') {
  138. consume_one();
  139. continue;
  140. }
  141. if (peek() == '{')
  142. break;
  143. }
  144. }
  145. bool is_valid_property_name_char(char ch) const
  146. {
  147. return !isspace(ch) && ch != ':';
  148. }
  149. bool is_valid_property_value_char(char ch) const
  150. {
  151. return ch != '!' && ch != ';';
  152. }
  153. Optional<StyleProperty> parse_property()
  154. {
  155. consume_whitespace();
  156. if (peek() == ';') {
  157. consume_one();
  158. return {};
  159. }
  160. buffer.clear();
  161. while (is_valid_property_name_char(peek()))
  162. buffer.append(consume_one());
  163. auto property_name = String::copy(buffer);
  164. buffer.clear();
  165. consume_whitespace();
  166. consume_specific(':');
  167. consume_whitespace();
  168. while (is_valid_property_value_char(peek()))
  169. buffer.append(consume_one());
  170. auto property_value = String::copy(buffer);
  171. buffer.clear();
  172. consume_whitespace();
  173. bool is_important = false;
  174. if (peek() == '!') {
  175. consume_specific('!');
  176. consume_specific('i');
  177. consume_specific('m');
  178. consume_specific('p');
  179. consume_specific('o');
  180. consume_specific('r');
  181. consume_specific('t');
  182. consume_specific('a');
  183. consume_specific('n');
  184. consume_specific('t');
  185. consume_whitespace();
  186. is_important = true;
  187. }
  188. consume_specific(';');
  189. return StyleProperty { property_name, parse_css_value(property_value), is_important };
  190. }
  191. void parse_declaration()
  192. {
  193. for (;;) {
  194. auto property = parse_property();
  195. if (property.has_value())
  196. current_rule.properties.append(property.value());
  197. consume_whitespace();
  198. if (peek() == '}')
  199. break;
  200. }
  201. }
  202. void parse_rule()
  203. {
  204. parse_selector_list();
  205. consume_specific('{');
  206. parse_declaration();
  207. consume_specific('}');
  208. rules.append(StyleRule::create(move(current_rule.selectors), StyleDeclaration::create(move(current_rule.properties))));
  209. consume_whitespace();
  210. }
  211. NonnullRefPtr<StyleSheet> parse_sheet()
  212. {
  213. while (index < css.length()) {
  214. parse_rule();
  215. }
  216. return StyleSheet::create(move(rules));
  217. }
  218. NonnullRefPtr<StyleDeclaration> parse_standalone_declaration()
  219. {
  220. consume_whitespace();
  221. for (;;) {
  222. parse_property();
  223. consume_whitespace();
  224. if (!peek())
  225. break;
  226. }
  227. return StyleDeclaration::create(move(current_rule.properties));
  228. }
  229. private:
  230. NonnullRefPtrVector<StyleRule> rules;
  231. enum class State {
  232. Free,
  233. InSelectorComponent,
  234. InPropertyName,
  235. InPropertyValue,
  236. };
  237. struct CurrentRule {
  238. Vector<Selector> selectors;
  239. Vector<StyleProperty> properties;
  240. };
  241. CurrentRule current_rule;
  242. Vector<char> buffer;
  243. int index = 0;
  244. String css;
  245. };
  246. NonnullRefPtr<StyleSheet> parse_css(const String& css)
  247. {
  248. CSSParser parser(css);
  249. return parser.parse_sheet();
  250. }
  251. NonnullRefPtr<StyleDeclaration> parse_css_declaration(const String& css)
  252. {
  253. CSSParser parser(css);
  254. return parser.parse_standalone_declaration();
  255. }