use super::FormContext;
use super::option::{handle_option, handle_option_list};
use super::spacing::{ControlStart, line_goes_on_after_parent, separate_from_next_text};
use super::walk_node;
use crate::converter::walk::{Close, Open};
#[cfg(feature = "visitor")]
use std::borrow::Cow;
#[cfg(feature = "visitor")]
pub fn dispatch_form_visitor(
node_handle: &crate::dom::NodeHandle,
tag: &crate::dom::HTMLTag,
parser: &crate::dom::Parser,
output: &mut String,
context: FormContext<'_>,
) -> bool {
let FormContext {
ctx, depth, dom_ctx, ..
} = context;
use crate::visitor::{NodeContext, NodeType, VisitResult};
let Some(ref visitor_handle) = ctx.visitor else {
return false;
};
let node_id = node_handle.get_inner();
let parent_tag = dom_ctx.parent_tag_name(node_id, parser);
let index_in_parent = dom_ctx.get_sibling_index(node_id).unwrap_or(0);
let action = tag
.attributes()
.get("action")
.flatten()
.map(|v| v.as_utf8_str().into_owned());
let method = tag
.attributes()
.get("method")
.flatten()
.map(|v| v.as_utf8_str().into_owned());
let node_ctx = NodeContext::with_lazy_attributes(
NodeType::Form,
Cow::Borrowed("form"),
tag,
depth,
index_in_parent,
parent_tag.map(Cow::Borrowed),
false,
);
let visit_result = {
let mut visitor = visitor_handle.lock().expect("visitor mutex poisoned");
visitor.visit_form(&node_ctx, action.as_deref(), method.as_deref())
};
match visit_result {
VisitResult::Continue => false,
VisitResult::Skip => true,
VisitResult::Custom(custom) => {
if !output.is_empty() && !output.ends_with("\n\n") {
output.push_str("\n\n");
#[cfg(feature = "metadata")]
crate::converter::heading_metadata::append(ctx, "\n\n");
}
output.push_str(&custom);
#[cfg(feature = "metadata")]
crate::converter::heading_metadata::append(ctx, &custom);
output.push_str("\n\n");
#[cfg(feature = "metadata")]
crate::converter::heading_metadata::append(ctx, "\n\n");
true
}
VisitResult::PreserveHtml => {
use crate::converter::utility::serialization::serialize_node;
output.push_str(&serialize_node(node_handle, parser));
true
}
VisitResult::Error(err) => {
if ctx.visitor_error.borrow().is_none() {
*ctx.visitor_error.borrow_mut() = Some(err);
}
true
}
}
}
#[cfg_attr(not(feature = "visitor"), allow(unused_variables))]
pub fn handle_form(
_tag_name: &str,
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
context: FormContext<'_>,
) {
let FormContext {
options,
ctx,
depth,
dom_ctx,
} = context;
if let Some(crate::dom::Node::Tag(tag)) = node_handle.get(parser) {
#[cfg(feature = "visitor")]
if dispatch_form_visitor(node_handle, tag, parser, output, context) {
return;
}
if ctx.convert_as_inline {
let children = tag.children();
{
for child_handle in children.top() {
super::walk_node(
child_handle,
parser,
output,
crate::converter::block::container::HandlerContext::new(options, ctx, depth + 1, dom_ctx),
);
}
}
return;
}
let mut rendered = String::new();
let children = tag.children();
{
let block_ctx = super::Context {
list_item_open: false,
..ctx.clone()
};
for child_handle in children.top() {
walk_node(
child_handle,
parser,
&mut rendered,
crate::converter::block::container::HandlerContext::new(options, &block_ctx, depth + 1, dom_ctx),
);
}
}
let trimmed = rendered.trim();
if !trimmed.is_empty() {
if !output.is_empty() && !output.ends_with("\n\n") {
output.push_str("\n\n");
}
output.push_str(trimmed);
output.push_str("\n\n");
}
}
}
pub fn handle_fieldset(
_tag_name: &str,
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
context: FormContext<'_>,
) {
let FormContext {
options,
ctx,
depth,
dom_ctx,
} = context;
if ctx.in_code && !ctx.in_code_block {
if let Some(crate::dom::Node::Tag(tag)) = node_handle.get(parser) {
write_code_form_group(tag, parser, output, context);
}
return;
}
crate::converter::block::div::handle(
node_handle,
parser,
output,
crate::converter::block::container::HandlerContext::new(options, ctx, depth, dom_ctx),
);
}
pub fn handle_legend(
_tag_name: &str,
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
context: FormContext<'_>,
) {
let FormContext {
options,
ctx,
depth,
dom_ctx,
} = context;
if let Some(crate::dom::Node::Tag(tag)) = node_handle.get(parser) {
if ctx.in_code && !ctx.in_code_block {
write_code_form_group(tag, parser, output, context);
return;
}
let mut rendered = String::new();
let mut legend_ctx = ctx.inline_buffer(output, !ctx.convert_as_inline);
if !ctx.convert_as_inline {
legend_ctx.in_strong = true;
}
let children = tag.children();
{
for child_handle in children.top() {
super::walk_node(
child_handle,
parser,
&mut rendered,
crate::converter::block::container::HandlerContext::new(options, &legend_ctx, depth + 1, dom_ctx),
);
}
}
let trimmed = rendered.trim();
if !trimmed.is_empty() {
if ctx.convert_as_inline {
output.push_str(trimmed);
} else {
let mut bold = String::with_capacity(trimmed.len() + 4);
crate::converter::inline::emphasis::emit_strong_wrapped_blocks(
&mut bold,
trimmed,
crate::converter::inline::wrapped::InlineSite {
node_handle,
parser,
dom_ctx,
ctx,
options,
},
);
crate::converter::block::div::push_block(output, options, ctx, &bold);
}
}
}
}
fn write_code_form_group(
tag: &crate::dom::HTMLTag<'_>,
parser: &crate::dom::Parser<'_>,
output: &mut String,
context: FormContext<'_>,
) {
let original_len = output.len();
if !output.is_empty() && !output.ends_with(char::is_whitespace) {
output.push(' ');
}
let content_start = output.len();
let code_ctx = super::Context {
convert_as_inline: true,
..context.ctx.clone()
};
for child in tag.children().top() {
walk_node(
child,
parser,
output,
crate::converter::block::container::HandlerContext::new(
context.options,
&code_ctx,
context.depth + 1,
context.dom_ctx,
),
);
}
if output.len() == content_start {
output.truncate(original_len);
} else if !output.ends_with(char::is_whitespace) {
output.push(' ');
}
}
pub fn handle_label(
_tag_name: &str,
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
context: FormContext<'_>,
) {
let FormContext {
options,
ctx,
depth,
dom_ctx,
} = context;
if let Some(crate::dom::Node::Tag(tag)) = node_handle.get(parser) {
let children = tag.children();
if ctx.in_code_block {
for child_handle in children.top() {
super::walk_node(
child_handle,
parser,
output,
crate::converter::block::container::HandlerContext::new(options, ctx, depth + 1, dom_ctx),
);
}
return;
}
let mut rendered = String::new();
let label_ctx = ctx.inline_buffer(output, false);
let first = first_content(tag, parser);
let mut first_button_wrote = false;
{
for child_handle in children.top() {
ctx.button_wrote.set(false);
super::walk_node(
child_handle,
parser,
&mut rendered,
crate::converter::block::container::HandlerContext::new(options, &label_ctx, depth + 1, dom_ctx),
);
if first.is_some_and(|handle| handle.get_inner() == child_handle.get_inner()) {
first_button_wrote = ctx.button_wrote.get();
}
}
}
let trimmed = rendered.trim();
if !trimmed.is_empty() {
let start = ControlStart::new(output);
output.push_str(trimmed);
if !ctx.in_code && starts_with_control(first, parser, dom_ctx, first_button_wrote) {
start.finish(output);
}
if rendered.ends_with([' ', '\t']) {
output.push(' ');
}
}
}
}
fn first_content(tag: &crate::dom::HTMLTag, parser: &crate::dom::Parser) -> Option<crate::dom::NodeHandle> {
tag.children()
.top()
.iter()
.copied()
.find(|child| first_visible_content(*child, parser).is_some())
}
fn first_visible_content(
handle: crate::dom::NodeHandle,
parser: &crate::dom::Parser,
) -> Option<crate::dom::NodeHandle> {
let mut pending = vec![handle];
while let Some(handle) = pending.pop() {
match handle.get(parser) {
Some(crate::dom::Node::Raw(raw)) if !raw.as_utf8_str().trim().is_empty() => return Some(handle),
Some(crate::dom::Node::Tag(tag)) if tag.name().as_utf8_str().eq_ignore_ascii_case("span") => {
let start = pending.len();
pending.extend(tag.children().top().iter().copied());
pending[start..].reverse();
}
Some(crate::dom::Node::Tag(_)) => return Some(handle),
_ => {}
}
}
None
}
fn starts_with_control(
first: Option<crate::dom::NodeHandle>,
parser: &crate::dom::Parser,
dom_ctx: &crate::converter::DomContext,
button_wrote: bool,
) -> bool {
let Some(handle) = first.and_then(|handle| first_visible_content(handle, parser)) else {
return false;
};
let Some(crate::dom::Node::Tag(control)) = handle.get(parser) else {
return false;
};
match crate::converter::utility::content::normalized_tag_name(control.name().as_utf8_str()).as_ref() {
"input" => super::spacing::white_space_follows(handle.get_inner(), parser, dom_ctx),
"button" => button_wrote,
name => super::spacing::writes_own_text(name),
}
}
fn has_checkbox_type(tag: &crate::dom::HTMLTag) -> bool {
tag.attributes()
.get("type")
.flatten()
.is_some_and(|value| value.as_utf8_str().eq_ignore_ascii_case("checkbox"))
}
pub fn checkbox_state(tag: &crate::dom::HTMLTag) -> Option<bool> {
let attributes = tag.attributes();
let is_checkbox = has_checkbox_type(tag);
let has_other_role = attributes.get("role").flatten().is_some_and(|role| {
role.as_utf8_str().split_ascii_whitespace().next().is_some_and(|first| {
!["checkbox", "switch", "menuitemcheckbox"]
.iter()
.any(|checkbox_role| first.eq_ignore_ascii_case(checkbox_role))
})
});
(is_checkbox && !has_other_role).then(|| attributes.get("checked").is_some())
}
fn emit_input(
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
context: FormContext<'_>,
) {
let FormContext {
options, ctx, dom_ctx, ..
} = context;
let Some(crate::dom::Node::Tag(tag)) = node_handle.get(parser) else {
return;
};
if !has_checkbox_type(tag) {
return;
}
let id = node_handle.get_inner();
let space_goes = if crate::converter::utility::content::keeps_source_white_space(options, ctx) {
output.ends_with(' ')
} else {
ends_with_a_space_between_words(output)
};
match checkbox_state(tag) {
Some(checked) if ctx.in_cell_of_inputs => output.push_str(if checked { "[x]" } else { "[ ]" }),
_ if !ctx.in_code && space_goes && super::spacing::white_space_follows(id, parser, dom_ctx) => {
output.pop();
}
_ => {}
}
}
fn ends_with_a_space_between_words(output: &str) -> bool {
let before_spaces = output.trim_end_matches(' ');
before_spaces.len() < output.len() && !before_spaces.is_empty() && !before_spaces.ends_with('\n')
}
#[cfg_attr(not(feature = "visitor"), allow(unused_variables))]
pub fn handle_input(
_tag_name: &str,
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
context: FormContext<'_>,
) {
let FormContext {
ctx, depth, dom_ctx, ..
} = context;
#[cfg(feature = "visitor")]
if let Some(ref visitor_handle) = ctx.visitor {
use crate::visitor::{NodeContext, NodeType, VisitResult};
if let Some(crate::dom::Node::Tag(tag)) = node_handle.get(parser) {
let node_id = node_handle.get_inner();
let parent_tag = dom_ctx.parent_tag_name(node_id, parser);
let index_in_parent = dom_ctx.get_sibling_index(node_id).unwrap_or(0);
let input_type = tag
.attributes()
.get("type")
.flatten()
.map_or_else(|| std::borrow::Cow::Borrowed("text"), |v| v.as_utf8_str())
.into_owned();
let name = tag
.attributes()
.get("name")
.flatten()
.map(|v| v.as_utf8_str().into_owned());
let value = tag
.attributes()
.get("value")
.flatten()
.map(|v| v.as_utf8_str().into_owned());
let node_ctx = NodeContext::with_lazy_attributes(
NodeType::Input,
Cow::Borrowed("input"),
tag,
depth,
index_in_parent,
parent_tag.map(Cow::Borrowed),
true,
);
let visit_result = {
let mut visitor = visitor_handle.lock().expect("visitor mutex poisoned");
visitor.visit_input(&node_ctx, &input_type, name.as_deref(), value.as_deref())
};
match visit_result {
VisitResult::Continue => {}
VisitResult::Skip => return,
VisitResult::Custom(custom) => {
output.push_str(&custom);
return;
}
VisitResult::PreserveHtml => {
use crate::converter::utility::serialization::serialize_node;
output.push_str(&serialize_node(node_handle, parser));
return;
}
VisitResult::Error(err) => {
if ctx.visitor_error.borrow().is_none() {
*ctx.visitor_error.borrow_mut() = Some(err);
}
return;
}
}
}
}
emit_input(node_handle, parser, output, context);
}
pub fn open_line_end_control(
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &str,
children: &mut Vec<crate::dom::NodeHandle>,
) -> Option<Open> {
let Some(crate::dom::Node::Tag(tag)) = node_handle.get(parser) else {
return None;
};
children.extend(tag.children().top().iter().copied());
Some(Open {
scope: None,
close: Close::LineEndControl(LineEndControlEnd {
start: ControlStart::new(output),
node: *node_handle,
}),
})
}
pub struct LineEndControlEnd {
start: ControlStart,
node: crate::dom::NodeHandle,
}
pub fn close_line_end_control(
end: &LineEndControlEnd,
parser: &crate::dom::Parser,
output: &mut String,
handler: crate::converter::block::container::HandlerContext<'_>,
) {
let LineEndControlEnd {
start,
node: node_handle,
} = end;
let node_handle = *node_handle;
let ctx = handler.ctx;
let dom_ctx = handler.dom_ctx;
if ctx.in_code || !start.finish(output) {
return;
}
if ctx.convert_as_inline || line_goes_on_after_parent(node_handle.get_inner(), parser, dom_ctx) {
separate_from_next_text(output, node_handle, parser, dom_ctx);
} else {
output.push_str("\n\n");
}
}
#[cfg(feature = "visitor")]
pub fn visit_button(
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
context: FormContext<'_>,
) -> bool {
let FormContext {
ctx, depth, dom_ctx, ..
} = context;
if let Some(crate::dom::Node::Tag(tag)) = node_handle.get(parser) {
if let Some(ref visitor_handle) = ctx.visitor {
use crate::converter::get_text_content;
use crate::visitor::{NodeContext, NodeType, VisitResult};
let text = get_text_content(node_handle, parser, dom_ctx);
let node_id = node_handle.get_inner();
let parent_tag = dom_ctx.parent_tag_name(node_id, parser);
let index_in_parent = dom_ctx.get_sibling_index(node_id).unwrap_or(0);
let node_ctx = NodeContext::with_lazy_attributes(
NodeType::Button,
Cow::Borrowed("button"),
tag,
depth,
index_in_parent,
parent_tag.map(Cow::Borrowed),
true,
);
let visit_result = {
let mut visitor = visitor_handle.lock().expect("visitor mutex poisoned");
visitor.visit_button(&node_ctx, &text)
};
match visit_result {
VisitResult::Continue => {}
VisitResult::Skip => return true,
VisitResult::Custom(custom) => {
#[cfg(feature = "metadata")]
crate::converter::heading_metadata::append(ctx, &custom);
output.push_str(&custom);
return true;
}
VisitResult::PreserveHtml => {
use crate::converter::utility::serialization::serialize_node;
output.push_str(&serialize_node(node_handle, parser));
return true;
}
VisitResult::Error(err) => {
if ctx.visitor_error.borrow().is_none() {
*ctx.visitor_error.borrow_mut() = Some(err);
}
return true;
}
}
}
}
false
}
pub fn handle(
tag_name: &str,
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
context: FormContext<'_>,
) {
match tag_name {
"form" => handle_form(tag_name, node_handle, parser, output, context),
"fieldset" => handle_fieldset(tag_name, node_handle, parser, output, context),
"legend" => handle_legend(tag_name, node_handle, parser, output, context),
"label" => handle_label(tag_name, node_handle, parser, output, context),
"input" => handle_input(tag_name, node_handle, parser, output, context),
"select" | "datalist" => handle_option_list(tag_name, node_handle, parser, output, context),
"option" if !context.ctx.in_option_list => {
handle_option_list(tag_name, node_handle, parser, output, context);
}
"option" => handle_option(tag_name, node_handle, parser, output, context),
_ => {}
}
}
pub struct ButtonEnd {
start: ControlStart,
content_start: usize,
node: crate::dom::NodeHandle,
}
pub fn open_button(
node: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &str,
children: &mut Vec<crate::dom::NodeHandle>,
) -> Option<Open> {
let Some(crate::dom::Node::Tag(tag)) = node.get(parser) else {
return None;
};
children.extend(tag.children().top().iter().copied());
Some(Open {
scope: None,
close: Close::Button(ButtonEnd {
start: ControlStart::new(output),
content_start: output.len(),
node: *node,
}),
})
}
pub fn close_button(
end: &ButtonEnd,
parser: &crate::dom::Parser,
output: &mut String,
handler: crate::converter::block::container::HandlerContext<'_>,
) {
if output
.get(..end.content_start)
.is_some_and(|before| before.trim().is_empty())
{
handler.ctx.button_wrote.set(
output
.get(end.content_start..)
.is_some_and(|content| !content.trim().is_empty()),
);
}
if !handler.ctx.in_code && end.start.finish(output) {
separate_from_next_text(output, end.node, parser, handler.dom_ctx);
}
}
pub struct CodeGroupEnd {
original_len: usize,
content_start: usize,
}
pub fn open_fieldset(
node: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
handler: crate::converter::block::container::HandlerContext<'_>,
children: &mut Vec<crate::dom::NodeHandle>,
) -> Option<Open> {
if handler.ctx.in_code && !handler.ctx.in_code_block {
return open_code_group(node, parser, output, handler, children);
}
crate::converter::block::div::open(node, parser, output, handler, children)
}
fn open_code_group(
node: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
handler: crate::converter::block::container::HandlerContext<'_>,
children: &mut Vec<crate::dom::NodeHandle>,
) -> Option<Open> {
let Some(crate::dom::Node::Tag(tag)) = node.get(parser) else {
return None;
};
let original_len = output.len();
if !output.is_empty() && !output.ends_with(char::is_whitespace) {
output.push(' ');
}
let end = CodeGroupEnd {
original_len,
content_start: output.len(),
};
children.extend(tag.children().top().iter().copied());
Some(Open {
scope: Some(std::rc::Rc::new(super::Context {
convert_as_inline: true,
..handler.ctx.clone()
})),
close: Close::CodeGroup(end),
})
}
pub fn close_code_group(end: &CodeGroupEnd, output: &mut String) {
if output.len() == end.content_start {
output.truncate(end.original_len);
} else if !output.ends_with(char::is_whitespace) {
output.push(' ');
}
}
pub fn open_label(
node: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &str,
context: FormContext<'_>,
children: &mut Vec<crate::dom::NodeHandle>,
) -> Option<Open> {
let Some(crate::dom::Node::Tag(tag)) = node.get(parser) else {
return None;
};
children.extend(tag.children().top().iter().copied());
if context.ctx.in_code_block {
return Some(Open::children_only());
}
let first = first_content(tag, parser);
Some(Open {
scope: None,
close: Close::Hooked(Box::new(LabelHook {
content: String::new(),
ctx: std::rc::Rc::new(context.ctx.inline_buffer(output, false)),
handles: children.clone(),
first,
first_button_wrote: false,
})),
})
}
struct LabelHook {
content: String,
ctx: std::rc::Rc<super::Context>,
handles: Vec<crate::dom::NodeHandle>,
first: Option<crate::dom::NodeHandle>,
first_button_wrote: bool,
}
impl crate::converter::walk::ChildHook for LabelHook {
fn before_child(
&mut self,
_index: usize,
site: &mut crate::converter::walk::HookSite<'_>,
) -> crate::converter::walk::Child {
site.context.button_wrote.set(false);
crate::converter::walk::Child::Walk {
depth: None,
buffer: Some(std::mem::take(&mut self.content)),
scope: Some(std::rc::Rc::clone(&self.ctx)),
}
}
fn after_child(&mut self, index: usize, own: Option<String>, site: &mut crate::converter::walk::HookSite<'_>) {
self.content = own.unwrap_or_default();
if self.first == Some(self.handles[index]) {
self.first_button_wrote = site.context.button_wrote.get();
}
}
fn close(self: Box<Self>, site: &mut crate::converter::walk::HookSite<'_>) {
let trimmed = self.content.trim();
if trimmed.is_empty() {
crate::converter::inline::wrapped::emit_whitespace_only_inline_body(&self.content, site.output);
return;
}
if self.content.trim_start_matches([' ', '\t']).starts_with('\n') && !site.output.is_empty() {
site.output
.push_str(crate::converter::main_helpers::hard_break_marker(site.options));
}
let start = ControlStart::new(site.output);
site.output.push_str(trimmed);
if !site.context.in_code
&& starts_with_control(self.first, site.parser, site.dom_context, self.first_button_wrote)
{
start.finish(site.output);
}
let (_, suffix, _) = crate::converter::chomp_inline(&self.content);
if !suffix.is_empty() {
site.output.push_str(suffix);
}
}
}
pub fn open_legend(
node: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
handler: crate::converter::block::container::HandlerContext<'_>,
children: &mut Vec<crate::dom::NodeHandle>,
) -> Option<Open> {
if handler.ctx.in_code && !handler.ctx.in_code_block {
return open_code_group(node, parser, output, handler, children);
}
let Some(crate::dom::Node::Tag(tag)) = node.get(parser) else {
return None;
};
children.extend(tag.children().top().iter().copied());
let mut child_ctx = handler.ctx.inline_buffer(output, !handler.ctx.convert_as_inline);
if !handler.ctx.convert_as_inline {
child_ctx.in_strong = true;
}
Some(Open {
scope: Some(std::rc::Rc::new(child_ctx)),
close: Close::Capture(crate::converter::walk::CaptureEnd::Legend),
})
}
pub fn close_legend(
rendered: &str,
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
handler: crate::converter::block::container::HandlerContext<'_>,
) {
let options = handler.options;
let ctx = handler.ctx;
let dom_ctx = handler.dom_ctx;
let trimmed = rendered.trim();
if !trimmed.is_empty() {
if ctx.convert_as_inline {
output.push_str(trimmed);
} else {
let mut bold = String::with_capacity(trimmed.len() + 4);
crate::converter::inline::emphasis::emit_strong_wrapped_blocks(
&mut bold,
trimmed,
crate::converter::inline::wrapped::InlineSite {
node_handle,
parser,
dom_ctx,
ctx,
options,
},
);
crate::converter::block::div::push_block(output, options, ctx, &bold);
}
}
}