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: always use document.importNode for template cloning #11592

Closed
wants to merge 2 commits into from
Closed
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: 5 additions & 0 deletions .changeset/shaggy-feet-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: always use document.importNode for template cloning
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
EACH_ITEM_REACTIVE,
EACH_KEYED,
TEMPLATE_FRAGMENT,
TEMPLATE_USE_IMPORT_NODE,
TRANSITION_GLOBAL,
TRANSITION_IN,
TRANSITION_OUT
Expand Down Expand Up @@ -1077,10 +1076,6 @@ function create_block(parent, name, nodes, context) {
/** @type {import('estree').Expression[]} */
const args = [b.template([b.quasi(state.template.join(''), true)], [])];

if (state.metadata.context.template_needs_import_node) {
args.push(b.literal(TEMPLATE_USE_IMPORT_NODE));
}

add_template(template_name, args);

body.push(b.var(id, b.call(template_name)), ...state.before_init, ...state.init);
Expand Down Expand Up @@ -1121,10 +1116,6 @@ function create_block(parent, name, nodes, context) {
} else {
let flags = TEMPLATE_FRAGMENT;

if (state.metadata.context.template_needs_import_node) {
flags |= TEMPLATE_USE_IMPORT_NODE;
}

add_template(template_name, [
b.template([b.quasi(state.template.join(''), true)], []),
b.literal(flags)
Expand Down
1 change: 0 additions & 1 deletion packages/svelte/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const TRANSITION_OUT = 1 << 1;
export const TRANSITION_GLOBAL = 1 << 2;

export const TEMPLATE_FRAGMENT = 1;
export const TEMPLATE_USE_IMPORT_NODE = 1 << 1;

export const HYDRATION_START = '[';
export const HYDRATION_END = ']';
Expand Down
15 changes: 0 additions & 15 deletions packages/svelte/src/internal/client/dom/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ var text_prototype;
/** @type {typeof Node.prototype.appendChild} */
var append_child_method;

/** @type {typeof Node.prototype.cloneNode} */
var clone_node_method;

/** @type {(this: Node) => ChildNode | null} */
var first_child_get;

Expand Down Expand Up @@ -56,7 +53,6 @@ export function init_operations() {
text_prototype = Text.prototype;

append_child_method = node_prototype.appendChild;
clone_node_method = node_prototype.cloneNode;

$window = window;
$document = document;
Expand Down Expand Up @@ -107,17 +103,6 @@ export function append_child(element, child) {
append_child_method.call(element, child);
}

/**
* @template {Node} N
* @param {N} node
* @param {boolean} deep
* @returns {N}
*/
/*#__NO_SIDE_EFFECTS__*/
export function clone_node(node, deep) {
return /** @type {N} */ (clone_node_method.call(node, deep));
}

/** @returns {Text} */
export function empty() {
return document.createTextNode('');
Expand Down
11 changes: 5 additions & 6 deletions packages/svelte/src/internal/client/dom/template.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { hydrate_nodes, hydrating } from './hydration.js';
import { clone_node, empty } from './operations.js';
import { empty } from './operations.js';
import { create_fragment_from_html } from './reconciler.js';
import { current_effect } from '../runtime.js';
import { TEMPLATE_FRAGMENT, TEMPLATE_USE_IMPORT_NODE } from '../../../constants.js';
import { TEMPLATE_FRAGMENT } from '../../../constants.js';
import { effect } from '../reactivity/effects.js';
import { is_array } from '../utils.js';

Expand Down Expand Up @@ -41,7 +41,6 @@ export function push_template_node(
/*#__NO_SIDE_EFFECTS__*/
export function template(content, flags) {
var is_fragment = (flags & TEMPLATE_FRAGMENT) !== 0;
var use_import_node = (flags & TEMPLATE_USE_IMPORT_NODE) !== 0;

/** @type {Node} */
var node;
Expand All @@ -55,7 +54,7 @@ export function template(content, flags) {
node = create_fragment_from_html(content);
if (!is_fragment) node = /** @type {Node} */ (node.firstChild);
}
var clone = use_import_node ? document.importNode(node, true) : clone_node(node, true);
var clone = document.importNode(node, true);

push_template_node(
is_fragment
Expand Down Expand Up @@ -122,7 +121,7 @@ export function svg_template(content, flags) {
}
}

var clone = clone_node(node, true);
var clone = document.importNode(node, true);

push_template_node(
is_fragment
Expand Down Expand Up @@ -189,7 +188,7 @@ export function mathml_template(content, flags) {
}
}

var clone = clone_node(node, true);
var clone = document.importNode(node, true);

push_template_node(
is_fragment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "svelte/internal/disclose-version";
import * as $ from "svelte/internal/client";

var root = $.template(`<div></div> <svg></svg> <custom-element></custom-element> <div></div> <svg></svg> <custom-element></custom-element>`, 3);
var root = $.template(`<div></div> <svg></svg> <custom-element></custom-element> <div></div> <svg></svg> <custom-element></custom-element>`, 1);

export default function Main($$anchor) {
// needs to be a snapshot test because jsdom does auto-correct the attribute casing
Expand Down
Loading