export function append(target, node) { target.appendChild(node); } export function insert(target, node, anchor) { target.insertBefore(node, anchor || null); } export function detach(node) { node.parentNode.removeChild(node); } export function detach_between(before, after) { while (before.nextSibling && before.nextSibling !== after) { before.parentNode.removeChild(before.nextSibling); } } export function detach_before(after) { while (after.previousSibling) { after.parentNode.removeChild(after.previousSibling); } } export function detach_after(before) { while (before.nextSibling) { before.parentNode.removeChild(before.nextSibling); } } export function destroy_each(iterations, detaching) { for (let i = 0; i < iterations.length; i += 1) { if (iterations[i]) iterations[i].d(detaching); } } export function element(name) { return document.createElement(name); } export function object_without_properties(obj, exclude) { const target = {}; for (const k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k) && exclude.indexOf(k) === -1) { target[k] = obj[k]; } } return target; } export function svg_element(name) { return document.createElementNS('http://www.w3.org/2000/svg', name); } export function text(data) { return document.createTextNode(data); } export function space() { return text(' '); } export function empty() { return text(''); } export function listen(node, event, handler, options) { node.addEventListener(event, handler, options); return () => node.removeEventListener(event, handler, options); } export function prevent_default(fn) { return function(event) { event.preventDefault(); return fn.call(this, event); }; } export function stop_propagation(fn) { return function(event) { event.stopPropagation(); return fn.call(this, event); }; } export function attr(node, attribute, value) { if (value == null) node.removeAttribute(attribute); else node.setAttribute(attribute, value); } export function set_attributes(node, attributes) { for (const key in attributes) { if (key === 'style') { node.style.cssText = attributes[key]; } else if (key in node) { node[key] = attributes[key]; } else { attr(node, key, attributes[key]); } } } export function set_custom_element_data(node, prop, value) { if (prop in node) { node[prop] = value; } else { attr(node, prop, value); } } export function xlink_attr(node, attribute, value) { node.setAttributeNS('http://www.w3.org/1999/xlink', attribute, value); } export function get_binding_group_value(group) { const value = []; for (let i = 0; i < group.length; i += 1) { if (group[i].checked) value.push(group[i].__value); } return value; } export function to_number(value) { return value === '' ? undefined : +value; } export function time_ranges_to_array(ranges) { const array = []; for (let i = 0; i < ranges.length; i += 1) { array.push({ start: ranges.start(i), end: ranges.end(i) }); } return array; } export function children(element) { return Array.from(element.childNodes); } export function claim_element(nodes, name, attributes, svg) { for (let i = 0; i < nodes.length; i += 1) { const node = nodes[i]; if (node.nodeName === name) { for (let j = 0; j < node.attributes.length; j += 1) { const attribute = node.attributes[j]; if (!attributes[attribute.name]) node.removeAttribute(attribute.name); } return nodes.splice(i, 1)[0]; // TODO strip unwanted attributes } } return svg ? svg_element(name) : element(name); } export function claim_text(nodes, data) { for (let i = 0; i < nodes.length; i += 1) { const node = nodes[i]; if (node.nodeType === 3) { node.data = data; return nodes.splice(i, 1)[0]; } } return text(data); } export function set_data(text, data) { data = '' + data; if (text.data !== data) text.data = data; } export function set_input_type(input, type) { try { input.type = type; } catch (e) { // do nothing } } export function set_style(node, key, value) { node.style.setProperty(key, value); } export function select_option(select, value) { for (let i = 0; i < select.options.length; i += 1) { const option = select.options[i]; if (option.__value === value) { option.selected = true; return; } } } export function select_options(select, value) { for (let i = 0; i < select.options.length; i += 1) { const option = select.options[i]; option.selected = ~value.indexOf(option.__value); } } export function select_value(select) { const selected_option = select.querySelector(':checked') || select.options[0]; return selected_option && selected_option.__value; } export function select_multiple_value(select) { return [].map.call(select.querySelectorAll(':checked'), option => option.__value); } export function add_resize_listener(element, fn) { if (getComputedStyle(element).position === 'static') { element.style.position = 'relative'; } const object = document.createElement('object'); object.setAttribute('style', 'display: block; position: absolute; top: 0; left: 0; height: 100%; width: 100%; overflow: hidden; pointer-events: none; z-index: -1;'); object.type = 'text/html'; let win; object.onload = () => { win = object.contentDocument.defaultView; win.addEventListener('resize', fn); }; if (/Trident/.test(navigator.userAgent)) { element.appendChild(object); object.data = 'about:blank'; } else { object.data = 'about:blank'; element.appendChild(object); } return { cancel: () => { win && win.removeEventListener && win.removeEventListener('resize', fn); element.removeChild(object); } }; } export function toggle_class(element, name, toggle) { element.classList[toggle ? 'add' : 'remove'](name); } export function custom_event(type, detail) { const e = document.createEvent('CustomEvent'); e.initCustomEvent(type, false, false, detail); return e; }