From 55afc6fd496de4e75c27d725c2bf1470cc3cf5aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Tue, 1 Aug 2017 19:28:16 +0700 Subject: Add xml:space="preserve" in XML output when appropriate With current HTML escaping, sometimes we may produce an XML tag like Many XML parsers consider these spaces insignificant and strip them out but we need this. There's actually a test case like this in spec.txt (search "A code block can have all empty lines as its content:") and without proper hinting, an external xml->html converter will fail the spec. XML standard covers this case. If xml:space is "preserve", then whitespaces are significant and should be kept. Add this hint for text, code, code_block, html_inline and html_block tags. --- api_test/main.c | 50 +++++++++++++++++++++++++++----------------------- src/xml.c | 4 ++-- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/api_test/main.c b/api_test/main.c index c30dc71..a843530 100644 --- a/api_test/main.c +++ b/api_test/main.c @@ -527,7 +527,9 @@ static void render_xml(test_batch_runner *runner) { static const char markdown[] = "foo *bar*\n" "\n" - "paragraph 2\n"; + "paragraph 2\n" + "\n" + "```\ncode\n```\n"; cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1, CMARK_OPT_DEFAULT); @@ -536,14 +538,16 @@ static void render_xml(test_batch_runner *runner) { "\n" "\n" " \n" - " foo \n" + " foo \n" " \n" - " bar\n" + " bar\n" " \n" " \n" " \n" - " paragraph 2\n" + " paragraph 2\n" " \n" + " code\n" + "\n" "\n", "render document"); free(xml); @@ -552,9 +556,9 @@ static void render_xml(test_batch_runner *runner) { STR_EQ(runner, xml, "\n" "\n" "\n" - " foo \n" + " foo \n" " \n" - " bar\n" + " bar\n" " \n" "\n", "render first paragraph with source pos"); @@ -902,41 +906,41 @@ static void source_pos(test_batch_runner *runner) { "\n" "\n" " \n" - " Hi \n" + " Hi \n" " \n" - " there\n" + " there\n" " \n" - " .\n" + " .\n" " \n" " \n" - " Hello “ \n" + " Hello “ \n" " \n" - " http://www.google.com\n" + " http://www.google.com\n" " \n" " \n" - " there \n" - " hi\n" - " -- \n" + " there \n" + " hi\n" + " -- \n" " \n" - " okay\n" + " okay\n" " \n" - " .\n" + " .\n" " \n" " \n" " \n" " \n" " \n" - " Okay.\n" + " Okay.\n" " \n" - " Sure.\n" + " Sure.\n" " \n" " \n" " \n" " \n" - " Yes, okay.\n" + " Yes, okay.\n" " \n" " \n" - " ok\n" + " ok\n" " \n" " \n" " \n" @@ -960,11 +964,11 @@ static void ref_source_pos(test_batch_runner *runner) { "\n" "\n" " \n" - " Let's try \n" + " Let's try \n" " \n" - " reference\n" + " reference\n" " \n" - " links.\n" + " links.\n" " \n" "\n", "sourcepos are as expected"); diff --git a/src/xml.c b/src/xml.c index 4898cd2..48674cc 100644 --- a/src/xml.c +++ b/src/xml.c @@ -60,7 +60,7 @@ static int S_render_node(cmark_node *node, cmark_event_type ev_type, case CMARK_NODE_CODE: case CMARK_NODE_HTML_BLOCK: case CMARK_NODE_HTML_INLINE: - cmark_strbuf_puts(xml, ">"); + cmark_strbuf_puts(xml, " xml:space=\"preserve\">"); escape_xml(xml, node->as.literal.data, node->as.literal.len); cmark_strbuf_puts(xml, "as.code.info.data, node->as.code.info.len); cmark_strbuf_putc(xml, '"'); } - cmark_strbuf_puts(xml, ">"); + cmark_strbuf_puts(xml, " xml:space=\"preserve\">"); escape_xml(xml, node->as.code.literal.data, node->as.code.literal.len); cmark_strbuf_puts(xml, "