@@ -16,14 +16,19 @@ afterEach(() => {
1616 editor = null
1717} )
1818
19- function runPaste ( ed : Editor , text : string , html = '' ) : { handled : boolean ; prevented : boolean } {
19+ function runPaste (
20+ ed : Editor ,
21+ text : string ,
22+ html = '' ,
23+ extra : Record < string , string > = { }
24+ ) : { handled : boolean ; prevented : boolean } {
2025 let prevented = false
2126 const event = {
2227 clipboardData : {
2328 getData : ( type : string ) => {
2429 if ( type === 'text/plain' ) return text
2530 if ( type === 'text/html' ) return html
26- return ''
31+ return extra [ type ] ?? ''
2732 } ,
2833 } ,
2934 preventDefault : ( ) => {
@@ -38,11 +43,11 @@ function runPaste(ed: Editor, text: string, html = ''): { handled: boolean; prev
3843 return { handled : false , prevented }
3944}
4045
41- function dispatchPaste ( ed : Editor , text : string ) {
46+ function dispatchPaste ( ed : Editor , text : string , extra : Record < string , string > = { } ) {
4247 const event = new Event ( 'paste' , { bubbles : true , cancelable : true } )
4348 Object . defineProperty ( event , 'clipboardData' , {
4449 value : {
45- getData : ( type : string ) => ( type === 'text/plain' ? text : '' ) ,
50+ getData : ( type : string ) => ( type === 'text/plain' ? text : ( extra [ type ] ?? '' ) ) ,
4651 files : [ ] ,
4752 items : [ ] ,
4853 } ,
@@ -136,23 +141,62 @@ describe('rich Markdown paste admission', () => {
136141 expect ( onRejected ) . toHaveBeenCalledOnce ( )
137142 } )
138143
139- it ( 'rejects before downstream paste parsing when projected bytes exceed the document limit' , ( ) => {
140- const onRejected = vi . fn ( )
141- editor = new Editor ( {
142- extensions : [
143- ...createMarkdownContentExtensions ( ) ,
144- createRichMarkdownPasteAdmission ( {
145- maxResultBytes : 10 ,
146- getCurrentText : ( ) => '123456' ,
147- onRejected,
148- } ) ,
149- ] ,
150- content : '<p>123456</p>' ,
151- } )
144+ it . each ( [ 'text/plain' , 'text/markdown' ] ) (
145+ 'rejects oversized %s before downstream paste parsing' ,
146+ ( type ) => {
147+ const onRejected = vi . fn ( )
148+ editor = new Editor ( {
149+ extensions : [
150+ ...createMarkdownContentExtensions ( ) ,
151+ createRichMarkdownPasteAdmission ( {
152+ maxResultBytes : 10 ,
153+ getCurrentText : ( ) => '123456' ,
154+ onRejected,
155+ } ) ,
156+ ] ,
157+ content : '<p>123456</p>' ,
158+ } )
152159
153- expect ( runPaste ( editor , 'abcde' ) ) . toEqual ( { handled : true , prevented : true } )
154- expect ( onRejected ) . toHaveBeenCalledOnce ( )
155- } )
160+ expect (
161+ runPaste ( editor , type === 'text/plain' ? 'abcde' : 'x' , '' , { [ type ] : 'abcde' } )
162+ ) . toEqual ( { handled : true , prevented : true } )
163+ expect ( onRejected ) . toHaveBeenCalledOnce ( )
164+ }
165+ )
166+
167+ it . each ( [ 'plain shortcut' , 'code block' , 'inline code' ] ) (
168+ 'budgets the consumed plain representation for %s' ,
169+ ( context ) => {
170+ const onRejected = vi . fn ( )
171+ editor = new Editor ( {
172+ extensions : [
173+ ...createMarkdownContentExtensions ( ) ,
174+ MarkdownPaste ,
175+ createRichMarkdownPasteAdmission ( {
176+ maxResultBytes : 64 ,
177+ getCurrentText : ( ) => editor ?. getMarkdown ( ) ?? '' ,
178+ onRejected,
179+ } ) ,
180+ ] ,
181+ enablePasteRules : false ,
182+ } )
183+ if ( context === 'code block' ) editor . commands . setCodeBlock ( )
184+ else if ( context === 'inline code' ) editor . commands . setCode ( )
185+ else
186+ editor . view . dom . dispatchEvent (
187+ new KeyboardEvent ( 'keydown' , {
188+ key : 'v' ,
189+ keyCode : 86 ,
190+ ctrlKey : true ,
191+ shiftKey : true ,
192+ bubbles : true ,
193+ } )
194+ )
195+ dispatchPaste ( editor , 'x' , { 'text/markdown' : '# ' . repeat ( 100 ) } )
196+ expect ( editor . state . doc . textContent ) . toBe ( 'x' )
197+ expect ( onRejected ) . not . toHaveBeenCalled ( )
198+ }
199+ )
156200
157201 it ( 'rejects oversized rich HTML before downstream parsing' , ( ) => {
158202 const onRejected = vi . fn ( )
0 commit comments