1#[allow(
30 unused_imports,
31 clippy::wildcard_imports,
32 reason = "Standard workspace module prelude"
33)]
34use crate::utilities::*;
35
36pub fn truncate_html(html: &str, max_len: u64) -> String {
37 clip(
38 html,
39 max_len,
40 Some(&html_options(ClipHtmlOptions::default())),
41 )
42}
43
44pub fn truncate_text(text: &str, max_len: u64) -> String {
45 clip(text, max_len, None)
46}
47
48pub fn clip(text: &str, len: u64, options: Option<&ClipOptions>) -> String {
51 if text.is_empty() {
52 return String::new();
53 }
54
55 let Ok(max_len) = usize::try_from(len) else {
56 warn_fmt!("clip: len {len} does not fit in usize; returning input");
57 return text.to_string();
58 };
59
60 let (force_html, common, html_options) = match options {
61 Some(ClipOptions::Html(html)) => (true, &html.common, Some(html)),
62 Some(ClipOptions::Plain(plain)) => (false, &plain.common, None),
63 None => (false, &CommonClipOptions::default(), None),
64 };
65
66 let mut html_mode = force_html;
67 if !html_mode {
68 html_mode = text.contains('<') || looks_like_html_entity_encoded(text);
71 }
72 if let Some(true) = common.html {
73 html_mode = true;
74 }
75
76 let indicator = common
78 .indicator
79 .clone()
80 .unwrap_or_else(|| "\u{2026}".to_string());
81 let insert_indicator_at_linebreak =
83 common.insert_indicator_at_linebreak.unwrap_or(true);
84 let break_words = common.break_words.unwrap_or(false);
86 let max_lines = common
88 .max_lines
89 .and_then(|v| usize::try_from(v).ok())
90 .unwrap_or(usize::MAX);
91
92 if html_mode {
93 let image_weight =
95 html_options.and_then(|h| h.image_weight).unwrap_or(2);
96 let strip_tags = html_options.and_then(|h| h.strip_tags.clone());
97
98 if let Ok(result) = clip_html(
99 text,
100 max_len,
101 &indicator,
102 insert_indicator_at_linebreak,
103 max_lines,
104 break_words,
105 image_weight,
106 strip_tags,
107 ) {
108 result
109 } else {
110 clip_plain_text(
111 text,
112 max_len,
113 &indicator,
114 insert_indicator_at_linebreak,
115 max_lines,
116 break_words,
117 )
118 }
119 } else {
120 clip_plain_text(
121 text,
122 max_len,
123 &indicator,
124 insert_indicator_at_linebreak,
125 max_lines,
126 break_words,
127 )
128 }
129}
130
131fn looks_like_html_entity_encoded(text: &str) -> bool {
132 let bytes = text.as_bytes();
138 let mut i: usize = 0;
139 while let Some(&byte) = bytes.get(i) {
140 if byte != b'&' {
141 i = i.saturating_add(1);
142 continue;
143 }
144
145 let mut j = i.saturating_add(1);
146 let mut saw_alnum = false;
147 while let Some(&b) = bytes.get(j) {
148 if b == b';' {
149 if saw_alnum {
150 return true;
151 }
152 break;
153 }
154 if b.is_ascii_digit()
155 || b.is_ascii_uppercase()
156 || b.is_ascii_lowercase()
157 {
158 saw_alnum = true;
159 j = j.saturating_add(1);
160 continue;
161 }
162 break;
163 }
164
165 i = i.saturating_add(1);
166 }
167 false
168}
169
170const NEWLINE_CHAR_CODE: u16 = 10; const EXCLAMATION_CHAR_CODE: u16 = 33; const DOUBLE_QUOTE_CHAR_CODE: u16 = 34; const AMPERSAND_CHAR_CODE: u16 = 38; const SINGLE_QUOTE_CHAR_CODE: u16 = 39; const FORWARD_SLASH_CHAR_CODE: u16 = 47; const SEMICOLON_CHAR_CODE: u16 = 59; const TAG_OPEN_CHAR_CODE: u16 = 60; const EQUAL_SIGN_CHAR_CODE: u16 = 61; const TAG_CLOSE_CHAR_CODE: u16 = 62; const COMMENT_START: &[u16] = &[60, 33, 45, 45]; const COMMENT_END: &[u16] = &[45, 45, 62]; const CDATA_START: &[u16] = &[60, 33, 91, 67, 68, 65, 84, 65, 91]; const CDATA_END: &[u16] = &[93, 93, 62]; const VOID_ELEMENTS: &[&str] = &[
187 "area", "base", "br", "col", "command", "embed", "hr", "img", "input",
188 "keygen", "link", "meta", "param", "source", "track", "wbr",
189];
190
191const BLOCK_ELEMENTS: &[&str] = &[
192 "address",
193 "article",
194 "aside",
195 "blockquote",
196 "canvas",
197 "dd",
198 "div",
199 "dl",
200 "dt",
201 "fieldset",
202 "figcaption",
203 "figure",
204 "footer",
205 "form",
206 "h1",
207 "h2",
208 "h3",
209 "h4",
210 "h5",
211 "h6",
212 "header",
213 "hgroup",
214 "hr",
215 "li",
216 "main",
217 "nav",
218 "noscript",
219 "ol",
220 "output",
221 "p",
222 "pre",
223 "section",
224 "table",
225 "tbody",
226 "tfoot",
227 "thead",
228 "tr",
229 "ul",
230 "video",
231];
232
233const UNBREAKABLE_ELEMENTS: &[&str] = &["audio", "math", "svg", "video"];
234
235fn is_void_element(tag: &str) -> bool {
236 VOID_ELEMENTS.contains(&tag)
237}
238
239fn is_block_element(tag: &str) -> bool {
240 BLOCK_ELEMENTS.contains(&tag)
241}
242
243fn is_unbreakable_element(tag: &str) -> bool {
244 UNBREAKABLE_ELEMENTS.contains(&tag)
245}
246
247fn any_unbreakable_open(tag_stack: &[String]) -> bool {
248 UNBREAKABLE_ELEMENTS
249 .iter()
250 .any(|t| tag_stack.iter().any(|s| s == t))
251}
252
253fn is_high_surrogate(unit: u16) -> bool {
254 (unit & 0xfc00) == 0xd800
255}
256
257fn is_low_surrogate(unit: u16) -> bool {
258 (unit & 0xfc00) == 0xdc00
259}
260
261fn is_white_space(unit: u16) -> bool {
262 unit == 9 || unit == 10 || unit == 12 || unit == 13 || unit == 32
263}
264
265fn is_character_reference_character(unit: u16) -> bool {
266 (48..=57).contains(&unit)
267 || (65..=90).contains(&unit)
268 || (97..=122).contains(&unit)
269}
270
271fn encode_utf16(s: &str) -> Vec<u16> {
272 s.encode_utf16().collect()
273}
274
275fn units_to_string(units: &[u16]) -> String {
276 String::from_utf16_lossy(units)
277}
278
279fn find_subsequence(
280 haystack: &[u16],
281 needle: &[u16],
282 from: usize,
283) -> Option<usize> {
284 if needle.is_empty() {
285 return Some(from.min(haystack.len()));
286 }
287 if from > haystack.len() {
288 return None;
289 }
290 let remaining = haystack.len().saturating_sub(from);
291 if needle.len() > remaining {
292 return None;
293 }
294 let end = haystack.len().saturating_sub(needle.len());
295 let mut i = from;
296 while i <= end {
297 if haystack.get(i..i.saturating_add(needle.len())) == Some(needle) {
298 return Some(i);
299 }
300 i = i.saturating_add(1);
301 }
302 None
303}
304
305fn index_of_white_space(units: &[u16], from: usize) -> usize {
306 let mut i = from;
307 while let Some(&u) = units.get(i) {
308 if is_white_space(u) {
309 return i;
310 }
311 i = i.saturating_add(1);
312 }
313 units.len()
314}
315
316fn should_simplify_white_space(tag_stack: &[String]) -> bool {
317 for tag in tag_stack.iter().rev() {
318 if tag == "li" || tag == "td" {
319 return false;
320 }
321 if tag == "ol" || tag == "table" || tag == "ul" {
322 return true;
323 }
324 }
325 false
326}
327
328fn simplify_white_space(s: &str) -> String {
329 let trimmed = s.trim();
330 let mut out = String::with_capacity(trimmed.len());
331 let mut in_ws = false;
332 for ch in trimmed.chars() {
333 if ch.is_whitespace() {
334 if !in_ws {
335 out.push(' ');
336 in_ws = true;
337 }
338 } else {
339 out.push(ch);
340 in_ws = false;
341 }
342 }
343 out
344}
345
346fn search_next_char_of_interest(
347 units: &[u16],
348 from: usize,
349 include_newline: bool,
350) -> Option<usize> {
351 let mut i = from;
352 while let Some(&u) = units.get(i) {
353 if u == TAG_OPEN_CHAR_CODE || u == AMPERSAND_CHAR_CODE {
354 return Some(i);
355 }
356 if include_newline && u == NEWLINE_CHAR_CODE {
357 return Some(i);
358 }
359 if is_high_surrogate(u) {
360 return Some(i);
361 }
362 i = i.saturating_add(1);
363 }
364 None
365}
366
367fn has_non_whitespace(units: &[u16], from: usize) -> bool {
368 let mut i = from;
369 while let Some(&u) = units.get(i) {
370 if !is_white_space(u) {
371 return true;
372 }
373 i = i.saturating_add(1);
374 }
375 false
376}
377
378fn has_html_non_whitespace(units: &[u16], from: usize) -> bool {
379 let mut i = from;
380 while let Some(&u) = units.get(i) {
381 if u == TAG_OPEN_CHAR_CODE {
382 i = i.saturating_add(1);
383 while units.get(i).is_some_and(|&x| is_white_space(x)) {
384 i = i.saturating_add(1);
385 }
386
387 if units.get(i) == Some(&FORWARD_SLASH_CHAR_CODE) {
388 i = i.saturating_add(1);
389 while units.get(i).is_some_and(|&x| x != TAG_CLOSE_CHAR_CODE) {
390 i = i.saturating_add(1);
391 }
392 } else {
393 return true;
394 }
395 } else if !is_white_space(u) {
396 return true;
397 }
398 i = i.saturating_add(1);
399 }
400 false
401}
402
403fn is_line_break(units: &[u16], index: usize) -> bool {
404 let Some(&u) = units.get(index) else {
405 return false;
406 };
407 match u {
408 NEWLINE_CHAR_CODE => true,
409 TAG_OPEN_CHAR_CODE => {
410 let mut i = index.saturating_add(1);
412 if i >= units.len() {
413 return false;
414 }
415
416 let name_start = i;
418 while let Some(&u) = units.get(i) {
419 if is_white_space(u)
420 || u == FORWARD_SLASH_CHAR_CODE
421 || u == TAG_CLOSE_CHAR_CODE
422 {
423 break;
424 }
425 i = i.saturating_add(1);
426 }
427 if i == name_start {
428 return false;
429 }
430 #[expect(
431 clippy::expect_used,
432 reason = "name_start < i <= units.len() guaranteed by loop termination"
433 )]
434 let tag = units
435 .get(name_start..i)
436 .map(units_to_string)
437 .expect("name_start < i <= units.len() is in bounds")
438 .to_ascii_lowercase();
439 if !(is_block_element(&tag) || tag == "br") {
440 return false;
441 }
442
443 while units.get(i).is_some_and(|&u| is_white_space(u)) {
444 i = i.saturating_add(1);
445 }
446 if units.get(i) == Some(&FORWARD_SLASH_CHAR_CODE) {
447 i = i.saturating_add(1);
448 }
449 units.get(i) == Some(&TAG_CLOSE_CHAR_CODE)
450 }
451 _ => false,
452 }
453}
454
455fn take_char_at(units: &[u16], index: usize) -> Vec<u16> {
456 let Some(&first) = units.get(index) else {
457 return Vec::new();
458 };
459 if is_high_surrogate(first) {
460 if let Some(&next) = units.get(index.saturating_add(1)) {
461 if is_low_surrogate(next) {
462 return vec![first, next];
463 }
464 }
465 }
466 vec![first]
467}
468
469fn take_html_char_at(units: &[u16], index: usize) -> Vec<u16> {
470 let mut taken = take_char_at(units, index);
471 if taken.first() == Some(&ERSAND_CHAR_CODE) {
472 let mut j = index.saturating_add(1);
473 while let Some(&u) = units.get(j) {
474 if is_character_reference_character(u) {
475 taken.push(u);
476 j = j.saturating_add(1);
477 } else if u == SEMICOLON_CHAR_CODE {
478 taken.push(u);
479 break;
480 } else {
481 break;
482 }
483 }
484 }
485 taken
486}
487
488fn should_strip(strip_tags: Option<&StripTags>, tag_name: &str) -> bool {
489 let Some(strip_tags) = strip_tags else {
490 return false;
491 };
492 match strip_tags {
493 StripTags::All(true) => true,
494 StripTags::All(false) => false,
495 StripTags::Tags(tags) => {
496 if tags.is_empty() {
497 return false;
498 }
499 tags.iter().any(|t| t == tag_name)
500 }
501 }
502}
503
504fn pop_tag_stack(
505 mut result: String,
506 tag_stack: &mut Vec<String>,
507 strip_tags: Option<&StripTags>,
508) -> String {
509 while let Some(tag) = tag_stack.pop() {
510 if !should_strip(strip_tags, &tag) {
511 result.push_str("</");
512 result.push_str(&tag);
513 result.push('>');
514 }
515 }
516 result
517}
518
519#[expect(clippy::too_many_lines, reason = "+/- more readable")]
520fn clip_plain_text(
521 text: &str,
522 max_len: usize,
523 indicator: &str,
524 insert_indicator_at_linebreak: bool,
525 max_lines: usize,
526 break_words: bool,
527) -> String {
528 let units = encode_utf16(text);
529 let indicator_units = encode_utf16(indicator);
530
531 let mut num_chars = indicator_units.len();
532 let mut num_lines: usize = 1;
533
534 let mut i: usize = 0;
535 while i < units.len() {
536 num_chars = num_chars.saturating_add(1);
537 if num_chars > max_len {
538 break;
539 }
540
541 let Some(&u) = units.get(i) else {
542 break;
543 };
544 if u == NEWLINE_CHAR_CODE {
545 num_lines = num_lines.saturating_add(1);
546 if num_lines > max_lines {
547 break;
548 }
549 } else if is_high_surrogate(u) {
550 if let Some(&next) = units.get(i.saturating_add(1)) {
551 if is_low_surrogate(next) {
552 i = i.saturating_add(1);
553 }
554 }
555 }
556
557 i = i.saturating_add(1);
558 }
559
560 if num_chars > max_len {
561 let mut next_char = take_char_at(&units, i);
562 if !indicator_units.is_empty() {
563 let peek_index = i.saturating_add(next_char.len());
564 if peek_index == units.len() {
565 return text.to_string();
566 }
567 if let Some(&u) = units.get(peek_index) {
568 if u == NEWLINE_CHAR_CODE {
569 let insert_indicator = insert_indicator_at_linebreak
570 && has_non_whitespace(&units, peek_index);
571 #[expect(
572 clippy::expect_used,
573 reason = "peek_index < units.len() checked on line 543"
574 )]
575 let mut out = units
576 .get(..peek_index)
577 .expect("peek_index < units.len() is in bounds")
578 .to_vec();
579 if insert_indicator {
580 out.extend_from_slice(&indicator_units);
581 }
582 return units_to_string(&out);
583 }
584 }
585 }
586
587 if !break_words {
588 let backtrack_start = i.saturating_sub(indicator_units.len());
589 let mut j = backtrack_start;
590 loop {
591 let Some(&u) = units.get(j) else {
592 break;
593 };
594 if u == NEWLINE_CHAR_CODE {
595 i = j;
596 next_char = vec![NEWLINE_CHAR_CODE];
597 break;
598 }
599 if is_white_space(u) {
600 i = j.saturating_add(usize::from(
601 !indicator_units.is_empty(),
602 ));
603 break;
604 }
605 if j == 0 {
606 break;
607 }
608 j = j.saturating_sub(1);
609 }
610 }
611
612 let next_is_newline = next_char.first() == Some(&NEWLINE_CHAR_CODE);
613 let insert_indicator = (insert_indicator_at_linebreak
614 || !next_is_newline)
615 && has_non_whitespace(&units, i);
616
617 while i > 0 {
618 if let Some(&prev) = units.get(i.saturating_sub(1)) {
619 if is_white_space(prev) {
620 i = i.saturating_sub(1);
621 continue;
622 }
623 }
624 break;
625 }
626
627 #[expect(
628 clippy::expect_used,
629 reason = "i <= units.len() invariant in loop"
630 )]
631 let mut out = units
632 .get(..i)
633 .expect("i <= units.len() is in bounds")
634 .to_vec();
635 if insert_indicator {
636 out.extend_from_slice(&indicator_units);
637 }
638 return units_to_string(&out);
639 }
640
641 if num_lines > max_lines {
642 let insert_indicator =
643 insert_indicator_at_linebreak && has_non_whitespace(&units, i);
644
645 while i > 0 {
646 if let Some(&prev) = units.get(i.saturating_sub(1)) {
647 if is_white_space(prev) {
648 i = i.saturating_sub(1);
649 continue;
650 }
651 }
652 break;
653 }
654
655 #[expect(
656 clippy::expect_used,
657 reason = "i <= units.len() invariant in loop"
658 )]
659 let mut out = units
660 .get(..i)
661 .expect("i <= units.len() is in bounds")
662 .to_vec();
663 if insert_indicator {
664 out.extend_from_slice(&indicator_units);
665 }
666 return units_to_string(&out);
667 }
668
669 text.to_string()
670}
671
672#[expect(
673 clippy::too_many_arguments,
674 clippy::too_many_lines,
675 clippy::indexing_slicing,
676 reason = "complex HTML structure parsing logic"
677)]
678fn clip_html(
679 text: &str,
680 max_len: usize,
681 indicator: &str,
682 insert_indicator_at_linebreak: bool,
683 max_lines: usize,
684 break_words: bool,
685 image_weight: u32,
686 strip_tags: Option<StripTags>,
687) -> Result<String> {
688 let indicator_units = encode_utf16(indicator);
689 let mut units = encode_utf16(text);
690
691 let mut num_chars = indicator_units.len();
692 let mut num_lines: usize = 1;
693 let image_weight_usize = usize::try_from(image_weight)
694 .context("image_weight does not fit in usize")?;
695
696 let mut tag_stack: Vec<String> = Vec::new();
697 let mut i: usize = 0;
698 let mut unbreakable_element_index: Option<usize> = None;
699
700 while i < units.len() {
701 let will_simplify = should_simplify_white_space(&tag_stack);
702 let include_newline =
703 unbreakable_element_index.is_none() && !will_simplify;
704 let next_index =
705 search_next_char_of_interest(&units, i, include_newline);
706
707 let next_block_size = match next_index {
708 Some(idx) => idx.saturating_sub(i),
709 None => units.len().saturating_sub(i),
710 };
711
712 if unbreakable_element_index.is_none() {
713 if will_simplify {
714 let block_units = if next_index.is_none() {
715 &units[i..]
716 } else {
717 &units[i..i.saturating_add(next_block_size)]
718 };
719 let mut simplified =
720 simplify_white_space(&units_to_string(block_units));
721
722 if let Some(current_tag) = tag_stack.last() {
723 if should_strip(strip_tags.as_ref(), current_tag) {
724 let insert_space_before = i > 0
725 && units
726 .get(i.saturating_sub(1))
727 .copied()
728 .is_some_and(|u| !is_white_space(u));
729 let insert_space_after = units
730 .get(i.saturating_add(next_block_size))
731 .copied()
732 .is_some_and(|u| !is_white_space(u));
733
734 if !simplified.is_empty() {
735 if insert_space_before {
736 simplified = format!(" {simplified}");
737 }
738 if insert_space_after {
739 simplified.push(' ');
740 }
741 } else if insert_space_before && insert_space_after {
742 simplified = " ".to_string();
743 }
744
745 let simplified_units = encode_utf16(&simplified);
746 let replace_end = i.saturating_add(next_block_size);
747 units.splice(
748 i..replace_end,
749 simplified_units.iter().copied(),
750 );
751 let new_block_size = simplified.encode_utf16().count();
753 num_chars = num_chars.saturating_add(new_block_size);
754 if num_chars > max_len {
755 break;
756 }
757 i = i.saturating_add(new_block_size);
758 } else {
759 let simplified_len = simplified.encode_utf16().count();
760 num_chars = num_chars.saturating_add(simplified_len);
761 if num_chars > max_len {
762 break;
763 }
764 i = i.saturating_add(next_block_size);
765 }
766 } else {
767 let simplified_len = simplified.encode_utf16().count();
768 num_chars = num_chars.saturating_add(simplified_len);
769 if num_chars > max_len {
770 break;
771 }
772 i = i.saturating_add(next_block_size);
773 }
774 } else {
775 num_chars = num_chars.saturating_add(next_block_size);
776 if num_chars > max_len {
777 let next_block_size_i64 = i64::try_from(next_block_size)
778 .context("next_block_size does not fit in i64")?;
779 let num_chars_i64 = i64::try_from(num_chars)
780 .context("num_chars does not fit in i64")?;
781 let max_len_i64 = i64::try_from(max_len)
782 .context("max_len does not fit in i64")?;
783 let new_i = i64::try_from(i)?
785 .checked_add(next_block_size_i64)
786 .and_then(|v| v.checked_sub(num_chars_i64))
787 .and_then(|v| v.checked_add(max_len_i64))
788 .unwrap_or(0);
789 i = usize::try_from(new_i.max(0))
790 .context("clip index does not fit in usize")?;
791 break;
792 }
793 i = i.saturating_add(next_block_size);
794 }
795 } else {
796 i = i.saturating_add(next_block_size);
797 }
798
799 if next_index.is_none() {
800 break;
801 }
802
803 let Some(&u) = units.get(i) else {
804 break;
805 };
806 if u == TAG_OPEN_CHAR_CODE {
807 let next_u = units.get(i.saturating_add(1)).copied().unwrap_or(0);
809 let is_special_tag = next_u == EXCLAMATION_CHAR_CODE;
810 if is_special_tag {
811 if units
813 .get(i..i.saturating_add(COMMENT_START.len()))
814 .is_some_and(|s| s == COMMENT_START)
815 {
816 let search_from = i.saturating_add(COMMENT_START.len());
817 let Some(end_idx) =
818 find_subsequence(&units, COMMENT_END, search_from)
819 else {
820 bail!("Invalid HTML: {text}");
821 };
822 i = end_idx.saturating_add(COMMENT_END.len());
823 continue;
824 }
825
826 if units
828 .get(i..i.saturating_add(CDATA_START.len()))
829 .is_some_and(|s| s == CDATA_START)
830 {
831 let search_from = i.saturating_add(CDATA_START.len());
832 let Some(end_idx) =
833 find_subsequence(&units, CDATA_END, search_from)
834 else {
835 bail!("Invalid HTML: {text}");
836 };
837 i = end_idx.saturating_add(CDATA_END.len());
838 continue;
839 }
840 }
841
842 {
843 let is_end_tag = next_u == FORWARD_SLASH_CHAR_CODE;
844 if num_chars == max_len && !is_end_tag {
845 num_chars = num_chars.saturating_add(1);
846 break;
847 }
848
849 let mut attribute_quote: u16 = 0;
850 let mut end_index = i;
851 let mut is_attribute_value = false;
852
853 loop {
854 end_index = end_index.saturating_add(1);
855 if end_index >= units.len() {
856 bail!("Invalid HTML: {text}");
857 }
858
859 let Some(&cu) = units.get(end_index) else {
860 break;
861 };
862 if is_attribute_value {
863 if attribute_quote != 0 {
864 if cu == attribute_quote {
865 is_attribute_value = false;
866 }
867 } else if is_white_space(cu) {
868 is_attribute_value = false;
869 } else if cu == TAG_CLOSE_CHAR_CODE {
870 is_attribute_value = false;
871 end_index = end_index.saturating_sub(1);
872 }
873 } else if cu == EQUAL_SIGN_CHAR_CODE {
874 while units
875 .get(end_index.saturating_add(1))
876 .copied()
877 .is_some_and(is_white_space)
878 {
879 end_index = end_index.saturating_add(1);
880 }
881 is_attribute_value = true;
882
883 let first_attr = units
885 .get(end_index.saturating_add(1))
886 .copied()
887 .unwrap_or(0);
888 if first_attr == DOUBLE_QUOTE_CHAR_CODE
889 || first_attr == SINGLE_QUOTE_CHAR_CODE
890 {
891 attribute_quote = first_attr;
892 end_index = end_index.saturating_add(1);
893 } else {
894 attribute_quote = 0;
895 }
896 } else if cu == TAG_CLOSE_CHAR_CODE {
897 let tag_name_start =
898 i.saturating_add(if is_end_tag { 2 } else { 1 });
899 let tag_name_end =
900 index_of_white_space(&units, tag_name_start)
901 .min(end_index);
902 let mut tag_name = units
904 .get(tag_name_start..tag_name_end)
905 .map(units_to_string)
906 .unwrap_or_default()
907 .to_ascii_lowercase();
908 if let Some(last) = tag_name.as_bytes().last().copied()
909 {
910 if last == b'/' {
911 tag_name.pop();
912 }
913 }
914
915 let strip =
916 should_strip(strip_tags.as_ref(), &tag_name);
917
918 if is_end_tag {
919 let current = tag_stack.pop();
920 if current.as_deref() != Some(tag_name.as_str()) {
921 return Err(anyhow::anyhow!(
922 "Invalid HTML: {text}"
923 ));
924 }
925
926 if is_unbreakable_element(&tag_name) {
927 if any_unbreakable_open(&tag_stack) {
928 } else if strip {
930 if let Some(start) =
931 unbreakable_element_index
932 {
933 i = start;
934 }
935 unbreakable_element_index = None;
936 } else {
937 unbreakable_element_index = None;
938 num_chars = num_chars
939 .saturating_add(image_weight_usize);
940 if num_chars > max_len {
941 break;
942 }
943 }
944 }
945
946 if is_block_element(&tag_name)
947 && unbreakable_element_index.is_none()
948 && !strip
949 {
950 num_lines = num_lines.saturating_add(1);
951 if num_lines > max_lines {
952 tag_stack.push(tag_name);
953 break;
954 }
955 }
956 } else if is_void_element(&tag_name)
957 || units
959 .get(end_index.saturating_sub(1))
960 .copied()
961 .unwrap_or(0)
962 == FORWARD_SLASH_CHAR_CODE
963 {
964 if strip {
965 } else if tag_name == "br" {
967 num_lines = num_lines.saturating_add(1);
968 if num_lines > max_lines {
969 break;
970 }
971 } else if tag_name == "img" {
972 num_chars = num_chars
973 .saturating_add(image_weight_usize);
974 if num_chars > max_len {
975 break;
976 }
977 }
978 } else {
979 if any_unbreakable_open(&tag_stack) {
980 } else if is_unbreakable_element(&tag_name) {
982 unbreakable_element_index = Some(i);
983 }
984 tag_stack.push(tag_name.clone());
985 }
986
987 if strip && unbreakable_element_index.is_none() {
988 units.drain(i..=end_index);
989 i = i.saturating_sub(1);
990 } else {
991 i = end_index;
992 }
993
994 break;
995 }
996 }
997
998 if num_chars > max_len || num_lines > max_lines {
999 break;
1000 }
1001 }
1002 } else if u == AMPERSAND_CHAR_CODE {
1003 let mut end_index = i.saturating_add(1);
1004 let mut is_char_ref = true;
1005 while let Some(&cu) = units.get(end_index) {
1006 if is_character_reference_character(cu) {
1007 end_index = end_index.saturating_add(1);
1008 } else if cu == SEMICOLON_CHAR_CODE {
1009 break;
1010 } else {
1011 is_char_ref = false;
1012 break;
1013 }
1014 }
1015
1016 if unbreakable_element_index.is_none() {
1017 num_chars = num_chars.saturating_add(1);
1018 if num_chars > max_len {
1019 break;
1020 }
1021 }
1022
1023 if is_char_ref
1024 && end_index < units.len()
1025 && units.get(end_index) == Some(&SEMICOLON_CHAR_CODE)
1026 {
1027 i = end_index;
1028 }
1029 } else if u == NEWLINE_CHAR_CODE {
1030 num_chars = num_chars.saturating_add(1);
1031 if num_chars > max_len {
1032 break;
1033 }
1034 num_lines = num_lines.saturating_add(1);
1035 if num_lines > max_lines {
1036 break;
1037 }
1038 } else {
1039 if unbreakable_element_index.is_none() {
1040 num_chars = num_chars.saturating_add(1);
1041 if num_chars > max_len {
1042 break;
1043 }
1044 }
1045 if is_high_surrogate(u) {
1046 if let Some(&next) = units.get(i.saturating_add(1)) {
1047 if is_low_surrogate(next) {
1048 i = i.saturating_add(1);
1049 }
1050 }
1051 }
1052 }
1053
1054 i = i.saturating_add(1);
1055 }
1056
1057 if num_chars > max_len {
1058 let mut next_char = take_html_char_at(&units, i);
1059
1060 if !indicator_units.is_empty() {
1061 let mut peek_index = i.saturating_add(next_char.len());
1062 while units.get(peek_index) == Some(&TAG_OPEN_CHAR_CODE)
1063 && units.get(peek_index.saturating_add(1))
1064 == Some(&FORWARD_SLASH_CHAR_CODE)
1065 {
1066 let Some(tag_end) = units
1067 .get(peek_index.saturating_add(2)..)
1068 .and_then(|slice| {
1069 slice.iter().position(|u| *u == TAG_CLOSE_CHAR_CODE)
1070 })
1071 else {
1072 break;
1073 };
1074 let next_peek = peek_index
1075 .saturating_add(2)
1076 .saturating_add(tag_end)
1077 .saturating_add(1);
1078 peek_index = next_peek;
1079 }
1080
1081 if peek_index == units.len() || is_line_break(&units, peek_index) {
1082 i = i.saturating_add(next_char.len());
1083 next_char =
1085 units.get(i).copied().map(|u| vec![u]).unwrap_or_default();
1086 }
1087 }
1088
1089 while next_char.first() == Some(&TAG_OPEN_CHAR_CODE)
1090 && units.get(i.saturating_add(1)) == Some(&FORWARD_SLASH_CHAR_CODE)
1091 {
1092 let Some(tag_name) = tag_stack.pop() else {
1093 break;
1094 };
1095 let close_pat = TAG_CLOSE_CHAR_CODE;
1096 let mut tag_end_index: Option<usize> = None;
1097 let mut j = i.saturating_add(2);
1098 while j < units.len() {
1099 if units.get(j) == Some(&close_pat) {
1100 tag_end_index = Some(j);
1101 break;
1102 }
1103 j = j.saturating_add(1);
1104 }
1105 let Some(tag_end_index) = tag_end_index else {
1106 bail!("Invalid HTML: {text}");
1107 };
1108
1109 let Some(between_units) =
1110 units.get(i.saturating_add(2)..tag_end_index)
1111 else {
1112 bail!("Invalid HTML: {text}");
1113 };
1114 let between = units_to_string(between_units).trim().to_string();
1115 if between != tag_name {
1116 bail!("Invalid HTML: {text}");
1117 }
1118
1119 if should_strip(strip_tags.as_ref(), &tag_name) {
1120 units.drain(i..=tag_end_index);
1121 } else {
1122 i = tag_end_index.saturating_add(1);
1123 }
1124
1125 next_char =
1127 units.get(i).copied().map(|u| vec![u]).unwrap_or_default();
1128 }
1129
1130 if i < units.len() {
1131 if !break_words {
1132 let backtrack_start = i.saturating_sub(indicator_units.len());
1133 let mut j = backtrack_start;
1134 loop {
1135 let Some(&cu) = units.get(j) else {
1136 break;
1137 };
1138 if cu == TAG_CLOSE_CHAR_CODE || cu == SEMICOLON_CHAR_CODE {
1139 break;
1140 }
1141 if cu == NEWLINE_CHAR_CODE || cu == TAG_OPEN_CHAR_CODE {
1142 i = j;
1143 break;
1144 }
1145 if is_white_space(cu) {
1146 i = j.saturating_add(usize::from(
1147 !indicator_units.is_empty(),
1148 ));
1149 break;
1150 }
1151 if j == 0 {
1152 break;
1153 }
1154 j = j.saturating_sub(1);
1155 }
1156 }
1157
1158 let insert_indicator = (insert_indicator_at_linebreak
1159 || !is_line_break(&units, i))
1160 && has_html_non_whitespace(&units, i);
1161
1162 while i > 0 {
1163 if let Some(&prev) = units.get(i.saturating_sub(1)) {
1164 if is_white_space(prev) {
1165 i = i.saturating_sub(1);
1166 continue;
1167 }
1168 }
1169 break;
1170 }
1171
1172 #[expect(
1173 clippy::expect_used,
1174 reason = "i <= units.len() invariant in clip_html_text loop"
1175 )]
1176 let mut out_units = units
1177 .get(..i)
1178 .expect("i <= units.len() is in bounds")
1179 .to_vec();
1180 if insert_indicator {
1181 out_units.extend_from_slice(&indicator_units);
1182 }
1183
1184 let out = units_to_string(&out_units);
1185 return Ok(pop_tag_stack(out, &mut tag_stack, strip_tags.as_ref()));
1186 }
1187 } else if num_lines > max_lines {
1188 let insert_indicator =
1189 insert_indicator_at_linebreak && has_html_non_whitespace(&units, i);
1190
1191 while i > 0 {
1192 if let Some(&prev) = units.get(i.saturating_sub(1)) {
1193 if is_white_space(prev) {
1194 i = i.saturating_sub(1);
1195 continue;
1196 }
1197 }
1198 break;
1199 }
1200
1201 #[expect(
1202 clippy::expect_used,
1203 reason = "i <= units.len() invariant in clip_html_text loop"
1204 )]
1205 let mut out_units = units
1206 .get(..i)
1207 .expect("i <= units.len() is in bounds")
1208 .to_vec();
1209 if insert_indicator {
1210 out_units.extend_from_slice(&indicator_units);
1211 }
1212 let out = units_to_string(&out_units);
1213 return Ok(pop_tag_stack(out, &mut tag_stack, strip_tags.as_ref()));
1214 }
1215
1216 Ok(units_to_string(&units))
1217}
1218
1219#[derive(Debug, Clone, Default)]
1221pub struct CommonClipOptions {
1222 pub break_words: Option<bool>,
1224
1225 pub html: Option<bool>,
1228
1229 pub indicator: Option<String>,
1231
1232 pub insert_indicator_at_linebreak: Option<bool>,
1235
1236 pub max_lines: Option<u32>,
1238}
1239
1240#[derive(Debug, Clone, Default)]
1242pub struct ClipPlainTextOptions {
1243 pub common: CommonClipOptions,
1245}
1246
1247#[derive(Debug, Clone, Default)]
1249pub struct ClipHtmlOptions {
1250 pub common: CommonClipOptions,
1252
1253 pub image_weight: Option<u32>,
1255
1256 pub strip_tags: Option<StripTags>,
1259}
1260
1261#[derive(Debug, Clone)]
1263pub enum StripTags {
1264 All(bool), Tags(Vec<String>), }
1267
1268impl Default for StripTags {
1269 fn default() -> Self {
1270 Self::Tags(Vec::new())
1271 }
1272}
1273
1274#[derive(Debug, Clone)]
1276pub enum ClipOptions {
1277 Plain(ClipPlainTextOptions),
1278 Html(ClipHtmlOptions),
1279}
1280
1281impl Default for ClipOptions {
1282 fn default() -> Self {
1283 Self::Plain(ClipPlainTextOptions::default())
1284 }
1285}
1286
1287pub fn plain_options(common: CommonClipOptions) -> ClipOptions {
1288 ClipOptions::Plain(ClipPlainTextOptions { common })
1289}
1290
1291pub fn html_options(mut options: ClipHtmlOptions) -> ClipOptions {
1292 if options.common.html.is_none() {
1293 options.common.html = Some(true);
1294 }
1295 ClipOptions::Html(options)
1296}
1297
1298#[cfg(test)]
1299#[allow(
1300 clippy::panic,
1301 clippy::expect_used,
1302 clippy::unwrap_used,
1303 clippy::unwrap_in_result,
1304 clippy::panic_in_result_fn,
1305 clippy::indexing_slicing,
1306 clippy::arithmetic_side_effects,
1307 reason = "Standard repository test boilerplate"
1308)]
1309mod tests {
1310 use super::*;
1311
1312 fn get_html_table() -> String {
1313 r#"<table border="1" cellpadding="1" cellspacing="1" style="width: 500px">
1314 <tbody>
1315 <tr>
1316 <td>fb</td>
1317 <td>fbfbfb</td>
1318 </tr>
1319 <tr>
1320 <td>google</td>
1321 <td>twitter</td>
1322 </tr>
1323 <tr>
1324 <td>intel</td>
1325 <td>amazon</td>
1326 </tr>
1327 </tbody>
1328</table>"#
1329 .to_string()
1330 }
1331
1332 fn get_html_with_svg() -> String {
1333 "<p>\
1334 <svg width=\"100%\" height=\"100%\" viewBox=\"0 0 100 100\" \
1335 xmlns=\"http://www.w3.org/2000/svg\">\n\
1336 <style>\n\
1337 /* <![CDATA[ */\n\
1338 circle {\n\
1339 fill: orange;\n\
1340 stroke: black;\n\
1341 stroke-width: 10px; \
1342 // Note that the value of a pixel depend on the viewBox\n\
1343 }\n\
1344 /* ]]> */\n\
1345 </style>\n\
1346 \n\
1347 <circle cx=\"50\" cy=\"50\" r=\"40\" />\n\
1348 </svg>test\n\
1349 </p>"
1350 .to_string()
1351 }
1352
1353 #[crate::ctb_test]
1354 fn test_examples() {
1355 let opt: Option<&ClipOptions> = None;
1356 assert_eq!(clip("foo", 3, opt), "foo");
1357 assert_eq!(clip("foo", 2, opt), "f…");
1358 assert_eq!(clip("foo bar", 5, opt), "foo…");
1359 assert_eq!(clip("foo\nbar", 5, opt), "foo…");
1360 }
1361
1362 #[crate::ctb_test]
1363 fn test_basic_html() {
1364 let options: Option<&ClipOptions> = None;
1365
1366 assert_eq!(clip("<p>Lorum ipsum</p>", 5, options), "<p>Loru…</p>");
1367 assert_eq!(
1368 clip("<p><i>Lorum</i> <i>ipsum</i></p>", 5, options),
1369 "<p><i>Loru…</i></p>"
1370 );
1371 assert_eq!(
1372 clip("<p><i>Lorum</i> <i>ipsum</i></p>", 6, options),
1373 "<p><i>Lorum</i>…</p>"
1374 );
1375 assert_eq!(
1376 clip("<p><i>Lorum</i> <i>ipsum</i></p>", 7, options),
1377 "<p><i>Lorum</i>…</p>"
1378 );
1379 assert_eq!(
1380 clip("<p><i>Lorum</i>\n<i>ipsum</i></p>", 5, options),
1381 "<p><i>Lorum</i>…</p>"
1382 );
1383 assert_eq!(
1384 clip("<p><i>Lorum</i><br><i>ipsum</i></p>", 5, options),
1385 "<p><i>Lorum</i>…</p>"
1386 );
1387
1388 assert_eq!(
1389 clip("<p><i>Lorum</i></p>", 5, options),
1390 "<p><i>Lorum</i></p>"
1391 );
1392
1393 assert_eq!(
1394 clip("<p><i>Lorum</i>a</p>", 5, options),
1395 "<p><i>Loru…</i></p>"
1396 );
1397 assert_eq!(
1398 clip("<p><i>Lorum</i></p>a", 5, options),
1399 "<p><i>Loru…</i></p>"
1400 );
1401 assert_eq!(
1402 clip("<p><i>Lorum</i>a</p>", 6, options),
1403 "<p><i>Lorum</i>a</p>"
1404 );
1405 assert_eq!(
1406 clip("<p><i>Lorum</i></p>a", 6, options),
1407 "<p><i>Lorum</i></p>a"
1408 );
1409 assert_eq!(
1410 clip("<p><i>Lorum</i>aA</p>", 6, options),
1411 "<p><i>Lorum</i>…</p>"
1412 );
1413 assert_eq!(
1414 clip("<p><i>Lorum</i></p>aA", 6, options),
1415 "<p><i>Lorum</i></p>…"
1416 );
1417 assert_eq!(
1418 clip("<p><i>Lorum</i>a</p>", 7, options),
1419 "<p><i>Lorum</i>a</p>"
1420 );
1421 assert_eq!(
1422 clip("<p><i>Lorum</i></p>a", 7, options),
1423 "<p><i>Lorum</i></p>a"
1424 );
1425 assert_eq!(
1426 clip("<p><i>Lorum</i>aA</p>", 7, options),
1427 "<p><i>Lorum</i>aA</p>"
1428 );
1429 assert_eq!(
1430 clip("<p><i>Lorum</i></p>aA", 7, options),
1431 "<p><i>Lorum</i></p>aA"
1432 );
1433
1434 assert_eq!(
1435 clip("<p><i>Lorum</i> </p>", 5, options),
1436 "<p><i>Loru…</i></p>"
1437 );
1438 assert_eq!(
1439 clip("<p><i>Lorum</i></p> ", 5, options),
1440 "<p><i>Loru…</i></p>"
1441 );
1442 assert_eq!(
1443 clip("<p><i>Lorum</i> </p>", 6, options),
1444 "<p><i>Lorum</i> </p>"
1445 );
1446 assert_eq!(
1447 clip("<p><i>Lorum</i></p> ", 6, options),
1448 "<p><i>Lorum</i></p> "
1449 );
1450 assert_eq!(
1451 clip("<p><i>Lorum</i> </p>", 6, options),
1452 "<p><i>Lorum</i></p>"
1453 );
1454 assert_eq!(
1455 clip("<p><i>Lorum</i></p> ", 6, options),
1456 "<p><i>Lorum</i></p>"
1457 );
1458 assert_eq!(
1459 clip("<p><i>Lorum</i> </p>", 7, options),
1460 "<p><i>Lorum</i> </p>"
1461 );
1462 assert_eq!(
1463 clip("<p><i>Lorum</i></p> ", 7, options),
1464 "<p><i>Lorum</i></p> "
1465 );
1466 assert_eq!(
1467 clip("<p><i>Lorum</i> </p>", 7, options),
1468 "<p><i>Lorum</i> </p>"
1469 );
1470 assert_eq!(
1471 clip("<p><i>Lorum</i></p> ", 7, options),
1472 "<p><i>Lorum</i></p> "
1473 );
1474
1475 assert_eq!(clip("Lo<ins>rum</ins>", 4, options), "Lo<ins>r…</ins>");
1476 assert_eq!(clip("Lo<del>rum</del>", 4, options), "Lo<del>r…</del>");
1477
1478 assert_eq!(
1479 clip(
1480 "<a href=\"http://just-a-link.com\">Just a link</a>",
1481 8,
1482 options
1483 ),
1484 "<a href=\"http://just-a-link.com\">Just a…</a>"
1485 );
1486
1487 assert_eq!(
1488 clip(
1489 "<a href=\"http://just-a-link.com\">Just a link</a>, yo",
1490 13,
1491 options
1492 ),
1493 "<a href=\"http://just-a-link.com\">Just a link</a>,…"
1494 );
1495 }
1496
1497 #[crate::ctb_test]
1498 fn test_html_comments() {
1499 let options: Option<&ClipOptions> = None;
1500
1501 assert_eq!(
1502 clip("<b><!-- this is bold -->bold</b>", 4, options),
1503 "<b><!-- this is bold -->bold</b>"
1504 );
1505
1506 assert_eq!(
1507 clip("<b><!-- this is bold -->bold</b>", 3, options),
1508 "<b><!-- this is bold -->bo…</b>"
1509 );
1510 }
1511
1512 #[crate::ctb_test]
1513 fn test_special_characters_in_attribute_values() {
1514 let options: Option<&ClipOptions> = None;
1515
1516 assert_eq!(
1517 clip("<b class=\"<i>\">bold</b>", 4, options),
1518 "<b class=\"<i>\">bold</b>"
1519 );
1520 assert_eq!(
1521 clip("<b class=\"<i>\">bold</b>", 3, options),
1522 "<b class=\"<i>\">bo…</b>"
1523 );
1524 assert_eq!(
1525 clip("<b class=\"'test'\">bold</b>", 4, options),
1526 "<b class=\"'test'\">bold</b>"
1527 );
1528 assert_eq!(
1529 clip("<b class=\"'test'\">bold</b>", 3, options),
1530 "<b class=\"'test'\">bo…</b>"
1531 );
1532
1533 assert_eq!(
1534 clip("<b class='javascript:alert(\"hoi\");'>bold</b>", 4, options),
1535 "<b class='javascript:alert(\"hoi\");'>bold</b>"
1536 );
1537
1538 assert_eq!(
1539 clip("<b class='javascript:alert(\"hoi\");'>bold</b>", 3, options),
1540 "<b class='javascript:alert(\"hoi\");'>bo…</b>"
1541 );
1542 }
1543
1544 #[crate::ctb_test]
1545 fn test_embedded_svg() {
1546 let options = Some(&html_options(ClipHtmlOptions {
1547 common: CommonClipOptions {
1548 html: Some(true),
1549 ..Default::default()
1550 },
1551 image_weight: Some(5),
1552 ..Default::default()
1553 }));
1554
1555 let input = get_html_with_svg();
1556
1557 let expected = "<p>\
1559 <svg width=\"100%\" height=\"100%\" viewBox=\"0 0 100 100\" \
1560 xmlns=\"http://www.w3.org/2000/svg\">\n\
1561 <style>\n\
1562 /* <![CDATA[ */\n\
1563 circle {\n\
1564 fill: orange;\n\
1565 stroke: black;\n\
1566 stroke-width: 10px; \
1567 // Note that the value of a pixel depend on the viewBox\n\
1568 }\n\
1569 /* ]]> */\n\
1570 </style>\n\
1571 \n\
1572 <circle cx=\"50\" cy=\"50\" r=\"40\" />\n\
1573 </svg>test\
1574 </p>"
1575 .to_string();
1576
1577 assert_eq!(clip(&input.clone(), 9, options), expected);
1580 }
1581
1582 #[crate::ctb_test]
1583 fn test_unicode_surrogate_pairs() {
1584 let options: Option<&ClipOptions> = None;
1585
1586 assert_eq!(clip("Lorum 𝌆", 7, options), "Lorum 𝌆");
1587 assert_eq!(clip("𝌆𝌆𝌆𝌆", 4, options), "𝌆𝌆𝌆𝌆");
1588 assert_eq!(clip("𝌆𝌆𝌆𝌆", 3, options), "𝌆𝌆…");
1589 assert_eq!(clip("😔🙏👙😃🏧", 6, options), "😔🙏👙😃🏧");
1590 assert_eq!(clip("😔🙏👙😃🏧", 5, options), "😔🙏👙😃🏧");
1591 assert_eq!(clip("😔🙏👙😃🏧", 4, options), "😔🙏👙…");
1592 assert_eq!(clip("😔🙏👙😃🏧", 3, options), "😔🙏…");
1593 }
1594
1595 #[crate::ctb_test]
1596 fn test_plain_text() {
1597 let options: Option<&ClipOptions> = None;
1598
1599 assert_eq!(clip("Lorum ipsum", 5, options), "Loru…");
1600 assert_eq!(clip("Lorum ipsum", 6, options), "Lorum…");
1601 assert_eq!(clip("Lorum ipsum", 7, options), "Lorum…");
1602 assert_eq!(clip("Lorum ipsum", 8, options), "Lorum…");
1603 assert_eq!(clip("Lorum ipsum", 9, options), "Lorum…");
1604 assert_eq!(clip("Lorum ipsum", 10, options), "Lorum…");
1605 assert_eq!(clip("Lorum ipsum", 11, options), "Lorum ipsum");
1606
1607 assert_eq!(clip("Lorum\nipsum", 10, options), "Lorum…");
1608
1609 assert_eq!(clip("Lorum i", 7, options), "Lorum i");
1610 assert_eq!(clip("Lorum …", 7, options), "Lorum …");
1611 }
1612
1613 #[crate::ctb_test]
1614 fn test_word_breaking() {
1615 let options = Some(&plain_options(CommonClipOptions {
1616 break_words: Some(true),
1617 ..Default::default()
1618 }));
1619
1620 assert_eq!(clip("Lorum ipsum", 5, options), "Loru…");
1621 assert_eq!(clip("Lorum ipsum", 6, options), "Lorum…");
1622 assert_eq!(clip("Lorum ipsum", 7, options), "Lorum…");
1623 assert_eq!(clip("Lorum ipsum", 8, options), "Lorum i…");
1624 assert_eq!(clip("Lorum ipsum", 9, options), "Lorum ip…");
1625 assert_eq!(clip("Lorum ipsum", 10, options), "Lorum ips…");
1626 assert_eq!(clip("Lorum ipsum", 11, options), "Lorum ipsum");
1627 }
1628
1629 #[crate::ctb_test]
1630 fn test_word_breaking_without_indicator() {
1631 let options = Some(&plain_options(CommonClipOptions {
1632 break_words: Some(true),
1633 indicator: Some(String::new()),
1634 ..Default::default()
1635 }));
1636 assert_eq!(clip("Lorum ipsum", 5, options), "Lorum");
1637 assert_eq!(clip("Lorum ipsum", 6, options), "Lorum");
1638 assert_eq!(clip("Lorum ipsum", 7, options), "Lorum i");
1639 assert_eq!(clip("Lorum ipsum", 8, options), "Lorum ip");
1640 assert_eq!(clip("Lorum ipsum", 9, options), "Lorum ips");
1641 assert_eq!(clip("Lorum ipsum", 10, options), "Lorum ipsu");
1642 assert_eq!(clip("Lorum ipsum", 11, options), "Lorum ipsum");
1643 }
1644
1645 #[crate::ctb_test]
1646 fn test_max_lines() {
1647 let options_2 = Some(&plain_options(CommonClipOptions {
1648 max_lines: Some(2),
1649 ..Default::default()
1650 }));
1651 let options_1 = Some(&plain_options(CommonClipOptions {
1652 max_lines: Some(1),
1653 ..Default::default()
1654 }));
1655
1656 assert_eq!(clip("Lorum\nipsum", 100, options_2), "Lorum\nipsum");
1657 assert_eq!(clip("Lorum\nipsum", 100, options_1), "Lorum…");
1658 assert_eq!(clip("Lorum\n\nipsum", 100, options_2), "Lorum…");
1659
1660 assert_eq!(clip("Lorum\nipsum\n", 100, options_2), "Lorum\nipsum");
1662 assert_eq!(clip("Lorum\nipsum\n\n", 100, options_2), "Lorum\nipsum");
1663
1664 let html_2 = Some(&html_options(ClipHtmlOptions {
1665 common: CommonClipOptions {
1666 html: Some(true),
1667 max_lines: Some(2),
1668 ..Default::default()
1669 },
1670 ..Default::default()
1671 }));
1672 let html_1 = Some(&html_options(ClipHtmlOptions {
1673 common: CommonClipOptions {
1674 html: Some(true),
1675 max_lines: Some(1),
1676 ..Default::default()
1677 },
1678 ..Default::default()
1679 }));
1680
1681 assert_eq!(
1682 clip("<p>Lorem ipsum</p><p>Lorem ipsum</p>", 100, html_2),
1683 "<p>Lorem ipsum</p><p>Lorem ipsum</p>"
1684 );
1685 assert_eq!(
1686 clip("<p>Lorem ipsum</p><p>Lorem ipsum</p>", 100, html_1),
1687 "<p>Lorem ipsum…</p>"
1688 );
1689 assert_eq!(
1690 clip("<div>Lorem ipsum</div><div>Lorem ipsum</div>", 100, html_2),
1691 "<div>Lorem ipsum</div><div>Lorem ipsum</div>"
1692 );
1693 assert_eq!(
1694 clip("<div>Lorem ipsum</div><div>Lorem ipsum</div>", 100, html_1),
1695 "<div>Lorem ipsum…</div>"
1696 );
1697 }
1698
1699 #[crate::ctb_test]
1700 fn test_odd_html() {
1701 let options: Option<&ClipOptions> = None;
1702
1703 assert_eq!(clip("<p>foo > bar</p>", 9, options), "<p>foo > bar</p>");
1704 assert_eq!(
1705 clip("<p><i>Lorum>>></i> <i>ipsum</i></p>", 7, options),
1706 "<p><i>Lorum>…</i></p>"
1707 );
1708 }
1709
1710 #[crate::ctb_test]
1711 fn test_ampersand() {
1712 let options: Option<&ClipOptions> = None;
1713
1714 assert_eq!(clip("&", 1, options), "&");
1715 assert_eq!(clip("&", 2, options), "&");
1716 assert_eq!(clip("<", 1, options), "<");
1717 assert_eq!(clip("<", 2, options), "<");
1718 assert_eq!(clip("&", 1, options), "&");
1719 assert_eq!(clip("&", 2, options), "&");
1720 assert_eq!(clip("<p>&</p>", 1, options), "…");
1721 assert_eq!(clip("<p>&</p>", 2, options), "<p>&</p>");
1722 assert_eq!(clip("<p><</p>", 1, options), "…");
1723 assert_eq!(clip("<p><</p>", 2, options), "<p><</p>");
1724 assert_eq!(clip("<p>&</p>", 1, options), "…");
1725 assert_eq!(clip("<p>&</p>", 2, options), "<p>&</p>");
1726
1727 assert_eq!(clip("foo & bar", 5, options), "foo…");
1728 assert_eq!(clip("foo & bar", 9, options), "foo & bar");
1729 assert_eq!(clip("foo&<i>bar</i>", 5, options), "foo&…");
1730 assert_eq!(clip("foo&<i>bar</i>", 7, options), "foo&<i>bar</i>");
1731 assert_eq!(clip("foo&&& bar", 5, options), "foo&…");
1732 assert_eq!(clip("foo&&& bar", 10, options), "foo&&& bar");
1733
1734 assert_eq!(
1735 clip(
1736 "<a href=\"http://example.com/?x=1&y=2\">foo</a>",
1737 3,
1738 options
1739 ),
1740 "<a href=\"http://example.com/?x=1&y=2\">foo</a>"
1741 );
1742 assert_eq!(clip("&123", 4, options), "&123");
1743 assert_eq!(clip("&abc", 4, options), "&abc");
1744 assert_eq!(clip("foo &0 bar", 10, options), "foo &0 bar");
1745 assert_eq!(clip("foo &lolwat bar", 15, options), "foo &lolwat bar");
1746 }
1747
1748 #[crate::ctb_test]
1749 fn test_ampersand_without_indicator() {
1750 let options = Some(&html_options(ClipHtmlOptions {
1751 common: CommonClipOptions {
1752 html: Some(true),
1753 indicator: Some(String::new()),
1754 ..Default::default()
1755 },
1756 ..Default::default()
1757 }));
1758
1759 assert_eq!(clip("&", 1, options), "&");
1760 assert_eq!(clip("&", 2, options), "&");
1761 assert_eq!(clip("<", 1, options), "<");
1762 assert_eq!(clip("<", 2, options), "<");
1763 assert_eq!(clip("&", 1, options), "&");
1764 assert_eq!(clip("&", 2, options), "&");
1765 assert_eq!(clip("<p>&</p>", 1, options), "<p>&</p>");
1766 assert_eq!(clip("<p>&</p>", 2, options), "<p>&</p>");
1767 assert_eq!(clip("<p><</p>", 1, options), "<p><</p>");
1768 assert_eq!(clip("<p><</p>", 2, options), "<p><</p>");
1769 assert_eq!(clip("<p>&</p>", 1, options), "<p>&</p>");
1770 assert_eq!(clip("<p>&</p>", 2, options), "<p>&</p>");
1771
1772 assert_eq!(clip("foo & bar", 5, options), "foo &");
1773 assert_eq!(clip("foo & bar", 9, options), "foo & bar");
1774 assert_eq!(clip("foo&<i>bar</i>", 5, options), "foo&<i>b</i>");
1777 assert_eq!(clip("foo&<i>bar</i>", 7, options), "foo&<i>bar</i>");
1778 assert_eq!(clip("foo&&& bar", 5, options), "foo&&");
1779 assert_eq!(clip("foo&&& bar", 10, options), "foo&&& bar");
1780
1781 assert_eq!(
1782 clip(
1783 "<a href=\"http://example.com/?x=1&y=2\">foo</a>",
1784 3,
1785 options
1786 ),
1787 "<a href=\"http://example.com/?x=1&y=2\">foo</a>"
1788 );
1789 assert_eq!(clip("&123", 4, options), "&123");
1790 assert_eq!(clip("&abc", 4, options), "&abc");
1791 assert_eq!(clip("foo &0 bar", 10, options), "foo &0 bar");
1792 assert_eq!(clip("foo &lolwat bar", 15, options), "foo &lolwat bar");
1793 }
1794
1795 #[crate::ctb_test]
1796 fn test_ampersand_without_indicator_and_break_words() {
1797 let options = Some(&html_options(ClipHtmlOptions {
1798 common: CommonClipOptions {
1799 break_words: Some(true),
1800 html: Some(true),
1801 indicator: Some(String::new()),
1802 ..Default::default()
1803 },
1804 ..Default::default()
1805 }));
1806
1807 assert_eq!(clip("&", 1, options), "&");
1808 assert_eq!(clip("&", 2, options), "&");
1809 assert_eq!(clip("<", 1, options), "<");
1810 assert_eq!(clip("<", 2, options), "<");
1811 assert_eq!(clip("&", 1, options), "&");
1812 assert_eq!(clip("&", 2, options), "&");
1813 assert_eq!(clip("<p>&</p>", 1, options), "<p>&</p>");
1814 assert_eq!(clip("<p>&</p>", 2, options), "<p>&</p>");
1815 assert_eq!(clip("<p><</p>", 1, options), "<p><</p>");
1816 assert_eq!(clip("<p><</p>", 2, options), "<p><</p>");
1817 assert_eq!(clip("<p>&</p>", 1, options), "<p>&</p>");
1818 assert_eq!(clip("<p>&</p>", 2, options), "<p>&</p>");
1819
1820 assert_eq!(clip("foo & bar", 5, options), "foo &");
1821 assert_eq!(clip("foo & bar", 9, options), "foo & bar");
1822 assert_eq!(clip("foo&<i>bar</i>", 5, options), "foo&<i>b</i>");
1823 assert_eq!(clip("foo&<i>bar</i>", 7, options), "foo&<i>bar</i>");
1824 assert_eq!(clip("foo&&& bar", 5, options), "foo&&");
1825 assert_eq!(clip("foo&&& bar", 10, options), "foo&&& bar");
1826
1827 assert_eq!(
1828 clip(
1829 "<a href=\"http://example.com/?x=1&y=2\">foo</a>",
1830 3,
1831 options
1832 ),
1833 "<a href=\"http://example.com/?x=1&y=2\">foo</a>"
1834 );
1835 assert_eq!(clip("&123", 4, options), "&123");
1836 assert_eq!(clip("&abc", 4, options), "&abc");
1837 assert_eq!(clip("foo &0 bar", 10, options), "foo &0 bar");
1838 assert_eq!(clip("foo &lolwat bar", 15, options), "foo &lolwat bar");
1839 }
1840
1841 #[crate::ctb_test]
1842 fn test_edge_cases() {
1843 let options = Some(&html_options(ClipHtmlOptions {
1844 common: CommonClipOptions {
1845 break_words: Some(true),
1846 html: Some(true),
1847 indicator: Some("...".to_string()),
1848 ..Default::default()
1849 },
1850 ..Default::default()
1851 }));
1852
1853 assert_eq!(
1854 clip(
1855 "one <a href=\"#\">two - three <br>four</a> five",
1856 0,
1857 options
1858 ),
1859 "..."
1860 );
1861 assert_eq!(
1862 clip(
1863 "<p>one <a href=\"#\">two - three <br>four</a> five</p>",
1864 0,
1865 options
1866 ),
1867 "..."
1868 );
1869 assert_eq!(
1870 clip(
1871 "<p>one <a href=\"#\">two - three <br>four</a> five</p>",
1872 6,
1873 options
1874 ),
1875 "<p>one...</p>"
1876 );
1877 }
1878
1879 #[crate::ctb_test]
1880 fn test_upstream_issue_12_split_tables() {
1881 let html = get_html_table();
1882
1883 let options_26 = Some(&html_options(ClipHtmlOptions {
1884 common: CommonClipOptions {
1885 html: Some(true),
1886 break_words: Some(true),
1887 ..Default::default()
1888 },
1889 ..Default::default()
1890 }));
1891 let options_25 = Some(&html_options(ClipHtmlOptions {
1892 common: CommonClipOptions {
1893 html: Some(true),
1894 break_words: Some(true),
1895 ..Default::default()
1896 },
1897 ..Default::default()
1898 }));
1899 let options_25_2 = Some(&html_options(ClipHtmlOptions {
1900 common: CommonClipOptions {
1901 html: Some(true),
1902 break_words: Some(true),
1903 max_lines: Some(2),
1904 ..Default::default()
1905 },
1906 ..Default::default()
1907 }));
1908
1909 assert_eq!(
1910 clip(&html.clone(), 26, options_26),
1911 r#"<table border="1" cellpadding="1" cellspacing="1" style="width: 500px">
1912 <tbody>
1913 <tr>
1914 <td>fb</td>
1915 <td>fbfbfb</td>
1916 </tr>
1917 <tr>
1918 <td>google</td>
1919 <td>twitter</td>
1920 </tr>
1921 <tr>
1922 <td>intel</td>…</tr></tbody></table>"#
1923 .to_string()
1924 );
1925
1926 assert_eq!(
1927 clip(&html.clone(), 25, options_25),
1928 r#"<table border="1" cellpadding="1" cellspacing="1" style="width: 500px">
1929 <tbody>
1930 <tr>
1931 <td>fb</td>
1932 <td>fbfbfb</td>
1933 </tr>
1934 <tr>
1935 <td>google</td>
1936 <td>twitter</td>
1937 </tr>
1938 <tr>
1939 <td>int…</td></tr></tbody></table>"#
1940 .to_string()
1941 );
1942
1943 assert_eq!(
1944 clip(&html, 25, options_25_2),
1945 r#"<table border="1" cellpadding="1" cellspacing="1" style="width: 500px">
1946 <tbody>
1947 <tr>
1948 <td>fb</td>
1949 <td>fbfbfb</td>
1950 </tr>
1951 <tr>
1952 <td>google</td>
1953 <td>twitter</td>…</tr></tbody></table>"#
1954 .to_string()
1955 );
1956 }
1957
1958 #[crate::ctb_test]
1959 fn test_strip_tags() {
1960 let html_with_image =
1962 "<p>Image <img alt=\"blup\" src=\"#\"> and such</p>";
1963
1964 let options_strip_none = Some(&html_options(ClipHtmlOptions {
1965 strip_tags: Some(StripTags::Tags(Vec::new())),
1966 ..Default::default()
1967 }));
1968 let options_default: Option<&ClipOptions> = None;
1969 assert_eq!(
1970 clip(html_with_image, 12, options_strip_none),
1971 clip(html_with_image, 12, options_default)
1972 );
1973
1974 let options_strip_img = Some(&html_options(ClipHtmlOptions {
1975 strip_tags: Some(StripTags::Tags(vec!["img".to_string()])),
1976 ..Default::default()
1977 }));
1978 assert_eq!(
1979 clip(html_with_image, 12, options_strip_img),
1980 "<p>Image and…</p>"
1981 );
1982
1983 let options_strip_img_p = Some(&html_options(ClipHtmlOptions {
1984 strip_tags: Some(StripTags::Tags(vec![
1985 "img".to_string(),
1986 "p".to_string(),
1987 ])),
1988 ..Default::default()
1989 }));
1990 assert_eq!(
1991 clip(html_with_image, 12, options_strip_img_p),
1992 "Image and…"
1993 );
1994
1995 let options_strip_all = Some(&html_options(ClipHtmlOptions {
1996 strip_tags: Some(StripTags::All(true)),
1997 ..Default::default()
1998 }));
1999 assert_eq!(clip(html_with_image, 12, options_strip_all), "Image and…");
2000
2001 assert_eq!(
2002 clip(html_with_image, 15, options_strip_img),
2003 "<p>Image and such</p>"
2004 );
2005
2006 let html_with_link = "<a href=\"http://example.com/?x=1&y=2\">foo</a>";
2008 let options_strip_a = Some(&html_options(ClipHtmlOptions {
2009 strip_tags: Some(StripTags::Tags(vec!["a".to_string()])),
2010 ..Default::default()
2011 }));
2012 assert_eq!(clip(html_with_link, 3, options_strip_a), "foo");
2013
2014 let options_strip_b = Some(&html_options(ClipHtmlOptions {
2015 strip_tags: Some(StripTags::Tags(vec!["b".to_string()])),
2016 ..Default::default()
2017 }));
2018 assert_eq!(clip(html_with_link, 3, options_strip_b), html_with_link);
2019
2020 let html_with_table = r#"hello <table border="1" cellpadding="1" cellspacing="1" style="width: 500px">
2022 <tbody>
2023 <tr>
2024 <td>fb</td>
2025 <td>fbfbfb</td>
2026 </tr>
2027 <tr>
2028 <td>google</td>
2029 <td>twitter</td>
2030 </tr>
2031 <tr>
2032 <td>intel</td>
2033 <td>amazon</td>
2034 </tr>
2035 </tbody>
2036</table> world"#;
2037
2038 assert_eq!(clip(html_with_table, 10, options_strip_all), "hello fb…");
2039 assert_eq!(
2040 clip(html_with_table, 16, options_strip_all),
2041 "hello fb fbfbfb…"
2042 );
2043 assert_eq!(
2044 clip(html_with_table, 24, options_strip_all),
2045 "hello fb fbfbfb google…"
2046 );
2047
2048 let html_with_svg = get_html_with_svg();
2050 let options_strip_svg = Some(&html_options(ClipHtmlOptions {
2051 strip_tags: Some(StripTags::Tags(vec!["svg".to_string()])),
2052 ..Default::default()
2053 }));
2054 assert_eq!(
2055 clip(&html_with_svg.clone(), 3, options_strip_svg),
2056 "<p>te…</p>"
2057 );
2058 assert_eq!(clip(&html_with_svg, 4, options_strip_svg), "<p>test</p>");
2059 }
2060
2061 #[crate::ctb_test]
2062 fn test_disable_indicator_at_line_break() {
2063 let base = ClipHtmlOptions {
2064 common: CommonClipOptions {
2065 html: Some(true),
2066 insert_indicator_at_linebreak: Some(false),
2067 ..Default::default()
2068 },
2069 ..Default::default()
2070 };
2071
2072 let options = Some(&html_options(base.clone()));
2073 assert_eq!(clip("Lorum\nipsum", 10, options), "Lorum");
2074
2075 let options_max_lines_1 = Some(&html_options(ClipHtmlOptions {
2076 common: CommonClipOptions {
2077 max_lines: Some(1),
2078 ..base.common.clone()
2079 },
2080 ..base.clone()
2081 }));
2082 assert_eq!(clip("Lorum\nipsum", 100, options_max_lines_1), "Lorum");
2083
2084 assert_eq!(clip("Lorum<br/>ipsum", 100, options_max_lines_1), "Lorum");
2085
2086 let options_max_lines_2 = Some(&html_options(ClipHtmlOptions {
2087 common: CommonClipOptions {
2088 max_lines: Some(2),
2089 ..base.common.clone()
2090 },
2091 ..base.clone()
2092 }));
2093 assert_eq!(clip("Lorum\n\nipsum", 100, options_max_lines_2), "Lorum");
2094
2095 assert_eq!(clip("<p>&</p>", 1, options), "");
2096 assert_eq!(clip("<p><</p>", 1, options), "");
2097
2098 assert_eq!(
2099 clip(
2100 "<p>Lorem ipsum</p><p>Lorem ipsum</p>",
2101 100,
2102 options_max_lines_1
2103 ),
2104 "<p>Lorem ipsum</p>"
2105 );
2106
2107 let options_indicator_ellipsis = Some(&html_options(ClipHtmlOptions {
2108 common: CommonClipOptions {
2109 indicator: Some("...".to_string()),
2110 ..base.common.clone()
2111 },
2112 ..base.clone()
2113 }));
2114 assert_eq!(
2115 clip(
2116 "<p>one <a href=\"#\">two - three <br>four</a> five</p>",
2117 0,
2118 options_indicator_ellipsis
2119 ),
2120 ""
2121 );
2122
2123 let options_break_words = Some(&html_options(ClipHtmlOptions {
2124 common: CommonClipOptions {
2125 break_words: Some(true),
2126 ..base.common.clone()
2127 },
2128 ..base.clone()
2129 }));
2130 assert_eq!(
2131 clip(&get_html_table(), 26, options_break_words),
2132 r#"<table border="1" cellpadding="1" cellspacing="1" style="width: 500px">
2133 <tbody>
2134 <tr>
2135 <td>fb</td>
2136 <td>fbfbfb</td>
2137 </tr>
2138 <tr>
2139 <td>google</td>
2140 <td>twitter</td>
2141 </tr>
2142 <tr>
2143 <td>intel</td></tr></tbody></table>"#
2144 .to_string()
2145 );
2146
2147 let options_strip_tags_svg = Some(&html_options(ClipHtmlOptions {
2148 common: base.common.clone(),
2149 strip_tags: Some(StripTags::Tags(vec!["svg".to_string()])),
2150 ..base
2151 }));
2152 assert_eq!(
2153 clip(&get_html_with_svg(), 4, options_strip_tags_svg),
2154 "<p>test</p>"
2155 );
2156 }
2157
2158 #[crate::ctb_test]
2162 fn test_text_basics() {
2163 let options: Option<&ClipOptions> = None;
2164
2165 assert_eq!(clip("Lorum ipsum", 5, options), "Loru…");
2166 assert_eq!(clip("Lorum ipsum", 6, options), "Lorum…");
2167 assert_eq!(clip("Lorum ipsum", 7, options), "Lorum…");
2168 assert_eq!(clip("Lorum ipsum", 8, options), "Lorum…");
2169 assert_eq!(clip("Lorum ipsum", 9, options), "Lorum…");
2170 assert_eq!(clip("Lorum ipsum", 10, options), "Lorum…");
2171 assert_eq!(clip("Lorum ipsum", 11, options), "Lorum ipsum");
2172
2173 assert_eq!(clip("Lorum\nipsum", 10, options), "Lorum…");
2174
2175 assert_eq!(clip("Lorum i", 7, options), "Lorum i");
2176 assert_eq!(clip("Lorum …", 7, options), "Lorum …");
2177 }
2178
2179 #[crate::ctb_test]
2180 fn test_text_unicode_surrogate_pairs() {
2181 let options: Option<&ClipOptions> = None;
2182
2183 assert_eq!(clip("Lorum 𝌆", 7, options), "Lorum 𝌆");
2184 assert_eq!(clip("𝌆𝌆𝌆𝌆", 4, options), "𝌆𝌆𝌆𝌆");
2185 assert_eq!(clip("𝌆𝌆𝌆𝌆", 3, options), "𝌆𝌆…");
2186 assert_eq!(clip("😔🙏👙😃🏧", 6, options), "😔🙏👙😃🏧");
2187 assert_eq!(clip("😔🙏👙😃🏧", 5, options), "😔🙏👙😃🏧");
2188 assert_eq!(clip("😔🙏👙😃🏧", 4, options), "😔🙏👙…");
2189 assert_eq!(clip("😔🙏👙😃🏧", 3, options), "😔🙏…");
2190 }
2191
2192 #[crate::ctb_test]
2193 fn test_text_word_breaking() {
2194 let options = Some(&plain_options(CommonClipOptions {
2195 break_words: Some(true),
2196 ..Default::default()
2197 }));
2198
2199 assert_eq!(clip("Lorum ipsum", 5, options), "Loru…");
2200 assert_eq!(clip("Lorum ipsum", 6, options), "Lorum…");
2201 assert_eq!(clip("Lorum ipsum", 7, options), "Lorum…");
2202 assert_eq!(clip("Lorum ipsum", 8, options), "Lorum i…");
2203 assert_eq!(clip("Lorum ipsum", 9, options), "Lorum ip…");
2204 assert_eq!(clip("Lorum ipsum", 10, options), "Lorum ips…");
2205 assert_eq!(clip("Lorum ipsum", 11, options), "Lorum ipsum");
2206 }
2207
2208 #[crate::ctb_test]
2209 fn test_text_word_breaking_without_indicator() {
2210 let options = Some(&plain_options(CommonClipOptions {
2211 break_words: Some(true),
2212 indicator: Some(String::new()),
2213 ..Default::default()
2214 }));
2215 assert_eq!(clip("Lorum ipsum", 5, options), "Lorum");
2216 assert_eq!(clip("Lorum ipsum", 6, options), "Lorum");
2217 assert_eq!(clip("Lorum ipsum", 7, options), "Lorum i");
2218 assert_eq!(clip("Lorum ipsum", 8, options), "Lorum ip");
2219 assert_eq!(clip("Lorum ipsum", 9, options), "Lorum ips");
2220 assert_eq!(clip("Lorum ipsum", 10, options), "Lorum ipsu");
2221 assert_eq!(clip("Lorum ipsum", 11, options), "Lorum ipsum");
2222 }
2223
2224 #[crate::ctb_test]
2225 fn test_text_max_lines() {
2226 let options_2 = Some(&plain_options(CommonClipOptions {
2227 max_lines: Some(2),
2228 ..Default::default()
2229 }));
2230 let options_1 = Some(&plain_options(CommonClipOptions {
2231 max_lines: Some(1),
2232 ..Default::default()
2233 }));
2234
2235 assert_eq!(clip("Lorum\nipsum", 100, options_2), "Lorum\nipsum");
2236 assert_eq!(clip("Lorum\nipsum", 100, options_1), "Lorum…");
2237 assert_eq!(clip("Lorum\n\nipsum", 100, options_2), "Lorum…");
2238
2239 assert_eq!(clip("Lorum\nipsum\n", 100, options_2), "Lorum\nipsum");
2240 assert_eq!(clip("Lorum\nipsum\n\n", 100, options_2), "Lorum\nipsum");
2241 }
2242
2243 #[crate::ctb_test]
2244 fn test_text_disable_indicator_at_line_break() {
2245 let options = Some(&plain_options(CommonClipOptions {
2246 insert_indicator_at_linebreak: Some(false),
2247 ..Default::default()
2248 }));
2249
2250 assert_eq!(clip("Lorum\nipsum", 10, options), "Lorum");
2251
2252 let options_max_lines_1 = Some(&plain_options(CommonClipOptions {
2253 insert_indicator_at_linebreak: Some(false),
2254 max_lines: Some(1),
2255 ..Default::default()
2256 }));
2257 assert_eq!(clip("Lorum\nipsum", 100, options_max_lines_1), "Lorum");
2258
2259 let options_max_lines_2 = Some(&plain_options(CommonClipOptions {
2260 insert_indicator_at_linebreak: Some(false),
2261 max_lines: Some(2),
2262 ..Default::default()
2263 }));
2264 assert_eq!(clip("Lorum\n\nipsum", 100, options_max_lines_2), "Lorum");
2265 }
2266
2267 #[crate::ctb_test]
2268 fn test_text_edge_cases() {
2269 let options = Some(&plain_options(CommonClipOptions {
2270 break_words: Some(true),
2271 indicator: Some("...".to_string()),
2272 ..Default::default()
2273 }));
2274 assert_eq!(clip("one two - three \nfour five", 0, options), "...");
2275 assert_eq!(clip("one two - three \nfour five", 6, options), "one...");
2276 }
2277}
2278
2279