use super::main::HTMLElement; use super::normalize_attr_quotes;
impl HTMLElement {
pub fn outer_html(&self) -> String {
if self.is_root() {
return self.inner_html();
} let tag = self.name();
let attrs = if self.raw_attrs.is_empty() {
String::new()
} else {
format!(" {}", self.raw_attrs.trim())
};
let should_normalize = self.attrs_modified;
if self.is_void {
if self.void_add_slash {
let mut norm_attrs = if attrs.is_empty() {
String::new()
} else if should_normalize {
normalize_attr_quotes(&attrs, true)
} else {
normalize_attr_quotes(&attrs, false)
};
if !norm_attrs.is_empty() {
let mut converted = String::with_capacity(norm_attrs.len());
let nb = norm_attrs.as_bytes();
let mut i = 0;
while i < nb.len() {
let c = nb[i] as char;
if c == '\'' {
converted.push(c);
i += 1;
continue;
}
if c == '=' && i + 1 < nb.len() && nb[i + 1] as char == '\'' {
converted.push('=');
converted.push('"');
i += 2; let start = i;
while i < nb.len() && nb[i] as char != '\'' {
i += 1;
}
let val = &norm_attrs[start..i];
if val.contains('"') {
converted.push('\'');
converted.push_str(val);
converted.push('\'');
} else {
converted.push_str(val);
converted.push('"');
}
if i < nb.len() && nb[i] as char == '\'' {
i += 1;
}
continue;
}
converted.push(c);
i += 1;
}
norm_attrs = converted;
}
if norm_attrs.is_empty() {
format!("<{}{}{}>", tag, norm_attrs, "/")
} else if norm_attrs.ends_with(' ') {
format!("<{}{}{}>", tag, norm_attrs, "/")
} else {
format!("<{}{} {}>", tag, norm_attrs, "/")
}
} else {
let norm_attrs = if attrs.is_empty() {
String::new()
} else if should_normalize {
normalize_attr_quotes(&attrs, true)
} else {
normalize_attr_quotes(&attrs, false)
};
format!("<{}{}>", tag, norm_attrs)
}
} else {
let norm_attrs = if attrs.is_empty() {
String::new()
} else if should_normalize {
normalize_attr_quotes(&attrs, true)
} else {
normalize_attr_quotes(&attrs, false)
};
let auto_closed_empty = match self.range() {
Some((s, e)) if s == e && self.children.is_empty() => true,
_ => false,
};
if auto_closed_empty {
format!("<{}{}>", tag, norm_attrs)
} else {
format!("<{}{}>{}</{}>", tag, norm_attrs, self.inner_html(), tag)
}
}
}
pub fn to_string(&self) -> String {
self.outer_html()
}
}