use crate::converter::DomContext;
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn get_next_sibling_tag<'a>(
node_handle: &tl::NodeHandle,
parser: &'a tl::Parser,
dom_ctx: &'a DomContext,
) -> Option<&'a str> {
dom_ctx.next_tag_name(*node_handle, parser)
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn get_previous_sibling_tag<'a>(
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
dom_ctx: &'a DomContext,
) -> Option<&'a str> {
let id = node_handle.get_inner();
let parent = dom_ctx.parent_of(id);
let siblings = if let Some(parent_id) = parent {
dom_ctx.children_of(parent_id)?
} else {
&dom_ctx.root_children
};
let position = dom_ctx.sibling_index(id).or_else(|| {
siblings
.iter()
.position(|handle: &tl::NodeHandle| handle.get_inner() == id)
})?;
for sibling in siblings.iter().take(position).rev() {
if let Some(info) = dom_ctx.tag_info(sibling.get_inner(), parser) {
return Some(info.name.as_str());
}
if let Some(tl::Node::Raw(raw)) = sibling.get(parser) {
if !raw.as_utf8_str().trim().is_empty() {
return None;
}
}
}
None
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn previous_content_block<'a>(
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
dom_ctx: &'a DomContext,
) -> Option<&'a str> {
let id = node_handle.get_inner();
let siblings = match dom_ctx.parent_of(id) {
Some(parent_id) => match dom_ctx.children_of(parent_id) {
Some(children) => children.as_slice(),
None => return None,
},
None => dom_ctx.root_children.as_slice(),
};
let position = dom_ctx
.sibling_index(id)
.or_else(|| siblings.iter().position(|handle| handle.get_inner() == id))?;
let mut nodes = &siblings[..position.min(siblings.len())];
'outer: loop {
for sibling in nodes.iter().rev() {
if let Some(info) = dom_ctx.tag_info(sibling.get_inner(), parser) {
if crate::converter::utility::content::is_block_level_element(&info.name) {
return Some(info.name.as_str());
}
if !crate::converter::main_helpers::is_inline_element(&info.name) {
return None;
}
match dom_ctx.children_of(sibling.get_inner()) {
Some(children) => {
nodes = children.as_slice();
continue 'outer;
}
None => return None,
}
}
if let Some(tl::Node::Raw(raw)) = sibling.get(parser) {
if !raw.as_utf8_str().trim().is_empty() {
return None;
}
}
}
return None;
}
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn previous_sibling_is_inline_tag(node_handle: &tl::NodeHandle, parser: &tl::Parser, dom_ctx: &DomContext) -> bool {
dom_ctx.previous_inline_like(*node_handle, parser)
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn next_sibling_is_whitespace_text(
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
dom_ctx: &DomContext,
) -> bool {
dom_ctx.next_whitespace_text(*node_handle, parser)
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn next_sibling_is_inline_tag(node_handle: &tl::NodeHandle, parser: &tl::Parser, dom_ctx: &DomContext) -> bool {
dom_ctx.next_inline_like(*node_handle, parser)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FollowingContent {
Inline,
NotInline,
Absent,
}
pub fn following_sibling_content(id: u32, parser: &tl::Parser, dom_ctx: &DomContext) -> FollowingContent {
let siblings = match dom_ctx.parent_of(id) {
Some(parent_id) => match dom_ctx.children_of(parent_id) {
Some(children) => children,
None => return FollowingContent::Absent,
},
None => &dom_ctx.root_children,
};
let Some(position) = dom_ctx
.sibling_index(id)
.or_else(|| siblings.iter().position(|handle| handle.get_inner() == id))
else {
return FollowingContent::Absent;
};
for sibling in siblings.iter().skip(position + 1) {
if let Some(info) = dom_ctx.tag_info(sibling.get_inner(), parser) {
return if info.is_inline_like {
FollowingContent::Inline
} else {
FollowingContent::NotInline
};
}
if let Some(tl::Node::Raw(raw)) = sibling.get(parser) {
let decoded = raw.as_utf8_str();
if decoded.trim().is_empty() {
continue;
}
return if decoded.starts_with(char::is_whitespace) {
FollowingContent::NotInline
} else {
FollowingContent::Inline
};
}
}
FollowingContent::Absent
}
pub fn br_follows_enclosing_elements(id: u32, parser: &tl::Parser, dom_ctx: &DomContext) -> bool {
let mut id = id;
while following_sibling_content(id, parser, dom_ctx) == FollowingContent::Absent {
let Some(parent) = dom_ctx.parent_of(id) else {
return false;
};
if let Some(next) = dom_ctx.next_tag_id(parent, parser) {
return dom_ctx.tag_info(next, parser).is_some_and(|info| info.name == "br");
}
id = parent;
}
false
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn append_inline_suffix(
output: &mut String,
suffix: &str,
has_core_content: bool,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
dom_ctx: &DomContext,
) {
if suffix.is_empty() {
return;
}
if suffix == " " && has_core_content && next_sibling_is_whitespace_text(node_handle, parser, dom_ctx) {
return;
}
output.push_str(suffix);
}