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), CommentEnd(CommentRangeEnd),
FieldChar(FieldChar), FieldChar(FieldChar),
InstrText(Box<InstrText>), InstrText(Box<InstrText>),
// For reader
InstrTextString(String),
} }
impl Serialize for RunChild { impl Serialize for RunChild {
@ -102,6 +104,12 @@ impl Serialize for RunChild {
t.serialize_field("data", i)?; t.serialize_field("data", i)?;
t.end() 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::CommentEnd(c) => b = b.add_child(c),
RunChild::FieldChar(c) => b = b.add_child(c), RunChild::FieldChar(c) => b = b.add_child(c),
RunChild::InstrText(c) => b = b.add_child(c), RunChild::InstrText(c) => b = b.add_child(c),
_ => {}
} }
} }
b.close().build() 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 hyperlink;
mod ignore; mod ignore;
mod insert; mod insert;
mod instr_text;
mod level; mod level;
mod level_override; mod level_override;
mod mc_fallback; mod mc_fallback;

View File

@ -93,11 +93,26 @@ impl ElementReader for Run {
run.children.push(RunChild::FieldChar(f)); run.children.push(RunChild::FieldChar(f));
} }
} }
XMLElement::InstrText => { XMLElement::InstrText => loop {
if let Ok(i) = InstrText::read(r, &attributes) { let e = r.next();
run.children.push(RunChild::InstrText(Box::new(i))); 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 | CommentRangeStartJSON
| CommentRangeEndJSON | CommentRangeEndJSON
| FieldCharJSON | FieldCharJSON
| InstrTextJSON; | InstrTextStringJSON;
export type TextJSON = { export type TextJSON = {
type: "text"; type: "text";
@ -112,3 +112,8 @@ export type InstrTextJSON = {
data: InstrToC; data: InstrToC;
}; };
}; };
export type InstrTextStringJSON = {
type: "instrTextString";
data: string;
};

View File

@ -1,6 +1,6 @@
{ {
"name": "docx-wasm", "name": "docx-wasm",
"version": "0.0.276-rc12", "version": "0.0.276-rc14",
"main": "dist/node/index.js", "main": "dist/node/index.js",
"browser": "dist/web/index.js", "browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>", "author": "bokuweb <bokuweb12@gmail.com>",

View File

@ -12204,14 +12204,8 @@ Object {
"data": Object { "data": Object {
"children": Array [ "children": Array [
Object { Object {
"data": Object { "data": "HYPERLINK \\"https://example.com/\\"",
"data": Object { "type": "instrTextString",
"anchor": false,
"target": "https://example.com/",
},
"type": "hyperlink",
},
"type": "instrText",
}, },
], ],
"runProperty": Object { "runProperty": Object {
@ -12351,14 +12345,8 @@ Object {
"data": Object { "data": Object {
"children": Array [ "children": Array [
Object { Object {
"data": Object { "data": "HYPERLINK \\"https://google.com/\\"",
"data": Object { "type": "instrTextString",
"anchor": false,
"target": "https://google.com/",
},
"type": "hyperlink",
},
"type": "instrText",
}, },
], ],
"runProperty": Object { "runProperty": Object {
@ -28671,11 +28659,8 @@ Object {
"data": Object { "data": Object {
"children": Array [ "children": Array [
Object { Object {
"data": Object {
"data": " PAGE ", "data": " PAGE ",
"type": "unsupported", "type": "instrTextString",
},
"type": "instrText",
}, },
], ],
"runProperty": Object { "runProperty": Object {
@ -28795,11 +28780,8 @@ Object {
"data": Object { "data": Object {
"children": Array [ "children": Array [
Object { Object {
"data": Object {
"data": " NUMPAGES ", "data": " NUMPAGES ",
"type": "unsupported", "type": "instrTextString",
},
"type": "instrText",
}, },
], ],
"runProperty": Object { "runProperty": Object {
@ -47364,23 +47346,8 @@ Object {
"data": Object { "data": Object {
"children": Array [ "children": Array [
Object { Object {
"data": Object { "data": "TOC \\\\o \\"1-3\\" \\\\h \\\\z \\\\u",
"data": Object { "type": "instrTextString",
"captionLabel": null,
"headingStylesRange": Array [
1,
3,
],
"hideTabAndPageNumbersInWebview": true,
"hyperlink": true,
"preserveNewLine": false,
"preserveTab": false,
"stylesWithLevels": Array [],
"useAppliedParagraphLineLevel": true,
},
"type": "toc",
},
"type": "instrText",
}, },
], ],
"runProperty": Object {}, "runProperty": Object {},
@ -47491,15 +47458,8 @@ Object {
"data": Object { "data": Object {
"children": Array [ "children": Array [
Object { Object {
"data": Object { "data": " PAGEREF _Toc89816785 \\\\h ",
"data": Object { "type": "instrTextString",
"hyperlink": true,
"pageRef": "PAGEREF",
"relativePosition": false,
},
"type": "pageref",
},
"type": "instrText",
}, },
], ],
"runProperty": Object {}, "runProperty": Object {},
@ -47675,15 +47635,8 @@ Object {
"data": Object { "data": Object {
"children": Array [ "children": Array [
Object { Object {
"data": Object { "data": " PAGEREF _Toc89816786 \\\\h ",
"data": Object { "type": "instrTextString",
"hyperlink": true,
"pageRef": "PAGEREF",
"relativePosition": false,
},
"type": "pageref",
},
"type": "instrText",
}, },
], ],
"runProperty": Object {}, "runProperty": Object {},
@ -47859,15 +47812,8 @@ Object {
"data": Object { "data": Object {
"children": Array [ "children": Array [
Object { Object {
"data": Object { "data": " PAGEREF _Toc89816787 \\\\h ",
"data": Object { "type": "instrTextString",
"hyperlink": true,
"pageRef": "PAGEREF",
"relativePosition": false,
},
"type": "pageref",
},
"type": "instrText",
}, },
], ],
"runProperty": Object {}, "runProperty": Object {},
@ -48043,15 +47989,8 @@ Object {
"data": Object { "data": Object {
"children": Array [ "children": Array [
Object { Object {
"data": Object { "data": " PAGEREF _Toc89816788 \\\\h ",
"data": Object { "type": "instrTextString",
"hyperlink": true,
"pageRef": "PAGEREF",
"relativePosition": false,
},
"type": "pageref",
},
"type": "instrText",
}, },
], ],
"runProperty": Object {}, "runProperty": Object {},
@ -54265,11 +54204,8 @@ Object {
"data": Object { "data": Object {
"children": Array [ "children": Array [
Object { Object {
"data": Object {
"data": "PAGE ", "data": "PAGE ",
"type": "unsupported", "type": "instrTextString",
},
"type": "instrText",
}, },
], ],
"runProperty": Object {}, "runProperty": Object {},