Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

shadow-dom-testing-library

Package Overview
Dependencies
Maintainers
2
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

shadow-dom-testing-library - npm Package Compare versions

Comparing version 1.7.1 to 1.8.0

3

dist/index.d.ts

@@ -70,5 +70,2 @@ import * as _testing_library_dom from '@testing-library/dom';

/**
* This is an extension of prettyDOM / logDOM that provides proper printing of shadow roots.
*/
declare function prettyShadowDOM(...args: Parameters<typeof prettyDOM>): ReturnType<typeof prettyDOM>;

@@ -75,0 +72,0 @@

3

dist/index.js

@@ -1,2 +0,3 @@

var me=Object.defineProperty;var ie=(e,t)=>{for(var a in t)me(e,a,{get:t[a],enumerable:!0})};import{queries as ve,within as Je}from"@testing-library/dom";var M={};ie(M,{findAllByShadowAltText:()=>z,findAllByShadowDisplayValue:()=>k,findAllByShadowLabelText:()=>g,findAllByShadowPlaceholderText:()=>I,findAllByShadowRole:()=>x,findAllByShadowTestId:()=>ne,findAllByShadowText:()=>F,findAllByShadowTitle:()=>ee,findByShadowAltText:()=>G,findByShadowDisplayValue:()=>v,findByShadowLabelText:()=>Q,findByShadowPlaceholderText:()=>O,findByShadowRole:()=>P,findByShadowTestId:()=>re,findByShadowText:()=>U,findByShadowTitle:()=>te,getAllByShadowAltText:()=>W,getAllByShadowDisplayValue:()=>N,getAllByShadowLabelText:()=>R,getAllByShadowPlaceholderText:()=>H,getAllByShadowRole:()=>A,getAllByShadowTestId:()=>ae,getAllByShadowText:()=>_,getAllByShadowTitle:()=>X,getByShadowAltText:()=>Y,getByShadowDisplayValue:()=>j,getByShadowLabelText:()=>q,getByShadowPlaceholderText:()=>D,getByShadowRole:()=>E,getByShadowTestId:()=>le,getByShadowText:()=>$,getByShadowTitle:()=>Z,queryAllByShadowAltText:()=>y,queryAllByShadowDisplayValue:()=>w,queryAllByShadowLabelText:()=>m,queryAllByShadowPlaceholderText:()=>i,queryAllByShadowRole:()=>s,queryAllByShadowTestId:()=>f,queryAllByShadowText:()=>S,queryAllByShadowTitle:()=>T,queryByShadowAltText:()=>J,queryByShadowDisplayValue:()=>C,queryByShadowLabelText:()=>L,queryByShadowPlaceholderText:()=>b,queryByShadowRole:()=>p,queryByShadowTestId:()=>oe,queryByShadowText:()=>V,queryByShadowTitle:()=>K});import{buildQueries as h,queryAllByRole as we,queryAllByLabelText as ye,queryAllByPlaceholderText as Te,queryAllByText as fe,queryAllByDisplayValue as Me,queryAllByAltText as Be,queryAllByTitle as pe,queryAllByTestId as Ae}from"@testing-library/dom";function Se(e,t,a={shallow:!1}){let o=ce(e,t,a);return Array.isArray(o)&&o.length>0?o[0]:null}function ce(e,t,a={shallow:!1}){let l=d(e,a).map(r=>Array.from(r.querySelectorAll(t))).flat(1/0);return[...new Set(l)]}function d(e,t={shallow:!1}){return ue(e,"*",t)}function ue(e,t,a={shallow:!1},o=[],l=[]){return e instanceof Document&&(e=document.documentElement),o=[e],l.push(e),e instanceof HTMLElement&&e.shadowRoot!=null&&e.shadowRoot.mode!=="closed"&&(l.push(e.shadowRoot),o.push(e.shadowRoot)),o.forEach(r=>{r.querySelectorAll(t).forEach(n=>{if(n.shadowRoot==null||n.shadowRoot.mode==="closed"){l.push(n);return}if(l.push(n.shadowRoot),a.shallow===!0){n.shadowRoot.querySelectorAll(t).forEach(u=>{l.push(u)});return}n.shadowRoot.querySelectorAll(t).forEach(u=>{l.push(u),o.push(u)}),ue(n.shadowRoot,t,a,o,l)})}),[...new Set(l)]}function c(e){return e.map(t=>(...a)=>{let[o,l,r,...n]=a;return r==null&&(r={}),r.suggest=!1,t(o,l,r,...n)})}function s(...e){let[t,a,o]=e;return o==null&&(o={}),o.suggest=!1,[...new Set(d(t,o).map(l=>we(l,a,o)).flat(1/0))]}var Ee=(e,t)=>`Found multiple elements with the role of: ${t}`,xe=(e,t)=>`Unable to find an element with the role of: ${t}`,[p,A,E,x,P]=c(h(s,Ee,xe));function m(...e){let[t,a,o]=e;return o==null&&(o={}),o.suggest=!1,[...new Set(d(t,o).map(l=>ye(l,a,o)).flat(1/0))]}var Pe=(e,t)=>`Found multiple elements with the label text of: ${t}`,Le=(e,t)=>`Unable to find an element with the label text of: ${t}`,[L,R,q,g,Q]=c(h(m,Pe,Le));function i(...e){let[t,a,o]=e;return o==null&&(o={}),o.suggest=!1,[...new Set(d(t,o).map(l=>Te(l,a,o)).flat(1/0))]}var Re=(e,t)=>`Found multiple elements with the placeholder text of: ${t}`,qe=(e,t)=>`Unable to find an element with the placeholder text of: ${t}`,[b,H,D,I,O]=c(h(i,Re,qe));function S(...e){let[t,a,o]=e;return o==null&&(o={}),o.suggest=!1,[...new Set(d(t,o).map(l=>fe(l,a,o)).flat(1/0))]}var ge=(e,t)=>`Found multiple elements with the text of: ${t}`,Qe=(e,t)=>`Unable to find an element with the text of: ${t}`,[V,_,$,F,U]=c(h(S,ge,Qe));function w(...e){let[t,a,o]=e;return o==null&&(o={}),o.suggest=!1,[...new Set(d(t,o).map(l=>Me(l,a,o)).flat(1/0))]}var be=(e,t)=>`Found multiple elements with the display value of: ${t}`,He=(e,t)=>`Unable to find an element with the display value of: ${t}`,[C,N,j,k,v]=c(h(w,be,He));function y(...e){let[t,a,o]=e;return o==null&&(o={}),o.suggest=!1,[...new Set(d(t,o).map(l=>Be(l,a,o)).flat(1/0))]}var De=(e,t)=>`Found multiple elements with the alt text of: ${t}`,Ie=(e,t)=>`Unable to find an element with the alt text of: ${t}`,[J,W,Y,z,G]=c(h(y,De,Ie));function T(...e){let[t,a,o]=e;return o==null&&(o={}),o.suggest=!1,[...new Set(d(t,o).map(l=>pe(l,a,o)).flat(1/0))]}var Oe=(e,t)=>`Found multiple elements with the title of: ${t}`,Ve=(e,t)=>`Unable to find an element with the title of: ${t}`,[K,X,Z,ee,te]=c(h(T,Oe,Ve));function f(...e){let[t,a,o]=e;return o==null&&(o={}),o.suggest=!1,[...new Set(d(t,o).map(l=>Ae(l,a,o)).flat(1/0))]}var _e=(e,t)=>`Found multiple elements with the test id of: ${t}`,$e=(e,t)=>`Unable to find an element with the test id of: ${t}`,[oe,ae,le,ne,re]=c(h(f,_e,$e));import{logDOM as Ne}from"@testing-library/dom";import{prettyDOM as Fe}from"@testing-library/dom";function Ue(...e){let[t,a,o]=e;return Fe(de(t),a,o)}function de(e){e==null&&(e=document.documentElement);let t="";"outerHTML"in e&&(t=e.outerHTML),t=Ce(e,t),t=t.replace(/<!--.*?-->/g,""),t=t.replace(/>\s+</g,"><");let o=new DOMParser().parseFromString(t,"text/html");return e instanceof Document||e instanceof HTMLHtmlElement||e instanceof HTMLBodyElement?o.body:o.body.firstElementChild}function Ce(e,t="",a=Array.from(e.children)){for(("shadowRoot"in e)&&e.shadowRoot!=null&&a.unshift(e.shadowRoot),a.unshift(e);a.length>0;){let o=a.shift();if(o&&"shadowRoot"in o&&o.shadowRoot!=null){let l=o.outerHTML,r=document.createElement("shadow-root");r.innerHTML=o.shadowRoot.innerHTML;let n=o.cloneNode(!0);n.insertAdjacentElement("afterbegin",r),t=t.replace(l,n.outerHTML),t=t.replace(l,o.outerHTML),a.push(...Array.from(o.shadowRoot.children))}a.push(...Array.from(o.children))}return t}function B(...e){let[t,a,o]=e;Ne(de(t),a,o)}function he(...e){let[t,a,o]=e;Array.isArray(t)?t.forEach(l=>B(l,a,o)):B(t,a,o)}import{screen as je}from"@testing-library/dom";var ke={...je,debug:he,queryAllByShadowRole:(...e)=>s(document.documentElement,e[0],e[1]),queryByShadowRole:(...e)=>p(document.documentElement,e[0],e[1]),getAllByShadowRole:(...e)=>A(document.documentElement,e[0],e[1]),getByShadowRole:(...e)=>E(document.documentElement,e[0],e[1]),findAllByShadowRole:(...e)=>x(document.documentElement,e[0],e[1],e[2]),findByShadowRole:(...e)=>P(document.documentElement,e[0],e[1],e[2]),queryAllByShadowLabelText:(...e)=>m(document.documentElement,e[0],e[1]),queryByShadowLabelText:(...e)=>L(document.documentElement,e[0],e[1]),getAllByShadowLabelText:(...e)=>R(document.documentElement,e[0],e[1]),getByShadowLabelText:(...e)=>q(document.documentElement,e[0],e[1]),findAllByShadowLabelText:(...e)=>g(document.documentElement,e[0],e[1],e[2]),findByShadowLabelText:(...e)=>Q(document.documentElement,e[0],e[1],e[2]),queryAllByShadowPlaceholderText:(...e)=>i(document.documentElement,e[0],e[1]),queryByShadowPlaceholderText:(...e)=>b(document.documentElement,e[0],e[1]),getAllByShadowPlaceholderText:(...e)=>H(document.documentElement,e[0],e[1]),getByShadowPlaceholderText:(...e)=>D(document.documentElement,e[0],e[1]),findAllByShadowPlaceholderText:(...e)=>I(document.documentElement,e[0],e[1],e[2]),findByShadowPlaceholderText:(...e)=>O(document.documentElement,e[0],e[1],e[2]),queryAllByShadowText:(...e)=>S(document.documentElement,e[0],e[1]),queryByShadowText:(...e)=>V(document.documentElement,e[0],e[1]),getAllByShadowText:(...e)=>_(document.documentElement,e[0],e[1]),getByShadowText:(...e)=>$(document.documentElement,e[0],e[1]),findAllByShadowText:(...e)=>F(document.documentElement,e[0],e[1],e[2]),findByShadowText:(...e)=>U(document.documentElement,e[0],e[1],e[2]),queryAllByShadowDisplayValue:(...e)=>w(document.documentElement,e[0],e[1]),queryByShadowDisplayValue:(...e)=>C(document.documentElement,e[0],e[1]),getAllByShadowDisplayValue:(...e)=>N(document.documentElement,e[0],e[1]),getByShadowDisplayValue:(...e)=>j(document.documentElement,e[0],e[1]),findAllByShadowDisplayValue:(...e)=>k(document.documentElement,e[0],e[1],e[2]),findByShadowDisplayValue:(...e)=>v(document.documentElement,e[0],e[1],e[2]),queryAllByShadowAltText:(...e)=>y(document.documentElement,e[0],e[1]),queryByShadowAltText:(...e)=>J(document.documentElement,e[0],e[1]),getAllByShadowAltText:(...e)=>W(document.documentElement,e[0],e[1]),getByShadowAltText:(...e)=>Y(document.documentElement,e[0],e[1]),findAllByShadowAltText:(...e)=>z(document.documentElement,e[0],e[1],e[2]),findByShadowAltText:(...e)=>G(document.documentElement,e[0],e[1],e[2]),queryAllByShadowTitle:(...e)=>T(document.documentElement,e[0],e[1]),queryByShadowTitle:(...e)=>K(document.documentElement,e[0],e[1]),getAllByShadowTitle:(...e)=>X(document.documentElement,e[0],e[1]),getByShadowTitle:(...e)=>Z(document.documentElement,e[0],e[1]),findAllByShadowTitle:(...e)=>ee(document.documentElement,e[0],e[1],e[2]),findByShadowTitle:(...e)=>te(document.documentElement,e[0],e[1],e[2]),queryAllByShadowTestId:(...e)=>f(document.documentElement,e[0],e[1]),queryByShadowTestId:(...e)=>oe(document.documentElement,e[0],e[1]),getAllByShadowTestId:(...e)=>ae(document.documentElement,e[0],e[1]),getByShadowTestId:(...e)=>le(document.documentElement,e[0],e[1]),findAllByShadowTestId:(...e)=>ne(document.documentElement,e[0],e[1],e[2]),findByShadowTestId:(...e)=>re(document.documentElement,e[0],e[1],e[2])};function se(){if(typeof ShadowRoot>"u")throw"Your environment does not support shadow roots.";ShadowRoot.prototype.matches==null&&Object.defineProperties(ShadowRoot.prototype,{matches:{get(){return function(e){return e.trim()==="*"?!0:this.querySelector(e)!=null}}}}),ShadowRoot.prototype.outerHTML==null&&Object.defineProperties(ShadowRoot.prototype,{outerHTML:{get(){return this.innerHTML}}})}se();var We={...ve,...M};function Tt(e){return Je(e,We)}export{he as debug,Se as deepQuerySelector,ce as deepQuerySelectorAll,z as findAllByShadowAltText,k as findAllByShadowDisplayValue,g as findAllByShadowLabelText,I as findAllByShadowPlaceholderText,x as findAllByShadowRole,ne as findAllByShadowTestId,F as findAllByShadowText,ee as findAllByShadowTitle,G as findByShadowAltText,v as findByShadowDisplayValue,Q as findByShadowLabelText,O as findByShadowPlaceholderText,P as findByShadowRole,re as findByShadowTestId,U as findByShadowText,te as findByShadowTitle,W as getAllByShadowAltText,N as getAllByShadowDisplayValue,R as getAllByShadowLabelText,H as getAllByShadowPlaceholderText,A as getAllByShadowRole,ae as getAllByShadowTestId,_ as getAllByShadowText,X as getAllByShadowTitle,d as getAllElementsAndShadowRoots,Y as getByShadowAltText,j as getByShadowDisplayValue,q as getByShadowLabelText,D as getByShadowPlaceholderText,E as getByShadowRole,le as getByShadowTestId,$ as getByShadowText,Z as getByShadowTitle,B as logShadowDOM,Ue as prettyShadowDOM,y as queryAllByShadowAltText,w as queryAllByShadowDisplayValue,m as queryAllByShadowLabelText,i as queryAllByShadowPlaceholderText,s as queryAllByShadowRole,f as queryAllByShadowTestId,S as queryAllByShadowText,T as queryAllByShadowTitle,J as queryByShadowAltText,C as queryByShadowDisplayValue,L as queryByShadowLabelText,b as queryByShadowPlaceholderText,p as queryByShadowRole,oe as queryByShadowTestId,V as queryByShadowText,K as queryByShadowTitle,ke as screen,Tt as within};
var xe=Object.defineProperty;var ge=(e,o)=>{for(var n in o)xe(e,n,{get:o[n],enumerable:!0})};import{queries as mt,within as wt}from"@testing-library/dom";var E={};ge(E,{findAllByShadowAltText:()=>v,findAllByShadowDisplayValue:()=>W,findAllByShadowLabelText:()=>H,findAllByShadowPlaceholderText:()=>V,findAllByShadowRole:()=>L,findAllByShadowTestId:()=>se,findAllByShadowText:()=>j,findAllByShadowTitle:()=>ae,findByShadowAltText:()=>ee,findByShadowDisplayValue:()=>Y,findByShadowLabelText:()=>I,findByShadowPlaceholderText:()=>_,findByShadowRole:()=>q,findByShadowTestId:()=>ce,findByShadowText:()=>k,findByShadowTitle:()=>le,getAllByShadowAltText:()=>K,getAllByShadowDisplayValue:()=>X,getAllByShadowLabelText:()=>b,getAllByShadowPlaceholderText:()=>N,getAllByShadowRole:()=>P,getAllByShadowTestId:()=>de,getAllByShadowText:()=>$,getAllByShadowTitle:()=>oe,getByShadowAltText:()=>Z,getByShadowDisplayValue:()=>z,getByShadowLabelText:()=>D,getByShadowPlaceholderText:()=>C,getByShadowRole:()=>R,getByShadowTestId:()=>he,getByShadowText:()=>U,getByShadowTitle:()=>ne,queryAllByShadowAltText:()=>M,queryAllByShadowDisplayValue:()=>p,queryAllByShadowLabelText:()=>y,queryAllByShadowPlaceholderText:()=>T,queryAllByShadowRole:()=>S,queryAllByShadowTestId:()=>A,queryAllByShadowText:()=>f,queryAllByShadowTitle:()=>B,queryByShadowAltText:()=>J,queryByShadowDisplayValue:()=>G,queryByShadowLabelText:()=>Q,queryByShadowPlaceholderText:()=>O,queryByShadowRole:()=>g,queryByShadowTestId:()=>re,queryByShadowText:()=>F,queryByShadowTitle:()=>te});import{buildQueries as c,queryAllByRole as Re,queryAllByLabelText as Le,queryAllByPlaceholderText as qe,queryAllByText as Qe,queryAllByDisplayValue as be,queryAllByAltText as De,queryAllByTitle as He,queryAllByTestId as Ie}from"@testing-library/dom";function Pe(e,o,n={shallow:!1}){let t=Se(e,o,n);return Array.isArray(t)&&t.length>0?t[0]:null}function Se(e,o,n={shallow:!1}){let a=h(e,n).map(l=>Array.from(l.querySelectorAll(o))).flat(1/0);return[...new Set(a)]}function h(e,o={shallow:!1}){return ye(e,"*",o)}function ye(e,o,n={shallow:!1},t=[],a=[]){return e instanceof Document&&(e=document.documentElement),t=[e],a.push(e),e instanceof HTMLElement&&e.shadowRoot!=null&&e.shadowRoot.mode!=="closed"&&(a.push(e.shadowRoot),t.push(e.shadowRoot)),t.forEach(l=>{l.querySelectorAll(o).forEach(r=>{if(r.shadowRoot==null||r.shadowRoot.mode==="closed"){a.push(r);return}if(a.push(r.shadowRoot),n.shallow===!0){r.shadowRoot.querySelectorAll(o).forEach(d=>{a.push(d)});return}r.shadowRoot.querySelectorAll(o).forEach(d=>{a.push(d),t.push(d)}),ye(r.shadowRoot,o,n,t,a)})}),[...new Set(a)]}function u(e){return e.map(o=>(...n)=>{let[t,a,l,...r]=n;return l==null&&(l={}),l.suggest=!1,o(t,a,l,...r)})}function S(...e){let[o,n,t]=e;return t==null&&(t={}),t.suggest=!1,[...new Set(h(o,t).map(a=>Re(a,n,t)).flat(1/0))]}var Oe=(e,o)=>`Found multiple elements with the role of: ${o}`,Ne=(e,o)=>`Unable to find an element with the role of: ${o}`,[g,P,R,L,q]=u(c(S,Oe,Ne));function y(...e){let[o,n,t]=e;return t==null&&(t={}),t.suggest=!1,[...new Set(h(o,t).map(a=>Le(a,n,t)).flat(1/0))]}var Ce=(e,o)=>`Found multiple elements with the label text of: ${o}`,Ve=(e,o)=>`Unable to find an element with the label text of: ${o}`,[Q,b,D,H,I]=u(c(y,Ce,Ve));function T(...e){let[o,n,t]=e;return t==null&&(t={}),t.suggest=!1,[...new Set(h(o,t).map(a=>qe(a,n,t)).flat(1/0))]}var _e=(e,o)=>`Found multiple elements with the placeholder text of: ${o}`,Fe=(e,o)=>`Unable to find an element with the placeholder text of: ${o}`,[O,N,C,V,_]=u(c(T,_e,Fe));function f(...e){let[o,n,t]=e;return t==null&&(t={}),t.suggest=!1,[...new Set(h(o,t).map(a=>Qe(a,n,t)).flat(1/0))]}var $e=(e,o)=>`Found multiple elements with the text of: ${o}`,Ue=(e,o)=>`Unable to find an element with the text of: ${o}`,[F,$,U,j,k]=u(c(f,$e,Ue));function p(...e){let[o,n,t]=e;return t==null&&(t={}),t.suggest=!1,[...new Set(h(o,t).map(a=>be(a,n,t)).flat(1/0))]}var je=(e,o)=>`Found multiple elements with the display value of: ${o}`,ke=(e,o)=>`Unable to find an element with the display value of: ${o}`,[G,X,z,W,Y]=u(c(p,je,ke));function M(...e){let[o,n,t]=e;return t==null&&(t={}),t.suggest=!1,[...new Set(h(o,t).map(a=>De(a,n,t)).flat(1/0))]}var Ge=(e,o)=>`Found multiple elements with the alt text of: ${o}`,Xe=(e,o)=>`Unable to find an element with the alt text of: ${o}`,[J,K,Z,v,ee]=u(c(M,Ge,Xe));function B(...e){let[o,n,t]=e;return t==null&&(t={}),t.suggest=!1,[...new Set(h(o,t).map(a=>He(a,n,t)).flat(1/0))]}var ze=(e,o)=>`Found multiple elements with the title of: ${o}`,We=(e,o)=>`Unable to find an element with the title of: ${o}`,[te,oe,ne,ae,le]=u(c(B,ze,We));function A(...e){let[o,n,t]=e;return t==null&&(t={}),t.suggest=!1,[...new Set(h(o,t).map(a=>Ie(a,n,t)).flat(1/0))]}var Ye=(e,o)=>`Found multiple elements with the test id of: ${o}`,Je=(e,o)=>`Unable to find an element with the test id of: ${o}`,[re,de,he,se,ce]=u(c(A,Ye,Je));import{logDOM as ct}from"@testing-library/dom";import{prettyDOM as Ke,getConfig as Ze}from"@testing-library/dom";function ve(...e){let[o,n,t]=e,a=me((t==null?void 0:t.filterNode)||ue);return t==null&&(t={}),t.plugins==null&&(t.plugins=[]),t.plugins.push(a),Ke(o,n,{...t,plugins:[a]})}function fe(e){return e.replace(/</g,"&lt;").replace(/>/g,"&gt;")}function ue(e){return e.nodeType!==ie&&(e.nodeType!==Me||!e.matches(Ze().defaultIgnore))}var et=(e,o,n,t,a,l,r)=>{let d=t+n.indent,s=n.colors;return e.map(i=>{let w=o[i],m=r(w,n,d,a,l);return typeof w!="string"&&(m.indexOf(`
`)!==-1&&(m=n.spacingOuter+d+m+n.spacingOuter+t),m="{"+m+"}"),n.spacingInner+t+s.prop.open+i+s.prop.close+"="+s.value.open+m+s.value.close}).join("")},tt=3,ot=(e,o,n,t,a,l)=>e.map(r=>{let d=typeof r=="string"?pe(r,o):l(r,o,n,t,a);return d===""&&typeof r=="object"&&r!==null&&r.nodeType!==tt?"":o.spacingOuter+n+d}).join(""),pe=(e,o)=>{let n=o.colors.content;return n.open+fe(e)+n.close},nt=(e,o)=>{let n=o.colors.comment;return n.open+"<!--"+fe(e)+"-->"+n.close},at=(e,o,n,t,a)=>{let l=t.colors.tag;return l.open+"<"+e+(o&&l.close+o+t.spacingOuter+a+l.open)+(n?">"+l.close+n+t.spacingOuter+a+l.open+"</"+e:(o&&!t.min?"":" ")+"/")+">"+l.close},lt=(e,o)=>{let n=o.colors.tag;return n.open+"<"+e+n.close+" \u2026"+n.open+" />"+n.close},Me=1,Be=3,ie=8,Ae=11,rt=/^((HTML|SVG)\w*)?Element$/,dt=e=>{var l;let o=((l=e==null?void 0:e.constructor)==null?void 0:l.name)||"",{nodeType:n,tagName:t}=e,a=typeof t=="string"&&t.includes("-")||typeof e.hasAttribute=="function"&&e.hasAttribute("is")||e instanceof HTMLElement;return n===Me&&(rt.test(o)||a)||n===Be&&o==="Text"||n===ie&&o==="Comment"||n===Ae};function ht(e){return e.nodeType===Be}function st(e){return e.nodeType===ie}function Te(e){return e.nodeType===Ae}function me(e){function o(n){let t=Array.prototype.slice.call(n.childNodes||n.children);return"shadowRoot"in n&&n.shadowRoot!=null&&n.shadowRoot.mode!=="closed"&&t.unshift(n.shadowRoot),t.filter(e)}return{test:n=>(n==null?void 0:n.constructor)&&dt(n),serialize:(n,t,a,l,r,d)=>{if(ht(n))return pe(n.data,t);if(st(n))return nt(n.data,t);let s="DocumentFragment";return"tagName"in n&&n.tagName?s=n.tagName.toLowerCase():n instanceof ShadowRoot&&(s="ShadowRoot"),++l>t.maxDepth?lt(s,t):at(s,et(Te(n)?[]:Array.from(n.attributes).map(i=>i.name).sort(),Te(n)?{}:Array.from(n.attributes).reduce((i,w)=>(i[w.name]=w.value,i),{}),t,a+t.indent,l,r,d),ot(o(n),t,a+t.indent,l,r,d),t,a)}}}function x(...e){let[o,n,t]=e,a=me((t==null?void 0:t.filterNode)||ue);t==null&&(t={}),t.plugins==null&&(t.plugins=[]),t.plugins.push(a),ct(o,n,t)}function we(...e){let[o,n,t]=e;Array.isArray(o)?o.forEach(a=>x(a,n,t)):x(o,n,t)}import{screen as ut}from"@testing-library/dom";var it={...ut,debug:we,queryAllByShadowRole:(...e)=>S(document.documentElement,e[0],e[1]),queryByShadowRole:(...e)=>g(document.documentElement,e[0],e[1]),getAllByShadowRole:(...e)=>P(document.documentElement,e[0],e[1]),getByShadowRole:(...e)=>R(document.documentElement,e[0],e[1]),findAllByShadowRole:(...e)=>L(document.documentElement,e[0],e[1],e[2]),findByShadowRole:(...e)=>q(document.documentElement,e[0],e[1],e[2]),queryAllByShadowLabelText:(...e)=>y(document.documentElement,e[0],e[1]),queryByShadowLabelText:(...e)=>Q(document.documentElement,e[0],e[1]),getAllByShadowLabelText:(...e)=>b(document.documentElement,e[0],e[1]),getByShadowLabelText:(...e)=>D(document.documentElement,e[0],e[1]),findAllByShadowLabelText:(...e)=>H(document.documentElement,e[0],e[1],e[2]),findByShadowLabelText:(...e)=>I(document.documentElement,e[0],e[1],e[2]),queryAllByShadowPlaceholderText:(...e)=>T(document.documentElement,e[0],e[1]),queryByShadowPlaceholderText:(...e)=>O(document.documentElement,e[0],e[1]),getAllByShadowPlaceholderText:(...e)=>N(document.documentElement,e[0],e[1]),getByShadowPlaceholderText:(...e)=>C(document.documentElement,e[0],e[1]),findAllByShadowPlaceholderText:(...e)=>V(document.documentElement,e[0],e[1],e[2]),findByShadowPlaceholderText:(...e)=>_(document.documentElement,e[0],e[1],e[2]),queryAllByShadowText:(...e)=>f(document.documentElement,e[0],e[1]),queryByShadowText:(...e)=>F(document.documentElement,e[0],e[1]),getAllByShadowText:(...e)=>$(document.documentElement,e[0],e[1]),getByShadowText:(...e)=>U(document.documentElement,e[0],e[1]),findAllByShadowText:(...e)=>j(document.documentElement,e[0],e[1],e[2]),findByShadowText:(...e)=>k(document.documentElement,e[0],e[1],e[2]),queryAllByShadowDisplayValue:(...e)=>p(document.documentElement,e[0],e[1]),queryByShadowDisplayValue:(...e)=>G(document.documentElement,e[0],e[1]),getAllByShadowDisplayValue:(...e)=>X(document.documentElement,e[0],e[1]),getByShadowDisplayValue:(...e)=>z(document.documentElement,e[0],e[1]),findAllByShadowDisplayValue:(...e)=>W(document.documentElement,e[0],e[1],e[2]),findByShadowDisplayValue:(...e)=>Y(document.documentElement,e[0],e[1],e[2]),queryAllByShadowAltText:(...e)=>M(document.documentElement,e[0],e[1]),queryByShadowAltText:(...e)=>J(document.documentElement,e[0],e[1]),getAllByShadowAltText:(...e)=>K(document.documentElement,e[0],e[1]),getByShadowAltText:(...e)=>Z(document.documentElement,e[0],e[1]),findAllByShadowAltText:(...e)=>v(document.documentElement,e[0],e[1],e[2]),findByShadowAltText:(...e)=>ee(document.documentElement,e[0],e[1],e[2]),queryAllByShadowTitle:(...e)=>B(document.documentElement,e[0],e[1]),queryByShadowTitle:(...e)=>te(document.documentElement,e[0],e[1]),getAllByShadowTitle:(...e)=>oe(document.documentElement,e[0],e[1]),getByShadowTitle:(...e)=>ne(document.documentElement,e[0],e[1]),findAllByShadowTitle:(...e)=>ae(document.documentElement,e[0],e[1],e[2]),findByShadowTitle:(...e)=>le(document.documentElement,e[0],e[1],e[2]),queryAllByShadowTestId:(...e)=>A(document.documentElement,e[0],e[1]),queryByShadowTestId:(...e)=>re(document.documentElement,e[0],e[1]),getAllByShadowTestId:(...e)=>de(document.documentElement,e[0],e[1]),getByShadowTestId:(...e)=>he(document.documentElement,e[0],e[1]),findAllByShadowTestId:(...e)=>se(document.documentElement,e[0],e[1],e[2]),findByShadowTestId:(...e)=>ce(document.documentElement,e[0],e[1],e[2])};function Ee(){if(typeof ShadowRoot>"u")throw"Your environment does not support shadow roots.";ShadowRoot.prototype.matches==null&&Object.defineProperties(ShadowRoot.prototype,{matches:{get(){return function(e){return e.trim()==="*"?!0:this.querySelector(e)!=null}}}}),ShadowRoot.prototype.outerHTML==null&&Object.defineProperties(ShadowRoot.prototype,{outerHTML:{get(){return this.innerHTML}}})}Ee();var St={...mt,...E};function _t(e){return wt(e,St)}export{we as debug,Pe as deepQuerySelector,Se as deepQuerySelectorAll,v as findAllByShadowAltText,W as findAllByShadowDisplayValue,H as findAllByShadowLabelText,V as findAllByShadowPlaceholderText,L as findAllByShadowRole,se as findAllByShadowTestId,j as findAllByShadowText,ae as findAllByShadowTitle,ee as findByShadowAltText,Y as findByShadowDisplayValue,I as findByShadowLabelText,_ as findByShadowPlaceholderText,q as findByShadowRole,ce as findByShadowTestId,k as findByShadowText,le as findByShadowTitle,K as getAllByShadowAltText,X as getAllByShadowDisplayValue,b as getAllByShadowLabelText,N as getAllByShadowPlaceholderText,P as getAllByShadowRole,de as getAllByShadowTestId,$ as getAllByShadowText,oe as getAllByShadowTitle,h as getAllElementsAndShadowRoots,Z as getByShadowAltText,z as getByShadowDisplayValue,D as getByShadowLabelText,C as getByShadowPlaceholderText,R as getByShadowRole,he as getByShadowTestId,U as getByShadowText,ne as getByShadowTitle,x as logShadowDOM,ve as prettyShadowDOM,M as queryAllByShadowAltText,p as queryAllByShadowDisplayValue,y as queryAllByShadowLabelText,T as queryAllByShadowPlaceholderText,S as queryAllByShadowRole,A as queryAllByShadowTestId,f as queryAllByShadowText,B as queryAllByShadowTitle,J as queryByShadowAltText,G as queryByShadowDisplayValue,Q as queryByShadowLabelText,O as queryByShadowPlaceholderText,g as queryByShadowRole,re as queryByShadowTestId,F as queryByShadowText,te as queryByShadowTitle,it as screen,_t as within};
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

