UNPKG

native-fn

Version:
513 lines (507 loc) 17.9 kB
'use strict'; function isSecureContext() { if (typeof globalThis.isSecureContext !== 'undefined') return globalThis.isSecureContext; var protocol = location.protocol; var hostname = location.hostname; return protocol === 'https:' || hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '[::1]'; } function createHiddenElement(tagName, focusable) { if (focusable === void 0) { focusable = true; } if (typeof globalThis.document === 'undefined') return undefined; var element = globalThis.document.createElement(tagName); if ('width' in element) element.width = '0'; if ('height' in element) element.height = '0'; if ('border' in element) element.border = '0'; if ('frameBorder' in element) element.frameBorder = '0'; if ('scrolling' in element) element.scrolling = 'no'; if ('cellPadding' in element) element.cellPadding = '0'; if ('cellSpacing' in element) element.cellSpacing = '0'; if ('frame' in element) element.frame = 'void'; if ('rules' in element) element.rules = 'none'; if ('noWrap' in element) element.noWrap = true; element.tabIndex = -1; element.setAttribute('role', 'presentation'); if (focusable) { element.style.width = '1px'; element.style.height = '1px'; } else { element.setAttribute('aria-hidden', 'true'); element.style.width = '0'; element.style.height = '0'; element.style.zIndex = '-9999'; element.style.display = 'none'; element.style.visibility = 'hidden'; element.style.pointerEvents = 'none'; } element.style.position = 'absolute'; element.style.top = '0'; element.style.left = '0'; element.style.padding = '0'; element.style.margin = '0'; element.style.border = 'none'; element.style.outline = 'none'; element.style.clip = 'rect(1px, 1px, 1px, 1px)'; element.style.clipPath = 'inset(50%)'; element.style.overflow = 'hidden'; element.style.whiteSpace = 'nowrap'; return element; } function capitalize(_) { var groups = []; for (var _i = 1; _i < arguments.length; _i++) { groups[_i - 1] = arguments[_i]; } var result = ''; for (var i = 0; i < groups.length - 2; i++) { var arg = groups[i]; if (typeof arg !== 'undefined') result = result + arg.charAt(0).toUpperCase() + arg.slice(1); } return result; } function preventDefaultPolyfill() { this.returnValue = false; } function stopPropagationPolyfill() { this.cancelBubble = true; } var EVENT_TYPE_REGEXP = /(animation)(start|iteration|end|cancel)|(transition)(start|run|end|cancel)|(fullscreen)(change|error)|(lost|got)(pointer)(capture)|(pointer)(lock)(change|error)|(pointer)(cancel|down|enter|leave|move|out|over|up)/i; var VENDORS = ['', 'webkit', 'moz', 'ms', 'MS', 'o', 'O']; var LEGACY_TYPE_MAP = { 'wheel': ['wheel', 'mousewheel', 'DOMMouseScroll'], 'focus': ['focus', 'focusin'], 'blur': ['blur', 'focusout'], 'beforeinput': ['beforeinput', 'textInput'], }; var EventListenerUtils = { useStd: typeof globalThis.document !== 'undefined' && typeof globalThis.document.addEventListener === 'function', add: function (target, eventListenerOptions) { if (eventListenerOptions === void 0) { eventListenerOptions = { callback: null, options: false }; } var callback = eventListenerOptions.callback; if (typeof target === 'undefined' || callback === null) return; var type = EventListenerUtils.withVender(target, eventListenerOptions.type); var options = eventListenerOptions.options; if (typeof target.addEventListener === 'function') { try { return target.addEventListener(type, callback, options); } catch (_) { } } if (typeof target.addListener === 'function') { if (typeof target.matches === 'boolean') { try { return target.addListener(callback); } catch (_) { } } else { try { return target.addListener(type, callback); } catch (_) { } } } function wrapper(event) { if (typeof event === 'undefined') event = globalThis.event; if (typeof event === 'undefined') return; event.currentTarget = target; if (typeof event.preventDefault !== 'function') event.preventDefault = preventDefaultPolyfill; if (typeof event.stopPropagation !== 'function') event.stopPropagation = stopPropagationPolyfill; if (typeof callback === 'function') callback.call(target, event); else if (callback && typeof callback.handleEvent === 'function') callback.handleEvent.call(target, event); } if (typeof target.attachEvent === 'function') { if (callback.__ieWrapper) return target.attachEvent('on' + type, callback.__ieWrapper); return target.attachEvent('on' + type, callback.__ieWrapper = wrapper); } }, remove: function (target, eventListenerOptions) { if (eventListenerOptions === void 0) { eventListenerOptions = { callback: null, options: false }; } var callback = eventListenerOptions.callback; if (typeof target === 'undefined' || callback === null) return; var type = EventListenerUtils.withVender(target, eventListenerOptions.type); var options = eventListenerOptions.options; if (typeof target.removeEventListener === 'function') { try { return target.removeEventListener(type, callback, options); } catch (_) { } } if (typeof target.removeListener === 'function') { if (typeof target.matches === 'boolean') { try { return target.removeListener(callback); } catch (_) { } } else { try { return target.removeListener(type, callback); } catch (_) { } } } if (typeof target.detachEvent === 'function') { var wrapper = callback.__ieWrapper; if (typeof wrapper !== 'undefined') { target.detachEvent('on' + type, wrapper); delete callback.__ieWrapper; } return; } }, withVender: function (target, type) { if (typeof type === 'undefined') return ''; if (target === globalThis.document && ['deviceready', 'pause', 'resume', 'backbutton', 'menubutton', 'searchbutton', 'startcallbutton', 'endcallbutton', 'volumedownbutton', 'volumeupbutton', 'activated', 'cordovacallbackerror'].indexOf(type) > -1) return type; if (typeof target.webkitEnterFullscreen !== 'undefined' && ['webkitbeginfullscreen', 'webkitendfullscreen', 'webkitpresentationmodechanged'].indexOf(type) > -1) return type; var types; if (type in LEGACY_TYPE_MAP) types = LEGACY_TYPE_MAP[type]; else if (EVENT_TYPE_REGEXP.test(type)) types = [type, type.replace(EVENT_TYPE_REGEXP, capitalize)]; else types = [type]; for (var i = 0; i < VENDORS.length; i++) { for (var j = 0; j < types.length; j++) { var name_1 = VENDORS[i] + types[j]; if (typeof target['on' + name_1] !== 'undefined') return name_1; } } return ''; } }; var Clipboard = { copy: copy, paste: paste, Constants: {}, Errors: {}, }; function isObject(item) { return item !== null && typeof item === 'object'; } function isArray(item) { return Array.isArray(item); } function isSerializable(item) { return isObject(item) || isArray(item); } function isElement(item) { return isObject(item) && typeof item.innerText !== 'undefined'; } function isSelection(item) { return Object.prototype.toString.call(item) === '[object Selection]'; } function convertToString(item) { if (isElement(item)) { if ('value' in item && typeof item.value === 'string') return item.value; return item.textContent; } if (isSelection(item)) return item.toString(); if (isSerializable(item)) { try { return JSON.stringify(item); } catch (_) { return '' + item; } } else if (typeof item !== 'string') { return '' + item; } return item; } function convertToHTML(item) { var html = null; if (isElement(item)) html = item.outerHTML; if (isSelection(item) && item.rangeCount > 0) { var div = globalThis.document.createElement('div'); for (var i = 0; i < item.rangeCount; i++) div.appendChild(item.getRangeAt(i).cloneContents()); html = div.innerHTML; } if (html === null) return; return html; } function supportsClipboardAPI() { return (isSecureContext() && typeof navigator !== 'undefined' && 'clipboard' in navigator); } function supportsClipboardWriteAPI() { return supportsClipboardAPI() && ('write' in navigator.clipboard || 'writeText' in navigator.clipboard); } function supportsClipboardReadAPI() { return supportsClipboardAPI() && ('read' in navigator.clipboard || 'readText' in navigator.clipboard); } function copy(item) { var text = convertToString(item); var html = convertToHTML(item); if (supportsClipboardWriteAPI()) { return copyViaClipboardAPI(text, html) .then(function (success) { if (success) return true; return copyViaLegacy(text, html); }) .catch(function () { return copyViaLegacy(text, html); }); } return Promise.resolve(copyViaLegacy(text, html)); } function copyViaClipboardAPI(text, html) { try { if ('ClipboardItem' in window && 'write' in navigator.clipboard) { var items = {}; if (typeof html !== 'undefined') items['text/html'] = new Blob([html], { type: 'text/html' }); items['text/plain'] = new Blob([text], { type: 'text/plain' }); return navigator.clipboard.write([new ClipboardItem(items)]) .then(function () { return true; }) .catch(function () { return false; }); } else if ('writeText' in navigator.clipboard) { return navigator.clipboard.writeText(text) .then(function () { return true; }) .catch(function () { return false; }); } } catch (_) { return Promise.resolve(false); } return Promise.resolve(false); } function copyViaSelection(text, html) { if (!globalThis.getSelection || !globalThis.document.createRange) return false; var div = createHiddenElement('div'); if (typeof div === 'undefined') return false; div.contentEditable = 'true'; div.innerHTML = text; div.style.whiteSpace = 'pre'; div.style.userSelect = 'text'; div.style.setProperty('-webkit-user-select', 'text'); div.style.setProperty('-moz-user-select', 'text'); div.style.setProperty('-ms-user-select', 'text'); globalThis.document.body.appendChild(div); var selection = globalThis.getSelection(); var range = globalThis.document.createRange(); var onCopy = function (event) { try { if (event.clipboardData !== null && typeof event.clipboardData.setData === 'function') { event.preventDefault(); if (typeof html !== 'undefined') event.clipboardData.setData('text/html', html); event.clipboardData.setData('text/plain', text); } } catch (_) { } }; EventListenerUtils.add(globalThis.document, { type: 'copy', callback: onCopy, options: { once: true, capture: true } }); try { if (selection === null) { cleanupSelection(div, selection, onCopy); return false; } selection.removeAllRanges(); range.selectNodeContents(div); selection.addRange(range); var success = globalThis.document.execCommand('copy'); cleanupSelection(div, selection, onCopy); return success; } catch (_) { cleanupSelection(div, selection, onCopy); return false; } } function copyViaClipboardData(text, html) { var windowWithClipboardData = window; var clipboardData = windowWithClipboardData.clipboardData; if (typeof clipboardData !== 'undefined' && typeof clipboardData.setData === 'function') { try { if (typeof html !== 'undefined') clipboardData.setData('HTML', html); return clipboardData.setData('Text', text); } catch (_) { return false; } } return false; } function copyViaLegacy(text, html) { return copyViaSelection(text, html) || copyViaClipboardData(text, html); } function paste() { if (supportsClipboardReadAPI()) { return pasteViaClipboardAPI() .then(function (text) { if (text !== null) return text; return pasteViaLegacy(); }) .catch(function () { return pasteViaLegacy(); }); } return Promise.resolve(pasteViaLegacy()); } function pasteViaClipboardAPI() { try { if ('ClipboardItem' in window && 'read' in navigator.clipboard) { return navigator.clipboard.read() .then(function (items) { if (items.length === 0) return Promise.resolve(null); var item = items[0]; var types = item.types; for (var i = 0; i < types.length; i++) { if (types[i] === 'text/html') { return item.getType('text/html') .then(function (blob) { return blob.text(); }) .catch(function () { return null; }); } } for (var i = 0; i < types.length; i++) { if (types[i] === 'text/plain') { return item.getType('text/plain') .then(function (blob) { return blob.text(); }) .catch(function () { return null; }); } } return Promise.resolve(null); }) .catch(function () { return null; }); } else if ('readText' in navigator.clipboard) { return navigator.clipboard.readText() .then(function (text) { return text; }) .catch(function () { return null; }); } } catch (_) { return Promise.resolve(null); } return Promise.resolve(null); } function pasteViaSelection() { var div = createHiddenElement('div'); if (typeof div === 'undefined') return null; div.contentEditable = 'true'; globalThis.document.body.appendChild(div); div.focus(); var pastedText = null; var onPaste = function (event) { try { if (event.clipboardData !== null && typeof event.clipboardData.getData === 'function') { event.preventDefault(); pastedText = event.clipboardData.getData('text/html') || event.clipboardData.getData('text/plain') || null; } } catch (_) { } }; EventListenerUtils.add(globalThis.document, { type: 'paste', callback: onPaste, options: { once: true, capture: true } }); try { var success = globalThis.document.execCommand('paste'); if (!success && !pastedText) { pastedText = div.innerHTML || div.textContent || null; } cleanupPaste(div, onPaste); return pastedText; } catch (_) { cleanupPaste(div, onPaste); return null; } } function pasteViaClipboardData() { var windowWithClipboardData = window; var clipboardData = windowWithClipboardData.clipboardData; if (typeof clipboardData !== 'undefined' && typeof clipboardData.getData === 'function') { try { return clipboardData.getData('Text') || null; } catch (_) { return null; } } return null; } function pasteViaLegacy() { return pasteViaSelection() || pasteViaClipboardData() || ''; } function cleanupSelection(span, selection, onCopy) { if (selection !== null) selection.removeAllRanges(); globalThis.document.body.removeChild(span); EventListenerUtils.remove(globalThis.document, { type: 'copy', callback: onCopy }); } function cleanupPaste(div, onPaste) { globalThis.document.body.removeChild(div); EventListenerUtils.remove(globalThis.document, { type: 'paste', callback: onPaste }); } module.exports = Clipboard;