fix: use instrTextString in reader (#528)

* fix: use instrTextString in reader

* fix: clippy

* fix: snaps

* fix

* fix: instrTextString
main
bokuweb 2022-08-31 10:59:48 +09:00 committed by GitHub
parent a974dcd9ec
commit 5489d03dd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 153 deletions

View File

@ -35,6 +35,8 @@ pub enum RunChild {
CommentEnd(CommentRangeEnd),
FieldChar(FieldChar),
InstrText(Box<InstrText>),
// 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()

View File

@ -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: Read>(
r: &mut EventReader<R>,
_attrs: &[OwnedAttribute],
) -> Result<Self, ReaderError> {
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),
_ => {}
}
}
}
}

View File

@ -24,7 +24,6 @@ mod header;
mod hyperlink;
mod ignore;
mod insert;
mod instr_text;
mod level;
mod level_override;
mod mc_fallback;

View File

@ -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),
_ => {}
}
}
},
_ => {}
}
}

View File

@ -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;
};

View File

@ -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 <bokuweb12@gmail.com>",

View File

@ -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 {},