@@ -74,6 +74,9 @@ pub struct SAXParser<'a> {
74
74
75
75
// Event Handling
76
76
event_handler : & ' a dyn EventHandler ,
77
+ // Used to make sure dispatched objects
78
+ // stick around until the next write
79
+ dispatched : Vec < Dispatched > ,
77
80
78
81
// Parsing Buffers
79
82
tags : Vec < Tag > ,
@@ -154,6 +157,7 @@ impl<'a> SAXParser<'a> {
154
157
155
158
// Event Handling
156
159
event_handler,
160
+ dispatched : Vec :: new ( ) ,
157
161
158
162
// Parsing Buffers
159
163
text : None ,
@@ -223,6 +227,7 @@ impl<'a> SAXParser<'a> {
223
227
///
224
228
/// ```
225
229
pub fn write ( & mut self , source : & [ u8 ] ) {
230
+ self . dispatched . clear ( ) ;
226
231
let mut bytes = source;
227
232
228
233
let frag_len = self . fragment . len ( ) ;
@@ -494,8 +499,11 @@ impl<'a> SAXParser<'a> {
494
499
}
495
500
496
501
if self . events [ Event :: OpenTagStart ] {
497
- self . tag . hydrate ( self . source_ptr ) ;
498
- self . event_handler . handle_event ( Event :: OpenTagStart , Entity :: Tag ( & self . tag ) ) ;
502
+ let mut tag = Box :: new ( self . tag . clone ( ) ) ;
503
+ tag. hydrate ( self . source_ptr ) ;
504
+
505
+ self . event_handler . handle_event ( Event :: OpenTagStart , Entity :: Tag ( & * tag) ) ;
506
+ self . dispatched . push ( Dispatched :: Tag ( tag) ) ;
499
507
}
500
508
501
509
match byte {
@@ -557,25 +565,29 @@ impl<'a> SAXParser<'a> {
557
565
}
558
566
559
567
fn flush_text ( & mut self , line : u32 , character : u32 , offset : usize ) {
560
- if let Some ( mut text) = self . text . take ( ) {
561
- text. end = [ line, character] ;
562
- text. header . 1 = offset;
568
+ if self . text . is_none ( ) {
569
+ return ;
570
+ }
571
+ let mut text = Box :: new ( unsafe { self . text . take ( ) . unwrap_unchecked ( ) } ) ;
572
+ text. end = [ line, character] ;
573
+ text. header . 1 = offset;
563
574
564
- // Empty
565
- if text. header . 0 == text. header . 1 && text. value . is_empty ( ) {
566
- return ;
567
- }
575
+ // Empty
576
+ if text. header . 0 == text. header . 1 && text. value . is_empty ( ) {
577
+ return ;
578
+ }
568
579
569
- if self . events [ Event :: Text ] && text. hydrate ( self . source_ptr ) {
570
- self . event_handler . handle_event ( Event :: Text , Entity :: Text ( & text) ) ;
571
- }
580
+ let len = self . tags . len ( ) ;
581
+ // Store these only if we're interested in CloseTag events
582
+ if len != 0 && self . events [ Event :: CloseTag ] {
583
+ self . tags [ len - 1 ] . text_nodes . push ( * text. clone ( ) ) ;
584
+ }
572
585
573
- let len = self . tags . len ( ) ;
574
- // Store these only if we're interested in CloseTag events
575
- if len != 0 && self . events [ Event :: CloseTag ] {
576
- self . tags [ len - 1 ] . text_nodes . push ( text) ;
577
- }
586
+ if self . events [ Event :: Text ] && text. hydrate ( self . source_ptr ) {
587
+ self . event_handler . handle_event ( Event :: Text , Entity :: Text ( & text) ) ;
588
+ self . dispatched . push ( Dispatched :: Text ( text) ) ;
578
589
}
590
+
579
591
}
580
592
581
593
fn markup_decl ( & mut self , gc : & mut GraphemeClusters , current : & [ u8 ] ) {
@@ -652,8 +664,10 @@ impl<'a> SAXParser<'a> {
652
664
if len > 2 && & markup_slice[ ( len - 3 ) ..] == b"-->" {
653
665
markup_decl. end = [ gc. line , gc. character ] ;
654
666
if self . events [ Event :: Comment ] && markup_decl. hydrate ( self . source_ptr ) {
667
+ let mut markup_decl = Box :: new ( self . markup_decl . take ( ) . unwrap ( ) ) ;
655
668
markup_decl. value . truncate ( markup_decl. value . len ( ) - 3 ) ; // remove '-->'
656
669
self . event_handler . handle_event ( Event :: Comment , Entity :: Text ( & markup_decl) ) ;
670
+ self . dispatched . push ( Dispatched :: Text ( markup_decl) ) ;
657
671
}
658
672
self . markup_decl = None ;
659
673
self . state = State :: BeginWhitespace ;
@@ -674,10 +688,11 @@ impl<'a> SAXParser<'a> {
674
688
if len > 2 && & markup_slice[ ( len - 3 ) ..] == b"]]>" {
675
689
markup_decl. end = [ gc. line , gc. character ] ;
676
690
if self . events [ Event :: Cdata ] && markup_decl. hydrate ( self . source_ptr ) {
691
+ let mut markup_decl = Box :: new ( self . markup_decl . take ( ) . unwrap ( ) ) ;
677
692
markup_decl. value . truncate ( markup_decl. value . len ( ) - 3 ) ; // remove ]]>
678
693
self . event_handler . handle_event ( Event :: Cdata , Entity :: Text ( & markup_decl) ) ;
694
+ self . dispatched . push ( Dispatched :: Text ( markup_decl) ) ;
679
695
}
680
- self . markup_decl = None ;
681
696
self . state = State :: BeginWhitespace ;
682
697
}
683
698
}
@@ -697,13 +712,13 @@ impl<'a> SAXParser<'a> {
697
712
fn doctype ( & mut self , gc : & mut GraphemeClusters , current : & [ u8 ] ) {
698
713
let mut byte = current[ 0 ] ;
699
714
700
- let markup_decl = self . markup_decl . as_mut ( ) . unwrap ( ) ;
701
715
// determine where to stop taking bytes for
702
716
// for the doctype value. e.g. '<!DOCTYPE movie ' <----- take 'movie' but not 'movie '
703
717
if self . state != State :: DoctypeEntity && !DOCTYPE_VALUE_END . contains ( & byte) {
704
718
if let Some ( ( span, _) ) = gc. take_until_one_found ( DOCTYPE_VALUE_END , true ) {
705
719
byte = span[ span. len ( ) - 1 ] ;
706
720
}
721
+ let markup_decl = self . markup_decl . as_mut ( ) . unwrap ( ) ;
707
722
markup_decl. header . 1 = gc. cursor ;
708
723
}
709
724
@@ -724,13 +739,14 @@ impl<'a> SAXParser<'a> {
724
739
}
725
740
726
741
if byte == b'>' {
742
+ let mut markup_decl = Box :: new ( self . markup_decl . take ( ) . unwrap ( ) ) ;
727
743
markup_decl. end = [ gc. line , gc. character ] ;
728
744
if self . events [ Event :: Doctype ] && markup_decl. hydrate ( self . source_ptr ) {
729
745
markup_decl. value . truncate ( markup_decl. value . len ( ) - 1 ) ; // remove '>' or '['
730
746
731
747
self . event_handler . handle_event ( Event :: Cdata , Entity :: Text ( & markup_decl) ) ;
748
+ self . dispatched . push ( Dispatched :: Text ( markup_decl) ) ;
732
749
}
733
- self . markup_decl = None ;
734
750
self . state = State :: BeginWhitespace ;
735
751
}
736
752
}
@@ -745,14 +761,14 @@ impl<'a> SAXParser<'a> {
745
761
}
746
762
747
763
if byte == b'>' {
748
- let markup_entity = self . markup_entity . as_mut ( ) . unwrap ( ) ;
764
+ let mut markup_entity = Box :: new ( self . markup_entity . take ( ) . unwrap ( ) ) ;
749
765
markup_entity. header . 1 = gc. cursor - 1 ;
750
766
markup_entity. end = [ gc. line , gc. character . saturating_sub ( 1 ) ] ;
751
767
752
768
if self . events [ Event :: Declaration ] && markup_entity. hydrate ( self . source_ptr ) {
753
769
self . event_handler . handle_event ( Event :: Cdata , Entity :: Text ( & markup_entity) ) ;
770
+ self . dispatched . push ( Dispatched :: Text ( markup_entity) ) ;
754
771
}
755
- self . markup_entity = None ;
756
772
// if we have a markup_decl, we previously
757
773
// were processing a doctype and encountered
758
774
// entities and now need to complete the doctype
@@ -818,7 +834,7 @@ impl<'a> SAXParser<'a> {
818
834
819
835
fn process_proc_inst ( & mut self , gc : & mut GraphemeClusters ) {
820
836
self . state = State :: BeginWhitespace ;
821
- let proc_inst = & mut self . proc_inst . take ( ) . unwrap ( ) ;
837
+ let mut proc_inst = Box :: new ( self . proc_inst . take ( ) . unwrap ( ) ) ;
822
838
823
839
if self . events [ Event :: ProcessingInstruction ] && proc_inst. hydrate ( self . source_ptr ) {
824
840
proc_inst. end = [ gc. line , gc. character ] ;
@@ -827,6 +843,7 @@ impl<'a> SAXParser<'a> {
827
843
proc_inst. target . value . drain ( ..2 ) ; // remove '<?'
828
844
proc_inst. content . value . truncate ( proc_inst. content . value . len ( ) . saturating_sub ( 2 ) ) ; // remove '?>'
829
845
self . event_handler . handle_event ( Event :: ProcessingInstruction , Entity :: ProcInst ( & proc_inst) ) ;
846
+ self . dispatched . push ( Dispatched :: ProcInst ( proc_inst) ) ;
830
847
}
831
848
}
832
849
@@ -970,7 +987,9 @@ impl<'a> SAXParser<'a> {
970
987
fn process_attribute ( & mut self ) {
971
988
let mut attr = mem:: replace ( & mut self . attribute , Attribute :: new ( ) ) ;
972
989
if self . events [ Event :: Attribute ] && attr. hydrate ( self . source_ptr ) {
973
- self . event_handler . handle_event ( Event :: Attribute , Entity :: Attribute ( & attr) ) ;
990
+ let attr_box = Box :: new ( attr. clone ( ) ) ;
991
+ self . event_handler . handle_event ( Event :: Attribute , Entity :: Attribute ( & attr_box) ) ;
992
+ self . dispatched . push ( Dispatched :: Attribute ( attr_box) ) ;
974
993
}
975
994
// Store them only if we're interested in Open and Close tag events
976
995
if self . events [ Event :: OpenTag ] || self . events [ Event :: CloseTag ] {
@@ -985,17 +1004,11 @@ impl<'a> SAXParser<'a> {
985
1004
986
1005
if self . events [ Event :: OpenTag ] {
987
1006
tag. hydrate ( self . source_ptr ) ;
988
- self . event_handler . handle_event ( Event :: OpenTag , Entity :: Tag ( & tag) ) ;
989
- }
990
-
991
- if self_closing && self . events [ Event :: CloseTag ] {
992
- tag. hydrate ( self . source_ptr ) ;
993
- self . event_handler . handle_event ( Event :: CloseTag , Entity :: Tag ( & tag) ) ;
994
- }
995
-
996
- if !self_closing {
997
- self . tags . push ( tag) ;
1007
+ let tag_box = Box :: new ( tag. clone ( ) ) ;
1008
+ self . event_handler . handle_event ( Event :: OpenTag , Entity :: Tag ( & tag_box) ) ;
1009
+ self . dispatched . push ( Dispatched :: Tag ( tag_box) ) ;
998
1010
}
1011
+ self . tags . push ( tag) ;
999
1012
1000
1013
self . state = State :: BeginWhitespace ;
1001
1014
}
@@ -1037,10 +1050,13 @@ impl<'a> SAXParser<'a> {
1037
1050
return ;
1038
1051
}
1039
1052
1040
- for tag in self . tags . drain ( tag_index..) . rev ( ) {
1041
- let mut tag = tag; // Create a mutable binding
1053
+ let mut i = self . tags . len ( ) ;
1054
+ while i > tag_index {
1055
+ let mut tag = Box :: new ( unsafe { self . tags . pop ( ) . unwrap_unchecked ( ) } ) ;
1042
1056
tag. hydrate ( self . source_ptr ) ;
1043
1057
self . event_handler . handle_event ( Event :: CloseTag , Entity :: Tag ( & tag) ) ;
1058
+ self . dispatched . push ( Dispatched :: Tag ( tag) ) ;
1059
+ i -= 1 ;
1044
1060
}
1045
1061
}
1046
1062
0 commit comments