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

Text chunk handlers complications #256

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
28 changes: 27 additions & 1 deletion src/rewritable_units/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,11 @@ mod tests {
#[test]
fn parsed() {
test!(
|_| {},
|el| {
assert_eq!(el.get_attribute("a1").unwrap(), "foo \" baré \" baz");
assert_eq!(el.get_attribute("a3").unwrap(), "foo/bar");
assert_eq!(el.get_attribute("a4").unwrap(), "");
},
r#"<a a1='foo " baré " baz' / a2="foo ' bar ' baz" a3=foo/bar a4></a>"#
);
}
Expand Down Expand Up @@ -1523,6 +1527,26 @@ mod tests {
assert_eq!(output, r"<img/>");
}

#[test]
fn value_trailing_slash() {
let mut output = rewrite_element(b"<img path=//>", UTF_8, "img", |el| {
assert_eq!(el.get_attribute("path").unwrap(), "//");
el.set_attribute("slash", "/").unwrap();

assert!(!el.can_have_content());
});

assert_eq!(output, r#"<img path=// slash="/">"#);

output = rewrite_element(b"<img path=//>", UTF_8, "img", |el| {
el.remove_attribute("path");

assert!(!el.can_have_content());
});

assert_eq!(output, r"<img>");
}

#[test]
fn remove_non_existent_attr() {
test!(
Expand All @@ -1537,6 +1561,8 @@ mod tests {
fn without_attrs() {
test!(
|el| {
assert!(el.can_have_content());

for name in &["a1", "a2", "a3", "a4"] {
el.remove_attribute(name);
}
Expand Down
4 changes: 3 additions & 1 deletion src/rewritable_units/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ mod test_utils {
|c: &[u8]| output.push(c),
);

rewriter.write(html).unwrap();
for ch in html.chunks(15) {
rewriter.write(ch).unwrap();
}
rewriter.end().unwrap();
}

Expand Down
2 changes: 1 addition & 1 deletion src/rewritable_units/tokens/capturer/text_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl TextDecoder {
encoding,
pending_text_streaming_decoder: None,
// TODO make adjustable
text_buffer: String::from_utf8(vec![0u8; 1024]).unwrap(),
text_buffer: "\0".repeat(if cfg!(debug_assertions) { 12 } else { 1024 }),
last_text_type: TextType::Data,
}
}
Expand Down
77 changes: 42 additions & 35 deletions src/rewritable_units/tokens/text_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,31 +413,27 @@ mod tests {
};
}

macro_rules! skip_eof_chunk {
($c:ident) => {
if $c.last_in_text_node() {
assert!($c.as_str().is_empty());
return;
}
};
}

#[test]
fn parsed() {
test!(|_| {}, HTML);
}

#[test]
fn with_prepends_and_appends() {
let mut first = true;
test!(
|c| {
skip_eof_chunk!(c);
c.before("<span>", ContentType::Text);
c.before("<div>Hey</div>", ContentType::Html);
c.before("<foo>", ContentType::Html);
c.after("</foo>", ContentType::Html);
c.after("<!-- 42 -->", ContentType::Html);
c.after("<foo & bar>", ContentType::Text);
let is_first = std::mem::replace(&mut first, c.last_in_text_node());
if is_first {
c.before("<span>", ContentType::Text);
c.before("<div>Hey</div>", ContentType::Html);
c.before("<foo>", ContentType::Html);
}
if c.last_in_text_node() {
c.after("</foo>", ContentType::Html);
c.after("<!-- 42 -->", ContentType::Html);
c.after("<foo & bar>", ContentType::Text);
}
},
concat!(
"&lt;span&gt;<div>Hey</div><foo>",
Expand All @@ -452,17 +448,22 @@ mod tests {

#[test]
fn removed() {
let mut first = true;
test!(
|c| {
skip_eof_chunk!(c);
let is_first = std::mem::replace(&mut first, c.last_in_text_node());
assert!(!c.removed());

c.remove();

assert!(c.removed());

c.before("<before>", ContentType::Html);
c.after("<after>", ContentType::Html);
if is_first {
c.before("<before>", ContentType::Html);
}
if c.last_in_text_node() {
c.after("<after>", ContentType::Html);
}
},
"<before><after>"
);
Expand All @@ -472,17 +473,20 @@ mod tests {
fn replaced_with_text() {
test!(
|c| {
skip_eof_chunk!(c);
c.before("<before>", ContentType::Html);
c.after("<after>", ContentType::Html);
if c.last_in_text_node() {
c.before("<before>", ContentType::Html);
c.after("<after>", ContentType::Html);

assert!(!c.removed());
assert!(!c.removed());

c.replace("<div></div>", ContentType::Html);
c.replace("<!--42-->", ContentType::Html);
c.replace("<foo & bar>", ContentType::Text);
c.replace("<div></div>", ContentType::Html);
c.replace("<!--42-->", ContentType::Html);
c.replace("<foo & bar>", ContentType::Text);

assert!(c.removed());
assert!(c.removed());
} else {
c.remove();
}
},
"<before>&lt;foo &amp; bar&gt;<after>"
);
Expand All @@ -492,17 +496,20 @@ mod tests {
fn replaced_with_html() {
test!(
|c| {
skip_eof_chunk!(c);
c.before("<before>", ContentType::Html);
c.after("<after>", ContentType::Html);
if c.last_in_text_node() {
c.before("<before>", ContentType::Html);
c.after("<after>", ContentType::Html);

assert!(!c.removed());
assert!(!c.removed());

c.replace("<div></div>", ContentType::Html);
c.replace("<!--42-->", ContentType::Html);
c.replace("<foo & bar>", ContentType::Html);
c.replace("<div></div>", ContentType::Html);
c.replace("<!--42-->", ContentType::Html);
c.replace("<foo & bar>", ContentType::Html);

assert!(c.removed());
assert!(c.removed());
} else {
c.remove();
}
},
"<before><foo & bar><after>"
);
Expand Down
Loading