CSSParser.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <LibHTML/CSS/StyleSheet.h>
  2. #include <LibHTML/Parser/CSSParser.h>
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. static Optional<Color> parse_css_color(const StringView& view)
  6. {
  7. auto color = Color::from_string(view);
  8. if (color.has_value())
  9. return color;
  10. // FIXME: Parse all valid color strings :^)
  11. return {};
  12. }
  13. NonnullRefPtr<StyleValue> parse_css_value(const StringView& view)
  14. {
  15. String string(view);
  16. bool ok;
  17. int as_int = string.to_int(ok);
  18. if (ok)
  19. return LengthStyleValue::create(Length(as_int, Length::Type::Absolute));
  20. unsigned as_uint = string.to_uint(ok);
  21. if (ok)
  22. return LengthStyleValue::create(Length(as_uint, Length::Type::Absolute));
  23. if (string == "inherit")
  24. return InheritStyleValue::create();
  25. if (string == "initial")
  26. return InitialStyleValue::create();
  27. if (string == "auto")
  28. return LengthStyleValue::create(Length());
  29. auto color = parse_css_color(view);
  30. if (color.has_value())
  31. return ColorStyleValue::create(color.value());
  32. return StringStyleValue::create(string);
  33. }
  34. class CSSParser {
  35. public:
  36. CSSParser(const StringView& input)
  37. : css(input)
  38. {
  39. }
  40. char peek() const
  41. {
  42. if (index < css.length())
  43. return css[index];
  44. return 0;
  45. }
  46. char consume_specific(char ch)
  47. {
  48. ASSERT(peek() == ch);
  49. ++index;
  50. return ch;
  51. }
  52. char consume_one()
  53. {
  54. return css[index++];
  55. };
  56. void consume_whitespace()
  57. {
  58. while (isspace(peek()))
  59. ++index;
  60. }
  61. bool is_valid_selector_char(char ch) const
  62. {
  63. return isalnum(ch) || ch == '-' || ch == '_';
  64. }
  65. void parse_selector()
  66. {
  67. consume_whitespace();
  68. Selector::Component::Type type;
  69. if (peek() == '.') {
  70. type = Selector::Component::Type::Class;
  71. consume_one();
  72. } else if (peek() == '#') {
  73. type = Selector::Component::Type::Id;
  74. consume_one();
  75. } else {
  76. type = Selector::Component::Type::TagName;
  77. }
  78. while (is_valid_selector_char(peek()))
  79. buffer.append(consume_one());
  80. ASSERT(!buffer.is_null());
  81. auto component_string = String::copy(buffer);
  82. Vector<Selector::Component> components;
  83. components.append({ type, component_string });
  84. buffer.clear();
  85. current_rule.selectors.append(Selector(move(components)));
  86. };
  87. void parse_selector_list()
  88. {
  89. for (;;) {
  90. parse_selector();
  91. consume_whitespace();
  92. if (peek() == ',') {
  93. consume_one();
  94. continue;
  95. }
  96. if (peek() == '{')
  97. break;
  98. }
  99. }
  100. bool is_valid_property_name_char(char ch) const
  101. {
  102. return !isspace(ch) && ch != ':';
  103. }
  104. bool is_valid_property_value_char(char ch) const
  105. {
  106. return !isspace(ch) && ch != ';';
  107. }
  108. void parse_property()
  109. {
  110. consume_whitespace();
  111. buffer.clear();
  112. while (is_valid_property_name_char(peek()))
  113. buffer.append(consume_one());
  114. auto property_name = String::copy(buffer);
  115. buffer.clear();
  116. consume_whitespace();
  117. consume_specific(':');
  118. consume_whitespace();
  119. while (is_valid_property_value_char(peek()))
  120. buffer.append(consume_one());
  121. auto property_value = String::copy(buffer);
  122. buffer.clear();
  123. consume_specific(';');
  124. current_rule.properties.append({ property_name, parse_css_value(property_value) });
  125. }
  126. void parse_declaration()
  127. {
  128. for (;;) {
  129. parse_property();
  130. consume_whitespace();
  131. if (peek() == '}')
  132. break;
  133. }
  134. }
  135. void parse_rule()
  136. {
  137. parse_selector_list();
  138. consume_specific('{');
  139. parse_declaration();
  140. consume_specific('}');
  141. rules.append(StyleRule::create(move(current_rule.selectors), StyleDeclaration::create(move(current_rule.properties))));
  142. consume_whitespace();
  143. }
  144. NonnullRefPtr<StyleSheet> parse_sheet()
  145. {
  146. while (index < css.length()) {
  147. parse_rule();
  148. }
  149. return StyleSheet::create(move(rules));
  150. }
  151. NonnullRefPtr<StyleDeclaration> parse_standalone_declaration()
  152. {
  153. consume_whitespace();
  154. for (;;) {
  155. parse_property();
  156. consume_whitespace();
  157. if (!peek())
  158. break;
  159. }
  160. return StyleDeclaration::create(move(current_rule.properties));
  161. }
  162. private:
  163. NonnullRefPtrVector<StyleRule> rules;
  164. enum class State {
  165. Free,
  166. InSelectorComponent,
  167. InPropertyName,
  168. InPropertyValue,
  169. };
  170. struct CurrentRule {
  171. Vector<Selector> selectors;
  172. Vector<StyleProperty> properties;
  173. };
  174. CurrentRule current_rule;
  175. Vector<char> buffer;
  176. int index = 0;
  177. String css;
  178. };
  179. NonnullRefPtr<StyleSheet> parse_css(const String& css)
  180. {
  181. CSSParser parser(css);
  182. return parser.parse_sheet();
  183. }
  184. NonnullRefPtr<StyleDeclaration> parse_css_declaration(const String& css)
  185. {
  186. CSSParser parser(css);
  187. return parser.parse_standalone_declaration();
  188. }