From 2fa6122a3cfa0e801f4a36b39f50e0bf7d575e4b Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Sun, 20 Mar 2022 20:59:28 +0100 Subject: fixup! Nearly pretty-print subtitle XML (though not in nodes). --- src/subtitle_asset.cc | 10 +- test/interop_subtitle_test.cc | 60 ++++---- test/ref/write_interop_subtitle_test3/ASSETMAP | 2 +- .../pkl_e94b8a0d-27f7-408a-af16-78d3df419a91.xml | 6 +- test/ref/write_interop_subtitle_test3/subs.xml | 10 +- test/shared_subtitle_test.cc | 12 ++ test/smpte_subtitle_test.cc | 152 ++++++++++----------- test/verify_test.cc | 4 +- 8 files changed, 138 insertions(+), 118 deletions(-) diff --git a/src/subtitle_asset.cc b/src/subtitle_asset.cc index de6332e6..bd8bb993 100644 --- a/src/subtitle_asset.cc +++ b/src/subtitle_asset.cc @@ -803,6 +803,8 @@ SubtitleAsset::fix_empty_font_ids () } +namespace { + struct State { int indent; @@ -810,13 +812,19 @@ struct State int disable_formatting; }; +} + static void format_xml_node (xmlpp::Node const* node, State& state) { if (auto text_node = dynamic_cast(node)) { - state.xml += text_node->get_content(); + string content = text_node->get_content(); + boost::replace_all(content, "&", "&"); + boost::replace_all(content, "<", "<"); + boost::replace_all(content, ">", ">"); + state.xml += content; } else if (auto element = dynamic_cast(node)) { ++state.indent; diff --git a/test/interop_subtitle_test.cc b/test/interop_subtitle_test.cc index ff0940b0..3369fc24 100644 --- a/test/interop_subtitle_test.cc +++ b/test/interop_subtitle_test.cc @@ -762,21 +762,21 @@ BOOST_AUTO_TEST_CASE (write_interop_subtitle_test) c._id = "a6c58cff-3e1e-4b38-acec-a42224475ef6"; check_xml ( - "" - "a6c58cff-3e1e-4b38-acec-a42224475ef6" - "Test" - "1" - "EN" - "" - "" - "Hello world" - "" - "" - "" - "" - "What's going on" - "" - "" + "\n" + " a6c58cff-3e1e-4b38-acec-a42224475ef6\n" + " Test\n" + " 1\n" + " EN\n" + " \n" + " \n" + " Hello world\n" + " \n" + " \n" + " \n" + " \n" + " What's going on\n" + " \n" + " \n" "", c.xml_as_string (), vector() @@ -846,21 +846,21 @@ BOOST_AUTO_TEST_CASE (write_interop_subtitle_test2) c._id = "a6c58cff-3e1e-4b38-acec-a42224475ef6"; check_xml ( - "" - "a6c58cff-3e1e-4b38-acec-a42224475ef6" - "Test" - "1" - "EN" - "" - "" - "Hello world" - "" - "" - "" - "" - "What's going on" - "" - "" + "\n" + " a6c58cff-3e1e-4b38-acec-a42224475ef6\n" + " Test\n" + " 1\n" + " EN\n" + " \n" + " \n" + " Hello world\n" + " \n" + " \n" + " \n" + " \n" + " What's going on\n" + " \n" + " \n" "", c.xml_as_string (), vector() diff --git a/test/ref/write_interop_subtitle_test3/ASSETMAP b/test/ref/write_interop_subtitle_test3/ASSETMAP index 7c901513..570b0d5b 100644 --- a/test/ref/write_interop_subtitle_test3/ASSETMAP +++ b/test/ref/write_interop_subtitle_test3/ASSETMAP @@ -37,7 +37,7 @@ subs.xml 1 0 - 414 + 438 diff --git a/test/ref/write_interop_subtitle_test3/pkl_e94b8a0d-27f7-408a-af16-78d3df419a91.xml b/test/ref/write_interop_subtitle_test3/pkl_e94b8a0d-27f7-408a-af16-78d3df419a91.xml index 2da8e910..6f192259 100644 --- a/test/ref/write_interop_subtitle_test3/pkl_e94b8a0d-27f7-408a-af16-78d3df419a91.xml +++ b/test/ref/write_interop_subtitle_test3/pkl_e94b8a0d-27f7-408a-af16-78d3df419a91.xml @@ -9,15 +9,15 @@ urn:uuid:46c3eb45-15e5-47d6-8684-d8641e4dc516 46c3eb45-15e5-47d6-8684-d8641e4dc516 - 614bJ4VLsNZ6mLbdXbZXjGuoSsY= + Y8rZTMi93JjmkgwFK7Fu3W5rz/Y= 1061 text/xml;asdcpKind=CPL urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6 a6c58cff-3e1e-4b38-acec-a42224475ef6 - cVnFjMLTQnSIAlIzJpNB/p7B230= - 414 + ilLkOSjxgHZxkAdOPVJBoDFiUh8= + 438 text/xml;asdcpKind=Subtitle diff --git a/test/ref/write_interop_subtitle_test3/subs.xml b/test/ref/write_interop_subtitle_test3/subs.xml index d1c8fa7e..050ee930 100644 --- a/test/ref/write_interop_subtitle_test3/subs.xml +++ b/test/ref/write_interop_subtitle_test3/subs.xml @@ -1,2 +1,10 @@ -a6c58cff-3e1e-4b38-acec-a42224475ef6Test1ENd36f4bb3-c4fa-4a95-9915-6fec3110cd71.png + + a6c58cff-3e1e-4b38-acec-a42224475ef6 + Test + 1 + EN + + d36f4bb3-c4fa-4a95-9915-6fec3110cd71.png + + diff --git a/test/shared_subtitle_test.cc b/test/shared_subtitle_test.cc index 2231d631..ae05642a 100644 --- a/test/shared_subtitle_test.cc +++ b/test/shared_subtitle_test.cc @@ -205,3 +205,15 @@ BOOST_AUTO_TEST_CASE (format_xml_test2) check_xml (dcp::file_to_string(private_test / "DKH_UT_EN20160601def.reformatted.xml"), dcp::SubtitleAsset::format_xml(*document, {}), {}); } + +BOOST_AUTO_TEST_CASE (format_xml_entities_test) +{ + xmlpp::Document doc; + auto root = doc.create_root_node("Foo"); + root->add_child("Bar")->add_child_text("Don't panic & xml \"is\" 'great' & < > —"); + BOOST_REQUIRE_EQUAL(dcp::SubtitleAsset::format_xml(doc, {}), +"\n" +"\n" +" Don't panic &amp; xml \"is\" 'great' & < > —\n" +"\n"); +} diff --git a/test/smpte_subtitle_test.cc b/test/smpte_subtitle_test.cc index 2b8491a3..f1f66bfb 100644 --- a/test/smpte_subtitle_test.cc +++ b/test/smpte_subtitle_test.cc @@ -257,27 +257,27 @@ BOOST_AUTO_TEST_CASE (write_smpte_subtitle_test) c._xml_id = "a6c58cff-3e1e-4b38-acec-a42224475ef6"; check_xml ( - "" - "" - "urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6" - "Test" - "2016-04-01T03:52:00.000+00:00" - "1" - "en" - "24 1" - "24" - "" - "" - "" - "Hello world" - "" - "" - "" - "" - "What's going on" - "" - "" - "" + "\n" + "\n" + " urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6\n" + " Test\n" + " 2016-04-01T03:52:00.000+00:00\n" + " 1\n" + " en\n" + " 24 1\n" + " 24\n" + " \n" + " \n" + " \n" + " Hello world\n" + " \n" + " \n" + " \n" + " \n" + " What's going on\n" + " \n" + " \n" + " \n" "", c.xml_as_string (), vector() @@ -449,31 +449,23 @@ BOOST_AUTO_TEST_CASE (write_smpte_subtitle_test2) check_xml ( c.xml_as_string(), - "" - "" - "urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6" - "Test" - "2016-04-01T03:52:00.000+00:00" - "1" - "en" - "24 1" - "24" - "" - "" - "" - "" - "Testing is " - "really" - " fun" - "" - "" - "This is the " - "second" - " line" - "" - "" - "" - "" + "\n" + "\n" + " urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6\n" + " Test\n" + " 2016-04-01T03:52:00.000+00:00\n" + " 1\n" + " en\n" + " 24 1\n" + " 24\n" + " \n" + " \n" + " \n" + " Testing is really fun\n" + " This is the second line\n" + " \n" + " \n" + " \n" "", vector() ); @@ -593,23 +585,23 @@ BOOST_AUTO_TEST_CASE (write_subtitles_in_vertical_order_with_top_alignment) check_xml ( c.xml_as_string(), - "" - "" - "urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6" - "Test" - "2016-04-01T03:52:00.000+00:00" - "1" - "en" - "24 1" - "24" - "" - "" - "" - "Top line" - "Bottom line" - "" - "" - "" + "\n" + "\n" + " urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6\n" + " Test\n" + " 2016-04-01T03:52:00.000+00:00\n" + " 1\n" + " en\n" + " 24 1\n" + " 24\n" + " \n" + " \n" + " \n" + " Top line\n" + " Bottom line\n" + " \n" + " \n" + " \n" "", {} ); @@ -679,23 +671,23 @@ BOOST_AUTO_TEST_CASE (write_subtitles_in_vertical_order_with_bottom_alignment) check_xml ( c.xml_as_string(), - "" - "" - "urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6" - "Test" - "2016-04-01T03:52:00.000+00:00" - "1" - "en" - "24 1" - "24" - "" - "" - "" - "Top line" - "Bottom line" - "" - "" - "" + "\n" + "\n" + " urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6\n" + " Test\n" + " 2016-04-01T03:52:00.000+00:00\n" + " 1\n" + " en\n" + " 24 1\n" + " 24\n" + " \n" + " \n" + " \n" + " Top line\n" + " Bottom line\n" + " \n" + " \n" + " \n" "", {} ); diff --git a/test/verify_test.cc b/test/verify_test.cc index b2e98981..6a3dacc4 100644 --- a/test/verify_test.cc +++ b/test/verify_test.cc @@ -1292,7 +1292,7 @@ BOOST_AUTO_TEST_CASE (verify_invalid_closed_caption_xml_size_in_bytes) { dcp::VerificationNote::Type::BV21_ERROR, dcp::VerificationNote::Code::INVALID_CLOSED_CAPTION_XML_SIZE_IN_BYTES, - string("372207"), + string("419346"), canonical(dir / "subs.mxf") }, { dcp::VerificationNote::Type::WARNING, dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME }, @@ -1332,7 +1332,7 @@ verify_timed_text_asset_too_large (string name) check_verify_result ( { dir }, { - { dcp::VerificationNote::Type::BV21_ERROR, dcp::VerificationNote::Code::INVALID_TIMED_TEXT_SIZE_IN_BYTES, string("121695136"), canonical(dir / "subs.mxf") }, + { dcp::VerificationNote::Type::BV21_ERROR, dcp::VerificationNote::Code::INVALID_TIMED_TEXT_SIZE_IN_BYTES, string("121695542"), canonical(dir / "subs.mxf") }, { dcp::VerificationNote::Type::BV21_ERROR, dcp::VerificationNote::Code::INVALID_TIMED_TEXT_FONT_SIZE_IN_BYTES, string("121634816"), canonical(dir / "subs.mxf") }, { dcp::VerificationNote::Type::BV21_ERROR, dcp::VerificationNote::Code::MISSING_SUBTITLE_START_TIME, canonical(dir / "subs.mxf") }, { dcp::VerificationNote::Type::WARNING, dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME }, -- cgit v1.2.3