diff --git a/docx-core/src/documents/elements/comment.rs b/docx-core/src/documents/elements/comment.rs index 8d23ca3..46547e3 100644 --- a/docx-core/src/documents/elements/comment.rs +++ b/docx-core/src/documents/elements/comment.rs @@ -3,7 +3,7 @@ use crate::xml_builder::*; #[derive(Debug, Clone)] pub struct Comment { - pub id: String, + pub id: usize, pub author: String, pub date: String, pub paragraph: Paragraph, @@ -12,7 +12,7 @@ pub struct Comment { impl Default for Comment { fn default() -> Comment { Comment { - id: "invalidId".to_owned(), + id: 1, author: "unnamed".to_owned(), date: "1970-01-01T00:00:00Z".to_owned(), paragraph: Paragraph::new(), @@ -21,9 +21,9 @@ impl Default for Comment { } impl Comment { - pub fn new(id: impl Into) -> Comment { + pub fn new(id: usize) -> Comment { Self { - id: id.into(), + id, ..Default::default() } } @@ -43,15 +43,15 @@ impl Comment { self } - pub fn id(&self) -> String { - self.id.clone() + pub fn id(&self) -> usize { + self.id } } impl BuildXML for Comment { fn build(&self) -> Vec { XMLBuilder::new() - .open_comment(&self.id, &self.author, &self.date, "") + .open_comment(&format!("{}", self.id), &self.author, &self.date, "") .add_child(&self.paragraph) .close() .build() @@ -68,10 +68,10 @@ mod tests { #[test] fn test_ins_default() { - let b = Comment::new("123").build(); + let b = Comment::new(1).build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#""# + r#""# ); } } diff --git a/docx-core/src/documents/elements/comment_range_end.rs b/docx-core/src/documents/elements/comment_range_end.rs index 597273c..277f3ee 100644 --- a/docx-core/src/documents/elements/comment_range_end.rs +++ b/docx-core/src/documents/elements/comment_range_end.rs @@ -3,12 +3,12 @@ use crate::xml_builder::*; #[derive(Debug, Clone)] pub struct CommentRangeEnd { - id: String, + id: usize, } impl CommentRangeEnd { - pub fn new(id: impl Into) -> CommentRangeEnd { - CommentRangeEnd { id: id.into() } + pub fn new(id: usize) -> CommentRangeEnd { + CommentRangeEnd { id } } } @@ -19,9 +19,9 @@ impl BuildXML for CommentRangeEnd { .open_run_property() .close() .close() - .comment_range_end(&self.id) + .comment_range_end(&format!("{}", self.id)) .open_run() - .comment_reference(&self.id) + .comment_reference(&format!("{}", self.id)) .close() .build() } @@ -37,16 +37,16 @@ mod tests { #[test] fn test_comment_range_end() { - let c = CommentRangeEnd::new("mockid"); + let c = CommentRangeEnd::new(1); let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), r#" - + - + "# ); } diff --git a/docx-core/src/documents/elements/comment_range_start.rs b/docx-core/src/documents/elements/comment_range_start.rs index 7402034..3d45805 100644 --- a/docx-core/src/documents/elements/comment_range_start.rs +++ b/docx-core/src/documents/elements/comment_range_start.rs @@ -4,7 +4,7 @@ use crate::xml_builder::*; #[derive(Debug, Clone)] pub struct CommentRangeStart { - id: String, + id: usize, comment: Comment, } @@ -24,7 +24,7 @@ impl CommentRangeStart { impl BuildXML for CommentRangeStart { fn build(&self) -> Vec { let b = XMLBuilder::new(); - b.comment_range_start(&self.id).build() + b.comment_range_start(&format!("{}", self.id)).build() } } @@ -38,11 +38,11 @@ mod tests { #[test] fn test_comment_range_start() { - let c = CommentRangeStart::new(Comment::new("mockid")); + let c = CommentRangeStart::new(Comment::new(1)); let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#""# + r#""# ); } } diff --git a/docx-core/src/documents/elements/delete_text.rs b/docx-core/src/documents/elements/delete_text.rs new file mode 100644 index 0000000..848f3e7 --- /dev/null +++ b/docx-core/src/documents/elements/delete_text.rs @@ -0,0 +1,41 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct DeleteText { + text: String, + preserve_space: bool, +} + +impl DeleteText { + pub fn new(text: impl Into) -> DeleteText { + DeleteText { + text: text.into(), + preserve_space: true, + } + } +} + +impl BuildXML for DeleteText { + fn build(&self) -> Vec { + XMLBuilder::new().delete_text(&self.text, true).build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_build() { + let b = DeleteText::new("Hello").build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#"Hello"# + ); + } +} diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index 9248b47..54090d0 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -10,6 +10,7 @@ mod comment_range_end; mod comment_range_start; mod default_tab_stop; mod delete; +mod delete_text; mod doc_defaults; mod font; mod grid_span; @@ -59,6 +60,7 @@ mod table_row_property; mod table_width; mod text; mod underline; +mod vanish; mod vertical_merge; mod zoom; @@ -74,6 +76,7 @@ pub use comment_range_end::*; pub use comment_range_start::*; pub use default_tab_stop::*; pub use delete::*; +pub use delete_text::*; pub use doc_defaults::*; pub use font::*; pub use grid_span::*; @@ -123,5 +126,6 @@ pub use table_row_property::*; pub use table_width::*; pub use text::*; pub use underline::*; +pub use vanish::*; pub use vertical_merge::*; pub use zoom::*; diff --git a/docx-core/src/documents/elements/paragraph.rs b/docx-core/src/documents/elements/paragraph.rs index 5498615..1cf75f8 100644 --- a/docx-core/src/documents/elements/paragraph.rs +++ b/docx-core/src/documents/elements/paragraph.rs @@ -98,7 +98,7 @@ impl Paragraph { self } - pub fn add_comment_end(mut self, id: impl Into) -> Paragraph { + pub fn add_comment_end(mut self, id: usize) -> Paragraph { self.children .push(ParagraphChild::CommentEnd(CommentRangeEnd::new(id))); self @@ -183,18 +183,18 @@ mod tests { #[test] fn test_comment() { let b = Paragraph::new() - .add_comment_start(Comment::new("1234-5678")) + .add_comment_start(Comment::new(1)) .add_run(Run::new().add_text("Hello")) - .add_comment_end("1234-5678") + .add_comment_end(1) .build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#"Hello + r#"Hello - + - + "# ); } diff --git a/docx-core/src/documents/elements/run.rs b/docx-core/src/documents/elements/run.rs index 1893daa..6175190 100644 --- a/docx-core/src/documents/elements/run.rs +++ b/docx-core/src/documents/elements/run.rs @@ -1,4 +1,4 @@ -use super::{Break, RunProperty, Tab, Text}; +use super::{Break, DeleteText, RunProperty, Tab, Text}; use crate::documents::BuildXML; use crate::types::BreakType; use crate::xml_builder::*; @@ -22,6 +22,7 @@ impl Default for Run { #[derive(Debug, Clone)] pub enum RunChild { Text(Text), + DeleteText(DeleteText), Tab(Tab), Break(Break), } @@ -82,6 +83,11 @@ impl Run { self.run_property = self.run_property.underline(line_type); self } + + pub fn vanish(mut self) -> Run { + self.run_property = self.run_property.vanish(); + self + } } impl BuildXML for Run { @@ -91,6 +97,7 @@ impl BuildXML for Run { for c in &self.children { match c { RunChild::Text(t) => b = b.add_child(t), + RunChild::DeleteText(t) => b = b.add_child(t), RunChild::Tab(t) => b = b.add_child(t), RunChild::Break(t) => b = b.add_child(t), } diff --git a/docx-core/src/documents/elements/run_property.rs b/docx-core/src/documents/elements/run_property.rs index 602ebf9..fd126d2 100644 --- a/docx-core/src/documents/elements/run_property.rs +++ b/docx-core/src/documents/elements/run_property.rs @@ -1,4 +1,4 @@ -use super::{Bold, BoldCs, Color, Highlight, Italic, ItalicCs, Sz, SzCs, Underline}; +use super::{Bold, BoldCs, Color, Highlight, Italic, ItalicCs, Sz, SzCs, Underline, Vanish}; use crate::documents::BuildXML; use crate::xml_builder::*; @@ -13,6 +13,7 @@ pub struct RunProperty { bold_cs: Option, italic: Option, italic_cs: Option, + vanish: Option, } impl RunProperty { @@ -52,6 +53,11 @@ impl RunProperty { self.underline = Some(Underline::new(line_type)); self } + + pub fn vanish(mut self) -> RunProperty { + self.vanish = Some(Vanish::new()); + self + } } impl Default for RunProperty { @@ -66,6 +72,7 @@ impl Default for RunProperty { bold_cs: None, italic: None, italic_cs: None, + vanish: None, } } } @@ -83,6 +90,7 @@ impl BuildXML for RunProperty { .add_optional_child(&self.italic_cs) .add_optional_child(&self.highlight) .add_optional_child(&self.underline) + .add_optional_child(&self.vanish) .close() .build() } @@ -135,4 +143,14 @@ mod tests { r#""# ); } + + #[test] + fn test_vanish() { + let c = RunProperty::new().vanish(); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } } diff --git a/docx-core/src/documents/elements/vanish.rs b/docx-core/src/documents/elements/vanish.rs new file mode 100644 index 0000000..3e6552d --- /dev/null +++ b/docx-core/src/documents/elements/vanish.rs @@ -0,0 +1,34 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct Vanish {} + +impl Vanish { + pub fn new() -> Vanish { + Vanish {} + } +} + +impl BuildXML for Vanish { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.vanish().build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_build() { + let c = Vanish::new(); + let b = c.build(); + assert_eq!(str::from_utf8(&b).unwrap(), r#""#); + } +} diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index 2bdbf4f..21c6f11 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -199,6 +199,7 @@ impl XMLBuilder { only_str_val_el!(level_text, "w:lvlText"); only_str_val_el!(level_justification, "w:lvlJc"); only_str_val_el!(abstract_num_id, "w:abstractNumId"); + closed_el!(vanish, "w:vanish"); } #[cfg(test)] diff --git a/docx-core/tests/lib.rs b/docx-core/tests/lib.rs index 8cb54c7..5647b64 100644 --- a/docx-core/tests/lib.rs +++ b/docx-core/tests/lib.rs @@ -265,14 +265,14 @@ pub fn comments() -> Result<(), DocxError> { .add_paragraph( Paragraph::new() .add_comment_start( - Comment::new("1") + Comment::new(1) .author("bokuweb") .date("2019-01-01T00:00:00Z") .paragraph(Paragraph::new().add_run(Run::new().add_text("Hello"))), ) .add_run(Run::new().add_text("Hello").highlight("cyan")) .add_run(Run::new().add_text(" World!").highlight("yellow")) - .add_comment_end("1"), + .add_comment_end(1), ) .build() .pack(file)?; @@ -287,13 +287,13 @@ pub fn comments_to_table() -> Result<(), DocxError> { TableCell::new().add_paragraph( Paragraph::new() .add_comment_start( - Comment::new("1") + Comment::new(1) .author("bokuweb") .date("2019-01-01T00:00:00Z") .paragraph(Paragraph::new().add_run(Run::new().add_text("Hello"))), ) .add_run(Run::new().add_text("Hello")) - .add_comment_end("1"), + .add_comment_end(1), ), TableCell::new().add_paragraph(Paragraph::new().add_run(Run::new().add_text("World"))), ])]); @@ -301,13 +301,13 @@ pub fn comments_to_table() -> Result<(), DocxError> { .add_paragraph( Paragraph::new() .add_comment_start( - Comment::new("ABCD-1234") + Comment::new(1) .author("bokuweb") .date("2019-01-01T00:00:00Z") .paragraph(Paragraph::new().add_run(Run::new().add_text("Comment!!"))), ) .add_run(Run::new().add_text("Hello").highlight("cyan")) - .add_comment_end("ABCD-1234"), + .add_comment_end(1), ) .add_table(table) .build() @@ -386,3 +386,19 @@ pub fn escape() -> Result<(), DocxError> { .pack(file)?; Ok(()) } + +#[test] +pub fn vanish() -> Result<(), DocxError> { + let path = std::path::Path::new("./tests/output/vanish.docx"); + let file = std::fs::File::create(&path).unwrap(); + Docx::new() + .add_paragraph( + Paragraph::new() + .add_run(Run::new().add_text("Hello")) + .add_run(Run::new().add_text("Hidden").vanish()) + .add_run(Run::new().add_text(" World!!")), + ) + .build() + .pack(file)?; + Ok(()) +} diff --git a/docx-wasm/pkg/index.d.ts b/docx-wasm/pkg/index.d.ts index a7117cd..24b496b 100644 --- a/docx-wasm/pkg/index.d.ts +++ b/docx-wasm/pkg/index.d.ts @@ -4,10 +4,10 @@ */ export function createRun(): Run; /** -* @param {string} id +* @param {number} id * @returns {Comment} */ -export function createComment(id: string): Comment; +export function createComment(id: number): Comment; /** * @returns {TableCell} */ @@ -136,9 +136,9 @@ export class Comment { */ paragraph(p: Paragraph): Comment; /** -* @returns {string} +* @returns {number} */ - id(): string; + id(): number; } /** */ @@ -232,10 +232,10 @@ export class Paragraph { */ add_comment_start(comment: Comment): Paragraph; /** -* @param {string} id +* @param {number} id * @returns {Paragraph} */ - add_comment_end(id: string): Paragraph; + add_comment_end(id: number): Paragraph; /** * @param {number} alignment_type * @returns {Paragraph} diff --git a/docx-wasm/pkg/index.js b/docx-wasm/pkg/index.js index abd04d8..0b83719 100644 --- a/docx-wasm/pkg/index.js +++ b/docx-wasm/pkg/index.js @@ -69,11 +69,12 @@ function passStringToWasm(arg) { return ptr; } /** -* @param {string} id +* @param {number} id * @returns {Comment} */ export function createComment(id) { - const ret = wasm.createComment(passStringToWasm(id), WASM_VECTOR_LEN); + _assertNum(id); + const ret = wasm.createComment(id); return Comment.__wrap(ret); } @@ -83,22 +84,6 @@ function _assertClass(instance, klass) { } return instance.ptr; } - -let cachegetInt32Memory = null; -function getInt32Memory() { - if (cachegetInt32Memory === null || cachegetInt32Memory.buffer !== wasm.memory.buffer) { - cachegetInt32Memory = new Int32Array(wasm.memory.buffer); - } - return cachegetInt32Memory; -} - -let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); - -cachedTextDecoder.decode(); - -function getStringFromWasm(ptr, len) { - return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len)); -} /** * @returns {TableCell} */ @@ -123,6 +108,14 @@ export function createDocx() { return Docx.__wrap(ret); } +let cachegetInt32Memory = null; +function getInt32Memory() { + if (cachegetInt32Memory === null || cachegetInt32Memory.buffer !== wasm.memory.buffer) { + cachegetInt32Memory = new Int32Array(wasm.memory.buffer); + } + return cachegetInt32Memory; +} + function getArrayU8FromWasm(ptr, len) { return getUint8Memory().subarray(ptr / 1, ptr / 1 + len); } @@ -200,6 +193,14 @@ export function createParagraph() { return Paragraph.__wrap(ret); } +let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); + +cachedTextDecoder.decode(); + +function getStringFromWasm(ptr, len) { + return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len)); +} + const heap = new Array(32); heap.fill(undefined); @@ -323,18 +324,13 @@ export class Comment { return Comment.__wrap(ret); } /** - * @returns {string} + * @returns {number} */ id() { - const retptr = 8; if (this.ptr == 0) throw new Error('Attempt to use a moved value'); - _assertNum(retptr); _assertNum(this.ptr); - const ret = wasm.comment_id(retptr, this.ptr); - const memi32 = getInt32Memory(); - const v0 = getStringFromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1]).slice(); - wasm.__wbindgen_free(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1); - return v0; + const ret = wasm.comment_id(this.ptr); + return ret >>> 0; } } /** @@ -673,7 +669,7 @@ export class Paragraph { return Paragraph.__wrap(ret); } /** - * @param {string} id + * @param {number} id * @returns {Paragraph} */ add_comment_end(id) { @@ -681,7 +677,8 @@ export class Paragraph { const ptr = this.ptr; this.ptr = 0; _assertNum(ptr); - const ret = wasm.paragraph_add_comment_end(ptr, passStringToWasm(id), WASM_VECTOR_LEN); + _assertNum(id); + const ret = wasm.paragraph_add_comment_end(ptr, id); return Paragraph.__wrap(ret); } /** diff --git a/docx-wasm/pkg/index_bg.d.ts b/docx-wasm/pkg/index_bg.d.ts index 7d46f9f..4d83aa9 100644 --- a/docx-wasm/pkg/index_bg.d.ts +++ b/docx-wasm/pkg/index_bg.d.ts @@ -13,11 +13,11 @@ export function run_bold(a: number): number; export function run_italic(a: number): number; export function run_underline(a: number, b: number, c: number): number; export function __wbg_comment_free(a: number): void; -export function createComment(a: number, b: number): number; +export function createComment(a: number): number; export function comment_author(a: number, b: number, c: number): number; export function comment_date(a: number, b: number, c: number): number; export function comment_paragraph(a: number, b: number): number; -export function comment_id(a: number, b: number): void; +export function comment_id(a: number): number; export function __wbg_tablecell_free(a: number): void; export function createTableCell(): number; export function tablecell_add_paragraph(a: number, b: number): number; @@ -56,7 +56,7 @@ export function paragraph_add_delete(a: number, b: number): number; export function paragraph_add_bookmark_start(a: number, b: number, c: number, d: number, e: number): number; export function paragraph_add_bookmark_end(a: number, b: number, c: number): number; export function paragraph_add_comment_start(a: number, b: number): number; -export function paragraph_add_comment_end(a: number, b: number, c: number): number; +export function paragraph_add_comment_end(a: number, b: number): number; export function paragraph_align(a: number, b: number): number; export function paragraph_style(a: number, b: number, c: number): number; export function paragraph_indent(a: number, b: number, c: number, d: number, e: number): number; diff --git a/docx-wasm/pkg/index_bg.wasm b/docx-wasm/pkg/index_bg.wasm index 2202762..e2e1a22 100644 Binary files a/docx-wasm/pkg/index_bg.wasm and b/docx-wasm/pkg/index_bg.wasm differ diff --git a/docx-wasm/src/comment.rs b/docx-wasm/src/comment.rs index a39e305..47b307a 100644 --- a/docx-wasm/src/comment.rs +++ b/docx-wasm/src/comment.rs @@ -7,7 +7,7 @@ use wasm_bindgen::prelude::*; pub struct Comment(docx_core::Comment); #[wasm_bindgen(js_name = createComment)] -pub fn create_comment(id: String) -> Comment { +pub fn create_comment(id: usize) -> Comment { Comment(docx_core::Comment::new(id)) } @@ -34,7 +34,7 @@ impl Comment { self } - pub fn id(&self) -> String { - self.0.id.clone() + pub fn id(&self) -> usize { + self.0.id } } diff --git a/docx-wasm/src/paragraph.rs b/docx-wasm/src/paragraph.rs index efdc17e..eb8687e 100644 --- a/docx-wasm/src/paragraph.rs +++ b/docx-wasm/src/paragraph.rs @@ -57,7 +57,7 @@ impl Paragraph { self } - pub fn add_comment_end(mut self, id: &str) -> Paragraph { + pub fn add_comment_end(mut self, id: usize) -> Paragraph { self.0.children.push(docx_core::ParagraphChild::CommentEnd( docx_core::CommentRangeEnd::new(id), )); diff --git a/fixtures/hidden/[Content_Types].xml b/fixtures/hidden/[Content_Types].xml new file mode 100644 index 0000000..dc111cb --- /dev/null +++ b/fixtures/hidden/[Content_Types].xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/fixtures/hidden/_rels/.rels b/fixtures/hidden/_rels/.rels new file mode 100644 index 0000000..f0b72e7 --- /dev/null +++ b/fixtures/hidden/_rels/.rels @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/fixtures/hidden/docProps/app.xml b/fixtures/hidden/docProps/app.xml new file mode 100644 index 0000000..af84348 --- /dev/null +++ b/fixtures/hidden/docProps/app.xml @@ -0,0 +1,2 @@ + +1LibreOffice/6.0.7.3$Linux_X86_64 LibreOffice_project/00m0$Build-31210111 \ No newline at end of file diff --git a/fixtures/hidden/docProps/core.xml b/fixtures/hidden/docProps/core.xml new file mode 100644 index 0000000..ae6efd3 --- /dev/null +++ b/fixtures/hidden/docProps/core.xml @@ -0,0 +1,2 @@ + +2019-12-14T00:03:20Zja-JP2019-12-14T00:04:24Z1 \ No newline at end of file diff --git a/fixtures/hidden/hidden.docx b/fixtures/hidden/hidden.docx new file mode 100644 index 0000000..e8128cb Binary files /dev/null and b/fixtures/hidden/hidden.docx differ diff --git a/fixtures/hidden/word/_rels/document.xml.rels b/fixtures/hidden/word/_rels/document.xml.rels new file mode 100644 index 0000000..8a2db8a --- /dev/null +++ b/fixtures/hidden/word/_rels/document.xml.rels @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/fixtures/hidden/word/document.xml b/fixtures/hidden/word/document.xml new file mode 100644 index 0000000..1db05ad --- /dev/null +++ b/fixtures/hidden/word/document.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + Hello + + + + + + Hidden + + + + World + + + + + + + + + + + + \ No newline at end of file diff --git a/fixtures/hidden/word/fontTable.xml b/fixtures/hidden/word/fontTable.xml new file mode 100644 index 0000000..94f56db --- /dev/null +++ b/fixtures/hidden/word/fontTable.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/fixtures/hidden/word/settings.xml b/fixtures/hidden/word/settings.xml new file mode 100644 index 0000000..97dba84 --- /dev/null +++ b/fixtures/hidden/word/settings.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/fixtures/hidden/word/styles.xml b/fixtures/hidden/word/styles.xml new file mode 100644 index 0000000..72e6f53 --- /dev/null +++ b/fixtures/hidden/word/styles.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file