All files slots-light-vs-shadow.js

100% Statements 110/110
100% Branches 11/11
100% Functions 6/6
100% Lines 110/110

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 1111x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/* tests are using createTestTree(node) to
 * append the node clone with
 * initiated shadowDom on template parent ( id=host )
 */
import { CssChain as $ } from "./CssChain.js";
 
// from https://github.com/chromium/chromium/edit/main/third_party/blink/web_tests/external/wpt/shadow-dom/resources/shadow-dom.js
// changes:
//      * added walkLightDom() code to test $.template()
//      * createTestTree adds the cloned transformed node to parent
 
export function createTestTree( node )
{
 
    let ids = {light:{},native:{}};
 
    function attachShadowFromTemplate( template )
    {
        let parent = template.parentNode;
        parent.removeChild( template );
        let shadowRoot;
        if( template.getAttribute( 'data-slot-assignment' ) === 'manual' )
        {   /* c8 ignore next 5 */
            shadowRoot =
                parent.attachShadow( {
                    mode: template.getAttribute( 'data-mode' ),
                    slotAssignment: 'manual'
                } );
        }else
        {
            shadowRoot = parent.attachShadow(
                { mode: template.getAttribute( 'data-mode' ) } );
        }
        let id = template.id;
        /* c8 ignore next 5 */
        if( id )
        {
            shadowRoot.id = id;
            ids[ id ] = shadowRoot;
        }
        shadowRoot.appendChild( document.importNode( template.content, true ) );
        return shadowRoot;
    }
 
    function walkShadowDom( root )
    {   /* c8 ignore next 4 */
        if( root.id )
        {
            ids[ root.id ] = root;
        }
        for( let e of Array.from( root.querySelectorAll( '[id]' ) ) )
        {
            ids[ e.id ] = e;
        }
        for( let e of Array.from( root.querySelectorAll( 'template' ) ) )
        {
            walkShadowDom( attachShadowFromTemplate( e ) );
        }
    }
 
    function walkLightDom( root ) // using CssChain
    {   if( root.id )
            ids.light[ root.id ] = root;
 
        $( '[id]',root ).map( e => ids.light[ e.id ] = e );
 
        $( 'template', root )
            .map( t =>
            {   const p = t.parentNode;
                p.removeChild(t);
                const $rendered = $(p).template(t);
                $rendered.children.map( walkLightDom );
            });
    }
 
    const cloneAppend = (n,classname) =>
    {   let x = node.cloneNode( true );
        x.id=classname;
        x.classList.add(classname);
 
        if( classname==='light')
            ids.light[node.id]=x;
        else
            ids[node.id]=x;
        n.parentNode.appendChild(x);
        return x
    };
 
    walkLightDom( cloneAppend( node ,'light'  ) );
    walkShadowDom( cloneAppend( node,'shadow' ) );
 
    return ids;
}
 
// https://github.com/chromium/chromium/blob/main/third_party/blink/web_tests/external/wpt/shadow-dom/resources/shadow-dom.js#L1
export function removeWhiteSpaceOnlyTextNodes(node)
{
    for (var i = 0; i < node.childNodes.length; i++) {
        var child = node.childNodes[i];
        if (child.nodeType === Node.TEXT_NODE && child.nodeValue.trim().length == 0) {
            node.removeChild(child);
            i--;
        } else if (child.nodeType === Node.ELEMENT_NODE || child.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
            removeWhiteSpaceOnlyTextNodes(child);
        }
    }
    if (node.shadowRoot) {
        removeWhiteSpaceOnlyTextNodes(node.shadowRoot);
    }
}