UNPKG

light-print

Version:

Lightweight HTML element printing for browsers.

7 lines (6 loc) 7.65 kB
/*! * light-print v2.8.0 * (c) 2020-2026 xunmi * Released under the MIT License. */ function e(e,t){return t.indexOf(e)>=0}function t(e,t){e.appendChild(t)}function n(e){var t;null===(t=e.parentNode)||void 0===t||t.removeChild(e)}function r(e,t){return e.localName===t}function o(e){return r(e,"audio")||r(e,"video")}var i=["source","track","wbr"];var u=["block","inline-block","list-item","flow-root","table-caption","table-cell","table-column","table-column-group"];function a(e,t,n,r){e.style.setProperty(t,String(n),r)}function c(e){var t;return function(e){return e instanceof Element}(e)?e:"string"==typeof e&&null!==(t=window.document.querySelector(e))&&void 0!==t?t:void 0}function l(e,t,n,r){var o=function(r){n(r),e.removeEventListener(t,o)};e.addEventListener(t,o,r)}function d(){return null!=Promise.withResolvers?Promise.withResolvers():{promise:new Promise(function(n,r){e=n,t=r}),resolve:e,reject:t};var e,t}function f(){}function s(e){return e.ownerDocument.defaultView}function m(e){return window.document.createTreeWalker(e,1,null,!1)}function v(e,t,r){for(var o=m(t),i=m(r);;){if(e(o.currentNode,i.currentNode)){if(!o.nextNode()||!i.nextNode())break}else{for(var u=o.currentNode,a=!0;;){if(o.nextSibling()&&i.nextSibling())break;if(!(a=!(!o.parentNode()||!i.parentNode())))break}if(n(u),!a)break}}}var p=["img","audio","video","iframe","object","embed","image"];function h(e){return r(e,"object")?e.data:r(e,"iframe")||r(e,"embed")?e.src:r(e,"image")?e.href.baseVal:e.currentSrc||e.src}function y(e){var t=p.join(","),n=Array.from(e.querySelectorAll(t)).filter(function(e){return!!h(e)}).map(function(e){return function(e){if(!r(e,"img")||!e.complete){(r(e,"img")||r(e,"iframe"))&&(e.loading="eager");var t=d(),n=t.promise,i=t.resolve,u=t.reject;return o(e)?(e.readyState>=2&&i(),l(e,"canplay",function(){return i()})):l(e,"load",function(){return i()}),l(e,"error",function(){return u(new Error("Failed to load resource (".concat(e.localName,": ").concat(h(e),").")))}),n}}(e)});return n.push(function(e){var t;return null===(t=e.fonts)||void 0===t?void 0:t.ready.then(f)}(e)),Promise.all(n).then(f)}function b(e,t){return s(e).getComputedStyle(e,t)}function w(e,t){return"".concat(e,"{").concat(t,"}")}function g(e,t,n){return n?e+t+": "+n+";":e}function S(e,t,n,r){for(var o=0;o<t.length;o++){var i=t[o],u=r.getPropertyValue(i);u&&u!==n.getPropertyValue(i)&&(e=g(e,i,u))}return e}var k=["counter-reset","counter-set","counter-increment"];function x(e,t,n){var o,i,u="";return u=S(u,t,e,t),u=S(u,k,e,t),function(e,t){var n=t.borderStyle;return n!==e.borderStyle&&"none"!==n&&"hidden"!==n}(e,t)&&(u=g(u,"border-width",t.borderWidth)),function(e,t){return"border-box"===t.boxSizing&&(t.padding!==e.padding||t.borderWidth!==e.borderWidth)}(e,t)||(i=t,r(o=n,"img")||r(o,"video")||r(o,"svg")&&o.getAttribute("viewBox")||i.aspectRatio&&"auto"!==i.aspectRatio)?(u=g(u,"width",t.width),u=g(u,"height",t.height)):"table"===t.display&&(u=g(u,"width",t.width)),u}var T=["::before","::after","::marker","::first-letter","::first-line","::placeholder","::file-selector-button","::details-content"];function N(t,n,o,i){if("::placeholder"===i){if(!r(n,"input")&&!r(n,"textarea")||!n.placeholder)return}else if("::file-selector-button"===i){if(!r(n,"input")||"file"!==n.type)return}else if("::details-content"===i){if(!r(n,"details"))return}else if("::marker"===i){if("list-item"!==o.display)return}else if(("::first-letter"===i||"::first-line"===i)&&!e(o.display,u))return;var a=b(n,i);if("::before"===i||"::after"===i){var c=a.content;if(!c||"normal"===c||"none"===c)return}return x(b(t,i),a,n)}var E="data-print-id";function R(){var e,n,r=!1,o=1;var i=[];return{get document(){return n},bind:function(e){n=e},appendStyle:function(t){t&&(null!=e||(e=n.createElement("style")),e.textContent+=t)},mountStyle:function(o){!r&&e&&(t(o||n.head,e),r=!0)},getSelector:function(e){return"[".concat(E,'="').concat(function(e){var t=e.getAttribute(E);return t||(t=(o++).toString(),e.setAttribute(E,t)),t}(e),'"]')},addTask:function(e){i.push(e)},flushTasks:function(){i.forEach(function(e){return e()}),i.length=0}}}function C(e,t,n){if(!t.shadowRoot.clonable){var r=e.ownerDocument,o=R();o.bind(r),function(e,t){e.attachShadow({mode:"open",delegatesFocus:t.shadowRoot.delegatesFocus})}(e,t);var i=e.shadowRoot;i.appendChild(function(e,t){var n=e.createDocumentFragment();return t.childNodes.forEach(function(t){return n.appendChild(e.importNode(t,!0))}),n}(r,t.shadowRoot)),v(function(e,t){return e===i||n(e,t,o)},i,t.shadowRoot),o.flushTasks(),o.mountStyle(i)}!function(e,t){var n=t.shadowRoot.adoptedStyleSheets.flatMap(function(e){return Array.from(e.cssRules).map(function(e){return e.cssText})}).join("\n");if(n){var r=new(s(e).CSSStyleSheet);r.replaceSync(n),e.shadowRoot.adoptedStyleSheets.push(r)}}(e,t)}function P(e,t,n,r){var o=function(e,t,n){return x(b(e),n,t)}(e,t,n);if(o){var i=t.style.cssText+o;e.setAttribute("style",i);var u=w(r.getSelector(e),i);r.addTask(function(){e.removeAttribute("style"),r.appendStyle(u)})}}function j(t,n,u,a){if(e(t.localName,i))return!0;var c;if(a){var l=b(n);if(!(c=l,c.display&&"none"!==c.display||function(e){return r(e,"style")||r(e,"link")&&"stylesheet"===e.rel}(n)))return!1;P(t,n,l,u),function(e,t,n,r){if(!(t instanceof SVGElement)){for(var o,i="",u=0,a=T;u<a.length;u++){var c=a[u],l=N(e,t,n,c);l&&(null!=o||(o=r.getSelector(e)),i+=w(o+c,l))}r.appendStyle(i)}}(t,n,l,u)}return function(e){var t;return"open"===(null===(t=e.shadowRoot)||void 0===t?void 0:t.mode)}(n)&&C(t,n,function(e,t,n){return j(e,t,n,!!t.part.value)}),function(e,t){if(r(e,"select")||r(e,"textarea"))e.value=t.value;else if(r(e,"option"))e.selected=t.selected;else if(r(e,"input")){var n=t;e.value=n.value,e.checked=n.checked,e.indeterminate=n.indeterminate}r(e,"canvas")&&function(e,t){0!==t.width&&0!==t.height&&e.getContext("2d").drawImage(t,0,0)}(e,t),o(e)&&function(e,t){t.currentSrc&&(e.src=t.currentSrc,e.currentTime=t.currentTime,e.autoplay=!1)}(e,t),function(e,t){var n=t.scrollTop,r=t.scrollLeft;(n||r)&&(e.scrollTop=n,e.scrollLeft=r)}(e,t)}(t,n),!0}function A(e){var t=d(),n=t.promise,r=t.resolve;if(e.focus(),l(e,"afterprint",function(){return r()}),/msie|trident/i.test(window.navigator.userAgent))try{e.document.execCommand("print",!1)}catch(t){e.print()}else e.print();return n}function V(e,o){void 0===o&&(o={});var i=c(e);if(!i)return Promise.reject(new Error("Invalid HTML element."));var u=function(){var e=window.document.createElement("iframe");return e.srcdoc="<!DOCTYPE html>",a(e,"position","absolute","important"),a(e,"top","-9999px","important"),a(e,"visibility","hidden","important"),a(e,"transform","scale(0)","important"),e}(),f=R();return function(e,n){var r=d(),o=r.promise,i=r.resolve,u=r.reject;return l(e,"load",function(){return i()}),l(e,"error",function(){return u(new Error("Failed to mount document."))}),t(n,e),o}(u,window.document.body).then(function(){var e,a,c=u.contentWindow.document;f.bind(c),c.title=null!==(e=o.documentTitle)&&void 0!==e?e:window.document.title,f.appendStyle("html{zoom:".concat(null!==(a=o.zoom)&&void 0!==a?a:1,"}body{margin:0;print-color-adjust:exact;}")),function(e){if(e.fonts)try{for(var t=window.document.fonts.values();;){var n=t.next().value;if(!n)break;e.fonts.add(n)}}catch(e){}}(c),function(e,o){var i=e.document,u=i.importNode(o,!0);r(o,"body")?(n(i.body),t(i.documentElement,u)):t(i.body,u),v(function(t,n){return j(t,n,e,!0)},u,o),e.flushTasks()}(f,i),f.appendStyle(o.mediaPrintStyle),f.mountStyle()}).then(function(){return y(f.document)}).then(function(){return A(f.document.defaultView)}).finally(function(){return n(u)})}export{V as default};