{"inputs":{"src/deep-query-selectors.ts":{"bytes":3554,"imports":[]},"src/shadow-queries.ts":{"bytes":10219,"imports":[{"path":"src/deep-query-selectors.ts","kind":"import-statement"}]},"src/pretty-shadow-dom.ts":{"bytes":2216,"imports":[]},"src/log-shadow-dom.ts":{"bytes":282,"imports":[{"path":"src/pretty-shadow-dom.ts","kind":"import-statement"}]},"src/debug.ts":{"bytes":407,"imports":[{"path":"src/log-shadow-dom.ts","kind":"import-statement"}]},"src/shadow-screen.ts":{"bytes":9324,"imports":[{"path":"src/debug.ts","kind":"import-statement"},{"path":"src/shadow-queries.ts","kind":"import-statement"}]},"src/trick-dom-testing-library.ts":{"bytes":1227,"imports":[]},"src/types.ts":{"bytes":2196,"imports":[]},"src/index.ts":{"bytes":825,"imports":[{"path":"src/shadow-queries.ts","kind":"import-statement"},{"path":"src/debug.ts","kind":"import-statement"},{"path":"src/log-shadow-dom.ts","kind":"import-statement"},{"path":"src/pretty-shadow-dom.ts","kind":"import-statement"},{"path":"src/shadow-screen.ts","kind":"import-statement"},{"path":"src/trick-dom-testing-library.ts","kind":"import-statement"},{"path":"src/types.ts","kind":"import-statement"},{"path":"src/shadow-queries.ts","kind":"import-statement"},{"path":"src/deep-query-selectors.ts","kind":"import-statement"}]}},"outputs":{"dist/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":47152},"dist/index.cjs":{"imports":[],"exports":[],"entryPoint":"src/index.ts","inputs":{"src/index.ts":{"bytesInOutput":1688},"src/shadow-queries.ts":{"bytesInOutput":4034},"src/deep-query-selectors.ts":{"bytesInOutput":832},"src/log-shadow-dom.ts":{"bytesInOutput":94},"src/pretty-shadow-dom.ts":{"bytesInOutput":936},"src/debug.ts":{"bytesInOutput":80},"src/shadow-screen.ts":{"bytesInOutput":3432},"src/trick-dom-testing-library.ts":{"bytesInOutput":395}},"bytes":13138}}}
{"inputs":{"src/deep-query-selectors.ts":{"bytes":3555,"imports":[]},"src/shadow-queries.ts":{"bytes":10219,"imports":[{"path":"src/deep-query-selectors.ts","kind":"import-statement"}]},"src/pretty-shadow-dom.ts":{"bytes":7926,"imports":[]},"src/log-shadow-dom.ts":{"bytes":626,"imports":[{"path":"src/pretty-shadow-dom.ts","kind":"import-statement"}]},"src/debug.ts":{"bytes":407,"imports":[{"path":"src/log-shadow-dom.ts","kind":"import-statement"}]},"src/shadow-screen.ts":{"bytes":9324,"imports":[{"path":"src/debug.ts","kind":"import-statement"},{"path":"src/shadow-queries.ts","kind":"import-statement"}]},"src/trick-dom-testing-library.ts":{"bytes":1227,"imports":[]},"src/types.ts":{"bytes":2196,"imports":[]},"src/index.ts":{"bytes":825,"imports":[{"path":"src/shadow-queries.ts","kind":"import-statement"},{"path":"src/debug.ts","kind":"import-statement"},{"path":"src/log-shadow-dom.ts","kind":"import-statement"},{"path":"src/pretty-shadow-dom.ts","kind":"import-statement"},{"path":"src/shadow-screen.ts","kind":"import-statement"},{"path":"src/trick-dom-testing-library.ts","kind":"import-statement"},{"path":"src/types.ts","kind":"import-statement"},{"path":"src/shadow-queries.ts","kind":"import-statement"},{"path":"src/deep-query-selectors.ts","kind":"import-statement"}]}},"outputs":{"dist/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":57030},"dist/index.cjs":{"imports":[],"exports":[],"entryPoint":"src/index.ts","inputs":{"src/index.ts":{"bytesInOutput":1692},"src/shadow-queries.ts":{"bytesInOutput":4042},"src/deep-query-selectors.ts":{"bytesInOutput":832},"src/log-shadow-dom.ts":{"bytesInOutput":196},"src/pretty-shadow-dom.ts":{"bytesInOutput":2410},"src/debug.ts":{"bytesInOutput":80},"src/shadow-screen.ts":{"bytesInOutput":3436},"src/trick-dom-testing-library.ts":{"bytesInOutput":395}},"bytes":14730}}}

