Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Support white-space: pre-wrap #175

Merged
merged 4 commits into from
Oct 12, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Cargo fmt.
jugglerchris committed Oct 12, 2024
commit ace8cac56228129cee6e564a3ee374a7fb8f4bf5
8 changes: 3 additions & 5 deletions src/css.rs
Original file line number Diff line number Diff line change
@@ -257,7 +257,7 @@ impl<T: Copy + Clone> Default for WithSpec<T> {
}

#[derive(Debug, Copy, Clone, Default, PartialEq)]
pub (crate) enum WhiteSpace {
pub(crate) enum WhiteSpace {
#[default]
Normal,
// NoWrap,
@@ -271,8 +271,7 @@ impl WhiteSpace {
pub fn preserve_whitespace(&self) -> bool {
match self {
WhiteSpace::Normal => false,
WhiteSpace::Pre |
WhiteSpace::PreWrap => true,
WhiteSpace::Pre | WhiteSpace::PreWrap => true,
}
}
}
@@ -384,8 +383,7 @@ fn styles_from_properties(decls: &[parser::Declaration]) -> Vec<StyleDecl> {
style: Style::WhiteSpace(*value),
importance: decl.important,
});
}
/*
} /*
_ => {
html_trace_quiet!("CSS: Unhandled property {:?}", decl);
}
4 changes: 3 additions & 1 deletion src/css/parser.rs
Original file line number Diff line number Diff line change
@@ -687,7 +687,9 @@ fn parse_display(value: &RawValue) -> Result<Display, nom::Err<nom::error::Error
Ok(Display::Other)
}

fn parse_white_space(value: &RawValue) -> Result<WhiteSpace, nom::Err<nom::error::Error<&'static str>>> {
fn parse_white_space(
value: &RawValue,
) -> Result<WhiteSpace, nom::Err<nom::error::Error<&'static str>>> {
for tok in &value.tokens {
if let Token::Ident(word) = tok {
match word.deref() {
15 changes: 9 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -531,8 +531,7 @@ impl RenderNode {
}

Container(ref v) | Em(ref v) | Strong(ref v) | Strikeout(ref v) | Code(ref v)
| Block(ref v) | Div(ref v) | Dl(ref v) | Dt(ref v) | ListItem(ref v)
| Sup(ref v) => v
| Block(ref v) | Div(ref v) | Dl(ref v) | Dt(ref v) | ListItem(ref v) | Sup(ref v) => v
.iter()
.map(recurse)
.fold(Default::default(), SizeEstimate::add),
@@ -1332,7 +1331,12 @@ fn process_dom_node<'a, T: Write>(
}),
expanded_name!(html "pre") => pending(input, move |_, cs| {
let mut computed = computed;
computed.white_space.maybe_update(false, css::StyleOrigin::Agent, Default::default(), WhiteSpace::Pre);
computed.white_space.maybe_update(
false,
css::StyleOrigin::Agent,
Default::default(),
WhiteSpace::Pre,
);
Ok(Some(RenderNode::new_styled(Block(cs), computed)))
}),
expanded_name!(html "br") => Finished(RenderNode::new_styled(Break, computed)),
@@ -1508,9 +1512,8 @@ impl PushedStyleInfo {
}
if let Some(ws) = style.white_space.val() {
match ws {
WhiteSpace::Normal => {},
WhiteSpace::Pre |
WhiteSpace::PreWrap => {
WhiteSpace::Normal => {}
WhiteSpace::Pre | WhiteSpace::PreWrap => {
render.push_ws(ws);
result.white_space = true;
}
16 changes: 6 additions & 10 deletions src/render/text_renderer.rs
Original file line number Diff line number Diff line change
@@ -570,11 +570,7 @@ impl<T: Clone + Eq + Debug + Default> WrappedBlock<T> {
Ok(())
}

fn add_text_prewrap(
&mut self,
text: &str,
tag: &T,
) -> Result<(), Error> {
fn add_text_prewrap(&mut self, text: &str, tag: &T) -> Result<(), Error> {
html_trace!("WrappedBlock::add_text_prewrap({}), {:?}", text, tag);
for c in text.chars() {
if c.is_whitespace() {
@@ -591,10 +587,7 @@ impl<T: Clone + Eq + Debug + Default> WrappedBlock<T> {
if self.linelen >= self.width {
self.flush_line();
} else {
self.line.push_char(
' ',
tag
);
self.line.push_char(' ', tag);
self.linelen += 1;
at_least_one_space = true;
}
@@ -1258,7 +1251,10 @@ impl<D: TextDecorator> Renderer for SubRenderer<D> {

fn add_inline_text(&mut self, text: &str) -> crate::Result<()> {
html_trace!("add_inline_text({}, {})", self.width, text);
if !self.ws_mode().preserve_whitespace() && self.at_block_end && text.chars().all(char::is_whitespace) {
if !self.ws_mode().preserve_whitespace()
&& self.at_block_end
&& text.chars().all(char::is_whitespace)
{
// Ignore whitespace between blocks.
return Ok(());
}
9 changes: 6 additions & 3 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -2074,7 +2074,9 @@ foo

#[cfg(feature = "css")]
mod css_tests {
use super::{test_html_coloured, test_html_coloured_conf, test_html_conf, test_html_css, test_html_style};
use super::{
test_html_coloured, test_html_coloured_conf, test_html_conf, test_html_css, test_html_style,
};

#[test]
fn test_disp_none() {
@@ -2570,8 +2572,9 @@ c d e
"#,
10,
|conf| {
conf.add_css(r#".prewrap { white-space: pre-wrap; }"#).unwrap()
}
conf.add_css(r#".prewrap { white-space: pre-wrap; }"#)
.unwrap()
},
);
}
}