CssParserTest.C 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <boost/test/unit_test.hpp>
  2. #include <Wt/Render/CssParser.h>
  3. #include <iostream>
  4. #include <boost/version.hpp>
  5. #if !defined(WT_NO_SPIRIT) && BOOST_VERSION >= 104700
  6. # define CSS_PARSER
  7. #endif
  8. #ifdef CSS_PARSER
  9. bool isValid(Wt::Render::StyleSheet *s)
  10. {
  11. bool result = s;
  12. delete s;
  13. return result;
  14. }
  15. BOOST_AUTO_TEST_CASE( CssParser_test1 )
  16. {
  17. Wt::Render::CssParser parser;
  18. Wt::Render::StyleSheet* s = parser.parse(
  19. "h1 { color: green } h1 h2, h1 h3{color: red}");
  20. BOOST_REQUIRE( s );
  21. BOOST_REQUIRE( s->rulesetSize() == 3 );
  22. BOOST_REQUIRE( s->rulesetAt(1).selector().size() == 2 );
  23. BOOST_REQUIRE( s->rulesetAt(2).selector().size() == 2 );
  24. delete s;
  25. Wt::Render::StyleSheet* s2 = parser.parse("h1 h2 h3 h4 {color: green}");
  26. BOOST_REQUIRE( s2 );
  27. BOOST_REQUIRE( s2->rulesetSize() == 1 );
  28. BOOST_REQUIRE( s2->rulesetAt(0).selector().size() == 4 );
  29. delete s2;
  30. BOOST_REQUIRE( !isValid(parser.parse("h1 h2 h3 & h4 {inside: ok}")) );
  31. BOOST_REQUIRE( isValid(parser.parse("h1{}")) );
  32. BOOST_REQUIRE( !isValid(parser.parse("1h{}")) );
  33. BOOST_REQUIRE( !isValid(parser.parse("h 1{}")) );
  34. BOOST_REQUIRE( isValid(parser.parse(".class1{}")) );
  35. BOOST_REQUIRE( !isValid(parser.parse(".1class{}")) );
  36. BOOST_REQUIRE( isValid(parser.parse("#id1{}")) );
  37. BOOST_REQUIRE( !isValid(parser.parse("#1id{}")) );
  38. BOOST_REQUIRE( !isValid(parser.parse("{}")) );
  39. BOOST_REQUIRE( isValid(parser.parse("id_{id_:boo}")) );
  40. Wt::Render::StyleSheet* s6 = parser.parse("a{inside:\"}b{\"}");
  41. BOOST_REQUIRE( s6 );
  42. BOOST_REQUIRE( s6->rulesetSize() == 1 );
  43. BOOST_REQUIRE( isValid(parser.parse("h1{ a: a; b: b }")) );
  44. BOOST_REQUIRE( isValid(parser.parse("h1{ a: a; b: b; }")) );
  45. BOOST_REQUIRE( isValid(parser.parse("h1{ a: 2em }")) );
  46. delete s6;
  47. Wt::Render::StyleSheet* s3 = parser.parse(".class1.class2{}");
  48. BOOST_REQUIRE( s3 );
  49. BOOST_REQUIRE( s3->rulesetSize() == 1 );
  50. BOOST_REQUIRE( s3->rulesetAt(0).selector().size() == 1 );
  51. delete s3;
  52. Wt::Render::StyleSheet* s4 = parser.parse(".class1 .class2{}");
  53. BOOST_REQUIRE( s4 );
  54. BOOST_REQUIRE( s4->rulesetSize() == 1 );
  55. BOOST_REQUIRE( s4->rulesetAt(0).selector().size() == 2 );
  56. delete s4;
  57. Wt::Render::StyleSheet* s5 = parser.parse(
  58. "h1{color: 20px; something: blue; something_else: \"bla\" }");
  59. BOOST_REQUIRE( s5 );
  60. BOOST_REQUIRE( s5->rulesetSize() == 1 );
  61. /*BOOST_REQUIRE( s5->rulesetAt(0).declarationBlock().value("color").value_
  62. == 20.0 );
  63. BOOST_REQUIRE( s5->rulesetAt(0).declarationBlock().value("color").unit_
  64. == Wt::Render::Term::Px );
  65. BOOST_REQUIRE( s5->rulesetAt(0).declarationBlock().value("something")
  66. .identifier_ == "blue" );
  67. BOOST_REQUIRE( s5->rulesetAt(0).declarationBlock().value("something_else")
  68. .quotedString_ == "bla" );*/
  69. delete s5;
  70. // Test hex color
  71. BOOST_REQUIRE( isValid(parser.parse("h1{color:#123}")) );
  72. BOOST_REQUIRE( isValid(parser.parse("h1{color:#a11}")) );
  73. BOOST_REQUIRE( isValid(parser.parse("h1{color:#123456}")) );
  74. // Test multi term expressions
  75. BOOST_REQUIRE( isValid(parser.parse("h1{test: .1px}")) );
  76. BOOST_REQUIRE( isValid(parser.parse("hr{border: 1px 1px}")) );
  77. BOOST_REQUIRE( isValid(parser.parse("hr{outline: thin dotted invert}")) );
  78. // Test comments
  79. BOOST_REQUIRE( isValid(parser.parse("/*bla*/ h1{}")) );
  80. BOOST_REQUIRE( isValid(parser.parse("h1{/*bla*/ test: 1px}")) );
  81. BOOST_REQUIRE( isValid(parser.parse("h1{test: /*bla*/1px}")) );
  82. BOOST_REQUIRE( isValid(parser.parse("h1{test: /*bla* */1px}")) );
  83. BOOST_REQUIRE( isValid(parser.parse("<!--bla--> h1{test: 1px}")) );
  84. BOOST_REQUIRE( isValid(parser.parse("h1{test: 1px <!--bla--> }")) );
  85. BOOST_REQUIRE( isValid(parser.parse("h1{test: 1px <!--b-l-a--> }")) );
  86. // Test white spaces
  87. BOOST_REQUIRE( isValid(parser.parse("h1{test:\t\n\r\f 1px}")) );
  88. // Test URI's
  89. BOOST_REQUIRE( isValid(parser.parse("h1{test:url(\'bla\')}")) );
  90. BOOST_REQUIRE( isValid(parser.parse("h1{test:url(\"bla\")}")) );
  91. BOOST_REQUIRE( isValid(parser.parse("h1{test:url(bla)}")) );
  92. BOOST_REQUIRE( isValid(parser.parse("h1{test:url( \"bla\" )}")) );
  93. BOOST_REQUIRE( isValid(parser.parse("h1{test:url( \t\n\f\r \"bla\" )}")) );
  94. BOOST_REQUIRE( isValid(parser.parse("h1{test:url(\"folder\\image.gif\" )}")) );
  95. }
  96. BOOST_AUTO_TEST_CASE( CssParser_testDefaultStylesheet )
  97. {
  98. Wt::Render::CssParser parser;
  99. BOOST_REQUIRE( !isValid(parser.parseFile("")) );
  100. BOOST_REQUIRE( isValid(parser.parseFile("../resources/html4_default.css")) );
  101. }
  102. #endif // CSS_PARSER