Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for containing URL support #1936

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion js-api-spec/importer.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
// https://opensource.org/licenses/MIT.

import {URL} from 'url';
import {compile, compileString, compileStringAsync, Importer} from 'sass';
import {
compile,
compileString,
compileStringAsync,
CanonicalizeContext,
Importer,
} from 'sass';

import {sandbox} from './sandbox';

Expand Down Expand Up @@ -278,6 +284,62 @@ describe('FileImporter', () => {
}));
});

describe('containingUrl is', () => {
it('set for a relative URL', () =>
sandbox(dir => {
dir.write({'_other.css': 'a {b: c}'});
const result = compileString('@import "other";', {
importers: [
{
findFileUrl: (url: string, context: CanonicalizeContext) => {
expect(context.containingUrl).toEqual(
new URL('x:original.scss')
);
return dir.url('other');
},
},
],
url: new URL('x:original.scss'),
});
expect(result.css).toBe('a {\n b: c;\n}');
}));

it('set for an absolute URL', () =>
sandbox(dir => {
dir.write({'_other.css': 'a {b: c}'});
const result = compileString('@import "u:other";', {
importers: [
{
findFileUrl: (url: string, context: CanonicalizeContext) => {
expect(context.containingUrl).toEqual(
new URL('x:original.scss')
);
return dir.url('other');
},
},
],
url: new URL('x:original.scss'),
});
expect(result.css).toBe('a {\n b: c;\n}');
}));

it('unset when the URL is unavailable', () =>
sandbox(dir => {
dir.write({'_other.css': 'a {b: c}'});
const result = compileString('@import "u:other";', {
importers: [
{
findFileUrl: (url: string, context: CanonicalizeContext) => {
expect(context.containingUrl).toBeNull();
return dir.url('other');
},
},
],
});
expect(result.css).toBe('a {\n b: c;\n}');
}));
});

describe('async', () => {
it('resolves an @import', async () =>
sandbox(async dir => {
Expand Down
217 changes: 216 additions & 1 deletion js-api-spec/importer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import {compileString, compileStringAsync, Importer} from 'sass';
import {
compileString,
compileStringAsync,
CanonicalizeContext,
Importer,
} from 'sass';

import {sassImpl, URL} from './utils';

Expand Down Expand Up @@ -101,6 +106,216 @@ describe('the imported URL', () => {
});
});

describe('the containing URL', () => {
it('is null for a potentially canonical scheme', () => {
const result = compileString('@import "u:orange"', {
importers: [
{
canonicalize: (url: string, context: CanonicalizeContext) => {
expect(context.containingUrl).toBeNull();
return new URL(url);
},
load: () => ({contents: '@a', syntax: 'scss'}),
},
],
});

expect(result.css).toBe('@a;');
});

describe('for a non-canonical scheme', () => {
describe('in a list', () => {
it('is set to the original URL', () => {
const result = compileString('@import "u:orange"', {
importers: [
{
canonicalize: (url: string, context: CanonicalizeContext) => {
expect(context.containingUrl).toEqual(
new URL('x:original.scss')
);
return new URL(url.replace(/^u:/, 'x:'));
},
load: () => ({contents: '@a', syntax: 'scss'}),
nonCanonicalScheme: ['u'],
},
],
url: new URL('x:original.scss'),
});

expect(result.css).toBe('@a;');
});

it('is null if the original URL is null', () => {
const result = compileString('@import "u:orange"', {
importers: [
{
canonicalize: (url: string, context: CanonicalizeContext) => {
expect(context.containingUrl).toBeNull();
return new URL(url.replace(/^u:/, 'x:'));
},
load: () => ({contents: '@a', syntax: 'scss'}),
nonCanonicalScheme: ['u'],
},
],
});

expect(result.css).toBe('@a;');
});
});

describe('as a string', () => {
it('is set to the original URL', () => {
const result = compileString('@import "u:orange"', {
importers: [
{
canonicalize: (url: string, context: CanonicalizeContext) => {
expect(context.containingUrl).toEqual(
new URL('x:original.scss')
);
return new URL(url.replace(/^u:/, 'x:'));
},
load: () => ({contents: '@a', syntax: 'scss'}),
nonCanonicalScheme: 'u',
},
],
url: new URL('x:original.scss'),
});

expect(result.css).toBe('@a;');
});

it('is null if the original URL is null', () => {
const result = compileString('@import "u:orange"', {
importers: [
{
canonicalize: (url: string, context: CanonicalizeContext) => {
expect(context.containingUrl).toBeNull();
return new URL(url.replace(/^u:/, 'x:'));
},
load: () => ({contents: '@a', syntax: 'scss'}),
nonCanonicalScheme: 'u',
},
],
});

expect(result.css).toBe('@a;');
});
});
});

describe('for a schemeless load', () => {
it('is set to the original URL', () => {
const result = compileString('@import "orange"', {
importers: [
{
canonicalize: (url: string, context: CanonicalizeContext) => {
expect(context.containingUrl).toEqual(new URL('x:original.scss'));
return new URL(`u:${url}`);
},
load: () => ({contents: '@a', syntax: 'scss'}),
},
],
url: new URL('x:original.scss'),
});

expect(result.css).toBe('@a;');
});

it('is null if the original URL is null', () => {
const result = compileString('@import "orange"', {
importers: [
{
canonicalize: (url: string, context: CanonicalizeContext) => {
expect(context.containingUrl).toBeNull();
return new URL(`u:${url}`);
},
load: () => ({contents: '@a', syntax: 'scss'}),
},
],
});

expect(result.css).toBe('@a;');
});
});
});

describe(
'throws an error if the importer returns a canonical URL with a ' +
'non-canonical scheme',
() => {
it('set as a list', () =>
expect(() =>
compileString('@import "orange"', {
importers: [
{
canonicalize: (url: string) => {
return new URL(`u:${url}`);
},
load: () => ({contents: '@a', syntax: 'scss'}),
nonCanonicalScheme: ['u'],
},
],
})
).toThrowSassException({line: 0}));

it('set as a string', () =>
expect(() =>
compileString('@import "orange"', {
importers: [
{
canonicalize: (url: string) => {
return new URL(`u:${url}`);
},
load: () => ({contents: '@a', syntax: 'scss'}),
nonCanonicalScheme: 'u',
},
],
})
).toThrowSassException({line: 0}));
}
);

describe('throws an error for an invalid scheme:', () => {
it('empty', () =>
expect(() =>
compileString('a {b: c}', {
importers: [
{
canonicalize: () => null,
load: () => ({contents: '@a', syntax: 'scss'}),
nonCanonicalScheme: '',
},
],
})
).toThrow());

it('uppercase', () =>
expect(() =>
compileString('a {b: c}', {
importers: [
{
canonicalize: () => null,
load: () => ({contents: '@a', syntax: 'scss'}),
nonCanonicalScheme: 'U',
},
],
})
).toThrow());

it('colon', () =>
expect(() =>
compileString('a {b: c}', {
importers: [
{
canonicalize: () => null,
load: () => ({contents: '@a', syntax: 'scss'}),
nonCanonicalScheme: 'u:',
},
],
})
).toThrow());
});

it("uses an importer's source map URL", () => {
const result = compileString('@import "orange";', {
importers: [
Expand Down