@@ -1,1 +0,1 @@

{"inputs":{"src/deep-query-selectors.ts":{"bytes":3554,"imports":[]},"src/shadow-queries.ts":{"bytes":10219,"imports":[{"path":"src/deep-query-selectors.ts","kind":"import-statement"}]},"src/pretty-shadow-dom.ts":{"bytes":2216,"imports":[]},"src/log-shadow-dom.ts":{"bytes":282,"imports":[{"path":"src/pretty-shadow-dom.ts","kind":"import-statement"}]},"src/debug.ts":{"bytes":407,"imports":[{"path":"src/log-shadow-dom.ts","kind":"import-statement"}]},"src/shadow-screen.ts":{"bytes":9324,"imports":[{"path":"src/debug.ts","kind":"import-statement"},{"path":"src/shadow-queries.ts","kind":"import-statement"}]},"src/trick-dom-testing-library.ts":{"bytes":1227,"imports":[]},"src/types.ts":{"bytes":2196,"imports":[]},"src/index.ts":{"bytes":825,"imports":[{"path":"src/shadow-queries.ts","kind":"import-statement"},{"path":"src/debug.ts","kind":"import-statement"},{"path":"src/log-shadow-dom.ts","kind":"import-statement"},{"path":"src/pretty-shadow-dom.ts","kind":"import-statement"},{"path":"src/shadow-screen.ts","kind":"import-statement"},{"path":"src/trick-dom-testing-library.ts","kind":"import-statement"},{"path":"src/types.ts","kind":"import-statement"},{"path":"src/shadow-queries.ts","kind":"import-statement"},{"path":"src/deep-query-selectors.ts","kind":"import-statement"}]}},"outputs":{"dist/index.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":45586},"dist/index.js":{"imports":[],"exports":["debug","deepQuerySelector","deepQuerySelectorAll","findAllByShadowAltText","findAllByShadowDisplayValue","findAllByShadowLabelText","findAllByShadowPlaceholderText","findAllByShadowRole","findAllByShadowTestId","findAllByShadowText","findAllByShadowTitle","findByShadowAltText","findByShadowDisplayValue","findByShadowLabelText","findByShadowPlaceholderText","findByShadowRole","findByShadowTestId","findByShadowText","findByShadowTitle","getAllByShadowAltText","getAllByShadowDisplayValue","getAllByShadowLabelText","getAllByShadowPlaceholderText","getAllByShadowRole","getAllByShadowTestId","getAllByShadowText","getAllByShadowTitle","getAllElementsAndShadowRoots","getByShadowAltText","getByShadowDisplayValue","getByShadowLabelText","getByShadowPlaceholderText","getByShadowRole","getByShadowTestId","getByShadowText","getByShadowTitle","logShadowDOM","prettyShadowDOM","queryAllByShadowAltText","queryAllByShadowDisplayValue","queryAllByShadowLabelText","queryAllByShadowPlaceholderText","queryAllByShadowRole","queryAllByShadowTestId","queryAllByShadowText","queryAllByShadowTitle","queryByShadowAltText","queryByShadowDisplayValue","queryByShadowLabelText","queryByShadowPlaceholderText","queryByShadowRole","queryByShadowTestId","queryByShadowText","queryByShadowTitle","screen","within"],"entryPoint":"src/index.ts","inputs":{"src/index.ts":{"bytesInOutput":117},"src/shadow-queries.ts":{"bytesInOutput":3936},"src/deep-query-selectors.ts":{"bytesInOutput":832},"src/log-shadow-dom.ts":{"bytesInOutput":91},"src/pretty-shadow-dom.ts":{"bytesInOutput":934},"src/debug.ts":{"bytesInOutput":80},"src/shadow-screen.ts":{"bytesInOutput":3434},"src/trick-dom-testing-library.ts":{"bytesInOutput":395}},"bytes":11399}}}
{"inputs":{"src/deep-query-selectors.ts":{"bytes":3555,"imports":[]},"src/shadow-queries.ts":{"bytes":10219,"imports":[{"path":"src/deep-query-selectors.ts","kind":"import-statement"}]},"src/pretty-shadow-dom.ts":{"bytes":7926,"imports":[]},"src/log-shadow-dom.ts":{"bytes":626,"imports":[{"path":"src/pretty-shadow-dom.ts","kind":"import-statement"}]},"src/debug.ts":{"bytes":407,"imports":[{"path":"src/log-shadow-dom.ts","kind":"import-statement"}]},"src/shadow-screen.ts":{"bytes":9324,"imports":[{"path":"src/debug.ts","kind":"import-statement"},{"path":"src/shadow-queries.ts","kind":"import-statement"}]},"src/trick-dom-testing-library.ts":{"bytes":1227,"imports":[]},"src/types.ts":{"bytes":2196,"imports":[]},"src/index.ts":{"bytes":825,"imports":[{"path":"src/shadow-queries.ts","kind":"import-statement"},{"path":"src/debug.ts","kind":"import-statement"},{"path":"src/log-shadow-dom.ts","kind":"import-statement"},{"path":"src/pretty-shadow-dom.ts","kind":"import-statement"},{"path":"src/shadow-screen.ts","kind":"import-statement"},{"path":"src/trick-dom-testing-library.ts","kind":"import-statement"},{"path":"src/types.ts","kind":"import-statement"},{"path":"src/shadow-queries.ts","kind":"import-statement"},{"path":"src/deep-query-selectors.ts","kind":"import-statement"}]}},"outputs":{"dist/index.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":55489},"dist/index.js":{"imports":[],"exports":["debug","deepQuerySelector","deepQuerySelectorAll","findAllByShadowAltText","findAllByShadowDisplayValue","findAllByShadowLabelText","findAllByShadowPlaceholderText","findAllByShadowRole","findAllByShadowTestId","findAllByShadowText","findAllByShadowTitle","findByShadowAltText","findByShadowDisplayValue","findByShadowLabelText","findByShadowPlaceholderText","findByShadowRole","findByShadowTestId","findByShadowText","findByShadowTitle","getAllByShadowAltText","getAllByShadowDisplayValue","getAllByShadowLabelText","getAllByShadowPlaceholderText","getAllByShadowRole","getAllByShadowTestId","getAllByShadowText","getAllByShadowTitle","getAllElementsAndShadowRoots","getByShadowAltText","getByShadowDisplayValue","getByShadowLabelText","getByShadowPlaceholderText","getByShadowRole","getByShadowTestId","getByShadowText","getByShadowTitle","logShadowDOM","prettyShadowDOM","queryAllByShadowAltText","queryAllByShadowDisplayValue","queryAllByShadowLabelText","queryAllByShadowPlaceholderText","queryAllByShadowRole","queryAllByShadowTestId","queryAllByShadowText","queryAllByShadowTitle","queryByShadowAltText","queryByShadowDisplayValue","queryByShadowLabelText","queryByShadowPlaceholderText","queryByShadowRole","queryByShadowTestId","queryByShadowText","queryByShadowTitle","screen","within"],"entryPoint":"src/index.ts","inputs":{"src/index.ts":{"bytesInOutput":117},"src/shadow-queries.ts":{"bytesInOutput":3944},"src/deep-query-selectors.ts":{"bytesInOutput":832},"src/log-shadow-dom.ts":{"bytesInOutput":193},"src/pretty-shadow-dom.ts":{"bytesInOutput":2409},"src/debug.ts":{"bytesInOutput":80},"src/shadow-screen.ts":{"bytesInOutput":3438},"src/trick-dom-testing-library.ts":{"bytesInOutput":395}},"bytes":12992}}}
{
"name": "shadow-dom-testing-library",
"version": "1.7.1",
"version": "1.8.0",
"description": "An extension of DOM-testing-library to provide hooks into the shadow dom",

@@ -5,0 +5,0 @@ "type": "module",

@@ -77,3 +77,3 @@ import { Container, ShadowOptions } from "./types";

elements.push(container.shadowRoot);
elementsToProcess.push(container.shadowRoot)
elementsToProcess.push(container.shadowRoot);
}

