use markdown2pdf::markdown::*;
use super::common::parse;
fn first_html_block(tokens: &[Token]) -> Option<String> {
tokens.iter().find_map(|t| match t {
Token::HtmlBlock(s) => Some(s.clone()),
_ => None,
})
}
#[test]
fn doctype_html() {
let tokens = parse("<!DOCTYPE html>\n");
assert_eq!(
first_html_block(&tokens).as_deref(),
Some("<!DOCTYPE html>\n"),
);
}
#[test]
fn doctype_uppercase_keyword() {
let tokens = parse("<!DOCTYPE HTML>\n");
assert!(first_html_block(&tokens).is_some());
}
#[test]
fn element_declaration() {
let tokens = parse("<!ELEMENT br EMPTY>\n");
assert_eq!(
first_html_block(&tokens).as_deref(),
Some("<!ELEMENT br EMPTY>\n"),
);
}
#[test]
fn declaration_with_attlist() {
let tokens = parse("<!ATTLIST foo bar CDATA #IMPLIED>\n");
assert!(first_html_block(&tokens).is_some());
}
#[test]
fn declaration_with_one_space_indent() {
let tokens = parse(" <!DOCTYPE html>\n");
let block = first_html_block(&tokens).expect("expected HtmlBlock");
assert!(block.starts_with(" <!"), "got {:?}", block);
}
#[test]
fn declaration_with_three_space_indent() {
let tokens = parse(" <!DOCTYPE html>\n");
let block = first_html_block(&tokens).expect("expected HtmlBlock");
assert!(block.starts_with(" <!"), "got {:?}", block);
}
#[test]
fn four_space_indent_is_code_block_not_html() {
let tokens = parse(" <!DOCTYPE html>\n");
assert!(first_html_block(&tokens).is_none());
assert!(tokens.iter().any(|t| matches!(t, Token::Code { block: true, .. })));
}
#[test]
fn declaration_not_at_line_start_is_inline_text() {
let tokens = parse("paragraph <!DOCTYPE html>\n");
assert!(first_html_block(&tokens).is_none());
}
#[test]
fn declaration_inside_blockquote() {
let tokens = parse("> <!DOCTYPE html>\n");
let Some(Token::BlockQuote(body)) = tokens.first() else {
panic!("expected BlockQuote, got {:?}", tokens);
};
assert!(
body.iter().any(|t| matches!(t, Token::HtmlBlock(_))),
"expected HtmlBlock inside BlockQuote, got {:?}",
body
);
}
#[test]
fn opener_without_letter_is_not_a_declaration() {
let tokens = parse("<!?> not a declaration\n");
assert!(first_html_block(&tokens).is_none());
}
#[test]
fn unterminated_declaration_falls_through() {
let tokens = parse("<!DOCTYPE unterminated\nmore text\n");
assert!(first_html_block(&tokens).is_none());
}