diff --git a/src/servo/text/util.rs b/src/servo/text/util.rs index 90ad98fe001..4456ab81fbb 100644 --- a/src/servo/text/util.rs +++ b/src/servo/text/util.rs @@ -31,7 +31,7 @@ pub fn transform_text(text: &str, mode: CompressionMode) -> ~str { let out_str: ~str = ~""; match mode { CompressNone | DiscardNewline => { - do str::each_char(text) |ch: char| { + for str::each_char(text) |ch: char| { if is_discardable_char(ch, mode) { // TODO: record skipped char } else { @@ -41,14 +41,12 @@ pub fn transform_text(text: &str, mode: CompressionMode) -> ~str { } str::push_char(&out_str, ch); } - - true } }, CompressWhitespace | CompressWhitespaceNewline => { let mut in_whitespace: bool = false; - do str::each_char(text) |ch: char| { + for str::each_char(text) |ch: char| { // TODO: discard newlines between CJK chars let mut next_in_whitespace: bool = match (ch, mode) { // TODO: check for following char that may create @@ -60,7 +58,7 @@ pub fn transform_text(text: &str, mode: CompressionMode) -> ~str { (_, _) => false }; - if next_in_whitespace { + if !next_in_whitespace { if is_always_discardable_char(ch) { // revert whitespace setting, since this char was discarded next_in_whitespace = in_whitespace; @@ -69,18 +67,17 @@ pub fn transform_text(text: &str, mode: CompressionMode) -> ~str { // TODO: record kept char str::push_char(&out_str, ch); } - } else { + } else { /* next_in_whitespace; possibly add a space char */ if in_whitespace { // TODO: record skipped char } else { // TODO: record kept char - str::push_char(&out_str, ch); + str::push_char(&out_str, ' '); } } // save whitespace context for next char in_whitespace = next_in_whitespace; - true - } /* /do str::each_chari */ + } /* /for str::each_char */ } } @@ -128,3 +125,99 @@ pub fn true_type_tag(a: char, b: char, c: char, d: char) -> u32 { fn test_true_type_tag() { assert true_type_tag('c', 'm', 'a', 'p') == 0x_63_6D_61_70_u32; } + +#[test] +fn test_transform_compress_none() { + + let test_strs : ~[~str] = ~[~" foo bar", + ~"foo bar ", + ~"foo\n bar", + ~"foo \nbar", + ~" foo bar \nbaz", + ~"foo bar baz", + ~"foobarbaz\n\n"]; + let mode = CompressNone; + + for uint::range(0, test_strs.len()) |i| { + assert transform_text(test_strs[i], mode) == test_strs[i]; + } +} + +#[test] +fn test_transform_discard_newline() { + + let test_strs : ~[~str] = ~[~" foo bar", + ~"foo bar ", + ~"foo\n bar", + ~"foo \nbar", + ~" foo bar \nbaz", + ~"foo bar baz", + ~"foobarbaz\n\n"]; + + let oracle_strs : ~[~str] = ~[~" foo bar", + ~"foo bar ", + ~"foo bar", + ~"foo bar", + ~" foo bar baz", + ~"foo bar baz", + ~"foobarbaz"]; + + assert vec::same_length(test_strs, oracle_strs); + let mode = DiscardNewline; + + for uint::range(0, test_strs.len()) |i| { + assert transform_text(test_strs[i], mode) == oracle_strs[i]; + } +} + +#[test] +fn test_transform_compress_whitespace() { + let test_strs : ~[~str] = ~[~" foo bar", + ~"foo bar ", + ~"foo\n bar", + ~"foo \nbar", + ~" foo bar \nbaz", + ~"foo bar baz", + ~"foobarbaz\n\n"]; + + let oracle_strs : ~[~str] = ~[~" foo bar", + ~"foo bar ", + ~"foo\n bar", + ~"foo \nbar", + ~" foo bar \nbaz", + ~"foo bar baz", + ~"foobarbaz\n\n"]; + + assert vec::same_length(test_strs, oracle_strs); + let mode = CompressWhitespace; + + for uint::range(0, test_strs.len()) |i| { + assert transform_text(test_strs[i], mode) == oracle_strs[i]; + } +} + +#[test] +fn test_transform_compress_whitespace_newline() { + let test_strs : ~[~str] = ~[~" foo bar", + ~"foo bar ", + ~"foo\n bar", + ~"foo \nbar", + ~" foo bar \nbaz", + ~"foo bar baz", + ~"foobarbaz\n\n"]; + + let oracle_strs : ~[~str] = ~[~" foo bar", + ~"foo bar ", + ~"foo bar", + ~"foo bar", + ~" foo bar baz", + ~"foo bar baz", + ~"foobarbaz "]; + + assert vec::same_length(test_strs, oracle_strs); + let mode = CompressWhitespaceNewline; + + for uint::range(0, test_strs.len()) |i| { + assert transform_text(test_strs[i], mode) == oracle_strs[i]; + } +} \ No newline at end of file