@@ -80,0 +80,0 @@

import { logDOM } from "@testing-library/dom";
import { toJSDOM } from "./pretty-shadow-dom";
import {
createDOMElementFilter,
filterCommentsAndDefaultIgnoreTagsTags,
} from "./pretty-shadow-dom";
import type { NewPlugin } from "pretty-format";

@@ -7,5 +11,13 @@ export function logShadowDOM(

): ReturnType<typeof logDOM> {
const [dom, maxLength, options] = args;
let [dom, maxLength, options] = args;
logDOM(toJSDOM(dom), maxLength, options);
const plugin: NewPlugin = createDOMElementFilter(
options?.filterNode || filterCommentsAndDefaultIgnoreTagsTags
);
if (options == null) options = {};
if (options.plugins == null) options.plugins = [];
options.plugins.push(plugin);
logDOM(dom, maxLength, options);
}

@@ -1,75 +0,311 @@

import { prettyDOM } from "@testing-library/dom";
import { prettyDOM, getConfig } from "@testing-library/dom";
import type { Config, NewPlugin, Printer, Refs } from "pretty-format";
/**
* This is an extension of prettyDOM / logDOM that provides proper printing of shadow roots.
*/
export function prettyShadowDOM(
...args: Parameters<typeof prettyDOM>
): ReturnType<typeof prettyDOM> {
const [element, maxLength, options] = args;
return prettyDOM(toJSDOM(element), maxLength, options);
let [dom, maxLength, options] = args;
const plugin: NewPlugin = createDOMElementFilter(
options?.filterNode || filterCommentsAndDefaultIgnoreTagsTags
);
if (options == null) options = {};
if (options.plugins == null) options.plugins = [];
options.plugins.push(plugin);
return prettyDOM(dom, maxLength, {
...options,
plugins: [plugin],
});
}
export function toJSDOM(element?: Element | Document | undefined): HTMLElement {
if (element == null) element = document.documentElement;
function escapeHTML(str: string): string {
return str.replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
let htmlString: string = "";
export function filterCommentsAndDefaultIgnoreTagsTags(value: Node) {
return (
value.nodeType !== COMMENT_NODE &&
(value.nodeType !== ELEMENT_NODE ||
// @ts-expect-error
!value.matches(getConfig().defaultIgnore))
);
}
if ("outerHTML" in element) {
htmlString = element.outerHTML;
}
// Return empty string if keys is empty.
const printProps = (
keys: Array<string>,
props: Record<string, unknown>,
config: Config,
indentation: string,
depth: number,
refs: Refs,
printer: Printer
): string => {
const indentationNext = indentation + config.indent;
const colors = config.colors;
return keys
.map((key) => {
const value = props[key];
let printed = printer(value, config, indentationNext, depth, refs);
htmlString = processNodes(element, htmlString);
if (typeof value !== "string") {
if (printed.indexOf("\n") !== -1) {
printed =
config.spacingOuter +
indentationNext +
printed +
config.spacingOuter +
indentation;
}
printed = "{" + printed + "}";
}
// Remove Comment nodes
htmlString = htmlString.replace(/<!--.*?-->/g, "");
return (
config.spacingInner +
indentation +
colors.prop.open +
key +
colors.prop.close +
"=" +
colors.value.open +
printed +
colors.value.close
);
})
.join("");
};
// Remove extraneous whitespace as it creates bloated printing
htmlString = htmlString.replace(/>\s+</g, "><");
// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#node_type_constants
const NodeTypeTextNode = 3;
const parser = new DOMParser();
const dom = parser.parseFromString(htmlString, "text/html");
// Return empty string if children is empty.
const printChildren = (
children: Array<unknown>,
config: Config,
indentation: string,
depth: number,
refs: Refs,
printer: Printer
): string =>
children
.map((child) => {
const printedChild =
typeof child === "string"
? printText(child, config)
: printer(child, config, indentation, depth, refs);
if (
element instanceof Document ||
element instanceof HTMLHtmlElement ||
element instanceof HTMLBodyElement
) {
return dom.body;
}
if (
printedChild === "" &&
typeof child === "object" &&
child !== null &&
(child as Node).nodeType !== NodeTypeTextNode
) {
// A plugin serialized this Node to '' meaning we should ignore it.
return "";
}
return config.spacingOuter + indentation + printedChild;
})
.join("");
return dom.body.firstElementChild as HTMLElement;
}
const printText = (text: string, config: Config): string => {
const contentColor = config.colors.content;
return contentColor.open + escapeHTML(text) + contentColor.close;
};
function processNodes(
element: Element | Document | ShadowRoot,
stringBuffer: string = "",
nodes: Array<Element | ShadowRoot | Document> = Array.from(element.children)
) {
if ("shadowRoot" in element && element.shadowRoot != null) {
nodes.unshift(element.shadowRoot);
}
const printComment = (comment: string, config: Config): string => {
const commentColor = config.colors.comment;
return (
commentColor.open +
"<!--" +
escapeHTML(comment) +
"-->" +
commentColor.close
);
};
nodes.unshift(element);
// Separate the functions to format props, children, and element,
// so a plugin could override a particular function, if needed.
// Too bad, so sad: the traditional (but unnecessary) space
// in a self-closing tagColor requires a second test of printedProps.
const printElement = (
type: string,
printedProps: string,
printedChildren: string,
config: Config,
indentation: string
): string => {
const tagColor = config.colors.tag;
while (nodes.length > 0) {
const node = nodes.shift()!;
if (node && "shadowRoot" in node && node.shadowRoot != null) {
const outerHTML = node.outerHTML;
return (
tagColor.open +
"<" +
type +
(printedProps &&
tagColor.close +
printedProps +
config.spacingOuter +
indentation +
tagColor.open) +
(printedChildren
? ">" +
tagColor.close +
printedChildren +
config.spacingOuter +
indentation +
tagColor.open +
"</" +
type
: (printedProps && !config.min ? "" : " ") + "/") +
">" +
tagColor.close
);
};
const shadowRootPseudoNode = document.createElement("shadow-root");
shadowRootPseudoNode.innerHTML = node.shadowRoot.innerHTML;
const printElementAsLeaf = (type: string, config: Config): string => {
const tagColor = config.colors.tag;
return (
tagColor.open +
"<" +
type +
tagColor.close +
" …" +
tagColor.open +
" />" +
tagColor.close
);
};
const clonedNode = node.cloneNode(true) as Element;
clonedNode.insertAdjacentElement("afterbegin", shadowRootPseudoNode);
const ELEMENT_NODE = 1;
const TEXT_NODE = 3;
const COMMENT_NODE = 8;
const FRAGMENT_NODE = 11;
stringBuffer = stringBuffer.replace(outerHTML, clonedNode.outerHTML);
const ELEMENT_REGEXP = /^((HTML|SVG)\w*)?Element$/;
stringBuffer = stringBuffer.replace(outerHTML, node.outerHTML);
nodes.push(...Array.from(node.shadowRoot.children));
const testNode = (val: any) => {
const constructorName = val?.constructor?.name || "";
const { nodeType, tagName } = val;
const isCustomElement =
(typeof tagName === "string" && tagName.includes("-")) ||
(typeof val.hasAttribute === "function" && val.hasAttribute("is")) ||
val instanceof HTMLElement;
return (
(nodeType === ELEMENT_NODE &&
(ELEMENT_REGEXP.test(constructorName) || isCustomElement)) ||
(nodeType === TEXT_NODE && constructorName === "Text") ||
(nodeType === COMMENT_NODE && constructorName === "Comment") ||
// Don't check constructorName === "DocumentFragment" because it excludes ShadowRoot.
nodeType === FRAGMENT_NODE
);
};
export const test: NewPlugin["test"] = (val: any) =>
val?.constructor?.name && testNode(val);
type HandledType = Element | Text | Comment | DocumentFragment;
function nodeIsText(node: HandledType): node is Text {
return node.nodeType === TEXT_NODE;
}
function nodeIsComment(node: HandledType): node is Comment {
return node.nodeType === COMMENT_NODE;
}
function nodeIsFragment(
node: HandledType
): node is DocumentFragment | ShadowRoot {
return node.nodeType === FRAGMENT_NODE;
}
export function createDOMElementFilter(
filterNode: (node: Node) => boolean
): NewPlugin {
function getChildren(
node: Element | DocumentFragment | ShadowRoot
): (Node | Element | ShadowRoot)[] {
const children: (Node | Element | ShadowRoot)[] =
Array.prototype.slice.call(node.childNodes || node.children);
if (
"shadowRoot" in node &&
node.shadowRoot != null &&
node.shadowRoot.mode !== "closed"
) {
children.unshift(node.shadowRoot);
}
nodes.push(...Array.from(node.children));
return children.filter(filterNode);
}
return stringBuffer;
return {
test: (val: any) => val?.constructor && testNode(val),
serialize: (
node: HandledType,
config: Config,
indentation: string,
depth: number,
refs: Refs,
printer: Printer
) => {
if (nodeIsText(node)) {
return printText(node.data, config);
}
if (nodeIsComment(node)) {
return printComment(node.data, config);
}
let type = "DocumentFragment";
if ("tagName" in node && node.tagName) {
type = node.tagName.toLowerCase();
} else if (node instanceof ShadowRoot) {
type = "ShadowRoot";
}
if (++depth > config.maxDepth) {
return printElementAsLeaf(type, config);
}
return printElement(
type,
printProps(
nodeIsFragment(node)
? []
: Array.from(node.attributes)
.map((attr) => attr.name)
.sort(),
nodeIsFragment(node)
? {}
: Array.from(node.attributes).reduce<Record<string, string>>(
(props, attribute) => {
props[attribute.name] = attribute.value;
return props;
},
{}
),
config,
indentation + config.indent,
depth,
refs,
printer
),
printChildren(
getChildren(node) as unknown[],
config,
indentation + config.indent,
depth,
refs,
printer
),
config,
indentation
);
},
};
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc