From 5468036c271c4124a292e7dfdbfad40fa5334bdb Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 25 Feb 2025 18:58:23 +0900 Subject: [PATCH] fix: escape tc text (#802) * fix: escape tc text * fix: version * fix * fix --- docx-core/src/documents/elements/instr_tc.rs | 3 +- docx-wasm/package.json | 2 +- docx-wasm/src/run.rs | 17 ++++++---- .../test/__snapshots__/index.test.js.snap | 4 +++ docx-wasm/test/index.test.js | 33 +++++++++++++++++++ 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/docx-core/src/documents/elements/instr_tc.rs b/docx-core/src/documents/elements/instr_tc.rs index 11405fe..5f28d61 100644 --- a/docx-core/src/documents/elements/instr_tc.rs +++ b/docx-core/src/documents/elements/instr_tc.rs @@ -2,6 +2,7 @@ use serde::Serialize; use std::io::Write; use crate::documents::*; +use crate::escape::escape; use crate::xml_builder::XMLBuilder; // https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_TCTC_topic_ID0EU2N1.html @@ -19,7 +20,7 @@ pub struct InstrTC { impl InstrTC { pub fn new(text: impl Into) -> Self { Self { - text: text.into(), + text: escape(&text.into()), ..Default::default() } } diff --git a/docx-wasm/package.json b/docx-wasm/package.json index e205c38..717979a 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.4.18-rc35", + "version": "0.4.18-rc37", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ", diff --git a/docx-wasm/src/run.rs b/docx-wasm/src/run.rs index 4fb3f9f..89e42dd 100644 --- a/docx-wasm/src/run.rs +++ b/docx-wasm/src/run.rs @@ -58,12 +58,17 @@ impl Run { level: Option, id: Option, ) -> Run { - self.0 = self.0.add_tc(docx_rs::InstrTC { - text: text.into(), - omits_page_number, - level, - item_type_identifier: id, - }); + let mut tc = docx_rs::InstrTC::new(text); + if omits_page_number { + tc = tc.omits_page_number(); + } + if let Some(level) = level { + tc = tc.level(level); + } + if let Some(id) = id { + tc = tc.item_type_identifier(id); + } + self.0 = self.0.add_tc(tc); self } diff --git a/docx-wasm/test/__snapshots__/index.test.js.snap b/docx-wasm/test/__snapshots__/index.test.js.snap index 2e14d5d..7f375e0 100644 --- a/docx-wasm/test/__snapshots__/index.test.js.snap +++ b/docx-wasm/test/__snapshots__/index.test.js.snap @@ -171045,6 +171045,10 @@ exports[`writer should write ToC with instrText TC 1`] = `"TOC \\\\f \\\\h \\\\u \\\\zHello!!TC \\"Hello!!TC\\" \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; +exports[`writer should write ToC with instrText TC escaped text 1`] = `""`; + +exports[`writer should write ToC with instrText TC escaped text 2`] = `"TOC \\\\f "abc" \\\\h \\\\u \\\\zHello!!TC \\"Hello!!<div>\\" \\\\f abc \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; + exports[`writer should write ToC with instrText TC with id 1`] = `""`; exports[`writer should write ToC with instrText TC with id 2`] = `"TOC \\\\f "abc" \\\\h \\\\u \\\\zHello!!TC \\"Hello!!TC\\" \\\\f abc \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; diff --git a/docx-wasm/test/index.test.js b/docx-wasm/test/index.test.js index aaa353c..3581185 100644 --- a/docx-wasm/test/index.test.js +++ b/docx-wasm/test/index.test.js @@ -1246,4 +1246,37 @@ describe("writer", () => { } } }); + + test("should write ToC with instrText TC escaped text", () => { + const p1 = new w.Paragraph() + .addRun(new w.Run().addText("Hello!!")) + .addRun( + new w.Run().addTc(new w.Tc("Hello!!
").level(1).identifier("abc")) + ) + .pageBreakBefore(true); + const p2 = new w.Paragraph() + .addRun(new w.Run().addText("World")) + .addRun(new w.Run().addTc(new w.Tc("World!!TC").level(1))) + .pageBreakBefore(true); + const buffer = new w.Docx() + .addTableOfContents( + new w.TableOfContents("TOC \\f abc \\h \\z \\u") + .alias("Table of contents") + .dirty() + .paragraphProperty(new w.ParagraphProperty().style("11")) + ) + .addParagraph(p1) + .addParagraph(p2) + .build(); + writeFileSync( + "../output/js/toc_with_instrtext_tc_escaped_text.docx", + buffer + ); + const z = new Zip(Buffer.from(buffer)); + for (const e of z.getEntries()) { + if (e.entryName.match(/document.xml/)) { + expect(z.readAsText(e)).toMatchSnapshot(); + } + } + }); });