HTMLRewriter escaping ampersand when using text() method

I am writing some domain name replacement which has been covered in numerous topics. Specifically I have a few scenarios where I am replacing attributes in the element as well as text within script tags. Strangely all of my rewrites are working properly however it’s breaking when replacing chunks that are within tags and is strangely replacing & with &

				// Define the old and new URLs dynamically based on the request and mapping
				let oldUrl = ORIGINS[ basepath ];
				let newUrl = LIVE_DOMAIN  + basepath;

				let modifiedResponse = new Response( response.body, {
					status     : response.status,
					statusText : response.statusText,
					headers    : response.headers,
				} );

				// Check if the response is HTML and apply URL replacements
				if ( modifiedResponse.headers.get('content-type')?.includes('text/html') ) {

					let domainRewriter = {
						text( chunk ) {
							buffer += chunk.text;
							if ( chunk.lastInTextNode ) {
								chunk.replace( buffer.replace( 'http:\/\/' + ORIGINS[ basepath ], 'https:\/\/' + LIVE_DOMAIN + '\/' + basepath ) )
								chunk.replace( buffer.replace( oldUrl, newUrl ) );

								buffer = '';
							} else {

						 * There are certain scenarios where the data is output to the page in a strange way
						 * in those cases we may need to clean up the actual content in the page. Sometimes
						 * this is easy and we can target an attribute in a tag. Sometimes we need to use a
						 * buffer to do the replacement.
						 * This may need to be cleaned up over time.
						 * @param {} element
						element( element ) {

							const tagName = element.tagName;

							switch( tagName ) {
								case 'a' :
								case 'link' :

									adjustAttribute( element, 'href', oldUrl, newUrl );

									//    const value = element.getAttribute( 'href' );

									//   if ( value ) {
									//     element.setAttribute( 'href', value.replace( oldUrl, newUrl ) );
									//   }
								case 'script' :
									adjustAttribute( element, 'src', oldUrl, newUrl );

					// Use HTMLRewriter to replace URLs in the HTML content
					const newResponse = new HTMLRewriter()
						.on( '*', domainRewriter )
						.transform( modifiedResponse );

If I comment out the text method while my text in the <script> tag is not replaced it does not escape the ampersands that are in the <script> tag.

I see that the element does have some ability to pass an {html:true} in the ContentOptions but it doesn’t appear that the text has that same flag?

For anyone looking for a solution to this. I ended up finding an older post here with a great explanation that a lot of the HTMLRewriter methods including text() have the ability to pass {html:true} as an argument