CSSParser.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. NonnullRefPtr<StyleSheet> parse_css(const String& css)
  35. {
  36. NonnullRefPtrVector<StyleRule> rules;
  37. enum class State {
  38. Free,
  39. InSelectorComponent,
  40. InPropertyName,
  41. InPropertyValue,
  42. };
  43. struct CurrentRule {
  44. Vector<Selector> selectors;
  45. NonnullRefPtrVector<StyleDeclaration> declarations;
  46. };
  47. CurrentRule current_rule;
  48. Vector<char> buffer;
  49. int index = 0;
  50. auto peek = [&]() -> char {
  51. if (index < css.length())
  52. return css[index];
  53. return 0;
  54. };
  55. auto consume_specific = [&](char ch) {
  56. ASSERT(peek() == ch);
  57. ++index;
  58. return ch;
  59. };
  60. auto consume_one = [&]() -> char {
  61. return css[index++];
  62. };
  63. auto consume_whitespace = [&] {
  64. while (isspace(peek()))
  65. ++index;
  66. };
  67. auto is_valid_selector_char = [](char ch) {
  68. return isalnum(ch) || ch == '-' || ch == '_';
  69. };
  70. auto parse_selector = [&] {
  71. consume_whitespace();
  72. Selector::Component::Type type;
  73. if (peek() == '.')
  74. type = Selector::Component::Type::Class;
  75. else if (peek() == '#')
  76. type = Selector::Component::Type::Id;
  77. else
  78. type = Selector::Component::Type::TagName;
  79. while (is_valid_selector_char(peek()))
  80. buffer.append(consume_one());
  81. ASSERT(!buffer.is_null());
  82. auto component_string = String::copy(buffer);
  83. Vector<Selector::Component> components;
  84. components.append({ type, component_string });
  85. buffer.clear();
  86. current_rule.selectors.append(Selector(move(components)));
  87. };
  88. auto parse_selector_list = [&] {
  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. auto is_valid_property_name_char = [](char ch) {
  101. return !isspace(ch) && ch != ':';
  102. };
  103. auto is_valid_property_value_char = [](char ch) {
  104. return !isspace(ch) && ch != ';';
  105. };
  106. auto parse_declaration = [&] {
  107. consume_whitespace();
  108. buffer.clear();
  109. while (is_valid_property_name_char(peek()))
  110. buffer.append(consume_one());
  111. auto property_name = String::copy(buffer);
  112. buffer.clear();
  113. consume_whitespace();
  114. consume_specific(':');
  115. consume_whitespace();
  116. while (is_valid_property_value_char(peek()))
  117. buffer.append(consume_one());
  118. auto property_value = String::copy(buffer);
  119. buffer.clear();
  120. consume_specific(';');
  121. current_rule.declarations.append(StyleDeclaration::create(property_name, parse_css_value(property_value)));
  122. };
  123. auto parse_declarations = [&] {
  124. for (;;) {
  125. parse_declaration();
  126. consume_whitespace();
  127. if (peek() == '}')
  128. break;
  129. }
  130. };
  131. auto parse_rule = [&] {
  132. parse_selector_list();
  133. consume_specific('{');
  134. parse_declarations();
  135. consume_specific('}');
  136. rules.append(StyleRule::create(move(current_rule.selectors), move(current_rule.declarations)));
  137. };
  138. while (index < css.length()) {
  139. parse_rule();
  140. }
  141. return StyleSheet::create(move(rules));
  142. }