#[cfg(feature = "metadata")]
use std::collections::BTreeMap;
mod code;
use self::code::{CodeStage, CodeText, emit_code_link};
use crate::converter::Context;
use crate::converter::append_inline_suffix;
use crate::converter::block::heading::{find_single_heading_child, heading_allows_inline_images, push_heading};
use crate::converter::dom_context::DomContext;
use crate::converter::inline::HandlerContext;
use crate::converter::inline::link::{MarkdownLink, append_markdown_link_in_context, has_uri_scheme};
use crate::converter::media::inline_data_treatment;
use crate::converter::text_node::is_fresh_block_start;
use crate::converter::utility::content::{
collect_link_label_text, indent_continuation_lines, keeps_source_white_space, link_accessible_name,
link_label_edges, link_text_content, link_text_edges, node_is_block_level, normalize_link_label,
normalized_tag_name, push_inline_prefix,
};
use crate::converter::utility::escaping::escape_link_label;
use crate::converter::walk::{Child, ChildHook, Close, HookSite, Open};
use crate::options::{ConversionOptions, InlineDataMedia};
use crate::text;
use std::borrow::Cow;
use std::rc::Rc;
#[cfg(feature = "visitor")]
use crate::converter::utility::serialization::serialize_node;
fn indent_hard_break_continuations(label: &mut String, ctx: &Context, options: &ConversionOptions) {
if !ctx.in_list_item || !label.contains('\n') {
return;
}
let Some(indent) = crate::converter::list::utils::continuation_indent_string(ctx.list_indent_columns, options)
else {
return;
};
indent_continuation_lines(label, &indent);
}
pub fn open_link(
tag: &tl::HTMLTag,
handler: &mut HandlerContext<'_>,
children: &mut Vec<tl::NodeHandle>,
) -> Option<Open> {
let Some(data) = LinkData::new(tag, handler) else {
children.extend(tag.children().top().iter().copied());
return Some(Open::children_only());
};
let flow = if handler.context.inline_code_links.is_some() && !handler.context.in_code_block {
children.extend(data.children.iter().copied());
LinkFlow::CodeSpan {
raw: String::new(),
scope: Rc::new(Context {
inline_code_links: None,
..handler.context.clone()
}),
}
} else if handler.context.in_code {
handler.context.inline_data_replaced.set(false);
LinkFlow::Code(CodeText::open(&data, handler, children))
} else {
if emit_autolink(&data, handler) {
return None;
}
LinkFlow::Label(LabelFlow::open(&data, handler, children))
};
Some(Open {
scope: None,
close: Close::Hooked(Box::new(LinkHook { data, flow })),
})
}
struct LinkHook {
data: LinkData,
flow: LinkFlow,
}
enum LinkFlow {
CodeSpan { raw: String, scope: Rc<Context> },
Code(CodeText),
Label(LabelFlow),
}
impl ChildHook for LinkHook {
fn before_child(&mut self, index: usize, element: &mut HookSite<'_>) -> Child {
match &mut self.flow {
LinkFlow::CodeSpan { raw, scope } => Child::Walk {
depth: None,
buffer: Some(std::mem::take(raw)),
scope: Some(Rc::clone(scope)),
},
LinkFlow::Code(code) => code.before_child(index, &self.data, element),
LinkFlow::Label(label) => label.before_child(index, &self.data, element),
}
}
fn after_child(&mut self, index: usize, own: Option<String>, element: &mut HookSite<'_>) {
match &mut self.flow {
LinkFlow::CodeSpan { raw, .. } => *raw = own.unwrap_or_default(),
LinkFlow::Code(code) => code.after_child(index, own, &self.data, element),
LinkFlow::Label(label) => label.after_child(index, own, &self.data, element),
}
}
fn close(self: Box<Self>, element: &mut HookSite<'_>) {
let Some(tl::Node::Tag(tag)) = element.node_handle.get(element.parser) else {
return;
};
let Self { data, flow } = *self;
match flow {
LinkFlow::CodeSpan { raw, scope } => emit_code_link(tag, &data, raw, &scope, element),
LinkFlow::Code(mut code) => code.advance(CodeStage::Done, tag, &data, element),
LinkFlow::Label(mut label) => label.advance(LabelStage::Done, tag, &data, element),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum LabelStage {
Heading,
Label,
Deferred,
Done,
}
struct LabelFlow {
stage: LabelStage,
heading: Option<(usize, tl::NodeHandle)>,
heading_text: String,
label: Option<LabelWalk>,
emits_deferred: bool,
}
impl LabelFlow {
fn open(data: &LinkData, handler: &HandlerContext<'_>, children: &mut Vec<tl::NodeHandle>) -> Self {
let heading =
find_single_heading_child(*handler.node_handle, handler.parser).filter(|_| !data.href_addr_dropped);
children.extend(heading.map(|(_, heading_handle)| heading_handle));
children.extend(data.label_children().iter().copied());
if data.emit_blocks_separately {
children.extend(data.deferred.iter().copied());
}
Self {
stage: LabelStage::Heading,
heading,
heading_text: String::new(),
label: None,
emits_deferred: false,
}
}
fn part(&self, index: usize, data: &LinkData) -> (LabelStage, usize) {
let headings = usize::from(self.heading.is_some());
let labels = data.label_children().len();
if index < headings {
(LabelStage::Heading, index)
} else if index < headings + labels {
(LabelStage::Label, index - headings)
} else {
(LabelStage::Deferred, index - headings - labels)
}
}
fn before_child(&mut self, index: usize, data: &LinkData, element: &mut HookSite<'_>) -> Child {
let (part, _) = self.part(index, data);
if let Some(tl::Node::Tag(tag)) = element.node_handle.get(element.parser) {
self.advance(part, tag, data, element);
}
if self.stage != part {
return Child::Skip;
}
match (part, self.heading, self.label.as_mut()) {
(LabelStage::Heading, Some((_, heading_handle)), _) => {
heading_context(heading_handle, element.parser, element.context).map_or(Child::Skip, |context| {
Child::Walk {
depth: None,
buffer: Some(String::new()),
scope: Some(Rc::new(context)),
}
})
}
(LabelStage::Label, _, Some(label)) => label.before_child(),
(LabelStage::Deferred, _, _) if self.emits_deferred => Child::IN_PLACE,
_ => Child::Skip,
}
}
fn after_child(&mut self, index: usize, own: Option<String>, data: &LinkData, element: &HookSite<'_>) {
match (self.part(index, data), self.label.as_mut()) {
((LabelStage::Heading, _), _) => self.heading_text = own.unwrap_or_default(),
((LabelStage::Label, at), Some(label)) => {
label.after_child(data.label_children()[at], own, element);
}
_ => {}
}
}
fn advance(&mut self, to: LabelStage, tag: &tl::HTMLTag<'_>, data: &LinkData, element: &mut HookSite<'_>) {
while self.stage < to {
self.stage = match self.stage {
LabelStage::Heading => {
let heading_text = self.heading_text.trim();
match self.heading {
Some((level, _)) if !heading_text.is_empty() => {
emit_heading_link(tag, data, level, heading_text, element);
LabelStage::Done
}
_ => {
element.context.inline_data_replaced.set(false);
self.label = Some(LabelWalk::new(data, element.options, element.context));
LabelStage::Label
}
}
}
LabelStage::Label => {
let content = self
.label
.take()
.map(|label| label.into_content(data))
.unwrap_or_default();
self.emits_deferred = emit_label_link(tag, data, &content, element) && data.emit_blocks_separately;
LabelStage::Deferred
}
LabelStage::Deferred | LabelStage::Done => LabelStage::Done,
};
}
}
}
fn emit_label_link(tag: &tl::HTMLTag<'_>, data: &LinkData, content: &str, handler: &mut HandlerContext<'_>) -> bool {
let (prefix, suffix, trimmed) = if keeps_source_white_space(handler.options, handler.context) {
("", "", content)
} else {
link_label_edges(content)
};
let edges = LabelEdges {
prefix,
suffix,
has_text: !trimmed.is_empty(),
};
let mut label = normalize_link_label(trimmed);
apply_label_fallbacks(tag, data, &mut label, handler);
indent_hard_break_continuations(&mut label, handler.context, handler.options);
let drop_link = data.is_dropped(&label, handler);
let emit_deferred = emit_link(tag, data, &label, &edges, drop_link, handler);
#[cfg(feature = "metadata")]
record_link_metadata(tag, data, &label, handler.context);
emit_deferred
}
struct LabelEdges<'a> {
prefix: &'a str,
suffix: &'a str,
has_text: bool,
}
impl LabelEdges<'_> {
const NONE: Self = Self {
prefix: "",
suffix: "",
has_text: false,
};
}
struct LinkData {
href: String,
children: Vec<tl::NodeHandle>,
inline_label: String,
raw_text: String,
text_edges: (&'static str, &'static str),
inline_children: Vec<tl::NodeHandle>,
deferred: Vec<tl::NodeHandle>,
emit_blocks_separately: bool,
href_addr_dropped: bool,
link_allow_inline_images: bool,
saw_block: bool,
same_page_fragment: bool,
names_place: bool,
empty_span_content: bool,
}
impl LinkData {
fn label_children(&self) -> &[tl::NodeHandle] {
if self.emit_blocks_separately {
&self.inline_children
} else {
&self.children
}
}
fn is_dropped(&self, label: &str, handler: &HandlerContext<'_>) -> bool {
let content_removed = handler.context.inline_data_replaced.get()
&& handler.options.inline_data_media == InlineDataMedia::DropElement;
label.is_empty() && (content_removed || self.leads_nowhere_else())
}
const fn leads_nowhere_else(&self) -> bool {
self.same_page_fragment && (self.names_place || !(self.empty_span_content || self.children.is_empty()))
}
fn is_autolink(&self, options: &ConversionOptions) -> bool {
options.autolinks
&& !options.default_title
&& !self.emit_blocks_separately
&& !self.href.is_empty()
&& !self.href_addr_dropped
&& has_uri_scheme(&self.href)
&& (self.raw_text == self.href || (self.href.starts_with("mailto:") && self.raw_text == self.href[7..]))
}
fn new(tag: &tl::HTMLTag<'_>, handler: &HandlerContext<'_>) -> Option<Self> {
let raw_href = tag
.attributes()
.get("href")
.flatten()
.map(|value| text::decode_attribute_value_cow(&value.as_utf8_str()).into_owned())?;
let href = handler
.context
.resolve_link_url(&raw_href, handler.node_handle, handler.parser, handler.dom_context)
.unwrap_or_else(|| raw_href.clone());
let same_page_fragment = handler.context.own_page.holds(&raw_href, &href);
let children = handler
.dom_context
.children_of(handler.node_handle.get_inner())
.map_or_else(|| tag.children().top().iter().copied().collect(), ToOwned::to_owned);
let hidden_content = handler.options.hidden_content;
let (inline_text, _, saw_block) =
collect_link_label_text(&children, handler.parser, handler.dom_context, hidden_content);
let text_source = if saw_block {
link_text_content(&children, handler.parser, handler.dom_context, hidden_content)
} else {
inline_text.clone()
};
let inline_label = inline_text.text;
let raw_text = text::normalize_whitespace_cow(&text_source.text).trim().to_string();
let (inline_children, deferred) = partition_link_children(&children, handler.parser, handler.dom_context);
let holds_element = children
.iter()
.any(|child| matches!(child.get(handler.parser), Some(tl::Node::Tag(_))));
Some(Self {
href_addr_dropped: matches!(
inline_data_treatment(handler.options.inline_data_media, &href),
InlineDataMedia::AltTextOnly | InlineDataMedia::DropElement
),
emit_blocks_separately: should_defer_table_blocks(
handler.context,
&deferred,
handler.parser,
handler.dom_context,
),
link_allow_inline_images: handler.context.keep_inline_images_in.contains("a"),
same_page_fragment,
names_place: href.split_once('#').is_some_and(|(_, fragment)| !fragment.is_empty()),
empty_span_content: !children.is_empty()
&& (same_page_fragment || holds_element)
&& children
.iter()
.all(|child| empty_text_wrapper_child(child, handler.parser)),
href,
children,
inline_label,
text_edges: link_text_edges(&text_source),
raw_text,
inline_children,
deferred,
saw_block,
})
}
}
fn empty_text_wrapper_child(handle: &tl::NodeHandle, parser: &tl::Parser<'_>) -> bool {
let mut pending = vec![*handle];
while let Some(child) = pending.pop() {
match child.get(parser) {
Some(tl::Node::Raw(raw)) if text::decode_html_entities_cow(&raw.as_utf8_str()).trim().is_empty() => {}
Some(tl::Node::Comment(_)) => {}
Some(tl::Node::Tag(tag))
if matches!(
normalized_tag_name(tag.name().as_utf8_str()).as_ref(),
"span"
| "b"
| "strong"
| "em"
| "mark"
| "del"
| "s"
| "strike"
| "ins"
| "u"
| "small"
| "sub"
| "sup"
| "code"
| "kbd"
| "samp"
) || plain_wrapper(tag) =>
{
pending.extend(tag.children().top().iter().copied());
}
_ => return false,
}
}
true
}
fn plain_wrapper(tag: &tl::HTMLTag<'_>) -> bool {
tag.attributes().get("class").is_none()
&& matches!(normalized_tag_name(tag.name().as_utf8_str()).as_ref(), "i" | "div")
}
fn heading_context(heading_handle: tl::NodeHandle, parser: &tl::Parser<'_>, context: &Context) -> Option<Context> {
let tl::Node::Tag(heading_tag) = heading_handle.get(parser)? else {
return None;
};
let heading_name = normalized_tag_name(heading_tag.name().as_utf8_str());
Some(Context {
in_heading: true,
convert_as_inline: true,
heading_allow_inline_images: heading_allows_inline_images(&heading_name, &context.keep_inline_images_in),
..context.clone()
})
}
fn link_title<'a>(tag: &'a tl::HTMLTag<'a>) -> Option<Cow<'a, str>> {
crate::converter::utility::attributes::decoded_attribute(tag, "title").filter(|value| !value.is_empty())
}
fn emit_autolink(data: &LinkData, handler: &mut HandlerContext<'_>) -> bool {
if !data.is_autolink(handler.options) {
return false;
}
let (prefix, suffix) = if keeps_source_white_space(handler.options, handler.context) {
("", "")
} else {
data.text_edges
};
let context = handler.context;
let prefix = if is_fresh_block_start(context, context.at_fresh_block_start.get()) {
""
} else {
prefix
};
let edges = LabelEdges {
prefix,
suffix,
has_text: true,
};
write_with_edges(&edges, handler, |handler| {
handler.output.push('<');
if data.href.starts_with("mailto:") && data.raw_text == data.href[7..] {
handler.output.push_str(&data.raw_text);
} else {
handler.output.push_str(&data.href);
}
handler.output.push('>');
});
true
}
fn emit_heading_link(
tag: &tl::HTMLTag<'_>,
data: &LinkData,
level: usize,
heading_text: &str,
handler: &mut HandlerContext<'_>,
) {
let mut link = String::new();
append_link(
&mut link,
&MarkdownLink {
label: &escape_link_label(heading_text),
href: &data.href,
title: link_title(tag).as_deref(),
raw_text: &data.raw_text,
},
handler.options,
handler.context,
);
push_heading(handler.output, handler.context, handler.options, level, &link);
}
fn label_context(data: &LinkData, context: &Context, convert_as_inline: bool) -> Context {
Context {
inline_depth: context.inline_depth + 1,
in_link: context.in_link || !data.href.is_empty(),
media_boundary: context
.media_boundary
.clone()
.or_else(|| Some(std::rc::Rc::new(std::cell::Cell::new(Default::default())))),
convert_as_inline: context.convert_as_inline || convert_as_inline,
link_allow_inline_images: data.link_allow_inline_images,
..context.clone()
}
}
struct LabelWalk {
scope: Rc<Context>,
convert_as_inline: bool,
strict: bool,
content: String,
child_start: usize,
after_image: bool,
}
impl LabelWalk {
fn new(data: &LinkData, options: &ConversionOptions, context: &Context) -> Self {
let convert_as_inline = data.saw_block && !data.emit_blocks_separately;
Self {
scope: Rc::new(label_context(data, context, convert_as_inline)),
convert_as_inline,
strict: keeps_source_white_space(options, context),
content: String::new(),
child_start: 0,
after_image: false,
}
}
fn before_child(&mut self) -> Child {
self.child_start = self.content.len();
Child::Walk {
depth: None,
buffer: Some(std::mem::take(&mut self.content)),
scope: Some(Rc::clone(&self.scope)),
}
}
fn after_child(&mut self, child: tl::NodeHandle, own: Option<String>, element: &HookSite<'_>) {
self.content = own.unwrap_or_default();
let child_start = self.child_start;
if !self.convert_as_inline || self.content.len() == child_start {
return;
}
let is_image = element
.dom_context
.tag_info(child.get_inner(), element.parser)
.is_some_and(|info| matches!(info.name.as_str(), "img" | "picture" | "svg"));
let separate = if self.strict {
texts_touch_at(&self.content, child_start)
} else {
(is_image || self.after_image) && words_touch_at(&self.content, child_start)
};
if separate {
self.content.insert(child_start, ' ');
}
self.after_image = is_image;
}
fn into_content(self, data: &LinkData) -> String {
if self.convert_as_inline && self.content.trim().is_empty() {
data.inline_label.clone()
} else {
self.content
}
}
}
fn texts_touch_at(content: &str, at: usize) -> bool {
let (Some(before), Some(after)) = (content.get(..at), content.get(at..)) else {
return false;
};
before.ends_with(|character: char| !character.is_whitespace())
&& after.starts_with(|character: char| !character.is_whitespace())
}
fn words_touch_at(content: &str, at: usize) -> bool {
texts_touch_at(content, at)
&& !content[..at].ends_with(['(', '[', '{'])
&& !content[at..].starts_with(['.', ',', ';', ':', '!', '?', ')', ']', '}'])
}
fn apply_label_fallbacks(tag: &tl::HTMLTag<'_>, data: &LinkData, label: &mut String, handler: &HandlerContext<'_>) {
if !data.emit_blocks_separately && label.is_empty() && !data.raw_text.is_empty() {
*label = normalize_link_label(&data.raw_text);
}
if label.is_empty() && !data.leads_nowhere_else() {
let aria_label = crate::converter::utility::attributes::decoded_attribute(tag, "aria-label");
let title = link_title(tag);
if let Some(name) = link_accessible_name(aria_label.as_deref(), title.as_deref()) {
*label = normalize_link_label(name);
}
}
let drop_link = data.is_dropped(label, handler);
if label.is_empty()
&& !data.href.is_empty()
&& !data.children.is_empty()
&& !data.empty_span_content
&& !drop_link
&& !data.href_addr_dropped
{
*label = text::escape(
&data.href,
handler.options.escape_misc,
handler.options.escape_asterisks,
handler.options.escape_underscores,
handler.options.escape_ascii,
)
.into_owned();
}
if label == "^" && data.href.starts_with('#') {
*label = "↑".to_string();
}
}
fn emit_link(
tag: &tl::HTMLTag<'_>,
data: &LinkData,
label: &str,
edges: &LabelEdges<'_>,
drop_link: bool,
handler: &mut HandlerContext<'_>,
) -> bool {
if drop_link {
return true;
}
#[cfg(feature = "visitor")]
if let Some(visitor) = handler.context.visitor.clone() {
return visit_link(tag, data, label, edges, &visitor, handler);
}
write_link_with_edges(tag, data, label, edges, handler);
true
}
fn write_link_with_edges(
tag: &tl::HTMLTag<'_>,
data: &LinkData,
label: &str,
edges: &LabelEdges<'_>,
handler: &mut HandlerContext<'_>,
) {
if label.is_empty()
&& data.empty_span_content
&& !keeps_source_white_space(handler.options, handler.context)
&& !is_fresh_block_start(handler.context, handler.context.at_fresh_block_start.get())
{
push_inline_prefix(handler.output, data.text_edges.0, handler.options, handler.context);
}
write_with_edges(edges, handler, |handler| {
write_link(handler.output, tag, data, label, handler.options, handler.context);
});
}
fn write_with_edges(
edges: &LabelEdges<'_>,
handler: &mut HandlerContext<'_>,
write_marks: impl FnOnce(&mut HandlerContext<'_>),
) {
if edges.has_text {
push_inline_prefix(handler.output, edges.prefix, handler.options, handler.context);
}
write_marks(handler);
append_inline_suffix(
handler.output,
edges.suffix,
edges.has_text,
handler.node_handle,
handler.parser,
handler.dom_context,
);
}
fn write_link(
output: &mut String,
tag: &tl::HTMLTag<'_>,
data: &LinkData,
label: &str,
options: &ConversionOptions,
context: &Context,
) {
if data.href_addr_dropped
|| data.href.is_empty()
|| (context.in_code && !context.in_code_block)
|| (context.in_code_block && normalize_link_label(label).is_empty())
{
output.push_str(label);
return;
}
append_link(
output,
&MarkdownLink {
label: &escape_link_label(label),
href: &data.href,
title: link_title(tag).as_deref(),
raw_text: label,
},
options,
context,
);
}
fn append_link(output: &mut String, link: &MarkdownLink<'_>, options: &ConversionOptions, context: &Context) {
append_markdown_link_in_context(
output,
link,
options,
context.reference_collector.as_ref(),
context.in_table_cell,
);
}
#[cfg(feature = "visitor")]
fn visit_link(
tag: &tl::HTMLTag<'_>,
data: &LinkData,
label: &str,
edges: &LabelEdges<'_>,
visitor: &crate::visitor::VisitorHandle,
handler: &mut HandlerContext<'_>,
) -> bool {
use crate::visitor::{NodeContext, NodeType, VisitResult};
let node_id = handler.node_handle.get_inner();
let node_context = NodeContext::with_lazy_attributes(
NodeType::Link,
Cow::Borrowed("a"),
tag,
handler.depth,
handler.dom_context.get_sibling_index(node_id).unwrap_or(0),
handler
.dom_context
.parent_tag_name(node_id, handler.parser)
.map(Cow::Borrowed),
true,
);
let result = visitor.lock().expect("visitor mutex poisoned").visit_link(
&node_context,
&data.href,
label,
link_title(tag).as_deref(),
);
if handler.options.whitespace_mode == crate::options::WhitespaceMode::Strict
&& (matches!(&result, VisitResult::Skip)
|| matches!(&result, VisitResult::Custom(custom)
if !data.emit_blocks_separately && custom.trim_matches([' ', '\t']).is_empty()))
{
handler.dom_context.record_absent_breaks(node_id, true);
}
match result {
VisitResult::Continue => write_link_with_edges(tag, data, label, edges, handler),
VisitResult::Custom(custom) => {
if let Some(collector) = handler.context.structure_collector.as_ref() {
collector.borrow_mut().replace_current_element(Some(&custom));
}
handler.output.push_str(&custom);
}
VisitResult::Skip => {
if let Some(collector) = handler.context.structure_collector.as_ref() {
collector.borrow_mut().replace_current_element(None);
}
return false;
}
VisitResult::Error(error) => {
if let Some(collector) = handler.context.structure_collector.as_ref() {
collector.borrow_mut().replace_current_element(None);
}
if handler.context.visitor_error.borrow().is_none() {
*handler.context.visitor_error.borrow_mut() = Some(error);
}
}
VisitResult::PreserveHtml => {
let html = serialize_node(handler.node_handle, handler.parser);
if let Some(collector) = handler.context.structure_collector.as_ref() {
collector.borrow_mut().replace_current_element(Some(&html));
}
handler.output.push_str(&html);
return false;
}
}
true
}
#[cfg(feature = "metadata")]
fn record_link_metadata(tag: &tl::HTMLTag<'_>, data: &LinkData, label: &str, context: &Context) {
if !context.metadata_wants.links {
return;
}
let Some(collector) = context.metadata_collector.as_ref() else {
return;
};
let rel = tag
.attributes()
.get("rel")
.flatten()
.map(|value| value.as_utf8_str().to_string());
let attributes = tag
.attributes()
.iter()
.filter(|(key, _)| key.as_ref() != "href")
.map(|(key, value)| {
(
key.to_string(),
value.map(|value| value.to_string()).unwrap_or_default(),
)
})
.collect::<BTreeMap<_, _>>();
collector.borrow_mut().add_link(
data.href.clone(),
label.to_string(),
link_title(tag).map(Cow::into_owned),
rel,
attributes,
);
}
fn partition_link_children(
children: &[tl::NodeHandle],
parser: &tl::Parser,
dom_ctx: &DomContext,
) -> (Vec<tl::NodeHandle>, Vec<tl::NodeHandle>) {
children
.iter()
.copied()
.partition(|child| !node_is_block_level(child, parser, dom_ctx))
}
fn should_defer_table_blocks(
ctx: &Context,
deferred: &[tl::NodeHandle],
parser: &tl::Parser,
dom_ctx: &DomContext,
) -> bool {
(!ctx.convert_as_inline || ctx.in_layout_cell)
&& !ctx.in_heading
&& deferred.iter().any(|handle| subtree_has_table(handle, parser, dom_ctx))
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn subtree_has_table(handle: &tl::NodeHandle, parser: &tl::Parser, dom_ctx: &DomContext) -> bool {
let mut pending = vec![*handle];
while let Some(handle) = pending.pop() {
let Some(tl::Node::Tag(tag)) = handle.get(parser) else {
continue;
};
if normalized_tag_name(tag.name().as_utf8_str()) == "table" {
return true;
}
let descendants_start = pending.len();
if let Some(children) = dom_ctx.children_of(handle.get_inner()) {
pending.extend(children.iter().copied());
} else {
pending.extend(tag.children().top().iter().copied());
}
pending[descendants_start..].reverse();
}
false
}