Lint and format ./src/runtime/internal/dom.js

pull/8569/head
Simon Holthausen 3 years ago
parent 8e03ef9fb9
commit 67bcdc6df8

@ -29,8 +29,7 @@ function upper_bound(low, high, key, value) {
const mid = low + ((high - low) >> 1); const mid = low + ((high - low) >> 1);
if (key(mid) <= value) { if (key(mid) <= value) {
low = mid + 1; low = mid + 1;
} } else {
else {
high = mid; high = mid;
} }
} }
@ -42,8 +41,7 @@ function upper_bound(low, high, key, value) {
* @returns {void} * @returns {void}
*/ */
function init_hydrate(target) { function init_hydrate(target) {
if (target.hydrate_init) if (target.hydrate_init) return;
return;
target.hydrate_init = true; target.hydrate_init = true;
// We know that all children have claim_order values since the unclaimed have been detached if target is not <head> // We know that all children have claim_order values since the unclaimed have been detached if target is not <head>
@ -89,7 +87,8 @@ function init_hydrate(target) {
// Find the largest subsequence length such that it ends in a value less than our current value // Find the largest subsequence length such that it ends in a value less than our current value
// upper_bound returns first greater value, so we subtract one // upper_bound returns first greater value, so we subtract one
// with fast path for when we are on the current longest subsequence // with fast path for when we are on the current longest subsequence
const seqLen = (longest > 0 && children[m[longest]].claim_order <= current const seqLen =
(longest > 0 && children[m[longest]].claim_order <= current
? longest + 1 ? longest + 1
: upper_bound(1, longest, (idx) => children[m[idx]].claim_order, current)) - 1; : upper_bound(1, longest, (idx) => children[m[idx]].claim_order, current)) - 1;
p[i] = m[seqLen] + 1; p[i] = m[seqLen] + 1;
@ -162,8 +161,7 @@ export function append_styles(target, style_sheet_id, styles) {
* @returns {ShadowRoot | Document} * @returns {ShadowRoot | Document}
*/ */
export function get_root_for_style(node) { export function get_root_for_style(node) {
if (!node) if (!node) return document;
return document;
const root = node.getRootNode ? node.getRootNode() : node.ownerDocument; const root = node.getRootNode ? node.getRootNode() : node.ownerDocument;
if (root && root.host) { if (root && root.host) {
return root; return root;
@ -205,8 +203,10 @@ function append_stylesheet(node, style) {
export function append_hydration(target, node) { export function append_hydration(target, node) {
if (is_hydrating) { if (is_hydrating) {
init_hydrate(target); init_hydrate(target);
if (target.actual_end_child === undefined || if (
(target.actual_end_child !== null && target.actual_end_child.parentNode !== target)) { target.actual_end_child === undefined ||
(target.actual_end_child !== null && target.actual_end_child.parentNode !== target)
) {
target.actual_end_child = target.firstChild; target.actual_end_child = target.firstChild;
} }
// Skip nodes of undefined ordering // Skip nodes of undefined ordering
@ -218,12 +218,10 @@ export function append_hydration(target, node) {
if (node.claim_order !== undefined || node.parentNode !== target) { if (node.claim_order !== undefined || node.parentNode !== target) {
target.insertBefore(node, target.actual_end_child); target.insertBefore(node, target.actual_end_child);
} }
} } else {
else {
target.actual_end_child = node.nextSibling; target.actual_end_child = node.nextSibling;
} }
} } else if (node.parentNode !== target || node.nextSibling !== null) {
else if (node.parentNode !== target || node.nextSibling !== null) {
target.appendChild(node); target.appendChild(node);
} }
} }
@ -247,8 +245,7 @@ export function insert(target, node, anchor) {
export function insert_hydration(target, node, anchor) { export function insert_hydration(target, node, anchor) {
if (is_hydrating && !anchor) { if (is_hydrating && !anchor) {
append_hydration(target, node); append_hydration(target, node);
} } else if (node.parentNode !== target || node.nextSibling != anchor) {
else if (node.parentNode !== target || node.nextSibling != anchor) {
target.insertBefore(node, anchor || null); target.insertBefore(node, anchor || null);
} }
} }
@ -267,8 +264,7 @@ export function detach(node) {
* @returns {void} */ * @returns {void} */
export function destroy_each(iterations, detaching) { export function destroy_each(iterations, detaching) {
for (let i = 0; i < iterations.length; i += 1) { for (let i = 0; i < iterations.length; i += 1) {
if (iterations[i]) if (iterations[i]) iterations[i].d(detaching);
iterations[i].d(detaching);
} }
} }
@ -297,9 +293,11 @@ export function element_is(name, is) {
export function object_without_properties(obj, exclude) { export function object_without_properties(obj, exclude) {
const target = {}; const target = {};
for (const k in obj) { for (const k in obj) {
if (has_prop(obj, k) && if (
has_prop(obj, k) &&
// @ts-ignore // @ts-ignore
exclude.indexOf(k) === -1) { exclude.indexOf(k) === -1
) {
// @ts-ignore // @ts-ignore
target[k] = obj[k]; target[k] = obj[k];
} }
@ -390,8 +388,7 @@ export function stop_immediate_propagation(fn) {
export function self(fn) { export function self(fn) {
return function (event) { return function (event) {
// @ts-ignore // @ts-ignore
if (event.target === this) if (event.target === this) fn.call(this, event);
fn.call(this, event);
}; };
} }
@ -400,8 +397,7 @@ export function self(fn) {
export function trusted(fn) { export function trusted(fn) {
return function (event) { return function (event) {
// @ts-ignore // @ts-ignore
if (event.isTrusted) if (event.isTrusted) fn.call(this, event);
fn.call(this, event);
}; };
} }
@ -412,10 +408,8 @@ export function trusted(fn) {
* @returns {void} * @returns {void}
*/ */
export function attr(node, attribute, value) { export function attr(node, attribute, value) {
if (value == null) if (value == null) node.removeAttribute(attribute);
node.removeAttribute(attribute); else if (node.getAttribute(attribute) !== value) node.setAttribute(attribute, value);
else if (node.getAttribute(attribute) !== value)
node.setAttribute(attribute, value);
} }
/** /**
* List of attributes that should always be set through the attr method, * List of attributes that should always be set through the attr method,
@ -437,19 +431,17 @@ export function set_attributes(node, attributes) {
for (const key in attributes) { for (const key in attributes) {
if (attributes[key] == null) { if (attributes[key] == null) {
node.removeAttribute(key); node.removeAttribute(key);
} } else if (key === 'style') {
else if (key === 'style') {
node.style.cssText = attributes[key]; node.style.cssText = attributes[key];
} } else if (key === '__value') {
else if (key === '__value') {
node.value = node[key] = attributes[key]; node.value = node[key] = attributes[key];
} } else if (
else if (descriptors[key] && descriptors[key] &&
descriptors[key].set && descriptors[key].set &&
always_set_through_set_attribute.indexOf(key) === -1) { always_set_through_set_attribute.indexOf(key) === -1
) {
node[key] = attributes[key]; node[key] = attributes[key];
} } else {
else {
attr(node, key, attributes[key]); attr(node, key, attributes[key]);
} }
} }
@ -481,8 +473,7 @@ export function set_custom_element_data_map(node, data_map) {
export function set_custom_element_data(node, prop, value) { export function set_custom_element_data(node, prop, value) {
if (prop in node) { if (prop in node) {
node[prop] = typeof node[prop] === 'boolean' && value === '' ? true : value; node[prop] = typeof node[prop] === 'boolean' && value === '' ? true : value;
} } else {
else {
attr(node, prop, value); attr(node, prop, value);
} }
} }
@ -514,8 +505,7 @@ export function get_svelte_dataset(node) {
export function get_binding_group_value(group, __value, checked) { export function get_binding_group_value(group, __value, checked) {
const value = new Set(); const value = new Set();
for (let i = 0; i < group.length; i += 1) { for (let i = 0; i < group.length; i += 1) {
if (group[i].checked) if (group[i].checked) value.add(group[i].__value);
value.add(group[i].__value);
} }
if (!checked) { if (!checked) {
value.delete(__value); value.delete(__value);
@ -528,7 +518,6 @@ export function get_binding_group_value(group, __value, checked) {
* @returns {{ p(...inputs: HTMLInputElement[]): void; r(): void; }} * @returns {{ p(...inputs: HTMLInputElement[]): void; r(): void; }}
*/ */
export function init_binding_group(group) { export function init_binding_group(group) {
/** /**
* @type {HTMLInputElement[]} */ * @type {HTMLInputElement[]} */
let _inputs; let _inputs;
@ -548,7 +537,6 @@ export function init_binding_group(group) {
* @returns {{ u(new_indexes: number[]): void; p(...inputs: HTMLInputElement[]): void; r: () => void; }} * @returns {{ u(new_indexes: number[]): void; p(...inputs: HTMLInputElement[]): void; r: () => void; }}
*/ */
export function init_binding_group_dynamic(group, indexes) { export function init_binding_group_dynamic(group, indexes) {
/** /**
* @type {HTMLInputElement[]} */ * @type {HTMLInputElement[]} */
let _group = get_binding_group(group); let _group = get_binding_group(group);
@ -648,8 +636,7 @@ function claim_node(nodes, predicate, processNode, createNode, dontUpdateLastInd
const replacement = processNode(node); const replacement = processNode(node);
if (replacement === undefined) { if (replacement === undefined) {
nodes.splice(i, 1); nodes.splice(i, 1);
} } else {
else {
nodes[i] = replacement; nodes[i] = replacement;
} }
if (!dontUpdateLastIndex) { if (!dontUpdateLastIndex) {
@ -666,14 +653,12 @@ function claim_node(nodes, predicate, processNode, createNode, dontUpdateLastInd
const replacement = processNode(node); const replacement = processNode(node);
if (replacement === undefined) { if (replacement === undefined) {
nodes.splice(i, 1); nodes.splice(i, 1);
} } else {
else {
nodes[i] = replacement; nodes[i] = replacement;
} }
if (!dontUpdateLastIndex) { if (!dontUpdateLastIndex) {
nodes.claim_info.last_index = i; nodes.claim_info.last_index = i;
} } else if (replacement === undefined) {
else if (replacement === undefined) {
// Since we spliced before the last_index, we decrease it // Since we spliced before the last_index, we decrease it
nodes.claim_info.last_index--; nodes.claim_info.last_index--;
} }
@ -696,7 +681,10 @@ function claim_node(nodes, predicate, processNode, createNode, dontUpdateLastInd
* @returns {Element | SVGElement} * @returns {Element | SVGElement}
*/ */
function claim_element_base(nodes, name, attributes, create_element) { function claim_element_base(nodes, name, attributes, create_element) {
return claim_node(nodes, (node) => node.nodeName === name, (node) => { return claim_node(
nodes,
(node) => node.nodeName === name,
(node) => {
const remove = []; const remove = [];
for (let j = 0; j < node.attributes.length; j++) { for (let j = 0; j < node.attributes.length; j++) {
const attribute = node.attributes[j]; const attribute = node.attributes[j];
@ -706,7 +694,9 @@ function claim_element_base(nodes, name, attributes, create_element) {
} }
remove.forEach((v) => node.removeAttribute(v)); remove.forEach((v) => node.removeAttribute(v));
return undefined; return undefined;
}, () => create_element(name)); },
() => create_element(name)
);
} }
/** /**
@ -734,17 +724,21 @@ export function claim_svg_element(nodes, name, attributes) {
* @returns {Text} * @returns {Text}
*/ */
export function claim_text(nodes, data) { export function claim_text(nodes, data) {
return claim_node(nodes, (node) => node.nodeType === 3, (node) => { return claim_node(
nodes,
(node) => node.nodeType === 3,
(node) => {
const dataStr = '' + data; const dataStr = '' + data;
if (node.data.startsWith(dataStr)) { if (node.data.startsWith(dataStr)) {
if (node.data.length !== dataStr.length) { if (node.data.length !== dataStr.length) {
return node.splitText(dataStr.length); return node.splitText(dataStr.length);
} }
} } else {
else {
node.data = dataStr; node.data = dataStr;
} }
}, () => text(data), true // Text nodes should not update last index since it is likely not worth it to eliminate an increasing subsequence of actual elements },
() => text(data),
true // Text nodes should not update last index since it is likely not worth it to eliminate an increasing subsequence of actual elements
); );
} }
@ -759,10 +753,16 @@ export function claim_space(nodes) {
* @returns {Comment} * @returns {Comment}
*/ */
export function claim_comment(nodes, data) { export function claim_comment(nodes, data) {
return claim_node(nodes, (node) => node.nodeType === 8, (node) => { return claim_node(
nodes,
(node) => node.nodeType === 8,
(node) => {
node.data = '' + data; node.data = '' + data;
return undefined; return undefined;
}, () => comment(data), true); },
() => comment(data),
true
);
} }
/** /**
@ -807,8 +807,7 @@ export function claim_html_tag(nodes, is_svg) {
*/ */
export function set_data(text, data) { export function set_data(text, data) {
data = '' + data; data = '' + data;
if (text.data === data) if (text.data === data) return;
return;
text.data = data; text.data = data;
} }
@ -819,8 +818,7 @@ export function set_data(text, data) {
*/ */
export function set_data_contenteditable(text, data) { export function set_data_contenteditable(text, data) {
data = '' + data; data = '' + data;
if (text.wholeText === data) if (text.wholeText === data) return;
return;
text.data = data; text.data = data;
} }
@ -833,8 +831,7 @@ export function set_data_contenteditable(text, data) {
export function set_data_maybe_contenteditable(text, data, attr_value) { export function set_data_maybe_contenteditable(text, data, attr_value) {
if (~contenteditable_truthy_values.indexOf(attr_value)) { if (~contenteditable_truthy_values.indexOf(attr_value)) {
set_data_contenteditable(text, data); set_data_contenteditable(text, data);
} } else {
else {
set_data(text, data); set_data(text, data);
} }
} }
@ -850,8 +847,7 @@ export function set_input_value(input, value) {
export function set_input_type(input, type) { export function set_input_type(input, type) {
try { try {
input.type = type; input.type = type;
} } catch (e) {
catch (e) {
// do nothing // do nothing
} }
} }
@ -861,8 +857,7 @@ export function set_input_type(input, type) {
export function set_style(node, key, value, important) { export function set_style(node, key, value, important) {
if (value == null) { if (value == null) {
node.style.removeProperty(key); node.style.removeProperty(key);
} } else {
else {
node.style.setProperty(key, value, important ? 'important' : ''); node.style.setProperty(key, value, important ? 'important' : '');
} }
} }
@ -919,8 +914,7 @@ export function is_crossorigin() {
if (typeof window !== 'undefined' && window.parent) { if (typeof window !== 'undefined' && window.parent) {
void window.parent.document; void window.parent.document;
} }
} } catch (error) {
catch (error) {
crossorigin = true; crossorigin = true;
} }
} }
@ -938,8 +932,11 @@ export function add_iframe_resize_listener(node, fn) {
node.style.position = 'relative'; node.style.position = 'relative';
} }
const iframe = element('iframe'); const iframe = element('iframe');
iframe.setAttribute('style', 'display: block; position: absolute; top: 0; left: 0; width: 100%; height: 100%; ' + iframe.setAttribute(
'overflow: hidden; border: 0; opacity: 0; pointer-events: none; z-index: -1;'); 'style',
'display: block; position: absolute; top: 0; left: 0; width: 100%; height: 100%; ' +
'overflow: hidden; border: 0; opacity: 0; pointer-events: none; z-index: -1;'
);
iframe.setAttribute('aria-hidden', 'true'); iframe.setAttribute('aria-hidden', 'true');
iframe.tabIndex = -1; iframe.tabIndex = -1;
const crossorigin = is_crossorigin(); const crossorigin = is_crossorigin();
@ -950,11 +947,9 @@ export function add_iframe_resize_listener(node, fn) {
if (crossorigin) { if (crossorigin) {
iframe.src = "data:text/html,<script>onresize=function(){parent.postMessage(0,'*')}</script>"; iframe.src = "data:text/html,<script>onresize=function(){parent.postMessage(0,'*')}</script>";
unsubscribe = listen(window, 'message', (event) => { unsubscribe = listen(window, 'message', (event) => {
if (event.source === iframe.contentWindow) if (event.source === iframe.contentWindow) fn();
fn();
}); });
} } else {
else {
iframe.src = 'about:blank'; iframe.src = 'about:blank';
iframe.onload = () => { iframe.onload = () => {
unsubscribe = listen(iframe.contentWindow, 'resize', fn); unsubscribe = listen(iframe.contentWindow, 'resize', fn);
@ -967,8 +962,7 @@ export function add_iframe_resize_listener(node, fn) {
return () => { return () => {
if (crossorigin) { if (crossorigin) {
unsubscribe(); unsubscribe();
} } else if (unsubscribe && iframe.contentWindow) {
else if (unsubscribe && iframe.contentWindow) {
unsubscribe(); unsubscribe();
} }
detach(iframe); detach(iframe);
@ -980,7 +974,9 @@ export const resize_observer_content_box = /* @__PURE__ */ new ResizeObserverSin
export const resize_observer_border_box = /* @__PURE__ */ new ResizeObserverSingleton({ export const resize_observer_border_box = /* @__PURE__ */ new ResizeObserverSingleton({
box: 'border-box' box: 'border-box'
}); });
export const resize_observer_device_pixel_content_box = /* @__PURE__ */ new ResizeObserverSingleton({ box: 'device-pixel-content-box' }); export const resize_observer_device_pixel_content_box = /* @__PURE__ */ new ResizeObserverSingleton(
{ box: 'device-pixel-content-box' }
);
export { ResizeObserverSingleton }; export { ResizeObserverSingleton };
/** /**
@ -995,7 +991,6 @@ export function toggle_class(element, name, toggle) {
* @returns {CustomEvent<T>} * @returns {CustomEvent<T>}
*/ */
export function custom_event(type, detail, { bubbles = false, cancelable = false } = {}) { export function custom_event(type, detail, { bubbles = false, cancelable = false } = {}) {
/** /**
* @type {CustomEvent<T>} */ * @type {CustomEvent<T>} */
const e = document.createEvent('CustomEvent'); const e = document.createEvent('CustomEvent');
@ -1026,13 +1021,11 @@ export function head_selector(nodeId, head) {
if (comment === `HEAD_${nodeId}_END`) { if (comment === `HEAD_${nodeId}_END`) {
started -= 1; started -= 1;
result.push(node); result.push(node);
} } else if (comment === `HEAD_${nodeId}_START`) {
else if (comment === `HEAD_${nodeId}_START`) {
started += 1; started += 1;
result.push(node); result.push(node);
} }
} } else if (started > 0) {
else if (started > 0) {
result.push(node); result.push(node);
} }
} }
@ -1040,7 +1033,6 @@ export function head_selector(nodeId, head) {
} }
/** */ /** */
export class HtmlTag { export class HtmlTag {
/** /**
* @private * @private
* @default false * @default false
@ -1079,10 +1071,9 @@ export class HtmlTag {
*/ */
m(html, target, anchor = null) { m(html, target, anchor = null) {
if (!this.e) { if (!this.e) {
if (this.is_svg) if (this.is_svg) this.e = svg_element(target.nodeName);
this.e = svg_element(target.nodeName);
/** #7364 target for <template> may be provided as #document-fragment(11) */ else /** #7364 target for <template> may be provided as #document-fragment(11) */ else
this.e = element((target.nodeType === 11 ? 'TEMPLATE' : target.nodeName)); this.e = element(target.nodeType === 11 ? 'TEMPLATE' : target.nodeName);
this.t = target.tagName !== 'TEMPLATE' ? target : target.content; this.t = target.tagName !== 'TEMPLATE' ? target : target.content;
this.c(html); this.c(html);
} }
@ -1095,9 +1086,9 @@ export class HtmlTag {
*/ */
h(html) { h(html) {
this.e.innerHTML = html; this.e.innerHTML = html;
this.n = Array.from(this.e.nodeName === 'TEMPLATE' this.n = Array.from(
? this.e.content.childNodes this.e.nodeName === 'TEMPLATE' ? this.e.content.childNodes : this.e.childNodes
: this.e.childNodes); );
} }
/** /**
@ -1144,8 +1135,7 @@ export class HtmlTagHydration extends HtmlTag {
c(html) { c(html) {
if (this.l) { if (this.l) {
this.n = this.l; this.n = this.l;
} } else {
else {
super.c(html); super.c(html);
} }
} }
@ -1189,7 +1179,6 @@ export function construct_svelte_component(component, props) {
return new component(props); return new component(props);
} }
/** /**
* @typedef {Node & { * @typedef {Node & {
* claim_order?: number; * claim_order?: number;
@ -1209,4 +1198,3 @@ export function construct_svelte_component(component, props) {
* }; * };
* }} ChildNodeArray * }} ChildNodeArray
*/ */

Loading…
Cancel
Save