pull/8569/head
Simon Holthausen 3 years ago
parent 39199cf5c3
commit bc5ebb0dd8

@ -82,7 +82,7 @@ export function handle_promise(promise, info) {
update(info.then, 1, info.value, promise); update(info.then, 1, info.value, promise);
return true; return true;
} }
info.resolved = promise; info.resolved = /** @type {T} */ (promise);
} }
} }

@ -47,10 +47,7 @@ function init_hydrate(target) {
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>
/** let children = /** @type {ArrayLike<NodeEx2>} */ (target.childNodes);
* @type {ArrayLike<NodeEx2>}
*/
let children = target.childNodes;
// If target is <head>, there may be children without claim_order // If target is <head>, there may be children without claim_order
if (target.nodeName === 'HEAD') { if (target.nodeName === 'HEAD') {
const myChildren = []; const myChildren = [];
@ -168,8 +165,8 @@ export function append_styles(target, style_sheet_id, styles) {
export function get_root_for_style(node) { export function get_root_for_style(node) {
if (!node) return document; if (!node) return document;
const root = node.getRootNode ? node.getRootNode() : node.ownerDocument; const root = node.getRootNode ? node.getRootNode() : node.ownerDocument;
if (root && root.host) { if (root && /** @type {ShadowRoot} */ (root).host) {
return root; return /** @type {ShadowRoot} */ (root);
} }
return node.ownerDocument; return node.ownerDocument;
} }
@ -196,7 +193,7 @@ export function append_empty_stylesheet(node) {
* @returns {CSSStyleSheet} * @returns {CSSStyleSheet}
*/ */
function append_stylesheet(node, style) { function append_stylesheet(node, style) {
append(node.head || node, style); append(/** @type {Document} */ (node).head || node, style);
return style.sheet; return style.sheet;
} }
@ -274,6 +271,7 @@ export function destroy_each(iterations, detaching) {
} }
/** /**
* @template {keyof HTMLElementTagNameMap} K
* @param {K} name * @param {K} name
* @returns {HTMLElementTagNameMap[K]} * @returns {HTMLElementTagNameMap[K]}
*/ */
@ -282,6 +280,7 @@ export function element(name) {
} }
/** /**
* @template {keyof HTMLElementTagNameMap} K
* @param {K} name * @param {K} name
* @param {string} is * @param {string} is
* @returns {HTMLElementTagNameMap[K]} * @returns {HTMLElementTagNameMap[K]}
@ -291,12 +290,14 @@ export function element_is(name, is) {
} }
/** /**
* @template T
* @template {keyof T} K
* @param {T} obj * @param {T} obj
* @param {K[]} exclude * @param {K[]} exclude
* @returns {Pick<T, Exclude<keyof T, K>>} * @returns {Pick<T, Exclude<keyof T, K>>}
*/ */
export function object_without_properties(obj, exclude) { export function object_without_properties(obj, exclude) {
const target = {}; const target = /** @type {Pick<T, Exclude<keyof T, K>>} */ ({});
for (const k in obj) { for (const k in obj) {
if ( if (
has_prop(obj, k) && has_prop(obj, k) &&
@ -311,6 +312,7 @@ export function object_without_properties(obj, exclude) {
} }
/** /**
* @template {keyof SVGElementTagNameMap} K
* @param {K} name * @param {K} name
* @returns {SVGElement} * @returns {SVGElement}
*/ */
@ -350,7 +352,7 @@ export function comment(content) {
* @param {EventTarget} node * @param {EventTarget} node
* @param {string} event * @param {string} event
* @param {EventListenerOrEventListenerObject} handler * @param {EventListenerOrEventListenerObject} handler
* @param {boolean | AddEventListenerOptions | EventListenerOptions} options * @param {boolean | AddEventListenerOptions | EventListenerOptions} [options]
* @returns {() => void} * @returns {() => void}
*/ */
export function listen(node, event, handler, options) { export function listen(node, event, handler, options) {
@ -439,7 +441,7 @@ export function set_attributes(node, attributes) {
} 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]; /** @type {any} */ (node).value = node[key] = attributes[key];
} else if ( } else if (
descriptors[key] && descriptors[key] &&
descriptors[key].set && descriptors[key].set &&
@ -485,14 +487,14 @@ export function set_custom_element_data(node, prop, value) {
/** /**
* @param {string} tag * @param {string} tag
* @returns {Class<import>}
*/ */
export function set_dynamic_element_data(tag) { export function set_dynamic_element_data(tag) {
return /-/.test(tag) ? set_custom_element_data_map : set_attributes; return /-/.test(tag) ? set_custom_element_data_map : set_attributes;
} }
/** /**
* @returns {void} */ * @returns {void}
*/
export function xlink_attr(node, attribute, value) { export function xlink_attr(node, attribute, value) {
node.setAttributeNS('http://www.w3.org/1999/xlink', attribute, value); node.setAttributeNS('http://www.w3.org/1999/xlink', attribute, value);
} }
@ -623,6 +625,7 @@ function init_claim_info(nodes) {
} }
/** /**
* @template {ChildNodeEx} R
* @param {ChildNodeArray} nodes * @param {ChildNodeArray} nodes
* @param {(node: ChildNodeEx) => node is R} predicate * @param {(node: ChildNodeEx) => node is R} predicate
* @param {(node: ChildNodeEx) => ChildNodeEx | undefined} processNode * @param {(node: ChildNodeEx) => ChildNodeEx | undefined} processNode
@ -688,7 +691,9 @@ function claim_node(nodes, predicate, processNode, createNode, dontUpdateLastInd
function claim_element_base(nodes, name, attributes, create_element) { function claim_element_base(nodes, name, attributes, create_element) {
return claim_node( return claim_node(
nodes, nodes,
/** @returns {node is Element | SVGElement} */
(node) => node.nodeName === name, (node) => node.nodeName === name,
/** @param {Element} node */
(node) => { (node) => {
const remove = []; const remove = [];
for (let j = 0; j < node.attributes.length; j++) { for (let j = 0; j < node.attributes.length; j++) {
@ -731,7 +736,9 @@ export function claim_svg_element(nodes, name, attributes) {
export function claim_text(nodes, data) { export function claim_text(nodes, data) {
return claim_node( return claim_node(
nodes, nodes,
/** @returns {node is Text} */
(node) => node.nodeType === 3, (node) => node.nodeType === 3,
/** @param {Text} node */
(node) => { (node) => {
const dataStr = '' + data; const dataStr = '' + data;
if (node.data.startsWith(dataStr)) { if (node.data.startsWith(dataStr)) {
@ -760,7 +767,9 @@ export function claim_space(nodes) {
export function claim_comment(nodes, data) { export function claim_comment(nodes, data) {
return claim_node( return claim_node(
nodes, nodes,
/** @returns {node is Comment} */
(node) => node.nodeType === 8, (node) => node.nodeType === 8,
/** @param {Comment} node */
(node) => { (node) => {
node.data = '' + data; node.data = '' + data;
return undefined; return undefined;
@ -784,7 +793,7 @@ function find_comment(nodes, text, start) {
/** /**
* @param {boolean} is_svg * @param {boolean} is_svg
* @returns {import("C:/repos/svelte/svelte/dom.ts-to-jsdoc").HtmlTagHydration} * @returns {HtmlTagHydration}
*/ */
export function claim_html_tag(nodes, is_svg) { export function claim_html_tag(nodes, is_svg) {
// find html opening tag // find html opening tag
@ -813,7 +822,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) return; if (text.data === data) return;
text.data = data; text.data = /** @type {string} */ (data);
} }
/** /**
@ -824,7 +833,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) return; if (text.wholeText === data) return;
text.data = data; text.data = /** @type {string} */ (data);
} }
/** /**
@ -947,13 +956,18 @@ export function add_iframe_resize_listener(node, fn) {
const crossorigin = is_crossorigin(); const crossorigin = is_crossorigin();
/** /**
* @type {() => void} */ * @type {() => void}
*/
let unsubscribe; let unsubscribe;
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',
/** @param {MessageEvent} event */ (event) => {
if (event.source === iframe.contentWindow) fn(); if (event.source === iframe.contentWindow) fn();
}); }
);
} else { } else {
iframe.src = 'about:blank'; iframe.src = 'about:blank';
iframe.onload = () => { iframe.onload = () => {
@ -991,8 +1005,9 @@ export function toggle_class(element, name, toggle) {
} }
/** /**
* @template T
* @param {string} type * @param {string} type
* @param {T} detail * @param {T} [detail]
* @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 } = {}) {
@ -1076,10 +1091,18 @@ export class HtmlTag {
*/ */
m(html, target, anchor = null) { m(html, target, anchor = null) {
if (!this.e) { if (!this.e) {
if (this.is_svg) this.e = svg_element(target.nodeName); if (this.is_svg)
this.e = svg_element(/** @type {keyof SVGElementTagNameMap} */ (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(
this.t = target.tagName !== 'TEMPLATE' ? target : target.content; /** @type {keyof HTMLElementTagNameMap} */ (
target.nodeType === 11 ? 'TEMPLATE' : target.nodeName
)
);
this.t =
target.tagName !== 'TEMPLATE'
? target
: /** @type {HTMLTemplateElement} */ (target).content;
this.c(html); this.c(html);
} }
this.i(anchor); this.i(anchor);
@ -1172,9 +1195,11 @@ export function attribute_to_object(attributes) {
*/ */
export function get_custom_elements_slots(element) { export function get_custom_elements_slots(element) {
const result = {}; const result = {};
element.childNodes.forEach((node) => { element.childNodes.forEach(
/** @param {Element} node */ (node) => {
result[node.slot || 'default'] = true; result[node.slot || 'default'] = true;
}); }
);
return result; return result;
} }

@ -32,6 +32,7 @@ function wait() {
function dispatch(node, direction, kind) { function dispatch(node, direction, kind) {
node.dispatchEvent(custom_event(`${direction ? 'intro' : 'outro'}${kind}`)); node.dispatchEvent(custom_event(`${direction ? 'intro' : 'outro'}${kind}`));
} }
const outroing = new Set(); const outroing = new Set();
/** /**
@ -60,7 +61,7 @@ export function check_outros() {
/** /**
* @param {import('./public.js').Fragment} block * @param {import('./public.js').Fragment} block
* @param {0 | 1} local * @param {0 | 1} [local]
* @returns {void} * @returns {void}
*/ */
export function transition_in(block, local) { export function transition_in(block, local) {
@ -285,7 +286,7 @@ export function create_bidirectional_transition(node, fn, params, intro) {
* @returns {Program} * @returns {Program}
*/ */
function init(program, duration) { function init(program, duration) {
const d = program.b - t; const d = /** @type {Program['d']} */ (program.b - t);
duration *= Math.abs(d); duration *= Math.abs(d);
return { return {
a: t, a: t,

@ -21,14 +21,14 @@ export function assign(tar, src) {
// Distributed under MIT License https://github.com/then/is-promise/blob/master/LICENSE // Distributed under MIT License https://github.com/then/is-promise/blob/master/LICENSE
/** /**
* @template T * @template T
* @param {any} value * @param {T} value
* @returns {value is PromiseLike<T>} * @returns {value is PromiseLike<T>}
*/ */
export function is_promise(value) { export function is_promise(value) {
return ( return (
!!value && !!value &&
(typeof value === 'object' || typeof value === 'function') && (typeof value === 'object' || typeof value === 'function') &&
typeof value.then === 'function' typeof (/** @type {any} */ (value).then) === 'function'
); );
} }
@ -259,7 +259,7 @@ export function action_destroyer(action_result) {
*/ */
export function split_css_unit(value) { export function split_css_unit(value) {
const split = typeof value === 'string' && value.match(/^\s*(-?[\d.]+)([^\s]*)\s*$/); const split = typeof value === 'string' && value.match(/^\s*(-?[\d.]+)([^\s]*)\s*$/);
return split ? [parseFloat(split[1]), split[2] || 'px'] : [value, 'px']; return split ? [parseFloat(split[1]), split[2] || 'px'] : [/** @type {number} */ (value), 'px'];
} }
export const contenteditable_truthy_values = ['', true, 1, 'true', 'contenteditable']; export const contenteditable_truthy_values = ['', true, 1, 'true', 'contenteditable'];

@ -1,4 +1,4 @@
const _boolean_attributes = [ const _boolean_attributes = /** @type {const} */ ([
'allowfullscreen', 'allowfullscreen',
'allowpaymentrequest', 'allowpaymentrequest',
'async', 'async',
@ -24,13 +24,14 @@ const _boolean_attributes = [
'required', 'required',
'reversed', 'reversed',
'selected' 'selected'
]; ]);
/** /**
* List of HTML boolean attributes (e.g. `<input disabled>`). * List of HTML boolean attributes (e.g. `<input disabled>`).
* Source: https://html.spec.whatwg.org/multipage/indices.html * Source: https://html.spec.whatwg.org/multipage/indices.html
*
* @type {Set<string>} * @type {Set<string>}
*/ */
export const boolean_attributes = new Set([..._boolean_attributes]); export const boolean_attributes = new Set([..._boolean_attributes]);
/** @typedef {Class<_boolean_attributes>} BooleanAttributes */ /** @typedef {typeof _boolean_attributes[number]} BooleanAttributes */

Loading…
Cancel
Save