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

support is attribute, with a warning. #3418

Merged
merged 1 commit into from
Aug 18, 2019
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
7 changes: 7 additions & 0 deletions src/compiler/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,13 @@ export default class Element extends Node {
}
}

if (name === 'is') {
component.warn(attribute, {
code: 'avoid-is',
message: `The 'is' attribute is not supported cross-browser and should be avoided`
});
}

attribute_map.set(attribute.name, attribute);
});

Expand Down
5 changes: 5 additions & 0 deletions src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ export default class ElementWrapper extends Wrapper {
return `@_document.createElementNS("${namespace}", "${name}")`;
}

const is = this.attributes.find(attr => attr.node.name === 'is');
if (is) {
return `@element_is("${name}", ${is.render_chunks().join(' + ')});`
}

return `@element("${name}")`;
}

Expand Down
4 changes: 4 additions & 0 deletions src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export function element<K extends keyof HTMLElementTagNameMap>(name: K) {
return document.createElement<K>(name);
}

export function element_is<K extends keyof HTMLElementTagNameMap>(name: K, is: string) {
return document.createElement<K>(name, { is });
}

export function object_without_properties<T, K extends keyof T>(obj: T, exclude: K[]) {
// eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion
const target = {} as Pick<T, Exclude<keyof T, K>>;
Expand Down
17 changes: 17 additions & 0 deletions test/custom-elements/samples/extended-builtin/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default {
warnings: [{
code: "avoid-is",
message: "The 'is' attribute is not supported cross-browser and should be avoided",
pos: 97,
start: {
character: 97,
column: 8,
line: 7
},
end: {
character: 114,
column: 25,
line: 7
}
}]
};
2 changes: 2 additions & 0 deletions test/custom-elements/samples/extended-builtin/fancy-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class FancyButton extends HTMLButtonElement {}
customElements.define('fancy-button', FancyButton, { extends: 'button' });
7 changes: 7 additions & 0 deletions test/custom-elements/samples/extended-builtin/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<svelte:options tag="custom-element"/>

<script>
import './fancy-button.js';
</script>

<button is="fancy-button">click me</button>
15 changes: 15 additions & 0 deletions test/custom-elements/samples/extended-builtin/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as assert from 'assert';
import CustomElement from './main.svelte';

export default function (target) {
new CustomElement({
target
});

assert.equal(target.innerHTML, '<custom-element></custom-element>');

const el = target.querySelector('custom-element');
const button = el.shadowRoot.querySelector('button');

assert.ok(button instanceof customElements.get('fancy-button'));
}