Skip to content

Commit

Permalink
Merge pull request #13 from letmutex/ref-element-fields
Browse files Browse the repository at this point in the history
Change all fields of Element to references
  • Loading branch information
letmutex committed Jun 26, 2024
2 parents ec1007b + 30585f2 commit c174eda
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 49 deletions.
11 changes: 5 additions & 6 deletions benches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ Memory: 15.9 GB
# Result

```
convert() time: [70.877 ms 71.319 ms 71.799 ms]
change: [-1.7418% -0.7475% +0.1900%] (p = 0.13 > 0.05)
convert() time: [68.311 ms 68.743 ms 69.198 ms]
change: [-1.0805% +0.1103% +1.2999%] (p = 0.86 > 0.05)
No change in performance detected.
Found 8 outliers among 100 measurements (8.00%)
7 (7.00%) high mild
1 (1.00%) high severe
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high mild
```

*Updated at Tue, 25 Jun 2024 08:27:01 GMT*
*Updated at Wed, 26 Jun 2024 12:45:36 GMT*

*Generated by [bench.ts](bench.ts)*
21 changes: 8 additions & 13 deletions src/dom_walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,13 @@ pub(crate) fn walk_node(
ref attrs,
..
} => {
let attrs_value = attrs.take();
// Put it back
attrs.replace(attrs_value.clone());
visit_element(
buffer,
node,
handler,
options,
name.local.to_string(),
attrs_value,
&name.local,
&attrs.borrow(),
is_pre,
);
}
Expand Down Expand Up @@ -110,8 +107,8 @@ fn visit_element(
node: &Rc<Node>,
handler: &Box<&dyn ElementHandler>,
options: &Options,
tag: String,
attrs: Vec<Attribute>,
tag: &str,
attrs: &Vec<Attribute>,
is_pre: bool,
) {
let is_head = tag == "head";
Expand All @@ -120,10 +117,10 @@ fn visit_element(
let is_block = is_block_element(&tag);
walk_children(buffer, node, is_block, handler, options, is_pre);
let md = handler.on_visit(
node.clone(),
node,
tag,
attrs,
join_contents(&buffer[prev_buffer_len..]),
&join_contents(&buffer[prev_buffer_len..]),
options,
);
// Remove the temporary text clips of children
Expand All @@ -150,10 +147,8 @@ fn join_contents(contents: &[String]) -> String {
let left = result.trim_end_matches(|ch| ch == '\n');
let right = content.trim_start_matches(|ch| ch == '\n');

let max_trimmed_new_lines = std::cmp::max(
result_len - left.len(),
content_len - right.len(),
);
let max_trimmed_new_lines =
std::cmp::max(result_len - left.len(), content_len - right.len());
let separator_new_lines = std::cmp::min(max_trimmed_new_lines, 2);
let separator = "\n".repeat(separator_new_lines);

Expand Down
14 changes: 7 additions & 7 deletions src/element_handler/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ impl ElementHandler for AnchorElementHandler {

fn on_visit(
&self,
_node: Rc<Node>,
_tag: String,
attrs: Vec<Attribute>,
content: String,
_node: &Rc<Node>,
_tag: &str,
attrs: &Vec<Attribute>,
content: &str,
options: &Options,
) -> Option<String> {
let mut link: Option<String> = None;
Expand All @@ -51,7 +51,7 @@ impl ElementHandler for AnchorElementHandler {
}

let Some(link) = link else {
return Some(content);
return Some(content.to_string());
};

let process_title = |text: String| {
Expand Down Expand Up @@ -81,7 +81,7 @@ impl AnchorElementHandler {
Self {}
}

fn build_inlined_anchor(&self, content: String, link: String, title: Option<String>) -> String {
fn build_inlined_anchor(&self, content: &str, link: String, title: Option<String>) -> String {
let has_spaces_in_link = link.contains(' ');
let (content, leading_whitespace) = content.strip_leading_whitespace();
let (content, trailing_whitespace) = content.strip_trailing_whitespace();
Expand All @@ -103,7 +103,7 @@ impl AnchorElementHandler {

fn build_referenced_anchor(
&self,
content: String,
content: &str,
link: String,
style: &LinkReferenceStyle,
) -> String {
Expand Down
33 changes: 14 additions & 19 deletions src/element_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ pub trait ElementHandler {

fn on_visit(
&self,
node: Rc<Node>,
tag: String,
attrs: Vec<Attribute>,
content: String,
node: &Rc<Node>,
tag: &str,
attrs: &Vec<Attribute>,
content: &str,
options: &Options,
) -> Option<String>;
}
Expand All @@ -57,10 +57,10 @@ where
{
fn on_visit(
&self,
node: Rc<Node>,
tag: String,
attrs: Vec<Attribute>,
content: String,
node: &Rc<Node>,
tag: &str,
attrs: &Vec<Attribute>,
content: &str,
options: &Options,
) -> Option<String> {
self(Element {
Expand Down Expand Up @@ -143,20 +143,15 @@ impl ElementHandlers {
impl ElementHandler for ElementHandlers {
fn on_visit(
&self,
node: Rc<Node>,
tag: String,
attrs: Vec<Attribute>,
content: String,
node: &Rc<Node>,
tag: &str,
attrs: &Vec<Attribute>,
content: &str,
options: &Options,
) -> Option<String> {
match self
.rules
.iter()
.rev()
.find(|rule| rule.tags.contains(&tag))
{
match self.rules.iter().rev().find(|rule| rule.tags.contains(tag)) {
Some(rule) => rule.handler.on_visit(node, tag, attrs, content, options),
None => Some(content),
None => Some(content.to_string()),
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ pub fn convert(html: &str) -> Result<String, std::io::Error> {
/// The DOM element.
pub struct Element<'a> {
/// The html5ever node of the element.
pub node: Rc<Node>,
pub node: &'a Rc<Node>,
/// The tag name.
pub tag: String,
pub tag: &'a str,
/// The attribute list.
pub attrs: Vec<Attribute>,
pub attrs: &'a Vec<Attribute>,
/// The content text, can be raw text or converted Markdown text.
pub content: String,
pub content: &'a str,
/// Converter options.
pub options: &'a Options,
}
Expand Down

0 comments on commit c174eda

Please sign in to comment.