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_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
}
#[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);
}