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

Introduce the foreign namespace to preserve attribute case and skip html specific a11y validations #5652

Merged
merged 8 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion site/content/docs/02-template-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,7 @@ The `<svelte:options>` element provides a place to specify per-component compile
* `immutable={false}` — the default. Svelte will be more conservative about whether or not mutable objects have changed
* `accessors={true}` — adds getters and setters for the component's props
* `accessors={false}` — the default
* `namespace="..."` — the namespace where this component will be used, most commonly "svg"
* `namespace="..."` — the namespace where this component will be used, most commonly "svg". (Use the "foreign" namespace to opt out of case insensitive attribute names and html specific warnings)
* `tag="..."` — the name to use when compiling this component as a custom element

```sv
Expand Down
2 changes: 1 addition & 1 deletion site/content/docs/04-compile-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ The following options can be passed to the compiler. None are required:
| `outputFilename` | `null` | A `string` used for your JavaScript sourcemap.
| `cssOutputFilename` | `null` | A `string` used for your CSS sourcemap.
| `sveltePath` | `"svelte"` | The location of the `svelte` package. Any imports from `svelte` or `svelte/[module]` will be modified accordingly.

| `namespace` | `null` | The namespace of the element if not `html` (eg `mathml`, `svg`, `foreign`).
halfnelson marked this conversation as resolved.
Show resolved Hide resolved

---

Expand Down
3 changes: 2 additions & 1 deletion src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,8 @@ function process_component_options(component: Component, nodes) {
'accessors' in component.compile_options
? component.compile_options.accessors
: !!component.compile_options.customElement,
preserveWhitespace: !!component.compile_options.preserveWhitespace
preserveWhitespace: !!component.compile_options.preserveWhitespace,
namespace: component.compile_options.namespace
};

const node = nodes.find(node => node.name === 'svelte:options');
Expand Down
13 changes: 12 additions & 1 deletion src/compiler/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CompileOptions, Warning } from '../interfaces';
import Component from './Component';
import fuzzymatch from '../utils/fuzzymatch';
import get_name_from_filename from './utils/get_name_from_filename';
import { valid_namespaces } from '../utils/namespaces';

const valid_options = [
'format',
Expand All @@ -22,6 +23,7 @@ const valid_options = [
'hydratable',
'legacy',
'customElement',
'namespace',
'tag',
'css',
'loopGuardTimeout',
Expand All @@ -30,7 +32,7 @@ const valid_options = [
];

function validate_options(options: CompileOptions, warnings: Warning[]) {
const { name, filename, loopGuardTimeout, dev } = options;
const { name, filename, loopGuardTimeout, dev, namespace } = options;

Object.keys(options).forEach(key => {
if (!valid_options.includes(key)) {
Expand Down Expand Up @@ -65,6 +67,15 @@ function validate_options(options: CompileOptions, warnings: Warning[]) {
toString: () => message
});
}

if (namespace && valid_namespaces.indexOf(namespace) === -1) {
const match = fuzzymatch(namespace, valid_namespaces);
if (match) {
throw new Error(`Invalid namespace '${namespace}' (did you mean '${match}'?)`);
} else {
throw new Error(`Invalid namespace '${namespace}'`);
}
}
}

export default function compile(source: string, options: CompileOptions = {}) {
Expand Down
Loading