Skip to content

Commit

Permalink
fix: fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
fefit committed Oct 4, 2021
1 parent e2f4b9f commit 96afe31
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rphtml"
version = "0.5.3"
version = "0.5.4"
authors = ["jxz_211 <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4964,10 +4964,10 @@ fn main() -> Result<(), Box<dyn Error>> {
let doc = Doc::parse(
&code,
ParseOptions {
auto_fix_unexpected_endtag: true,
allow_self_closing: true,
auto_fix_unclosed_tag: true,
auto_fix_unescaped_lt: true,
// auto_fix_unexpected_endtag: true,
// allow_self_closing: true,
// auto_fix_unclosed_tag: true,
// auto_fix_unescaped_lt: true,
..Default::default()
},
)?;
Expand Down
54 changes: 26 additions & 28 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn create_parse_error(kind: ErrorKind, position: usize, context: &str) -> HResul

fn is_void_tag(name: &[char]) -> bool {
for cur_name in VOID_ELEMENTS.iter() {
if is_equal_chars(&cur_name, name, &None) {
if is_equal_chars(cur_name, name, &None) {
return true;
}
}
Expand Down Expand Up @@ -284,7 +284,7 @@ impl Attr {
let mut ret = Vec::with_capacity(ALLOC_CHAR_CAPACITY);
let mut has_key = false;
if let Some(AttrData { content, .. }) = &self.key {
ret.extend_from_slice(&content);
ret.extend_from_slice(content);
has_key = true;
}
if let Some(AttrData { content, .. }) = &self.value {
Expand All @@ -294,12 +294,12 @@ impl Attr {
if let Some(quote) = self.quote {
if self.need_quote || !remove_quote {
ret.push(quote);
ret.extend_from_slice(&content);
ret.extend_from_slice(content);
ret.push(quote);
return ret;
}
}
ret.extend_from_slice(&content);
ret.extend_from_slice(content);
}
ret
}
Expand Down Expand Up @@ -450,7 +450,7 @@ pub struct Node {
pub root: Option<Weak<RefCell<Node>>>,

// document
pub document: Option<RefDoc>,
pub document: Option<Weak<RefCell<Doc>>>,

// the content,for text/comment/style/script nodes
pub content: Option<Vec<char>>,
Expand Down Expand Up @@ -543,11 +543,10 @@ impl Node {
continue;
}
prev_is_space = true;
result.push(*ch);
} else {
prev_is_space = false;
result.push(*ch);
}
result.push(*ch);
}
} else if ch == &';' {
// entity end
Expand All @@ -574,20 +573,19 @@ impl Node {
continue;
}
prev_is_space = true;
result.push(c);
} else {
prev_is_space = false;
result.push(c);
}
result.push(c);
}
}
} else {
// decode entity
if options.decode_entity {
// when need decode, the content should append to result
decode_chars_to(&content, result);
decode_chars_to(content, result);
} else {
result.extend_from_slice(&content);
result.extend_from_slice(content);
}
}
}
Expand All @@ -600,12 +598,12 @@ impl Node {
let tag_name = &meta.name;
// check if is in pre, only check if not in pre
status.is_in_pre =
is_in_pre || is_equal_chars(&tag_name, &PRE_TAG_NAME, &Some(NameCase::Lower));
is_in_pre || is_equal_chars(tag_name, &PRE_TAG_NAME, &Some(NameCase::Lower));
if !is_inner {
// add tag name
result.push('<');
if !options.lowercase_tagname {
result.extend_from_slice(&tag_name);
result.extend_from_slice(tag_name);
} else {
for ch in tag_name {
result.push(ch.to_ascii_lowercase());
Expand All @@ -627,12 +625,12 @@ impl Node {
// content for some special tags, such as style/script
if let Some(content) = &self.content {
let need_encode =
options.encode_content && is_plain_text_tag(&tag_name, &Some(NameCase::Lower));
options.encode_content && is_plain_text_tag(tag_name, &Some(NameCase::Lower));
if !need_encode {
result.extend_from_slice(&content);
result.extend_from_slice(content);
} else {
// content tag's html need encode
result.extend(get_content_encode(&content));
result.extend(get_content_encode(content));
}
}
}
Expand All @@ -644,7 +642,7 @@ impl Node {
let mut content = &content[..];
if is_in_pre
&& is_equal_chars(
chars_trim_end(&content),
chars_trim_end(content),
&PRE_TAG_NAME,
&Some(NameCase::Lower),
) {
Expand All @@ -653,7 +651,7 @@ impl Node {
if !is_inner {
result.extend_from_slice(&['<', '/']);
if options.remove_endtag_space {
content = chars_trim_end(&content);
content = chars_trim_end(content);
}
if options.lowercase_tagname {
let content = content
Expand All @@ -662,7 +660,7 @@ impl Node {
.collect::<Vec<char>>();
result.extend(content);
} else {
result.extend_from_slice(&content);
result.extend_from_slice(content);
}
result.push('>');
}
Expand All @@ -675,7 +673,7 @@ impl Node {
.content
.as_ref()
.expect("Spaces between node must have whitespcaes");
result.extend_from_slice(&content);
result.extend_from_slice(content);
}
}
HTMLDOCTYPE => {
Expand All @@ -696,7 +694,7 @@ impl Node {
// comment
result.extend_from_slice(&['<', '!', '-', '-']);
if let Some(content) = &self.content {
result.extend_from_slice(&content);
result.extend_from_slice(content);
}
result.extend_from_slice(&['-', '-', '>']);
}
Expand All @@ -705,7 +703,7 @@ impl Node {
// cdata
result.extend_from_slice(&['<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[']);
if let Some(content) = &self.content {
result.extend_from_slice(&content);
result.extend_from_slice(content);
}
result.extend_from_slice(&[']', ']', '>']);
}
Expand Down Expand Up @@ -770,7 +768,7 @@ impl Node {
if finded {
panic!("`inner_html` can't used in abstract root node which has multiple tag node childs.");
}
child_node = Some(Rc::clone(&child));
child_node = Some(Rc::clone(child));
finded = true;
}
}
Expand Down Expand Up @@ -1215,7 +1213,7 @@ fn parse_tagend(doc: &mut Doc, c: char, context: &str) -> HResult {
.borrow();
let start_tag_name = &meta.name;
let (is_equal, is_total_same) =
is_equal_chars_ignore_case(&start_tag_name, &fix_end_tag_name);
is_equal_chars_ignore_case(start_tag_name, fix_end_tag_name);
if is_equal {
if doc.parse_options.case_sensitive_tagname && !is_total_same {
return doc.error(
Expand Down Expand Up @@ -1514,7 +1512,7 @@ fn parse_exclamation_begin(doc: &mut Doc, c: char, context: &str) -> HResult {
}
} else {
return create_parse_error(
ErrorKind::UnrecognizedTag(doc.chars_to_string(), chars_to_string(&next_chars)),
ErrorKind::UnrecognizedTag(doc.chars_to_string(), chars_to_string(next_chars)),
doc.mem_position,
context,
);
Expand All @@ -1534,7 +1532,7 @@ fn parse_exclamation_begin(doc: &mut Doc, c: char, context: &str) -> HResult {
let special_tag_name = doc.in_special.as_ref().map(|(_, name)| name);
if let Some(tag_name) = special_tag_name {
if is_equal_chars(
&tag_name,
tag_name,
&doc
.chain_nodes
.last()
Expand Down Expand Up @@ -1651,7 +1649,7 @@ impl Doc {
// into root
pub fn into_root(self) -> DocHolder {
let doc = Rc::new(RefCell::new(self));
doc.borrow_mut().root.borrow_mut().document = Some(Rc::clone(&doc));
doc.borrow_mut().root.borrow_mut().document = Some(Rc::downgrade(&doc));
DocHolder { doc }
}

Expand Down Expand Up @@ -1815,7 +1813,7 @@ impl Doc {
let tag_name = &meta.borrow().name;
let mut end = Node::new(NodeType::TagEnd, self.position);
end.content = Some(tag_name.clone());
end.parent = Some(Rc::downgrade(&tag_node));
end.parent = Some(Rc::downgrade(tag_node));
end_tag = Some(end);
}
if let Some(end_tag) = end_tag {
Expand Down
10 changes: 5 additions & 5 deletions tests/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,16 @@ fn test_attrs() -> HResult {
assert_eq!(get_attr_content(&attrs[8].value), Some("A\\B\\C\\"));
// wrong value
assert!(parse(r#"<div id"1"></div>"#).is_ok());
assert_eq!(parse(r#"<div "1"'2'></div>"#).is_ok(), true);
assert_eq!(parse(r#"<div a="1\""></div>"#).is_ok(), true);
assert!(parse(r#"<div "1"'2'></div>"#).is_ok());
assert!(parse(r#"<div a="1\""></div>"#).is_ok());
Ok(())
}

#[test]
fn test_tag_close() -> HResult {
// tag not closed
let code = "<div>";
assert_eq!(parse(code).is_err(), true);
assert!(parse(code).is_err());
// allow code auto_fix
let doc = Doc::parse(
r#"<div id=1><div id=2><div id=3>3</div>"#,
Expand All @@ -194,13 +194,13 @@ fn test_tag_close() -> HResult {
..Default::default()
},
);
assert_eq!(doc.is_ok(), true);
assert!(doc.is_ok());
assert_eq!(
render(&doc?),
"<div id=1><div id=2><div id=3>3</div></div></div>"
);
// wrong tag end
assert_eq!(parse("<div></p>").is_err(), true);
assert!(parse("<div></p>").is_err());
Ok(())
}

Expand Down

0 comments on commit 96afe31

Please sign in to comment.