Skip to content

Commit 4266504

Browse files
committed
feat: Don't normailze title text
1 parent a12b7dd commit 4266504

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
@@ -571,8 +571,7 @@ impl Renderer {
571571
} else {
572572
ElementStyle::NoStyle
573573
};
574-
let text = &normalize_whitespace(title);
575-
let lines = text.split('\n').collect::<Vec<_>>();
574+
let lines = title.split('\n').collect::<Vec<_>>();
576575
if lines.len() > 1 {
577576
for (i, line) in lines.iter().enumerate() {
578577
if i != 0 {
@@ -582,7 +581,7 @@ impl Renderer {
582581
buffer.append(line_number, line, style);
583582
}
584583
} else {
585-
buffer.append(line_number, text, style);
584+
buffer.append(line_number, title, style);
586585
}
587586
line_number
588587
}
@@ -2588,7 +2587,7 @@ const OUTPUT_REPLACEMENTS: &[(char, &str)] = &[
25882587
('\u{2069}', "�"),
25892588
];
25902589

2591-
fn normalize_whitespace(s: &str) -> String {
2590+
pub(crate) fn normalize_whitespace(s: &str) -> String {
25922591
// Scan the input string for a character in the ordered table above.
25932592
// If it's present, replace it with its alternative string (it can be more than 1 char!).
25942593
// 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)