@@ -52,12 +52,16 @@ afterEach(() => {
5252
5353// The preview page: the saved markup with the preview's in-memory ids, a sub-composition
5454// mounted inline, and the runtime's hide on both clips.
55- function mountPreview ( sub : string , bundle : ( host : Element ) => void ) : HTMLIFrameElement {
55+ function mountPreview (
56+ sub : string ,
57+ bundle : ( host : Element ) => void ,
58+ saved = SAVED ,
59+ ) : HTMLIFrameElement {
5660 const iframe = document . createElement ( "iframe" ) ;
5761 document . body . appendChild ( iframe ) ;
5862 const doc = iframe . contentDocument as Document ;
5963 doc . open ( ) ;
60- doc . write ( ensureHfIds ( SAVED ) ) ;
64+ doc . write ( ensureHfIds ( saved ) ) ;
6165 doc . close ( ) ;
6266 const parsed = new DOMParser ( ) . parseFromString ( ensureHfIds ( sub ) , "text/html" ) ;
6367 const subRoot = ( parsed . querySelector ( "template" ) as HTMLTemplateElement ) . content
@@ -93,14 +97,15 @@ function mountClipboard(
9397 domSelection : DomEditSelection | null = null ,
9498 sub = SUB ,
9599 bundle : ( host : Element ) => void = ( ) => { } ,
100+ saved = SAVED ,
96101) {
97- const files : Record < string , string > = { "index.html" : SAVED , "compositions/sub.html" : sub } ;
102+ const files : Record < string , string > = { "index.html" : saved , "compositions/sub.html" : sub } ;
98103 const fail = { on : false } ;
99104 const delayMs : Record < string , number > = { } ;
100105 const domEditSave = { pending : Promise . resolve ( ) } ;
101106 const domSelectionRef = { current : domSelection } ;
102107 stubFiles ( files , fail , delayMs ) ;
103- const iframe = mountPreview ( sub , bundle ) ;
108+ const iframe = mountPreview ( sub , bundle , saved ) ;
104109 const writes : string [ ] = [ ] ;
105110 const deleted : string [ ] = [ ] ;
106111 const writeProjectFile = async ( _path : string , content : string ) => {
@@ -248,6 +253,39 @@ const SUB_SELECTION = {
248253 sourceFile : "compositions/sub.html" ,
249254} as DomEditSelection ;
250255
256+ describe ( "paste of an element styled by its id" , ( ) => {
257+ it ( "gives the copy the look its original has" , async ( ) => {
258+ clearSelection ( ) ;
259+ const saved = SAVED . replace (
260+ "<body>" ,
261+ "<body><style>#badge { color: rgb(200, 30, 40); letter-spacing: 3px; }</style>" ,
262+ ) . replace ( "</h1>" , '</h1><p id="badge">New</p>' ) ;
263+ const selection = {
264+ hfId : stampedHfId ( saved , "#badge" ) ,
265+ selector : "#badge" ,
266+ selectorIndex : 0 ,
267+ sourceFile : "index.html" ,
268+ } as DomEditSelection ;
269+ const { clipboard, writes } = mountClipboard ( selection , SUB , ( ) => { } , saved ) ;
270+ clipboard ( ) . handleCopy ( ) ;
271+ await clipboard ( ) . handlePaste ( ) ;
272+ const page = document . createElement ( "iframe" ) ;
273+ document . body . appendChild ( page ) ;
274+ const doc = page . contentDocument as Document ;
275+ doc . open ( ) ;
276+ doc . write ( writes [ 0 ] ?? "" ) ;
277+ doc . close ( ) ;
278+ const look = ( id : string ) => {
279+ const style = ( page . contentWindow as Window ) . getComputedStyle (
280+ doc . getElementById ( id ) as Element ,
281+ ) ;
282+ return { color : style . color , letterSpacing : style . letterSpacing } ;
283+ } ;
284+ expect ( look ( "badge" ) ) . toEqual ( { color : "rgb(200, 30, 40)" , letterSpacing : "3px" } ) ;
285+ expect ( look ( "badge-2" ) ) . toEqual ( look ( "badge" ) ) ;
286+ } ) ;
287+ } ) ;
288+
251289describe ( "copy of a sub-composition clip" , ( ) => {
252290 it ( "rebases its relative asset paths to the project root, as the preview does" , async ( ) => {
253291 clearSelection ( ) ;
0 commit comments