Skip to content

Commit 19f692d

Browse files
committed
feat: Don't normailze title text
1 parent 481920d commit 19f692d

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

examples/highlight_title.svg

Lines changed: 4 additions & 3 deletions
Loading

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@
4545
pub mod renderer;
4646
mod snippet;
4747

48+
/// Normalize the string to avoid any unicode control characters.
49+
/// This is important for untrusted input, as it can contain
50+
/// invalid unicode sequences.
51+
pub fn normalize_untrusted_str(s: &str) -> String {
52+
renderer::normalize_whitespace(s)
53+
}
54+
4855
#[doc(inline)]
4956
pub use renderer::Renderer;
5057
pub use snippet::ColumnSeparator;

src/renderer/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,7 @@ impl Renderer {
573573
} else {
574574
ElementStyle::NoStyle
575575
};
576-
let text = &normalize_whitespace(title);
577-
let lines = text.split('\n').collect::<Vec<_>>();
576+
let lines = title.split('\n').collect::<Vec<_>>();
578577
if lines.len() > 1 {
579578
for (i, line) in lines.iter().enumerate() {
580579
if i != 0 {
@@ -584,7 +583,7 @@ impl Renderer {
584583
buffer.append(line_number, line, style);
585584
}
586585
} else {
587-
buffer.append(line_number, text, style);
586+
buffer.append(line_number, title, style);
588587
}
589588
line_number
590589
}
@@ -2590,7 +2589,7 @@ const OUTPUT_REPLACEMENTS: &[(char, &str)] = &[
25902589
('\u{2069}', "�"),
25912590
];
25922591

2593-
fn normalize_whitespace(s: &str) -> String {
2592+
pub(crate) fn normalize_whitespace(s: &str) -> String {
25942593
// Scan the input string for a character in the ordered table above.
25952594
// If it's present, replace it with its alternative string (it can be more than 1 char!).
25962595
// Otherwise, retain the input char.

src/snippet.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ impl Level {
177177
}
178178
}
179179

180+
/// Text passed to this function is allowed to be pre-styled, as such all
181+
/// text is considered "trusted input" and has no normalizations applied to
182+
/// it. [`normalize_untrusted_str`](crate::normalize_untrusted_str) can be
183+
/// used to normalize untrusted text before it is passed to this function.
180184
pub fn title(self, title: &str) -> Title<'_> {
181185
Title {
182186
level: self,

0 commit comments

Comments
 (0)