diff --git a/docx-core/src/documents/elements/run.rs b/docx-core/src/documents/elements/run.rs index cfc22ea..a1542d7 100644 --- a/docx-core/src/documents/elements/run.rs +++ b/docx-core/src/documents/elements/run.rs @@ -35,6 +35,8 @@ pub enum RunChild { CommentEnd(CommentRangeEnd), FieldChar(FieldChar), InstrText(Box), + // For reader + InstrTextString(String), } impl Serialize for RunChild { @@ -102,6 +104,12 @@ impl Serialize for RunChild { t.serialize_field("data", i)?; t.end() } + RunChild::InstrTextString(ref i) => { + let mut t = serializer.serialize_struct("InstrTextString", 2)?; + t.serialize_field("type", "instrTextString")?; + t.serialize_field("data", i)?; + t.end() + } } } } @@ -256,6 +264,7 @@ impl BuildXML for Run { RunChild::CommentEnd(c) => b = b.add_child(c), RunChild::FieldChar(c) => b = b.add_child(c), RunChild::InstrText(c) => b = b.add_child(c), + _ => {} } } b.close().build() diff --git a/docx-core/src/reader/instr_text.rs b/docx-core/src/reader/instr_text.rs deleted file mode 100644 index 6aec674..0000000 --- a/docx-core/src/reader/instr_text.rs +++ /dev/null @@ -1,62 +0,0 @@ -#![allow(clippy::single_match)] - -use std::io::Read; -use std::str::FromStr; - -use xml::attribute::OwnedAttribute; -use xml::reader::{EventReader, XmlEvent}; - -use crate::reader::*; - -impl ElementReader for InstrText { - fn read( - r: &mut EventReader, - _attrs: &[OwnedAttribute], - ) -> Result { - let mut instr = "".to_owned(); - loop { - let e = r.next(); - match e { - Ok(XmlEvent::Characters(c)) => { - instr = c; - } - Ok(XmlEvent::EndElement { name, .. }) => { - let e = XMLElement::from_str(&name.local_name).unwrap(); - match e { - XMLElement::InstrText => { - let instr = instr.trim(); - if instr.is_empty() { - return Err(ReaderError::XMLReadError); - } else { - if instr.starts_with("TOC") { - if let Ok(instr) = InstrToC::from_str(instr) { - return Ok(InstrText::TOC(instr)); - } - } - if instr.starts_with("TC") { - if let Ok(instr) = InstrTC::from_str(instr) { - return Ok(InstrText::TC(instr)); - } - } - if instr.starts_with("HYPERLINK") { - if let Ok(instr) = InstrHyperlink::from_str(instr) { - return Ok(InstrText::HYPERLINK(instr)); - } - } - if instr.starts_with("PAGEREF") { - if let Ok(instr) = InstrPAGEREF::from_str(instr) { - return Ok(InstrText::PAGEREF(instr)); - } - } - return Ok(InstrText::Unsupported(instr.to_string())); - } - } - _ => {} - } - } - Err(_) => return Err(ReaderError::XMLReadError), - _ => {} - } - } - } -} diff --git a/docx-core/src/reader/mod.rs b/docx-core/src/reader/mod.rs index 7cfde0b..59cbbe9 100644 --- a/docx-core/src/reader/mod.rs +++ b/docx-core/src/reader/mod.rs @@ -24,7 +24,6 @@ mod header; mod hyperlink; mod ignore; mod insert; -mod instr_text; mod level; mod level_override; mod mc_fallback; diff --git a/docx-core/src/reader/run.rs b/docx-core/src/reader/run.rs index b80edc2..8ad94ca 100644 --- a/docx-core/src/reader/run.rs +++ b/docx-core/src/reader/run.rs @@ -93,11 +93,26 @@ impl ElementReader for Run { run.children.push(RunChild::FieldChar(f)); } } - XMLElement::InstrText => { - if let Ok(i) = InstrText::read(r, &attributes) { - run.children.push(RunChild::InstrText(Box::new(i))); + XMLElement::InstrText => loop { + let e = r.next(); + match e { + Ok(XmlEvent::Characters(c)) => { + run.children.push(RunChild::InstrTextString(c)); + break; + } + Ok(XmlEvent::EndElement { name, .. }) => { + let e = XMLElement::from_str(&name.local_name).unwrap(); + match e { + XMLElement::Run => { + return Ok(run); + } + _ => {} + } + } + Err(_) => return Err(ReaderError::XMLReadError), + _ => {} } - } + }, _ => {} } } diff --git a/docx-wasm/js/json/run.ts b/docx-wasm/js/json/run.ts index 7439239..9d81e74 100644 --- a/docx-wasm/js/json/run.ts +++ b/docx-wasm/js/json/run.ts @@ -58,7 +58,7 @@ export type RunChildJSON = | CommentRangeStartJSON | CommentRangeEndJSON | FieldCharJSON - | InstrTextJSON; + | InstrTextStringJSON; export type TextJSON = { type: "text"; @@ -112,3 +112,8 @@ export type InstrTextJSON = { data: InstrToC; }; }; + +export type InstrTextStringJSON = { + type: "instrTextString"; + data: string; +}; diff --git a/docx-wasm/package.json b/docx-wasm/package.json index 534e63e..81c0c24 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.0.276-rc12", + "version": "0.0.276-rc14", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ", diff --git a/docx-wasm/test/__snapshots__/index.test.js.snap b/docx-wasm/test/__snapshots__/index.test.js.snap index b73a15e..ed6efef 100644 --- a/docx-wasm/test/__snapshots__/index.test.js.snap +++ b/docx-wasm/test/__snapshots__/index.test.js.snap @@ -12204,14 +12204,8 @@ Object { "data": Object { "children": Array [ Object { - "data": Object { - "data": Object { - "anchor": false, - "target": "https://example.com/", - }, - "type": "hyperlink", - }, - "type": "instrText", + "data": "HYPERLINK \\"https://example.com/\\"", + "type": "instrTextString", }, ], "runProperty": Object { @@ -12351,14 +12345,8 @@ Object { "data": Object { "children": Array [ Object { - "data": Object { - "data": Object { - "anchor": false, - "target": "https://google.com/", - }, - "type": "hyperlink", - }, - "type": "instrText", + "data": "HYPERLINK \\"https://google.com/\\"", + "type": "instrTextString", }, ], "runProperty": Object { @@ -28671,11 +28659,8 @@ Object { "data": Object { "children": Array [ Object { - "data": Object { - "data": "PAGE", - "type": "unsupported", - }, - "type": "instrText", + "data": " PAGE ", + "type": "instrTextString", }, ], "runProperty": Object { @@ -28795,11 +28780,8 @@ Object { "data": Object { "children": Array [ Object { - "data": Object { - "data": "NUMPAGES", - "type": "unsupported", - }, - "type": "instrText", + "data": " NUMPAGES ", + "type": "instrTextString", }, ], "runProperty": Object { @@ -47364,23 +47346,8 @@ Object { "data": Object { "children": Array [ Object { - "data": Object { - "data": Object { - "captionLabel": null, - "headingStylesRange": Array [ - 1, - 3, - ], - "hideTabAndPageNumbersInWebview": true, - "hyperlink": true, - "preserveNewLine": false, - "preserveTab": false, - "stylesWithLevels": Array [], - "useAppliedParagraphLineLevel": true, - }, - "type": "toc", - }, - "type": "instrText", + "data": "TOC \\\\o \\"1-3\\" \\\\h \\\\z \\\\u", + "type": "instrTextString", }, ], "runProperty": Object {}, @@ -47491,15 +47458,8 @@ Object { "data": Object { "children": Array [ Object { - "data": Object { - "data": Object { - "hyperlink": true, - "pageRef": "PAGEREF", - "relativePosition": false, - }, - "type": "pageref", - }, - "type": "instrText", + "data": " PAGEREF _Toc89816785 \\\\h ", + "type": "instrTextString", }, ], "runProperty": Object {}, @@ -47675,15 +47635,8 @@ Object { "data": Object { "children": Array [ Object { - "data": Object { - "data": Object { - "hyperlink": true, - "pageRef": "PAGEREF", - "relativePosition": false, - }, - "type": "pageref", - }, - "type": "instrText", + "data": " PAGEREF _Toc89816786 \\\\h ", + "type": "instrTextString", }, ], "runProperty": Object {}, @@ -47859,15 +47812,8 @@ Object { "data": Object { "children": Array [ Object { - "data": Object { - "data": Object { - "hyperlink": true, - "pageRef": "PAGEREF", - "relativePosition": false, - }, - "type": "pageref", - }, - "type": "instrText", + "data": " PAGEREF _Toc89816787 \\\\h ", + "type": "instrTextString", }, ], "runProperty": Object {}, @@ -48043,15 +47989,8 @@ Object { "data": Object { "children": Array [ Object { - "data": Object { - "data": Object { - "hyperlink": true, - "pageRef": "PAGEREF", - "relativePosition": false, - }, - "type": "pageref", - }, - "type": "instrText", + "data": " PAGEREF _Toc89816788 \\\\h ", + "type": "instrTextString", }, ], "runProperty": Object {}, @@ -54265,11 +54204,8 @@ Object { "data": Object { "children": Array [ Object { - "data": Object { - "data": "PAGE", - "type": "unsupported", - }, - "type": "instrText", + "data": "PAGE ", + "type": "instrTextString", }, ], "runProperty": Object {},