@@ -100,6 +100,10 @@ export function utf8ByteLength(value: string, stopAfter = Number.POSITIVE_INFINI
100100 return utf8ByteLengthRange ( value , 0 , value . length , stopAfter )
101101}
102102
103+ function isGuaranteedWithinUtf8Limit ( characters : number , limit : number ) : boolean {
104+ return characters <= Math . floor ( limit / 3 )
105+ }
106+
103107function normalizedSelection ( input : TextPasteAdmissionInput ) : {
104108 currentText : string
105109 start : number
@@ -139,6 +143,40 @@ export function assessTextPaste(input: TextPasteAdmissionInput): TextPasteAdmiss
139143 }
140144 }
141145
146+ const projectsResult =
147+ input . maxResultBytes !== undefined || input . maxResultCharacters !== undefined
148+ if (
149+ ! projectsResult &&
150+ input . maxPastedBytes !== undefined &&
151+ isGuaranteedWithinUtf8Limit ( pastedText . length , input . maxPastedBytes )
152+ ) {
153+ return { accepted : true }
154+ }
155+
156+ const selection = projectsResult ? normalizedSelection ( input ) : null
157+ const resultCharacters = selection
158+ ? selection . currentText . length - ( selection . end - selection . start ) + pastedText . length
159+ : undefined
160+ if (
161+ input . maxResultCharacters !== undefined &&
162+ resultCharacters !== undefined &&
163+ resultCharacters > input . maxResultCharacters
164+ ) {
165+ return {
166+ accepted : false ,
167+ reason : 'result-characters' ,
168+ actual : resultCharacters ,
169+ limit : input . maxResultCharacters ,
170+ }
171+ }
172+ if (
173+ input . maxResultBytes !== undefined &&
174+ resultCharacters !== undefined &&
175+ isGuaranteedWithinUtf8Limit ( resultCharacters , input . maxResultBytes )
176+ ) {
177+ return { accepted : true , resultCharacters }
178+ }
179+
142180 const pastedByteLimit = Math . max ( input . maxPastedBytes ?? 0 , input . maxResultBytes ?? 0 )
143181 const pastedBytes = pastedByteLimit > 0 ? utf8ByteLength ( pastedText , pastedByteLimit ) : undefined
144182
@@ -155,20 +193,11 @@ export function assessTextPaste(input: TextPasteAdmissionInput): TextPasteAdmiss
155193 }
156194 }
157195
158- if ( input . maxResultBytes === undefined && input . maxResultCharacters === undefined ) {
196+ if ( ! projectsResult ) {
159197 return { accepted : true , pastedBytes }
160198 }
161199
162- const { currentText, start, end } = normalizedSelection ( input )
163- const resultCharacters = currentText . length - ( end - start ) + pastedText . length
164- if ( input . maxResultCharacters !== undefined && resultCharacters > input . maxResultCharacters ) {
165- return {
166- accepted : false ,
167- reason : 'result-characters' ,
168- actual : resultCharacters ,
169- limit : input . maxResultCharacters ,
170- }
171- }
200+ const { currentText, start, end } = selection as ReturnType < typeof normalizedSelection >
172201
173202 if ( input . maxResultBytes === undefined ) {
174203 return { accepted : true , pastedBytes, resultCharacters }
0 commit comments