Skip to content

Commit b59ae94

Browse files
committed
test: add tests for resource
1 parent 32d3483 commit b59ae94

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

lib/parsers.test.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,46 @@ describe('parseDashedIdentifier', () => {
298298
describe('parseImage', () => {
299299
it.todo('tests');
300300
});
301-
describe('parseUrl', () => {
301+
describe('parseResource', () => {
302+
it('returns undefined for invalid values', () => {
303+
const invalid = [
304+
// Empty (new in CSS Values 4, still valid some browsers)
305+
'url()',
306+
'url( )',
307+
'url("")',
308+
'src("")',
309+
'url(', // -> parsed to url("") by browsers
310+
// Invalid
311+
'uurl(valid.url)',
312+
'url(valid.url))',
313+
'url(")', // TOFIX (valid at computed time): url(")")
314+
"url(')", // TOFIX (valid at computed time): url(')')
315+
'url(val"id.url)',
316+
"url(val'id.url)",
317+
'url(val(id.url)',
318+
'url(val)id.url)',
319+
'url(\\)', // TOFIX (valid at computed time): url(")")
320+
'url(val\\\nid.url)', // ie. not a valid escape sequence
321+
'url(var(--url))',
322+
'src()', // ie. not a string
323+
'src(unquoted)',
324+
];
325+
invalid.forEach(input => expect(parsers.parseResource(input)).toBeUndefined());
326+
});
327+
it('parses a resource with an escape sequence', () => {
328+
// \0 is the only valid hexDigit escape sequence in strict mode
329+
expect(parsers.parseResource('url(file\\0 \\g.jpg)')).toBe('url("file\\0\\g.jpg")');
330+
expect(parsers.parseResource(`url(${String.fromCharCode(33)})`)).toBe('url("!")');
331+
});
332+
it('returns resource wrapped between double quotes', () => {
333+
expect(parsers.parseResource('url(file.jpg)')).toBe('url("file.jpg")');
334+
expect(parsers.parseResource("url('file.jpg')")).toBe('url("file.jpg")');
335+
expect(parsers.parseResource("src('file.jpg')")).toBe('src("file.jpg")');
336+
});
302337
it('works with custom variable', () => {
303-
expect(parsers.parseUrl('var(--url)')).toBe('var(--url)');
304-
expect(parsers.parseUrl('src(var(--url))')).toBe('src(var(--url))');
338+
expect(parsers.parseResource('var(--url)')).toBe('var(--url)');
339+
expect(parsers.parseResource('src(var(--url))')).toBe('src(var(--url))');
305340
});
306-
it.todo('more tests');
307341
});
308342
describe('parseString', () => {
309343
it('returns undefined for invalid values', () => {

0 commit comments

Comments
 (0)