MCPcopy
hub / github.com/Rich-Harris/ramjet / wrapNode

Function wrapNode

src/utils/node.js:35–104  ·  view source on GitHub ↗
( node, destinationIsFixed, overrideClone, appendToBody )

Source from the content-addressed store, hash-verified

33}
34
35export function wrapNode ( node, destinationIsFixed, overrideClone, appendToBody ) {
36 const isSvg = node.namespaceURI === svgns;
37
38 const { left, right, top, bottom } = node.getBoundingClientRect();
39 const style = window.getComputedStyle( node );
40 const clone = cloneNode( node, 0, overrideClone);
41
42 const wrapper = {
43 node, clone, isSvg,
44 cx: ( left + right ) / 2,
45 cy: ( top + bottom ) / 2,
46 width: right - left,
47 height: bottom - top,
48 transform: null,
49 borderRadius: null
50 };
51
52 if ( isSvg ) {
53 const ctm = node.getScreenCTM();
54 wrapper.transform = 'matrix(' + [ ctm.a, ctm.b, ctm.c, ctm.d, ctm.e, ctm.f ].join( ',' ) + ')';
55 wrapper.borderRadius = [ 0, 0, 0, 0 ];
56
57 svg.appendChild( clone );
58 } else {
59
60 if ( destinationIsFixed ){
61 // position relative to the viewport
62 clone.style.position = 'fixed';
63 clone.style.top = ( top - parseInt( style.marginTop, 10 )) + 'px';
64 clone.style.left = ( left - parseInt( style.marginLeft, 10 )) + 'px';
65 }
66 else {
67 const offsetParent = node.offsetParent;
68
69 if (offsetParent === null || offsetParent === document.body || appendToBody){ // parent is fixed, or I want to append the node to the body
70 // position relative to the document
71 const docElem = document.documentElement;
72 clone.style.position = 'absolute';
73 clone.style.top = (top + window.pageYOffset - docElem.clientTop - parseInt( style.marginTop, 10 )) + 'px';
74 clone.style.left = (left + window.pageXOffset - docElem.clientLeft - parseInt( style.marginLeft, 10 )) + 'px';
75 }
76 else {
77 //position relative to the parent
78 const offsetParentStyle = window.getComputedStyle( offsetParent );
79 const offsetParentBcr = offsetParent.getBoundingClientRect();
80
81 clone.style.position = 'absolute';
82 clone.style.top = ( top - parseInt( style.marginTop, 10 ) - ( offsetParentBcr.top - parseInt( offsetParentStyle.marginTop, 10 ) ) ) + 'px';
83 clone.style.left = ( left - parseInt( style.marginLeft, 10 ) - ( offsetParentBcr.left - parseInt( offsetParentStyle.marginLeft, 10 ) ) ) + 'px';
84 }
85 }
86
87 wrapper.transform = ''; // TODO...?
88 wrapper.borderRadius = [
89 parseFloat( style.borderTopLeftRadius ),
90 parseFloat( style.borderTopRightRadius ),
91 parseFloat( style.borderBottomRightRadius ),
92 parseFloat( style.borderBottomLeftRadius )

Callers 1

transformFunction · 0.90

Calls 1

cloneNodeFunction · 0.85

Tested by

no test coverage detected