@@ -9,9 +9,9 @@ use std::sync::Arc;
99use lol_html:: { element, html_content:: ContentType , text, Settings as RewriterSettings } ;
1010
1111use crate :: integrations:: {
12- AttributeRewriteOutcome , IntegrationAttributeContext , IntegrationHtmlContext ,
13- IntegrationHtmlPostProcessor , IntegrationRegistry , IntegrationScriptContext ,
14- ScriptRewriteAction ,
12+ AttributeRewriteOutcome , IntegrationAttributeContext , IntegrationDocumentState ,
13+ IntegrationHtmlContext , IntegrationHtmlPostProcessor , IntegrationRegistry ,
14+ IntegrationScriptContext , ScriptRewriteAction ,
1515} ;
1616use crate :: settings:: Settings ;
1717use crate :: streaming_processor:: { HtmlRewriterAdapter , StreamProcessor } ;
@@ -23,6 +23,7 @@ struct HtmlWithPostProcessing {
2323 origin_host : String ,
2424 request_host : String ,
2525 request_scheme : String ,
26+ document_state : IntegrationDocumentState ,
2627}
2728
2829impl StreamProcessor for HtmlWithPostProcessing {
@@ -40,6 +41,7 @@ impl StreamProcessor for HtmlWithPostProcessing {
4041 request_host : & self . request_host ,
4142 request_scheme : & self . request_scheme ,
4243 origin_host : & self . origin_host ,
44+ document_state : & self . document_state ,
4345 } ;
4446
4547 // Preflight to avoid allocating a `String` unless at least one post-processor wants to run.
@@ -77,6 +79,7 @@ impl StreamProcessor for HtmlWithPostProcessing {
7779
7880 fn reset ( & mut self ) {
7981 self . inner . reset ( ) ;
82+ self . document_state . clear ( ) ;
8083 }
8184}
8285
@@ -110,6 +113,7 @@ impl HtmlProcessorConfig {
110113/// Create an HTML processor with URL replacement and optional Prebid injection
111114pub fn create_html_processor ( config : HtmlProcessorConfig ) -> impl StreamProcessor {
112115 let post_processors = config. integrations . html_post_processors ( ) ;
116+ let document_state = IntegrationDocumentState :: default ( ) ;
113117
114118 // Simplified URL patterns structure - stores only core data and generates variants on-demand
115119 struct UrlPatterns {
@@ -404,15 +408,19 @@ pub fn create_html_processor(config: HtmlProcessorConfig) -> impl StreamProcesso
404408 let selector = script_rewriter. selector ( ) ;
405409 let rewriter = script_rewriter. clone ( ) ;
406410 let patterns = patterns. clone ( ) ;
411+ let document_state = document_state. clone ( ) ;
407412 element_content_handlers. push ( text ! ( selector, {
408413 let rewriter = rewriter. clone( ) ;
409414 let patterns = patterns. clone( ) ;
415+ let document_state = document_state. clone( ) ;
410416 move |text| {
411417 let ctx = IntegrationScriptContext {
412418 selector,
413419 request_host: & patterns. request_host,
414420 request_scheme: & patterns. request_scheme,
415421 origin_host: & patterns. origin_host,
422+ is_last_in_text_node: text. last_in_text_node( ) ,
423+ document_state: & document_state,
416424 } ;
417425 match rewriter. rewrite( text. as_str( ) , & ctx) {
418426 ScriptRewriteAction :: Keep => { }
@@ -439,6 +447,7 @@ pub fn create_html_processor(config: HtmlProcessorConfig) -> impl StreamProcesso
439447 origin_host : config. origin_host ,
440448 request_host : config. request_host ,
441449 request_scheme : config. request_scheme ,
450+ document_state,
442451 }
443452}
444453
0 commit comments