Skip to content

Commit 42cd523

Browse files
authored
Merge pull request commonmark#257 from pclouds/xml-space-preserve
Add xml:space="preserve" in XML output when appropriate
2 parents c44bced + 55afc6f commit 42cd523

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

api_test/main.c

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,9 @@ static void render_xml(test_batch_runner *runner) {
527527

528528
static const char markdown[] = "foo *bar*\n"
529529
"\n"
530-
"paragraph 2\n";
530+
"paragraph 2\n"
531+
"\n"
532+
"```\ncode\n```\n";
531533
cmark_node *doc =
532534
cmark_parse_document(markdown, sizeof(markdown) - 1, CMARK_OPT_DEFAULT);
533535

@@ -536,14 +538,16 @@ static void render_xml(test_batch_runner *runner) {
536538
"<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n"
537539
"<document xmlns=\"http://commonmark.org/xml/1.0\">\n"
538540
" <paragraph>\n"
539-
" <text>foo </text>\n"
541+
" <text xml:space=\"preserve\">foo </text>\n"
540542
" <emph>\n"
541-
" <text>bar</text>\n"
543+
" <text xml:space=\"preserve\">bar</text>\n"
542544
" </emph>\n"
543545
" </paragraph>\n"
544546
" <paragraph>\n"
545-
" <text>paragraph 2</text>\n"
547+
" <text xml:space=\"preserve\">paragraph 2</text>\n"
546548
" </paragraph>\n"
549+
" <code_block xml:space=\"preserve\">code\n"
550+
"</code_block>\n"
547551
"</document>\n",
548552
"render document");
549553
free(xml);
@@ -552,9 +556,9 @@ static void render_xml(test_batch_runner *runner) {
552556
STR_EQ(runner, xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
553557
"<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n"
554558
"<paragraph sourcepos=\"1:1-1:9\">\n"
555-
" <text sourcepos=\"1:1-1:4\">foo </text>\n"
559+
" <text sourcepos=\"1:1-1:4\" xml:space=\"preserve\">foo </text>\n"
556560
" <emph sourcepos=\"1:5-1:9\">\n"
557-
" <text sourcepos=\"1:6-1:8\">bar</text>\n"
561+
" <text sourcepos=\"1:6-1:8\" xml:space=\"preserve\">bar</text>\n"
558562
" </emph>\n"
559563
"</paragraph>\n",
560564
"render first paragraph with source pos");
@@ -902,41 +906,41 @@ static void source_pos(test_batch_runner *runner) {
902906
"<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n"
903907
"<document sourcepos=\"1:1-10:20\" xmlns=\"http://commonmark.org/xml/1.0\">\n"
904908
" <heading sourcepos=\"1:1-1:13\" level=\"1\">\n"
905-
" <text sourcepos=\"1:3-1:5\">Hi </text>\n"
909+
" <text sourcepos=\"1:3-1:5\" xml:space=\"preserve\">Hi </text>\n"
906910
" <emph sourcepos=\"1:6-1:12\">\n"
907-
" <text sourcepos=\"1:7-1:11\">there</text>\n"
911+
" <text sourcepos=\"1:7-1:11\" xml:space=\"preserve\">there</text>\n"
908912
" </emph>\n"
909-
" <text sourcepos=\"1:13-1:13\">.</text>\n"
913+
" <text sourcepos=\"1:13-1:13\" xml:space=\"preserve\">.</text>\n"
910914
" </heading>\n"
911915
" <paragraph sourcepos=\"3:1-4:42\">\n"
912-
" <text sourcepos=\"3:1-3:14\">Hello “ </text>\n"
916+
" <text sourcepos=\"3:1-3:14\" xml:space=\"preserve\">Hello “ </text>\n"
913917
" <link sourcepos=\"3:15-3:37\" destination=\"http://www.google.com\" title=\"\">\n"
914-
" <text sourcepos=\"3:16-3:36\">http://www.google.com</text>\n"
918+
" <text sourcepos=\"3:16-3:36\" xml:space=\"preserve\">http://www.google.com</text>\n"
915919
" </link>\n"
916920
" <softbreak />\n"
917-
" <text sourcepos=\"4:1-4:6\">there </text>\n"
918-
" <code sourcepos=\"4:8-4:9\">hi</code>\n"
919-
" <text sourcepos=\"4:11-4:14\"> -- </text>\n"
921+
" <text sourcepos=\"4:1-4:6\" xml:space=\"preserve\">there </text>\n"
922+
" <code sourcepos=\"4:8-4:9\" xml:space=\"preserve\">hi</code>\n"
923+
" <text sourcepos=\"4:11-4:14\" xml:space=\"preserve\"> -- </text>\n"
920924
" <link sourcepos=\"4:15-4:41\" destination=\"www.google.com\" title=\"ok\">\n"
921-
" <text sourcepos=\"4:16-4:19\">okay</text>\n"
925+
" <text sourcepos=\"4:16-4:19\" xml:space=\"preserve\">okay</text>\n"
922926
" </link>\n"
923-
" <text sourcepos=\"4:42-4:42\">.</text>\n"
927+
" <text sourcepos=\"4:42-4:42\" xml:space=\"preserve\">.</text>\n"
924928
" </paragraph>\n"
925929
" <block_quote sourcepos=\"6:1-10:20\">\n"
926930
" <list sourcepos=\"6:3-10:20\" type=\"ordered\" start=\"1\" delim=\"period\" tight=\"false\">\n"
927931
" <item sourcepos=\"6:3-8:1\">\n"
928932
" <paragraph sourcepos=\"6:6-7:10\">\n"
929-
" <text sourcepos=\"6:6-6:10\">Okay.</text>\n"
933+
" <text sourcepos=\"6:6-6:10\" xml:space=\"preserve\">Okay.</text>\n"
930934
" <softbreak />\n"
931-
" <text sourcepos=\"7:6-7:10\">Sure.</text>\n"
935+
" <text sourcepos=\"7:6-7:10\" xml:space=\"preserve\">Sure.</text>\n"
932936
" </paragraph>\n"
933937
" </item>\n"
934938
" <item sourcepos=\"9:3-10:20\">\n"
935939
" <paragraph sourcepos=\"9:6-10:20\">\n"
936-
" <text sourcepos=\"9:6-9:15\">Yes, okay.</text>\n"
940+
" <text sourcepos=\"9:6-9:15\" xml:space=\"preserve\">Yes, okay.</text>\n"
937941
" <softbreak />\n"
938942
" <image sourcepos=\"10:6-10:20\" destination=\"hi\" title=\"yes\">\n"
939-
" <text sourcepos=\"10:8-10:9\">ok</text>\n"
943+
" <text sourcepos=\"10:8-10:9\" xml:space=\"preserve\">ok</text>\n"
940944
" </image>\n"
941945
" </paragraph>\n"
942946
" </item>\n"
@@ -960,11 +964,11 @@ static void ref_source_pos(test_batch_runner *runner) {
960964
"<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n"
961965
"<document sourcepos=\"1:1-3:40\" xmlns=\"http://commonmark.org/xml/1.0\">\n"
962966
" <paragraph sourcepos=\"1:1-1:28\">\n"
963-
" <text sourcepos=\"1:1-1:10\">Let's try </text>\n"
967+
" <text sourcepos=\"1:1-1:10\" xml:space=\"preserve\">Let's try </text>\n"
964968
" <link sourcepos=\"1:11-1:21\" destination=\"https://github.com\" title=\"GitHub\">\n"
965-
" <text sourcepos=\"1:12-1:20\">reference</text>\n"
969+
" <text sourcepos=\"1:12-1:20\" xml:space=\"preserve\">reference</text>\n"
966970
" </link>\n"
967-
" <text sourcepos=\"1:22-1:28\"> links.</text>\n"
971+
" <text sourcepos=\"1:22-1:28\" xml:space=\"preserve\"> links.</text>\n"
968972
" </paragraph>\n"
969973
"</document>\n",
970974
"sourcepos are as expected");

src/xml.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static int S_render_node(cmark_node *node, cmark_event_type ev_type,
6060
case CMARK_NODE_CODE:
6161
case CMARK_NODE_HTML_BLOCK:
6262
case CMARK_NODE_HTML_INLINE:
63-
cmark_strbuf_puts(xml, ">");
63+
cmark_strbuf_puts(xml, " xml:space=\"preserve\">");
6464
escape_xml(xml, node->as.literal.data, node->as.literal.len);
6565
cmark_strbuf_puts(xml, "</");
6666
cmark_strbuf_puts(xml, cmark_node_get_type_string(node));
@@ -100,7 +100,7 @@ static int S_render_node(cmark_node *node, cmark_event_type ev_type,
100100
escape_xml(xml, node->as.code.info.data, node->as.code.info.len);
101101
cmark_strbuf_putc(xml, '"');
102102
}
103-
cmark_strbuf_puts(xml, ">");
103+
cmark_strbuf_puts(xml, " xml:space=\"preserve\">");
104104
escape_xml(xml, node->as.code.literal.data, node->as.code.literal.len);
105105
cmark_strbuf_puts(xml, "</");
106106
cmark_strbuf_puts(xml, cmark_node_get_type_string(node));

0 commit comments

Comments
 (0)