Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Split text trim into start and end #253

Merged
merged 1 commit into from
Feb 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ pub struct Reader<B: BufRead> {
tag_state: TagState,
/// expand empty element into an opening and closing element
expand_empty_elements: bool,
/// trims Text events, skip the element if text is empty
trim_text: bool,
/// trims leading whitespace in Text events, skip the element if text is empty
trim_text_start: bool,
/// trims trailing whitespace in Text events.
trim_text_end: bool,
/// trims trailing whitespaces from markup names in closing tags `</a >`
trim_markup_names_in_closing_tags: bool,
/// check if End nodes match last Start node
Expand Down Expand Up @@ -104,7 +106,8 @@ impl<B: BufRead> Reader<B> {
opened_starts: Vec::new(),
tag_state: TagState::Closed,
expand_empty_elements: false,
trim_text: false,
trim_text_start: false,
trim_text_end: false,
trim_markup_names_in_closing_tags: true,
check_end_names: true,
buf_position: 0,
Expand Down Expand Up @@ -142,7 +145,20 @@ impl<B: BufRead> Reader<B> {
///
/// [`Text`]: events/enum.Event.html#variant.Text
pub fn trim_text(&mut self, val: bool) -> &mut Reader<B> {
self.trim_text = val;
self.trim_text_start = val;
self.trim_text_end = val;
self
}

/// Changes whether whitespace after character data should be removed.
///
/// When set to `true`, trailing whitespace is trimmed in [`Text`] events.
///
/// (`false` by default)
///
/// [`Text`]: events/enum.Event.html#variant.Text
pub fn trim_text_end(&mut self, val: bool) -> &mut Reader<B> {
self.trim_text_end = val;
self
}

Expand Down Expand Up @@ -216,19 +232,23 @@ impl<B: BufRead> Reader<B> {
match read_until(&mut self.reader, b'<', buf, &mut self.buf_position) {
Ok(0) => Ok(Event::Eof),
Ok(_) => {
let (start, len) = if self.trim_text {
match buf.iter().skip(buf_start).position(|&b| !is_whitespace(b)) {
Some(start) => (
buf_start + start,
buf.iter()
.rposition(|&b| !is_whitespace(b))
.map_or_else(|| buf.len(), |p| p + 1),
),
None => return self.read_event(buf),
let (start, len) = (
buf_start + if self.trim_text_start {
match buf.iter().skip(buf_start).position(|&b| !is_whitespace(b)) {
Some(start) => start,
None => return self.read_event(buf),
}
} else {
0
},
if self.trim_text_end {
buf.iter()
.rposition(|&b| !is_whitespace(b))
.map_or_else(|| buf.len(), |p| p + 1)
} else {
buf.len()
}
} else {
(buf_start, buf.len())
};
);
Ok(Event::Text(BytesText::from_escaped(&buf[start..len])))
}
Err(e) => Err(e),
Expand Down