use generic_btree::{rle::HasLength, rle::Sliceable as _, Cursor}; use loro_common::{ContainerID, InternalString, LoroError, LoroResult, LoroValue, ID}; use loro_delta::{DeltaRope, DeltaRopeBuilder}; use rustc_hash::{FxHashMap, FxHashSet}; use smallvec::SmallVec; use std::ops::Range; use std::sync::{Arc, Weak}; use crate::{ container::{ idx::ContainerIdx, list::list_op, richtext::{ config::StyleConfigMap, richtext_state::{DrainInfo, EntityRanges, IterRangeItem, PosType, RichtextStateChunk}, AnchorType, RichtextState as InnerState, StyleKey, StyleOp, Styles, }, }, delta::{StyleMeta, StyleMetaItem}, event::{Diff, Index, InternalDiff, TextDiff}, handler::TextDelta, op::{Op, RawOp}, sync::RwLock, utils::{lazy::LazyLoad, string_slice::StringSlice}, LoroDocInner, }; use super::{ApplyLocalOpReturn, ContainerState, DiffApplyContext}; #[derive(Debug)] pub struct RichtextState { idx: ContainerIdx, config: Arc>, state: LazyLoad, /// This is used to indicate whether the richtext state is changed, so the downstream has an easy way to cache /// NOTE: We need to ensure the invariance that the version id is always increased when the richtext state is changed version_id: usize, } struct Pos { entity_index: usize, event_index: usize, } impl RichtextState { #[inline] pub fn new(idx: ContainerIdx, config: Arc>) -> Self { Self { idx, config, state: LazyLoad::Src(Default::default()), version_id: 0, } } #[inline] fn update_version(&mut self) { self.version_id = self.version_id.wrapping_add(1); } /// Get the version id of the richtext /// /// This can be used to detect whether the richtext is changed #[inline] pub fn get_version_id(&self) -> usize { self.version_id } /// Get the text content of the richtext /// /// This uses `mut` because we may need to build the state from snapshot #[inline] pub fn to_string_mut(&mut self) -> String { self.state.get_mut().to_string() } #[allow(unused)] #[inline(always)] pub(crate) fn is_empty(&self) -> bool { match &self.state { LazyLoad::Src(s) => s.elements.is_empty(), LazyLoad::Dst(d) => d.is_empty(), } } pub(crate) fn diagnose(&self) { match &self.state { LazyLoad::Src(_) => {} LazyLoad::Dst(d) => d.diagnose(), } } #[allow(unused)] pub(crate) fn get_text_slice_by_event_index( &mut self, pos: usize, len: usize, ) -> LoroResult { self.state.get_mut().get_text_slice_by_event_index(pos, len) } pub(crate) fn slice_delta( &mut self, start_index: usize, end_index: usize, pos_type: PosType, ) -> LoroResult> { self.state .get_mut() .slice_delta(start_index, end_index, pos_type) } #[allow(unused)] pub(crate) fn get_char_by_event_index(&mut self, pos: usize) -> Result { self.state.get_mut().get_char_by_event_index(pos) } pub(crate) fn iter(&mut self, mut callback: impl FnMut(&str) -> bool) { for span in self.state.get_mut().iter_chunk() { match span { RichtextStateChunk::Text(text_chunk) => { if !callback(text_chunk.as_str()) { return; } } RichtextStateChunk::Style { .. } => {} } } } pub(crate) fn iter_raw(&self, callback: &mut dyn FnMut(&RichtextStateChunk)) { let iter: &mut dyn Iterator; let mut a; let mut b; match &self.state { LazyLoad::Src(s) => { a = Some(s.elements.iter()); iter = &mut *a.as_mut().unwrap(); } LazyLoad::Dst(s) => { b = Some(s.iter_chunk()); iter = &mut *b.as_mut().unwrap(); } } for c in iter { callback(c); } } fn get_style_start( &mut self, style_starts: &mut FxHashMap, Pos>, style: &Arc, ) -> Pos { self.update_version(); match style_starts.remove(style) { Some(x) => x, None => { // this should happen rarely, so it should be fine to scan let mut pos = Pos { entity_index: 0, event_index: 0, }; for c in self.state.get_mut().iter_chunk() { match c { RichtextStateChunk::Style { style: s, anchor_type: AnchorType::Start, } if style == s => { break; } RichtextStateChunk::Text(t) => { pos.entity_index += t.unicode_len() as usize; pos.event_index += t.event_len() as usize; } RichtextStateChunk::Style { .. } => { pos.entity_index += 1; } } } pos } } } pub fn get_index_of_id(&self, id: ID) -> Option { let iter: &mut dyn Iterator; let mut a; let mut b; match &self.state { LazyLoad::Src(s) => { a = Some(s.elements.iter()); iter = &mut *a.as_mut().unwrap(); } LazyLoad::Dst(s) => { b = Some(s.iter_chunk()); iter = &mut *b.as_mut().unwrap(); } } let mut index = 0; for elem in iter { let span = elem.get_id_span(); if span.contains(id) { return Some(index + (id.counter - span.counter.start) as usize); } index += elem.rle_len(); } None } pub fn get_text_index_of_id(&self, id: ID, use_event_index: bool) -> Option { let iter: &mut dyn Iterator; let mut a; let mut b; match &self.state { LazyLoad::Src(s) => { a = Some(s.elements.iter()); iter = &mut *a.as_mut().unwrap(); } LazyLoad::Dst(s) => { b = Some(s.iter_chunk()); iter = &mut *b.as_mut().unwrap(); } } let mut index = 0; for elem in iter { let span = elem.get_id_span(); if span.contains(id) { match elem { RichtextStateChunk::Text(t) => { if use_event_index { let event_offset = t.convert_unicode_offset_to_event_offset( (id.counter - span.counter.start) as usize, ); return Some(index + event_offset); } else { return Some(index + (id.counter - span.counter.start) as usize); } } RichtextStateChunk::Style { .. } => { return Some(index); } } } index += match elem { RichtextStateChunk::Text(t) => { if use_event_index { t.event_len() as usize } else { t.unicode_len() as usize } } RichtextStateChunk::Style { .. } => 0, }; } None } pub(crate) fn get_delta(&mut self) -> Vec { let mut delta = Vec::new(); for span in self.state.get_mut().iter() { let next_attr = span.attributes.to_option_map(); match delta.last_mut() { Some(TextDelta::Insert { insert, attributes }) if &next_attr == attributes => { insert.push_str(span.text.as_str()); continue; } _ => {} } delta.push(TextDelta::Insert { insert: span.text.as_str().to_string(), attributes: next_attr, }) } delta } } #[cfg(test)] mod tests { use crate::{container::richtext::StyleKey, cursor::PosType, handler::HandlerTrait, LoroDoc}; #[test] fn has_style_key_in_entity_range_basic() { let loro = LoroDoc::new_auto_commit(); let text = loro.get_text("text"); text.insert(0, "abcdef", PosType::Unicode).unwrap(); text.mark(1, 3, "bold", true.into(), PosType::Unicode) .unwrap(); let bold_key = StyleKey::Key("bold".into()); let has_style = text .with_state(|state| { let st = state.as_richtext_state_mut().unwrap(); let (entity_range, _) = st.get_entity_range_and_styles_at_range(1..3, PosType::Unicode); Ok(st.has_style_key_in_entity_range(entity_range, &bold_key)) }) .unwrap(); assert!(has_style); let missing = text .with_state(|state| { let st = state.as_richtext_state_mut().unwrap(); let (entity_range, _) = st.get_entity_range_and_styles_at_range(4..5, PosType::Unicode); Ok(st.has_style_key_in_entity_range(entity_range, &bold_key)) }) .unwrap(); assert!(!missing); } #[test] fn has_style_key_in_entity_range_spans_elements() { let loro = LoroDoc::new_auto_commit(); let text = loro.get_text("text"); text.insert(0, "abcdefgh", PosType::Unicode).unwrap(); text.mark(0, 2, "bold", true.into(), PosType::Unicode) .unwrap(); text.mark(3, 5, "bold", true.into(), PosType::Unicode) .unwrap(); let bold_key = StyleKey::Key("bold".into()); let has_style_across_segments = text .with_state(|state| { let st = state.as_richtext_state_mut().unwrap(); let (entity_range, _) = st.get_entity_range_and_styles_at_range(0..5, PosType::Unicode); Ok(st.has_style_key_in_entity_range(entity_range, &bold_key)) }) .unwrap(); assert!(has_style_across_segments); let gap_has_style = text .with_state(|state| { let st = state.as_richtext_state_mut().unwrap(); let (entity_range, _) = st.get_entity_range_and_styles_at_range(6..7, PosType::Unicode); Ok(st.has_style_key_in_entity_range(entity_range, &bold_key)) }) .unwrap(); assert!(!gap_has_style); } } impl Clone for RichtextState { fn clone(&self) -> Self { Self { idx: self.idx, config: self.config.clone(), state: self.state.clone(), version_id: self.version_id, } } } impl ContainerState for RichtextState { fn container_idx(&self) -> ContainerIdx { self.idx } fn is_state_empty(&self) -> bool { match &self.state { LazyLoad::Src(s) => s.is_empty(), LazyLoad::Dst(s) => s.is_empty(), } } // TODO: refactor fn apply_diff_and_convert(&mut self, diff: InternalDiff, _ctx: DiffApplyContext) -> Diff { self.update_version(); let InternalDiff::RichtextRaw(richtext) = diff else { unreachable!() }; // tracing::info!("Self state = {:#?}", &self); // PERF: compose delta let mut ans: TextDiff = TextDiff::new(); let mut style_delta: TextDiff = TextDiff::new(); let mut style_starts: FxHashMap, Pos> = FxHashMap::default(); let mut entity_index = 0; let mut event_index = 0; let mut new_style_deltas: Vec = Vec::new(); for span in richtext.iter() { match span { loro_delta::DeltaItem::Retain { len, .. } => { entity_index += len; } loro_delta::DeltaItem::Replace { value, delete, .. } => { if *delete > 0 { // Deletions let mut deleted_style_keys: FxHashSet = FxHashSet::default(); let DrainInfo { start_event_index: start, end_event_index: end, affected_style_range, } = self.state.get_mut().drain_by_entity_index( entity_index, *delete, Some(&mut |c| match c { RichtextStateChunk::Style { style, anchor_type: AnchorType::Start, } => { deleted_style_keys.insert(style.key.clone()); } RichtextStateChunk::Style { style, anchor_type: AnchorType::End, } => { deleted_style_keys.insert(style.key.clone()); } _ => {} }), ); if start > event_index { ans.push_retain(start - event_index, Default::default()); event_index = start; } if let Some((entity_range, event_range)) = affected_style_range { let mut delta: TextDiff = DeltaRopeBuilder::new() .retain(event_range.start, Default::default()) .build(); let mut entity_len_sum = 0; let expected_sum = entity_range.len(); for IterRangeItem { event_len, chunk, styles, entity_len, .. } in self.state.get_mut().iter_range(entity_range) { entity_len_sum += entity_len; match chunk { RichtextStateChunk::Text(_) => { let mut style_meta: StyleMeta = styles.into(); for key in deleted_style_keys.iter() { if !style_meta.contains_key(key) { style_meta.insert( key.clone(), StyleMetaItem { lamport: 0, peer: 0, value: LoroValue::Null, }, ) } } delta.push_retain( event_len, style_meta.to_option_map().unwrap_or_default().into(), ); } RichtextStateChunk::Style { .. } => {} } } debug_assert_eq!(entity_len_sum, expected_sum); delta.chop(); style_delta.compose(&delta); } ans.push_delete(end - start); } if value.rle_len() > 0 { // Insertions match value { RichtextStateChunk::Text(s) => { let (pos, styles) = self.state.get_mut().insert_elem_at_entity_index( entity_index, RichtextStateChunk::Text(s.clone()), ); // PERF: this can be optimized let insert_styles = Into::::into(styles.clone()) .to_option_map() .unwrap_or_default(); if pos > event_index { ans.push_retain(pos - event_index, Default::default()); } event_index = pos + s.event_len() as usize; ans.push_insert( StringSlice::from(s.bytes().clone()), insert_styles.into(), ); } RichtextStateChunk::Style { anchor_type, style } => { let (new_event_index, _) = self.state.get_mut().insert_elem_at_entity_index( entity_index, RichtextStateChunk::Style { style: style.clone(), anchor_type: *anchor_type, }, ); if new_event_index > event_index { ans.push_retain( new_event_index - event_index, Default::default(), ); // inserting style anchor will not affect event_index's positions event_index = new_event_index; } match anchor_type { AnchorType::Start => { style_starts.insert( style.clone(), Pos { entity_index, event_index: new_event_index, }, ); } AnchorType::End => { // get the pair of style anchor. now we can annotate the range let Pos { entity_index: start_entity_index, event_index: start_event_index, } = self.get_style_start(&mut style_starts, style); let mut delta: TextDiff = DeltaRopeBuilder::new() .retain(start_event_index, Default::default()) .build(); // we need to + 1 because we also need to annotate the end anchor let event = self.state.get_mut().annotate_style_range_with_event( start_entity_index..entity_index + 1, style.clone(), ); for (s, l) in event { delta.push_retain( l, s.to_option_map().unwrap_or_default().into(), ); } delta.chop(); new_style_deltas.push(delta); } } } } entity_index += value.rle_len(); } } } } for s in new_style_deltas { style_delta.compose(&s); } // self.check_consistency_between_content_and_style_ranges(); ans.compose(&style_delta); Diff::Text(ans) } fn validate_diff(&self, diff: &InternalDiff) -> LoroResult<()> { let InternalDiff::RichtextRaw(delta) = diff else { unreachable!() }; // Text items are addressed by entity index (style anchors included). let mut cursor = 0usize; let mut projected = match &self.state { LazyLoad::Src(s) => s.entity_index, LazyLoad::Dst(s) => s.len_entity(), }; for span in delta.iter() { match span { loro_delta::DeltaItem::Retain { len, .. } => { cursor += len; if cursor > projected { return Err(LoroError::DecodeError( format!( "text diff retains {cursor} items but state only has {projected}" ) .into_boxed_str(), )); } } loro_delta::DeltaItem::Replace { value, delete, .. } => { if cursor + delete > projected { return Err(LoroError::DecodeError( format!("text diff deletes {delete} at {cursor} but state only has {projected}") .into_boxed_str(), )); } projected = projected - delete + value.rle_len(); cursor += value.rle_len(); } } } Ok(()) } fn apply_diff(&mut self, diff: InternalDiff, _ctx: DiffApplyContext) -> LoroResult<()> { self.update_version(); let InternalDiff::RichtextRaw(richtext) = diff else { unreachable!() }; if let LazyLoad::Src(loader) = &mut self.state { if loader.try_apply_append_delta(&richtext) { return Ok(()); } } // Fast path for plain-text deltas (no style anchors / style ranges). // // Rebuilding avoids repeated BTree queries and mutations when the delta is very "choppy" // (many small edit spans), but it allocates and clones chunks, so it can be slower for // small deltas. Use a cheap cost model to enable it only when it's likely beneficial. let should_fast_apply = { #[inline] fn ilog2_ceil(x: usize) -> usize { debug_assert!(x > 0); (usize::BITS - (x - 1).leading_zeros()) as usize } let state = self.state.get_mut(); if state.has_styles() { false } else { // `edit_actions` approximates how many BTree mutations the incremental path will do: // each Replace with delete>0 becomes a drain, and each Replace with value>0 becomes an insert. let mut edit_actions: usize = 0; let mut is_plain_text_delta = true; for span in richtext.iter() { match span { loro_delta::DeltaItem::Retain { .. } => {} loro_delta::DeltaItem::Replace { value, delete, .. } => { if *delete > 0 { edit_actions += 1; } if value.rle_len() > 0 { if !matches!(value, RichtextStateChunk::Text(_)) { is_plain_text_delta = false; break; } edit_actions += 1; } } } } if !is_plain_text_delta || edit_actions == 0 { false } else { let content_nodes = state.content_node_len().max(1); let log_n = ilog2_ceil(content_nodes + 1).max(1); let incremental_score = edit_actions.saturating_mul(log_n); let rebuild_score = content_nodes.saturating_add(edit_actions); let old_len = richtext.old_len().max(1); let avg_action_span = old_len / edit_actions; // A very rough proxy for "choppiness": many edit actions with small average span. // The thresholds are intentionally conservative to avoid rebuilding for small or // localized deltas. let is_choppy = edit_actions >= 256 && avg_action_span <= 32; is_choppy && incremental_score >= rebuild_score.saturating_mul(4) } } }; if should_fast_apply { let new_state = { let state = self.state.get_mut(); let mut chunks: Vec = Vec::new(); let mut src_iter = state.iter_chunk(); let mut cur = src_iter.next(); let mut cur_offset: usize = 0; for span in richtext.iter() { match span { loro_delta::DeltaItem::Retain { len, .. } => { let mut left = *len; while left > 0 { let chunk = cur.expect("Delta retain exceeds source length"); let chunk_len = chunk.rle_len(); if chunk_len == 0 { cur = src_iter.next(); cur_offset = 0; continue; } let available = chunk_len - cur_offset; let take = left.min(available); if take == chunk_len && cur_offset == 0 { chunks.push(chunk.clone()); } else { chunks.push(chunk.slice(cur_offset..cur_offset + take)); } left -= take; cur_offset += take; if cur_offset == chunk_len { cur = src_iter.next(); cur_offset = 0; } } } loro_delta::DeltaItem::Replace { value, delete, .. } => { let mut left = *delete; while left > 0 { let chunk = cur.expect("Delta delete exceeds source length"); let chunk_len = chunk.rle_len(); if chunk_len == 0 { cur = src_iter.next(); cur_offset = 0; continue; } let available = chunk_len - cur_offset; let take = left.min(available); left -= take; cur_offset += take; if cur_offset == chunk_len { cur = src_iter.next(); cur_offset = 0; } } if value.rle_len() > 0 { chunks.push(value.clone()); } } } } if let Some(chunk) = cur { let chunk_len = chunk.rle_len(); if cur_offset < chunk_len { if cur_offset == 0 { chunks.push(chunk.clone()); } else { chunks.push(chunk.slice(cur_offset..chunk_len)); } } } for chunk in src_iter { chunks.push(chunk.clone()); } InnerState::from_chunks(chunks.into_iter()) }; *self.state.get_mut() = new_state; return Ok(()); } let mut style_starts: FxHashMap, usize> = FxHashMap::default(); let mut entity_index = 0; for span in richtext.iter() { match span { loro_delta::DeltaItem::Retain { len, .. } => { entity_index += len; } loro_delta::DeltaItem::Replace { value, delete, .. } => { if *delete > 0 { // Deletions self.state .get_mut() .drain_by_entity_index(entity_index, *delete, None); } if value.rle_len() > 0 { // Insertions match value { RichtextStateChunk::Text(s) => { self.state.get_mut().insert_elem_at_entity_index( entity_index, RichtextStateChunk::Text(s.clone()), ); } RichtextStateChunk::Style { style, anchor_type } => { self.state.get_mut().insert_elem_at_entity_index( entity_index, RichtextStateChunk::Style { style: style.clone(), anchor_type: *anchor_type, }, ); if *anchor_type == AnchorType::Start { style_starts.insert(style.clone(), entity_index); } else { let start_pos = match style_starts.get(style) { Some(x) => *x, None => { // This should be rare, so it should be fine to scan let mut start_entity_index = 0; for c in self.state.get_mut().iter_chunk() { match c { RichtextStateChunk::Style { style: s, anchor_type: AnchorType::Start, } if style == s => { break; } RichtextStateChunk::Text(t) => { start_entity_index += t.unicode_len() as usize; } RichtextStateChunk::Style { .. } => { start_entity_index += 1; } } } start_entity_index } }; // we need to + 1 because we also need to annotate the end anchor self.state.get_mut().annotate_style_range( start_pos..entity_index + 1, style.clone(), ); } } } entity_index += value.rle_len(); } } } } // self.check_consistency_between_content_and_style_ranges() Ok(()) } fn apply_local_op(&mut self, r_op: &RawOp, op: &Op) -> LoroResult { self.update_version(); match &op.content { crate::op::InnerContent::List(l) => match l { list_op::InnerListOp::Insert { slice: _, pos: _ } => { unreachable!() } list_op::InnerListOp::InsertText { slice, unicode_len: _, pos, } => { self.state.get_mut().insert_at_entity_index( *pos as usize, slice.clone(), r_op.id_full(), ); } list_op::InnerListOp::Delete(del) => { self.state.get_mut().drain_by_entity_index( del.start() as usize, rle::HasLength::atom_len(&del), None, ); } list_op::InnerListOp::StyleStart { start, end, key, value, info, } => { // Behavior here is a little different from apply_diff. // // When apply_diff, we only do the mark when we have included both // StyleStart and StyleEnd. // // When applying local op, we can do the mark when we have StyleStart. // We can assume StyleStart and StyleEnd are always appear in a pair // for apply_local_op. (Because for local behavior, when we mark, // we always create a pair of style ops.) self.state.get_mut().mark_with_entity_index( *start as usize..*end as usize, Arc::new(StyleOp { lamport: r_op.lamport, peer: r_op.id.peer, cnt: r_op.id.counter, key: key.clone(), value: value.clone(), info: *info, }), ); } list_op::InnerListOp::Set { .. } => {} list_op::InnerListOp::StyleEnd => {} list_op::InnerListOp::Move { .. } => unreachable!(), }, _ => unreachable!(), } // self.check_consistency_between_content_and_style_ranges(); Ok(Default::default()) } fn to_diff(&mut self, _doc: &Weak) -> Diff { // Coalesce adjacent spans first: each `push_insert` re-measures the merged // slice, so pushing many small contiguous spans is quadratic. (project_note patch) use generic_btree::rle::Mergeable; let mut delta = TextDiff::new(); let mut pending: Option<(crate::utils::string_slice::StringSlice, crate::event::TextMeta)> = None; for span in self.state.get_mut().iter() { let meta: crate::event::TextMeta = span.attributes.to_option_map().unwrap_or_default().into(); if let Some((text, m)) = &mut pending { if *m == meta && text.can_merge(&span.text) { text.merge_right(&span.text); continue; } } if let Some((text, m)) = pending.replace((span.text, meta)) { delta.push_insert(text, m); } } if let Some((text, m)) = pending { delta.push_insert(text, m); } Diff::Text(delta) } // value is a list fn get_value(&mut self) -> LoroValue { match &self.state { LazyLoad::Src(loader) => loader.to_plain_string().into(), LazyLoad::Dst(_) => self.state.get_mut().to_string().into(), } } #[doc = r" Get the index of the child container"] #[allow(unused)] fn get_child_index(&self, id: &ContainerID) -> Option { None } #[allow(unused)] fn get_child_containers(&self) -> Vec { Vec::new() } fn contains_child(&self, _id: &ContainerID) -> bool { false } fn fork(&self, config: &crate::configure::Configure) -> Self { Self { idx: self.idx, config: config.text_style_config.clone(), state: self.state.clone(), version_id: 0, } } } impl RichtextState { #[inline(always)] pub fn len_utf8(&mut self) -> usize { self.state.get_mut().len_utf8() } #[inline(always)] pub fn len(&mut self, pos_type: PosType) -> usize { self.state.get_mut().len(pos_type) } #[inline(always)] pub fn len_utf16(&mut self) -> usize { self.state.get_mut().len_utf16() } #[inline(always)] pub fn len_entity(&self) -> usize { match &self.state { LazyLoad::Src(s) => s.entity_index, LazyLoad::Dst(d) => d.len_entity(), } } pub fn len_event(&mut self) -> usize { if cfg!(feature = "wasm") { self.len_utf16() } else { self.len_unicode() } } #[inline] #[allow(dead_code)] pub(crate) fn has_styles(&mut self) -> bool { self.state.get_mut().has_styles() } pub(crate) fn has_style_key_in_entity_range( &mut self, range: Range, key: &StyleKey, ) -> bool { self.state.get_mut().range_has_style_key(range, key) } pub(crate) fn has_style_key_value_in_entity_range( &mut self, range: Range, key: &str, value: &LoroValue, ) -> bool { self.state .get_mut() .range_has_style_key_value(range, key, value) } /// Check if the content and style ranges are consistent. /// /// Panic if inconsistent. #[allow(unused)] pub(crate) fn check_consistency_between_content_and_style_ranges(&mut self) { if !cfg!(debug_assertions) { return; } self.state .get_mut() .check_consistency_between_content_and_style_ranges(); } #[inline] pub fn len_unicode(&mut self) -> usize { self.state.get_mut().len_unicode() } #[cfg(test)] pub(crate) fn inner_state_mut(&mut self) -> &mut InnerState { self.state.get_mut() } #[inline] pub(crate) fn get_entity_index_for_text_insert( &mut self, index: usize, pos_type: PosType, ) -> Result<(usize, Option), LoroError> { self.state .get_mut() .get_entity_index_for_text_insert(index, pos_type) } #[inline] pub(crate) fn get_event_index_by_cursor(&mut self, cursor: Cursor) -> usize { self.state.get_mut().cursor_to_event_index_cached(cursor) } pub(crate) fn get_entity_range_and_styles_at_range( &mut self, range: Range, pos_type: PosType, ) -> (Range, Option<&Styles>) { self.state .get_mut() .get_entity_range_and_text_styles_at_range(range, pos_type) } #[inline] pub(crate) fn get_styles_at_entity_index(&mut self, entity_index: usize) -> StyleMeta { self.state .get_mut() .get_styles_at_entity_index_for_insert(entity_index) } #[inline] pub(crate) fn get_text_entity_ranges_in_event_index_range( &mut self, pos: usize, len: usize, ) -> LoroResult { self.state .get_mut() .get_text_entity_ranges(pos, len, PosType::Event) } #[inline] pub fn get_richtext_value(&mut self) -> LoroValue { self.state.get_mut().get_richtext_value() } #[inline] pub(crate) fn get_stable_position( &mut self, event_index: usize, get_by_event_index: bool, ) -> Option { self.state.get_mut().get_stable_position_at_event_index( event_index, if get_by_event_index { PosType::Event } else { PosType::Unicode }, ) } pub(crate) fn entity_index_to_event_index(&mut self, entity_index: usize) -> usize { self.state .get_mut() .entity_index_to_event_index(entity_index) } pub(crate) fn index_to_event_index(&mut self, index: usize, pos_type: PosType) -> usize { self.state.get_mut().index_to_event_index(index, pos_type) } pub(crate) fn event_index_to_unicode_index(&mut self, event_index: usize) -> usize { self.state .get_mut() .event_index_to_unicode_index(event_index) } pub(crate) fn event_index_to_index(&mut self, event_index: usize, pos_type: PosType) -> usize { self.state .get_mut() .event_index_to_index(event_index, pos_type) } } #[derive(Debug, Default, Clone)] pub(crate) struct RichtextStateLoader { start_anchor_pos: FxHashMap, elements: SmallVec<[RichtextStateChunk; 1]>, style_ranges: Vec<(Arc, Range)>, entity_index: usize, } impl From for InnerState { fn from(value: RichtextStateLoader) -> Self { value.into_state() } } impl RichtextStateLoader { pub fn push(&mut self, elem: RichtextStateChunk) { if let RichtextStateChunk::Style { style, anchor_type } = &elem { if *anchor_type == AnchorType::Start { self.start_anchor_pos .insert(ID::new(style.peer, style.cnt), self.entity_index); } else { let start_pos = self .start_anchor_pos .remove(&ID::new(style.peer, style.cnt)) .expect("Style start not found"); // we need to + 1 because we also need to annotate the end anchor self.style_ranges .push((style.clone(), start_pos..self.entity_index + 1)); } } self.entity_index += elem.rle_len(); if matches!(&elem, RichtextStateChunk::Text(t) if t.bytes().len() > 2048) { self.elements.extend(elem.into_pieces()); } else { self.elements.push(elem); } } pub fn into_state(self) -> InnerState { let mut state = InnerState::from_chunks(self.elements.into_iter()); for (style, range) in self.style_ranges { state.annotate_style_range(range, style); } if cfg!(debug_assertions) { state.check(); } state } fn is_empty(&self) -> bool { self.elements.is_empty() } fn to_plain_string(&self) -> String { let len = self .elements .iter() .map(|elem| match elem { RichtextStateChunk::Text(text) => text.bytes().len(), RichtextStateChunk::Style { .. } => 0, }) .sum(); let mut text = String::with_capacity(len); for elem in &self.elements { if let RichtextStateChunk::Text(chunk) = elem { text.push_str(chunk.as_str()); } } text } fn try_apply_append_delta(&mut self, delta: &DeltaRope) -> bool { // Style anchors/ranges need InnerState's range maintenance. if !self.start_anchor_pos.is_empty() || !self.style_ranges.is_empty() { return false; } let old_len = self.entity_index; let mut current_len = self.entity_index; let mut entity_index = 0; let mut appended = Vec::new(); for span in delta.iter() { match span { loro_delta::DeltaItem::Retain { len, .. } => { entity_index += len; if entity_index > old_len { return false; } } loro_delta::DeltaItem::Replace { value, delete, .. } => { if *delete > 0 { return false; } let insert_len = value.rle_len(); if insert_len == 0 { continue; } if !matches!(value, RichtextStateChunk::Text(_)) { return false; } if entity_index != current_len { return false; } appended.push(value.clone()); entity_index += insert_len; current_len += insert_len; } } } if appended.is_empty() { return false; } for elem in appended { self.push(elem); } true } } pub(crate) use snapshot::redact_dead_style_values; mod snapshot { use loro_common::{IdFull, InternalString, LoroResult, LoroValue, PeerID, ID}; use rustc_hash::{FxHashMap, FxHashSet}; use serde_columnar::columnar; use std::sync::Arc; use crate::{ container::richtext::{ self, richtext_state::RichtextStateChunk, str_slice::StrSlice, StyleOp, TextStyleInfoFlag, }, encoding::value_register::ValueRegister, state::{ decode_lamport_from_delta, decode_peer_from_table, decode_peer_table, state_decode_error, ContainerCreationContext, ContainerState, FastStateSnapshot, }, utils::lazy::LazyLoad, }; use super::{RichtextState, RichtextStateLoader}; #[columnar(vec, ser, de, iterable)] #[derive(Debug, Clone)] struct EncodedTextSpan { #[columnar(strategy = "DeltaRle")] peer_idx: usize, #[columnar(strategy = "DeltaRle")] counter: i32, #[columnar(strategy = "DeltaRle")] lamport_sub_counter: i32, /// positive for text /// 0 for mark start /// -1 for mark end #[columnar(strategy = "DeltaRle")] len: i32, } #[columnar(vec, ser, de, iterable)] #[derive(Debug, Clone)] struct EncodedMark { key_idx: usize, value: LoroValue, info: u8, } #[columnar(ser, de)] struct EncodedText { #[columnar(class = "vec", iter = "EncodedTextSpan")] spans: Vec, keys: Vec, marks: Vec, } impl EncodedText { // The columnar derives encode the outer struct and each mark as // three-element postcard sequences. Only the nested value field needs // a dedicated codec; spans still use the existing columnar strategies. fn decode(mut bytes: &[u8]) -> LoroResult { fn take<'a, T: serde::Deserialize<'a>>(bytes: &mut &'a [u8]) -> LoroResult { let (value, rest) = postcard::take_from_bytes(bytes).map_err(|err| { state_decode_error(format!("Decode richtext fields failed: {err}")) })?; *bytes = rest; Ok(value) } if take::(&mut bytes)? != 3 { return Err(state_decode_error( "Decode richtext fields failed: invalid field count", )); } let spans: serde_columnar::ColumnarVec<'_, EncodedTextSpan, Vec> = take(&mut bytes)?; let keys = take(&mut bytes)?; let count = take::(&mut bytes)?; let mut marks = Vec::new(); for _ in 0..count { if take::(&mut bytes)? != 3 { return Err(state_decode_error( "Decode richtext mark failed: invalid field count", )); } let key_idx = take(&mut bytes)?; let (value, rest) = crate::state::value_decode::decode_value(bytes)?; bytes = rest; let info = take(&mut bytes)?; marks.push(EncodedMark { key_idx, value, info, }); } if !bytes.is_empty() { return Err(state_decode_error( "Decode richtext fields failed: trailing bytes", )); } Ok(Self { spans: spans.into_vec(), keys, marks, }) } fn encode(&self) -> Vec { let mut bytes = Vec::new(); postcard::to_io(&3usize, &mut bytes).unwrap(); postcard::to_io(&serde_columnar::ColumnarVec::new(&self.spans), &mut bytes).unwrap(); postcard::to_io(&self.keys, &mut bytes).unwrap(); postcard::to_io(&self.marks.len(), &mut bytes).unwrap(); for mark in &self.marks { postcard::to_io(&3usize, &mut bytes).unwrap(); postcard::to_io(&mark.key_idx, &mut bytes).unwrap(); crate::state::value_decode::encode_value(&mark.value, &mut bytes).unwrap(); postcard::to_io(&mark.info, &mut bytes).unwrap(); } bytes } } /// Nulls out the values of style-anchor pairs that no longer enclose any text /// ("dead" pairs) in an encoded richtext state payload (the bytes produced by /// `RichtextState::encode_snapshot_fast`). /// /// Shallow-snapshot export uses this: history before the shallow root is /// trimmed, so a dead pair's value would otherwise ship in the export even /// though no read API can reach it. See /// `context/shallow-snapshot-style-redaction.md` for the invariants: /// /// - Anchors themselves must be preserved: op positions are entity indexes /// that count each anchor, so removing them would corrupt replay. /// - Pairs whose style expands on both sides are kept intact: an empty /// both-expand pair still captures future inserts, so its value is live. /// - When `only_pairs` is `Some`, only pairs whose `StyleStart` id is in the /// set are touched (used to keep an exported latest state consistent with /// the redacted shallow-root state). /// /// Returns `None` when nothing changed, otherwise the rewritten payload and /// the `StyleStart` ids of the redacted pairs. pub(crate) fn redact_dead_style_values( payload: &[u8], only_pairs: Option<&FxHashSet>, ) -> LoroResult, Vec)>> { const CTX: &str = "Redact dead style values failed"; // Borrow the text prefix without copying it; only the columnar tail is // ever rewritten. let (_text, mut rest) = postcard::take_from_bytes::<&str>(payload) .map_err(|_| state_decode_error(format!("{CTX}: invalid text value")))?; let peers = decode_peer_table(&mut rest, CTX)?; let prefix_len = payload.len() - rest.len(); let encoded = EncodedText::decode(rest) .map_err(|err| state_decode_error(format!("{CTX}: invalid spans: {err}")))?; if encoded.marks.is_empty() { return Ok(None); } let EncodedText { spans, keys, mut marks, } = encoded; // Open pairs keyed by the id the matching `StyleEnd` span will carry // (`encode_snapshot_fast` writes the end span with start.counter + 1). // The value records the pair's mark index, its start counter, and how // many text spans had been seen when it opened: if that count is // unchanged when the pair closes, no text lies between the anchors. let mut open: FxHashMap<(usize, i32), (usize, i32, usize)> = FxHashMap::default(); let mut start_count = 0usize; let mut text_spans_seen = 0usize; let mut redacted = Vec::new(); for span in &spans { match span.len { 0 => { let mark_idx = start_count; start_count += 1; if mark_idx >= marks.len() { return Err(state_decode_error(format!("{CTX}: missing style mark"))); } let end_counter = span .counter .checked_add(1) .ok_or_else(|| state_decode_error(format!("{CTX}: counter overflow")))?; if open .insert( (span.peer_idx, end_counter), (mark_idx, span.counter, text_spans_seen), ) .is_some() { return Err(state_decode_error(format!("{CTX}: duplicated style start"))); } } -1 => { let Some((mark_idx, start_counter, text_spans_at_open)) = open.remove(&(span.peer_idx, span.counter)) else { return Err(state_decode_error(format!("{CTX}: unmatched style end"))); }; if text_spans_at_open != text_spans_seen { // Text lies between the anchors; the pair is alive. continue; } let mark = &mut marks[mark_idx]; let info = TextStyleInfoFlag::from_byte(mark.info); if info.expand_before() && info.expand_after() { // An empty both-expand pair still styles future inserts. continue; } if mark.value.is_null() { continue; } let peer = decode_peer_from_table(&peers, span.peer_idx, CTX)?; let id = ID::new(peer, start_counter); if only_pairs.is_some_and(|allow| !allow.contains(&id)) { continue; } mark.value = LoroValue::Null; redacted.push(id); } len if len > 0 => text_spans_seen += 1, _ => { return Err(state_decode_error(format!( "{CTX}: invalid text span length" ))); } } } // Pairs still open are a version that holds a mark's `StyleStart` but not its // `StyleEnd` (loro-dev/loro#1165). They style nothing yet, but the end may // still arrive, so leave their values alone. if start_count != marks.len() { return Err(state_decode_error(format!("{CTX}: unused style mark"))); } if redacted.is_empty() { return Ok(None); } let mut out = Vec::with_capacity(payload.len()); out.extend_from_slice(&payload[..prefix_len]); out.extend_from_slice(&EncodedText { spans, keys, marks }.encode()); Ok(Some((out, redacted))) } impl FastStateSnapshot for RichtextState { /// Encodes the RichtextState into a compact binary format for fast snapshot storage and retrieval. /// /// The encoding format consists of: /// 1. The full text content as a string, encoded using postcard serialization. /// 2. A series of EncodedTextSpan structs representing text chunks and style markers: /// - peer_idx: Index of the peer ID in a value register (delta-RLE encoded) /// - counter: Operation counter (delta-RLE encoded) /// - lamport_sub_counter: Lamport timestamp - counter (delta-RLE encoded) /// - len: Length of text chunk or marker type (-1 for end, 0 for start, positive for text) /// 3. A list of unique style keys as InternalString. /// 4. A series of EncodedMark structs for style information: /// - key_idx: Index of the style key in the keys list /// - value: The style value /// - info: Additional style information as a byte fn encode_snapshot_fast(&mut self, mut w: W) { let value = self.get_value().into_string().unwrap(); postcard::to_io(&*value, &mut w).unwrap(); let mut spans = Vec::new(); let mut marks = Vec::new(); let mut peers: ValueRegister = ValueRegister::new(); let iter: &mut dyn Iterator; let mut a; let mut b; match &self.state { LazyLoad::Src(s) => { a = Some(s.elements.iter()); iter = &mut *a.as_mut().unwrap(); } LazyLoad::Dst(s) => { b = Some(s.iter_chunk()); iter = &mut *b.as_mut().unwrap(); } } let mut keys: ValueRegister = ValueRegister::new(); for chunk in iter { match chunk { RichtextStateChunk::Text(t) => { let id = t.id_full(); assert!(t.unicode_len() > 0); spans.push(EncodedTextSpan { peer_idx: peers.register(&id.peer), counter: id.counter, lamport_sub_counter: id.lamport as i32 - id.counter, len: t.unicode_len(), }) } RichtextStateChunk::Style { style, anchor_type } => match anchor_type { richtext::AnchorType::Start => { let id = style.id_full(); spans.push(EncodedTextSpan { peer_idx: peers.register(&id.peer), counter: id.counter, lamport_sub_counter: id.lamport as i32 - id.counter, len: 0, }); marks.push(EncodedMark { key_idx: keys.register(&style.key), value: style.value.clone(), info: style.info.to_byte(), }) } richtext::AnchorType::End => { let id = style.id_full(); spans.push(EncodedTextSpan { peer_idx: peers.register(&id.peer), counter: id.counter + 1, lamport_sub_counter: id.lamport as i32 - id.counter, len: -1, }) } }, } } let peers = peers.unwrap_vec(); leb128::write::unsigned(&mut w, peers.len() as u64).unwrap(); for peer in peers { w.write_all(&peer.to_le_bytes()).unwrap(); } let bytes = EncodedText { spans, keys: keys.unwrap_vec(), marks, } .encode(); w.write_all(&bytes).unwrap(); } fn decode_value(bytes: &[u8]) -> loro_common::LoroResult<(loro_common::LoroValue, &[u8])> { let (value, bytes) = postcard::take_from_bytes(bytes).map_err(|_| { loro_common::LoroError::DecodeError( "Decode list value failed".to_string().into_boxed_str(), ) })?; let s: String = value; Ok((LoroValue::String(s.into()), bytes)) } fn decode_snapshot_fast( idx: crate::container::idx::ContainerIdx, (string, mut bytes): (loro_common::LoroValue, &[u8]), ctx: ContainerCreationContext, ) -> loro_common::LoroResult where Self: Sized, { let mut text = RichtextState::new(idx, ctx.configure.text_style_config.clone()); let mut loader = RichtextStateLoader::default(); let peers = decode_peer_table(&mut bytes, "Decode richtext state failed")?; let string = string.into_string().map_err(|_| { state_decode_error("Decode richtext state failed: value is not a string") })?; let mut s = StrSlice::new_from_str(&string); let iters = EncodedText::decode(bytes).map_err(|err| { state_decode_error(format!( "Decode richtext state failed: invalid spans: {err}" )) })?; let keys = iters.keys; let span_iter = iters.spans.into_iter(); let mut mark_iter = iters.marks.into_iter(); let mut id_to_style = FxHashMap::default(); for span in span_iter { let EncodedTextSpan { peer_idx, counter, lamport_sub_counter, len, } = span; let peer = decode_peer_from_table(&peers, peer_idx, "Decode richtext state failed")?; let lamport = decode_lamport_from_delta( counter, lamport_sub_counter, "Decode richtext state failed", )?; let id_full = IdFull::new(peer, counter, lamport); let chunk = match len { 0 => { // Style Start let EncodedMark { key_idx, value, info, } = mark_iter.next().ok_or_else(|| { state_decode_error("Decode richtext state failed: missing style mark") })?; let key = keys.get(key_idx).ok_or_else(|| { state_decode_error( "Decode richtext state failed: style key index out of range", ) })?; let style_op = Arc::new(StyleOp { lamport, peer: id_full.peer, cnt: id_full.counter, key: key.clone(), value, info: TextStyleInfoFlag::from_byte(info), }); id_to_style.insert(id_full.id(), style_op.clone()); RichtextStateChunk::new_style(style_op, richtext::AnchorType::Start) } -1 => { // Style End let style = id_to_style.remove(&id_full.id().inc(-1)).ok_or_else(|| { state_decode_error("Decode richtext state failed: unmatched style end") })?; RichtextStateChunk::new_style(style, richtext::AnchorType::End) } len => { if len < -1 { return Err(state_decode_error( "Decode richtext state failed: invalid text span length", )); } // Text if s.as_str().chars().take(len as usize).count() < len as usize { return Err(state_decode_error( "Decode richtext state failed: text span exceeds value length", )); } let (new, rest) = s.split_at_unicode_pos(len as usize); s = rest; RichtextStateChunk::new_text(new.bytes().clone(), id_full) } }; loader.push(chunk); } if !s.as_str().is_empty() { return Err(state_decode_error( "Decode richtext state failed: text value not fully covered", )); } if mark_iter.next().is_some() { return Err(state_decode_error( "Decode richtext state failed: unused style mark", )); } // A `StyleStart` without its `StyleEnd` is valid: a version can include the // start op but not the end op (their counters are consecutive), and // `checkout` keeps the lone start anchor the same way. The style applies // to nothing until the end anchor arrives (loro-dev/loro#1165). text.state = LazyLoad::Src(loader); // NOTE: We need to ensure the invariance that the version id is always increased when the richtext state is changed // This is used to avoid the version_id to be the same as the previous zero version text.version_id = 1; Ok(text) } } #[cfg(test)] mod tests { use loro_common::{ContainerType, LoroValue}; use crate::container::idx::ContainerIdx; use super::*; #[test] fn iterative_mark_codec_matches_the_original_columnar_bytes() { let encoded = EncodedText { spans: vec![span(0, 0), span(2, 5), span(1, -1)], keys: vec!["hl".into(), "x".into()], marks: vec![ mark(LoroValue::Null, richtext::ExpandType::After), mark( LoroValue::List( vec![ LoroValue::Binary(vec![0, 255].into()), LoroValue::Map(vec![("a".into(), 42.into())].into()), ] .into(), ), richtext::ExpandType::Before, ), ], }; let old = serde_columnar::to_vec(&encoded).unwrap(); assert_eq!(encoded.encode(), old); let decoded = EncodedText::decode(&old).unwrap(); assert_eq!(decoded.encode(), old); assert_eq!(decoded.marks[1].value, encoded.marks[1].value); for len in 0..old.len() { assert!(EncodedText::decode(&old[..len]).is_err()); } } #[test] fn richtext_fast_snapshot_rejects_corrupt_state_metadata() { let idx = ContainerIdx::from_index_and_type(0, ContainerType::Text); let configure = Default::default(); let ctx = ContainerCreationContext { configure: &configure, peer: 0, }; assert!(RichtextState::decode_snapshot_fast( idx, (LoroValue::String("a".into()), &[1]), ctx, ) .is_err()); let mut text = RichtextState::new(idx, configure.text_style_config.clone()); let mut bytes = Vec::new(); text.encode_snapshot_fast(&mut bytes); let (_, state_bytes) = RichtextState::decode_value(&bytes).unwrap(); assert!(RichtextState::decode_snapshot_fast( idx, (LoroValue::String("a".into()), state_bytes), ctx, ) .is_err()); } const PEER: PeerID = 7; fn payload(text: &str, spans: Vec, marks: Vec) -> Vec { let mut out = Vec::new(); postcard::to_io(text, &mut out).unwrap(); leb128::write::unsigned(&mut out, 1).unwrap(); out.extend_from_slice(&PEER.to_le_bytes()); out.extend_from_slice( &serde_columnar::to_vec(&EncodedText { spans, keys: vec!["hl".into(), "x".into()], marks, }) .unwrap(), ); out } fn span(counter: i32, len: i32) -> EncodedTextSpan { EncodedTextSpan { peer_idx: 0, counter, lamport_sub_counter: 0, len, } } fn mark(value: LoroValue, expand: richtext::ExpandType) -> EncodedMark { EncodedMark { key_idx: 0, value, info: TextStyleInfoFlag::new(expand).to_byte(), } } fn decoded_mark_values(payload: &[u8]) -> Vec { let (_, mut rest) = postcard::take_from_bytes::<&str>(payload).unwrap(); decode_peer_table(&mut rest, "test").unwrap(); serde_columnar::from_bytes::(rest) .unwrap() .marks .into_iter() .map(|m| m.value) .collect() } /// "a" [S] [E] "d" — a dead pair between two text chunks. fn dead_pair_payload(expand: richtext::ExpandType) -> Vec { payload( "ad", vec![span(0, 1), span(10, 0), span(11, -1), span(1, 1)], vec![mark(LoroValue::String("SECRET".into()), expand)], ) } #[test] fn redacts_dead_pair_and_is_idempotent() { let p = dead_pair_payload(richtext::ExpandType::After); let (new_payload, ids) = redact_dead_style_values(&p, None).unwrap().unwrap(); assert_eq!(ids, vec![ID::new(PEER, 10)]); assert_eq!(decoded_mark_values(&new_payload), vec![LoroValue::Null]); // The redacted payload still decodes into a valid state. let idx = ContainerIdx::from_index_and_type(0, ContainerType::Text); let configure = crate::configure::Configure::default(); let ctx = ContainerCreationContext { configure: &configure, peer: 0, }; let (value, rest) = RichtextState::decode_value(&new_payload).unwrap(); let mut state = RichtextState::decode_snapshot_fast(idx, (value, rest), ctx).unwrap(); assert_eq!(state.get_value().into_string().unwrap().to_string(), "ad"); // Idempotent: a second pass reports nothing to do. assert!(redact_dead_style_values(&new_payload, None) .unwrap() .is_none()); } #[test] fn keeps_alive_and_both_expand_pairs() { // Text between the anchors: alive, untouched. let alive = payload( "a", vec![span(10, 0), span(0, 1), span(11, -1)], vec![mark( LoroValue::String("SECRET".into()), richtext::ExpandType::After, )], ); assert!(redact_dead_style_values(&alive, None).unwrap().is_none()); // Dead but both-expand: still styles future inserts, untouched. let both = dead_pair_payload(richtext::ExpandType::Both); assert!(redact_dead_style_values(&both, None).unwrap().is_none()); } #[test] fn respects_only_pairs_whitelist() { let p = dead_pair_payload(richtext::ExpandType::None); let mut allow = FxHashSet::default(); allow.insert(ID::new(PEER, 999)); assert!(redact_dead_style_values(&p, Some(&allow)) .unwrap() .is_none()); allow.insert(ID::new(PEER, 10)); let (_, ids) = redact_dead_style_values(&p, Some(&allow)).unwrap().unwrap(); assert_eq!(ids, vec![ID::new(PEER, 10)]); } #[test] fn handles_interleaved_pairs() { // "ab" [S1 "a"..] [S2] [E1] [E2]: pair 1 encloses text via its start // before "b"? Layout: [S1] "b" [S2] [E1] [E2] over text "ab" with a // leading "a": S1 opens before "b" so it is alive; S2 opens after all // text and closes empty, so it is dead. let p = payload( "ab", vec![ span(0, 1), // "a" span(10, 0), // S1 span(1, 1), // "b" span(20, 0), // S2 span(11, -1), // E1 span(21, -1), // E2 ], vec![ mark( LoroValue::String("ALIVE".into()), richtext::ExpandType::After, ), mark( LoroValue::String("DEAD".into()), richtext::ExpandType::After, ), ], ); let (new_payload, ids) = redact_dead_style_values(&p, None).unwrap().unwrap(); assert_eq!(ids, vec![ID::new(PEER, 20)]); assert_eq!( decoded_mark_values(&new_payload), vec![LoroValue::String("ALIVE".into()), LoroValue::Null] ); } #[test] fn rejects_malformed_span_structures() { // Start without a matching end is valid (loro-dev/loro#1165): a version can // hold a mark's StyleStart but not its StyleEnd. It is left untouched. let unclosed = payload( "", vec![span(10, 0)], vec![mark(LoroValue::Bool(true), richtext::ExpandType::After)], ); assert!(redact_dead_style_values(&unclosed, None).unwrap().is_none()); // End without a start. (An empty mark list short-circuits before // validation, so give it one mark to reach the scan.) let unmatched = payload( "", vec![span(11, -1)], vec![mark(LoroValue::Bool(true), richtext::ExpandType::After)], ); assert!(redact_dead_style_values(&unmatched, None).is_err()); // More starts than marks. let missing_mark = payload( "", vec![span(10, 0), span(11, -1), span(20, 0), span(21, -1)], vec![mark(LoroValue::Bool(true), richtext::ExpandType::After)], ); assert!(redact_dead_style_values(&missing_mark, None).is_err()); } #[test] fn skips_payload_without_marks() { let p = payload("ab", vec![span(0, 2)], vec![]); assert!(redact_dead_style_values(&p, None).unwrap().is_none()); } } }