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

[fix] fix 'src' attribute judge logic (#6575) #6580

Merged
merged 1 commit into from
Aug 27, 2021
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
5 changes: 3 additions & 2 deletions src/compiler/compile/render_dom/wrappers/Element/Attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ export default class AttributeWrapper extends BaseAttributeWrapper {
this.is_select_value_attribute = this.name === 'value' && this.parent.node.name === 'select';
this.is_input_value = this.name === 'value' && this.parent.node.name === 'input';
}

this.is_src = this.name === 'src'; // TODO retire this exception in favour of https://github.com/sveltejs/svelte/issues/3750

// TODO retire this exception in favour of https://github.com/sveltejs/svelte/issues/3750
this.is_src = this.name === 'src' && (!this.parent.node.namespace || this.parent.node.namespace === namespaces.html);
this.should_cache = should_cache(this);
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export function get_root_for_style(node: Node): ShadowRoot | Document {
if (!node) return document;

const root = node.getRootNode ? node.getRootNode() : node.ownerDocument;
if ((root as ShadowRoot).host) {
if (root && (root as ShadowRoot).host) {
Copy link
Member

@Conduitry Conduitry Aug 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this change intended to address? This seems unrelated to the src attribute handling mentioned in #6575.

Copy link
Member Author

@baseballyama baseballyama Aug 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return of node.ownerDocument is possibly null.
And in the case of Svelte Native returns null at least in this issue #6575.
Therefore it needs to check nullability.

return root as ShadowRoot;
}
return document;
Expand Down
5 changes: 5 additions & 0 deletions test/js/samples/src-attribute-check-in-foreign/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
options: {
hydratable: true
}
};
70 changes: 70 additions & 0 deletions test/js/samples/src-attribute-check-in-foreign/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* generated by Svelte vX.Y.Z */
import {
SvelteComponent,
append_hydration,
attr,
children,
claim_element,
detach,
init,
insert_hydration,
noop,
safe_not_equal
} from "svelte/internal";

function create_fragment(ctx) {
let svg;
let img;

return {
c() {
svg = document.createElementNS("https://svelte.dev/docs#svelte_options", "svg");
img = document.createElementNS("https://svelte.dev/docs#svelte_options", "img");
this.h();
},
l(nodes) {
svg = claim_element(nodes, "svg", {});
var svg_nodes = children(svg);
img = claim_element(svg_nodes, "img", { alt: true, src: true });
svg_nodes.forEach(detach);
this.h();
},
h() {
attr(img, "alt", "potato");
attr(img, "src", /*url*/ ctx[0]);
},
m(target, anchor) {
insert_hydration(target, svg, anchor);
append_hydration(svg, img);
},
p(ctx, [dirty]) {
if (dirty & /*url*/ 1) {
attr(img, "src", /*url*/ ctx[0]);
}
},
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(svg);
}
};
}

function instance($$self, $$props, $$invalidate) {
let { url } = $$props;

$$self.$$set = $$props => {
if ('url' in $$props) $$invalidate(0, url = $$props.url);
};

return [url];
}

class Component extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, create_fragment, safe_not_equal, { url: 0 });
}
}

export default Component;
9 changes: 9 additions & 0 deletions test/js/samples/src-attribute-check-in-foreign/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<svelte:options namespace="foreign" />

<script>
export let url;
</script>

<svg>
<img alt="potato" src={url}>
</svg>
5 changes: 5 additions & 0 deletions test/js/samples/src-attribute-check-in-svg/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
options: {
hydratable: true
}
};
71 changes: 71 additions & 0 deletions test/js/samples/src-attribute-check-in-svg/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* generated by Svelte vX.Y.Z */
import {
SvelteComponent,
append_hydration,
attr,
children,
claim_element,
detach,
init,
insert_hydration,
noop,
safe_not_equal,
svg_element
} from "svelte/internal";

function create_fragment(ctx) {
let svg;
let img;

return {
c() {
svg = svg_element("svg");
img = svg_element("img");
this.h();
},
l(nodes) {
svg = claim_element(nodes, "svg", {}, 1);
var svg_nodes = children(svg);
img = claim_element(svg_nodes, "img", { alt: true, src: true }, 1);
svg_nodes.forEach(detach);
this.h();
},
h() {
attr(img, "alt", "potato");
attr(img, "src", /*url*/ ctx[0]);
},
m(target, anchor) {
insert_hydration(target, svg, anchor);
append_hydration(svg, img);
},
p(ctx, [dirty]) {
if (dirty & /*url*/ 1) {
attr(img, "src", /*url*/ ctx[0]);
}
},
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(svg);
}
};
}

function instance($$self, $$props, $$invalidate) {
let { url } = $$props;

$$self.$$set = $$props => {
if ('url' in $$props) $$invalidate(0, url = $$props.url);
};

return [url];
}

class Component extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, create_fragment, safe_not_equal, { url: 0 });
}
}

export default Component;
7 changes: 7 additions & 0 deletions test/js/samples/src-attribute-check-in-svg/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
export let url;
</script>

<svg>
<img alt="potato" src={url}>
</svg>