use super::FormContext;
use super::spacing::separate_from_next_text;
use super::walk_node;
use crate::converter::walk::{Close, Open};
pub fn handle_option_list(
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 mut rendered = String::new();
let children_ctx = if matches!(tag_name, "select" | "datalist") {
ctx.inline_buffer(output, false)
} else {
ctx.clone()
};
let list_ctx = super::Context {
in_option_list: true,
..children_ctx
};
if tag_name == "option" {
let list_context = FormContext {
ctx: &list_ctx,
..context
};
handle_option(tag_name, node_handle, parser, &mut rendered, list_context);
} else {
for child_handle in tag.children().top() {
walk_node(
child_handle,
parser,
&mut rendered,
crate::converter::block::container::HandlerContext::new(options, &list_ctx, depth + 1, dom_ctx),
);
}
}
if super::spacing::write_option_list(output, &rendered, ctx.in_code) {
separate_from_next_text(output, *node_handle, parser, dom_ctx);
}
}
}
pub fn handle_option(
_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 mut text = String::new();
let children = tag.children();
{
for child_handle in children.top() {
super::walk_node(
child_handle,
parser,
&mut text,
crate::converter::block::container::HandlerContext::new(options, ctx, depth + 1, dom_ctx),
);
}
}
super::spacing::write_option(output, &text);
}
}
pub fn open_option(
kind: &str,
node: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &str,
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 list = kind != "option" || !handler.ctx.in_option_list;
let child_ctx = if matches!(kind, "select" | "datalist") {
handler.ctx.inline_buffer(output, false)
} else {
handler.ctx.clone()
};
children.extend(tag.children().top().iter().copied());
Some(Open {
scope: Some(std::rc::Rc::new(super::Context {
in_option_list: true,
..child_ctx
})),
close: Close::Capture(crate::converter::walk::CaptureEnd::Option(OptionEnd {
list,
word: kind == "option",
})),
})
}
pub struct OptionEnd {
pub(crate) list: bool,
pub(crate) word: bool,
}
pub fn close_option(
end: &OptionEnd,
content: &str,
node: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
handler: crate::converter::block::container::HandlerContext<'_>,
) {
if !end.list {
super::spacing::write_option(output, content);
return;
}
let mut rendered = String::new();
let content = if end.word {
super::spacing::write_option(&mut rendered, content);
&rendered
} else {
content
};
if super::spacing::write_option_list(output, content, handler.ctx.in_code) {
separate_from_next_text(output, *node, parser, handler.dom_ctx);
}
}