Skip to content

Commit e13c0f7

Browse files
committed
Add namespace compiler option.
1 parent 1e5a495 commit e13c0f7

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

src/compiler/compile/Component.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,8 @@ function process_component_options(component: Component, nodes) {
13491349
'accessors' in component.compile_options
13501350
? component.compile_options.accessors
13511351
: !!component.compile_options.customElement,
1352-
preserveWhitespace: !!component.compile_options.preserveWhitespace
1352+
preserveWhitespace: !!component.compile_options.preserveWhitespace,
1353+
namespace: component.compile_options.namespace
13531354
};
13541355

13551356
const node = nodes.find(node => node.name === 'svelte:options');

src/compiler/compile/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { CompileOptions, Warning } from '../interfaces';
66
import Component from './Component';
77
import fuzzymatch from '../utils/fuzzymatch';
88
import get_name_from_filename from './utils/get_name_from_filename';
9+
import { valid_namespaces } from '../utils/namespaces';
910

1011
const valid_options = [
1112
'format',
@@ -22,6 +23,7 @@ const valid_options = [
2223
'hydratable',
2324
'legacy',
2425
'customElement',
26+
'namespace',
2527
'tag',
2628
'css',
2729
'loopGuardTimeout',
@@ -30,7 +32,7 @@ const valid_options = [
3032
];
3133

3234
function validate_options(options: CompileOptions, warnings: Warning[]) {
33-
const { name, filename, loopGuardTimeout, dev } = options;
35+
const { name, filename, loopGuardTimeout, dev, namespace } = options;
3436

3537
Object.keys(options).forEach(key => {
3638
if (!valid_options.includes(key)) {
@@ -65,6 +67,15 @@ function validate_options(options: CompileOptions, warnings: Warning[]) {
6567
toString: () => message
6668
});
6769
}
70+
71+
if (namespace && valid_namespaces.indexOf(namespace) === -1) {
72+
const match = fuzzymatch(namespace, valid_namespaces);
73+
if (match) {
74+
throw new Error(`Invalid namespace '${namespace}' (did you mean '${match}'?)`);
75+
} else {
76+
throw new Error(`Invalid namespace '${namespace}'`);
77+
}
78+
}
6879
}
6980

7081
export default function compile(source: string, options: CompileOptions = {}) {

src/compiler/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export interface CompileOptions {
124124
tag?: string;
125125
css?: boolean;
126126
loopGuardTimeout?: number;
127+
namespace?: string;
127128

128129
preserveComments?: boolean;
129130
preserveWhitespace?: boolean;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Test support for the native namespaces preserving attribute case (eg svelte-native, svelte-nodegui).
2+
3+
export default {
4+
html: `
5+
<page horizontalAlignment="center">
6+
<button textWrap="true" text="button">
7+
</page>
8+
`,
9+
options: {
10+
hydrate: false // Hydration test will fail as case sensitivity is only handled for svg elements.
11+
},
12+
compileOptions: {
13+
namespace: 'foreign'
14+
},
15+
test({ assert, target }) {
16+
const attr = sel => target.querySelector(sel).attributes[0].name;
17+
assert.equal(attr('page'), 'horizontalAlignment');
18+
assert.equal(attr('button'), 'textWrap');
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<page horizontalAlignment="center">
2+
<button textWrap="true" text="button">
3+
</page>

test/validator/index.ts

+18
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,22 @@ describe('validate', () => {
101101

102102
assert.deepEqual(warnings, []);
103103
});
104+
105+
it('errors if namespace is provided but unrecognised', () => {
106+
assert.throws(() => {
107+
svelte.compile('<div></div>', {
108+
name: 'test',
109+
namespace: 'svefefe'
110+
});
111+
}, /Invalid namespace 'svefefe'/);
112+
});
113+
114+
it('errors with a hint if namespace is provided but unrecognised but close', () => {
115+
assert.throws(() => {
116+
svelte.compile('<div></div>', {
117+
name: 'test',
118+
namespace: 'foriegn'
119+
});
120+
}, /Invalid namespace 'foriegn' \(did you mean 'foreign'\?\)/);
121+
});
104122
});

0 commit comments

Comments
 (0)