aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua/unit/css.lua
blob: 3078ed3bca47dd38a4f6c89a500e37f593bb1439 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
context("CSS parsing tests", function()
  local ffi = require("ffi")
  local rspamd_mempool = require "rspamd_mempool"
  local pool = rspamd_mempool.create()
  ffi.cdef[[
const char *rspamd_css_unescape (void *pool,
            const char *begin,
            size_t len,
            size_t *olen);
void* rspamd_css_parse_style (void *pool,
            const char *begin,
            size_t len, void *err);
]]

  local cases = {
    {'#\\31 a2b3c {', '#1a2b3c {'}
  }

  for _,t in ipairs(cases) do
    test("Unescape " .. t[1], function()
      local olen = ffi.new('size_t[1]')
      local escaped = ffi.C.rspamd_css_unescape(pool:topointer(), t[1], #t[1], olen)
      escaped = ffi.string(escaped, tonumber(olen[0]))
      assert_equal(escaped, t[2], escaped .. " not equal " .. t[2])
    end)
  end

  local cases = {[[
p {
  color: red;
  text-align: center;
}
]],
[[
.center {
  text-align: center;
  color: red;
}]],
[[
/* This is a single-line comment */
/* This is
a multi-line
comment */

p {
  color: red;
}
]],
[[
p:first-child {
  color: blue;
}
a[target=_blank] #id{
  background-color: yellow;
}
* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

/* Style the header */
.header {
  background-color: #f1f1f1;
  padding: 20px;
  text-align: center;
}

/* Style the top navigation bar */
.topnav {
  overflow: hidden;
  background-color: #333;
}

/* Style the topnav links */
.topnav a{
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

/* Clear floats after the columns */
.row:after{
  content: "";
  display: table;
  clear: both;
}

/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
  .column.side, .column.middle {
    width: 100%;
  }
}
]],
[[
/* Colors */
p { color: rgb(100%, 50%, 0%); opacity: -1; width: 1em; display: none; } /* very transparent solid orange */
p { color: rgb(100%, 50%, 0%); opacity: 2; display: inline; } /* very transparent solid orange */
p { color: rgb(100%, 50%, 0%); opacity: 0.5; } /* very transparent solid orange */
p { color: rgb(100%, 50%, 0%); opacity: 1; width: 99%; } /* very transparent solid orange */
p { color: rgb(100%, 50%, 0%); opacity: 10%; width: 99%; } /* very transparent solid orange */
p { color: rgb(100%, 50%, 0%); opacity: 10%; width: 100px; } /* very transparent solid orange */
p { color: rgb(100%, 50%, 0%); opacity: 10% } /* very transparent solid orange */

* { color: hsl(0, 100%, 50%) !important }   /* red */
* { color: hsl(120, 100%, 50%) important } /* lime */
* { color: hsl(120, 100%, 25%) } /* dark green */
* { color: hsl(120, 100%, 75%) } /* light green */
* { color: hsl(120, 75%, 75%) }  /* pastel green, and so on */
em { color: #f00 }              /* #rgb */
em { color: #ff0000 }           /* #rrggbb */
em { color: rgb(255,0,0) }
em { color: rgb(100%, 0%, 0%) }
body {color: black; background: white }
h1 { color: maroon }
h2 { color: olive }
em { color: rgb(255,0,0) }       /* integer range 0 - 255 */
em { color: rgb(300,0,0) }       /* clipped to rgb(255,0,0) */
em { color: rgb(255,-10,0) }     /* clipped to rgb(255,0,0) */
em { color: rgb(110%, 0%, 0%) }  /* clipped to rgb(100%,0%,0%) */
em { color: rgb(255,0,0) }      /* integer range 0 - 255 */
em { color: rgba(255,0,0,1)     /* the same, with explicit opacity of 1 */
em { color: rgb(100%,0%,0%) }   /* float range 0.0% - 100.0% */
em { color: rgba(100%,0%,0%,1) } /* the same, with explicit opacity of 1 */
p { color: rgba(0,0,255,0.5) }        /* semi-transparent solid blue */
p { color: rgba(100%, 50%, 0%, 0.1) } /* very transparent solid orange */
]]
  }
  local NULL = ffi.new 'void*'
  for i,t in ipairs(cases) do
    test("Parse css sample " .. i, function()
      local escaped = ffi.C.rspamd_css_parse_style(pool:topointer(), t, #t, NULL)
      --assert_not_nil(escaped)
    end)
  end
end)