Improve tr props (#161)

* feat: Read gridAfter

* fix: snaps

* fix: insta
main
bokuweb 2020-10-09 14:11:46 +09:00 committed by GitHub
parent e34757f2b1
commit 06a5341348
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 2152 additions and 28 deletions

20
Cargo.lock generated
View File

@ -139,7 +139,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "docx-rs"
version = "0.2.7"
dependencies = [
"image 0.23.9 (registry+https://github.com/rust-lang/crates.io-index)",
"image 0.23.10 (registry+https://github.com/rust-lang/crates.io-index)",
"insta 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
@ -186,11 +186,11 @@ dependencies = [
[[package]]
name = "gif"
version = "0.10.3"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"color_quant 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"weezl 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -203,12 +203,12 @@ dependencies = [
[[package]]
name = "image"
version = "0.23.9"
version = "0.23.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bytemuck 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"gif 0.10.3 (registry+https://github.com/rust-lang/crates.io-index)",
"gif 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
"jpeg-decoder 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)",
"num-iter 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
"num-rational 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -576,6 +576,11 @@ name = "wasm-bindgen-shared"
version = "0.2.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "weezl"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.3.8"
@ -641,9 +646,9 @@ dependencies = [
"checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
"checksum encode_unicode 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
"checksum flate2 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6bd6d6f4752952feb71363cffc9ebac9411b75b87c6ab6058c40c8900cf43c0f"
"checksum gif 0.10.3 (registry+https://github.com/rust-lang/crates.io-index)" = "471d90201b3b223f3451cd4ad53e34295f16a1df17b1edf3736d47761c3981af"
"checksum gif 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "02efba560f227847cb41463a7395c514d127d4f74fff12ef0137fff1b84b96c4"
"checksum hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eff2656d88f158ce120947499e971d743c05dbcbed62e5bd2f38f1698bbc3772"
"checksum image 0.23.9 (registry+https://github.com/rust-lang/crates.io-index)" = "974e194911d1f7efe3cd8a8f9db3b767e43536327e899e8bc9a12ef5711b74d2"
"checksum image 0.23.10 (registry+https://github.com/rust-lang/crates.io-index)" = "985fc06b1304d19c28d5c562ed78ef5316183f2b0053b46763a0b94862373c34"
"checksum insta 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d3463f26c58ee98b72e4eae1a6b4c28bc3320ab69d4836b55162362c2ec63fa6"
"checksum itoa 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e"
"checksum jpeg-decoder 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)" = "5b47b4c4e017b01abdc5bcc126d2d1002e5a75bbe3ce73f9f4f311a916363704"
@ -687,6 +692,7 @@ dependencies = [
"checksum wasm-bindgen-macro 0.2.53 (registry+https://github.com/rust-lang/crates.io-index)" = "41e80594782a241bf3d92ee5d1247b8fb496250a8a2ff1e136942d433fbbce14"
"checksum wasm-bindgen-macro-support 0.2.53 (registry+https://github.com/rust-lang/crates.io-index)" = "74b9950355b1d92ca09de0984bdd4de7edda5e8af12daf0c052a0a075e8c9157"
"checksum wasm-bindgen-shared 0.2.53 (registry+https://github.com/rust-lang/crates.io-index)" = "7493fe67ad99672ef3de3e6ba513fb03db276358c8cc9588ce5a008c6e48ad68"
"checksum weezl 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e0e26e7a4d998e3d7949c69444b8b4916bac810da0d3a82ae612c89e952782f4"
"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -22,6 +22,21 @@ impl TableRow {
has_numbering,
}
}
pub fn grid_after(mut self, grid_after: u32) -> TableRow {
self.property = self.property.grid_after(grid_after);
self
}
pub fn width_after(mut self, w: f32) -> TableRow {
self.property = self.property.width_after(w);
self
}
pub fn row_height(mut self, h: f32) -> TableRow {
self.property = self.property.row_height(h);
self
}
}
impl BuildXML for TableRow {
@ -56,7 +71,7 @@ mod tests {
let r = TableRow::new(vec![TableCell::new()]);
assert_eq!(
serde_json::to_string(&r).unwrap(),
r#"{"cells":[{"children":[],"property":{"width":null,"borders":null,"gridSpan":null,"verticalMerge":null,"verticalAlign":null},"hasNumbering":false}],"hasNumbering":false,"property":{}}"#
r#"{"cells":[{"children":[],"property":{"width":null,"borders":null,"gridSpan":null,"verticalMerge":null,"verticalAlign":null},"hasNumbering":false}],"hasNumbering":false,"property":{"grid_after":null,"width_after":null,"row_height":null}}"#
);
}
}

View File

@ -4,17 +4,40 @@ use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct TableRowProperty {}
pub struct TableRowProperty {
grid_after: Option<u32>,
width_after: Option<f32>,
row_height: Option<f32>,
}
impl TableRowProperty {
pub fn new() -> TableRowProperty {
Default::default()
}
pub fn grid_after(mut self, after: u32) -> Self {
self.grid_after = Some(after);
self
}
pub fn width_after(mut self, w: f32) -> Self {
self.width_after = Some(w);
self
}
pub fn row_height(mut self, h: f32) -> Self {
self.row_height = Some(h);
self
}
}
impl Default for TableRowProperty {
fn default() -> Self {
TableRowProperty {}
TableRowProperty {
grid_after: None,
width_after: None,
row_height: None,
}
}
}

View File

@ -1,11 +1,13 @@
mod bool_value;
mod border;
mod indent;
mod indent_level;
mod val;
mod width;
mod border;
pub use bool_value::*;
pub use indent::*;
pub use border::*;
pub use indent::*;
pub use indent_level::*;
pub use val::*;
pub use width::*;

View File

@ -0,0 +1,11 @@
use xml::attribute::OwnedAttribute;
pub fn read_val(attrs: &[OwnedAttribute]) -> Option<String> {
for a in attrs {
let local_name = &a.name.local_name;
if local_name == "val" {
return Some(a.value.to_owned());
}
}
None
}

View File

@ -4,11 +4,15 @@ use std::str::FromStr;
use xml::attribute::OwnedAttribute;
use xml::reader::{EventReader, XmlEvent};
use super::attributes::*;
use super::*;
impl ElementReader for TableRow {
fn read<R: Read>(r: &mut EventReader<R>, _: &[OwnedAttribute]) -> Result<Self, ReaderError> {
let mut cells = vec![];
let mut grid_after = None;
let mut width_after = None;
let mut row_height = None;
loop {
let e = r.next();
match e {
@ -19,15 +23,45 @@ impl ElementReader for TableRow {
ignore::ignore_element(e.clone(), XMLElement::TableRowPropertyChange, r);
if let XMLElement::TableCell = e {
cells.push(TableCell::read(r, &attributes)?);
continue;
match e {
XMLElement::TableCell => {
cells.push(TableCell::read(r, &attributes)?);
continue;
}
XMLElement::GridAfter => {
if let Some(v) = read_val(&attributes) {
grid_after = Some(u32::from_str(&v)?);
}
}
XMLElement::WidthAfter => {
if let Ok(v) = read_width(&attributes) {
width_after = Some(v.0 as f32);
}
}
XMLElement::TableRowHeight => {
if let Some(v) = read_val(&attributes) {
row_height = Some(f32::from_str(&v)?);
}
}
_ => {}
}
}
Ok(XmlEvent::EndElement { name, .. }) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
if e == XMLElement::TableRow {
return Ok(TableRow::new(cells));
let mut row = TableRow::new(cells);
if let Some(grid_after) = grid_after {
row = row.grid_after(grid_after);
}
if let Some(row_height) = row_height {
row = row.row_height(row_height);
}
if let Some(width_after) = width_after {
row = row.width_after(width_after);
}
return Ok(row);
}
}
Err(_) => return Err(ReaderError::XMLReadError),

View File

@ -46,6 +46,7 @@ pub enum XMLElement {
Table,
TableProperty,
TableRow,
TableRowHeight,
TableCell,
TableCellProperty,
TableCellWidth,
@ -69,6 +70,8 @@ pub enum XMLElement {
TableCellMargin,
TableGrid,
GridCol,
GridAfter,
WidthAfter,
Style,
BasedOn,
Next,
@ -194,12 +197,15 @@ impl FromStr for XMLElement {
"tbl" => Ok(XMLElement::Table),
"tblPr" => Ok(XMLElement::TableProperty),
"tr" => Ok(XMLElement::TableRow),
"trHeight" => Ok(XMLElement::TableRowHeight),
"tc" => Ok(XMLElement::TableCell),
"tcPr" => Ok(XMLElement::TableCellProperty),
"tcW" => Ok(XMLElement::TableCellWidth),
"tcBorders" => Ok(XMLElement::TableCellBorders),
"vMerge" => Ok(XMLElement::TableVMerge),
"gridSpan" => Ok(XMLElement::TableGridSpan),
"gridAfter" => Ok(XMLElement::GridAfter),
"wAfter" => Ok(XMLElement::WidthAfter),
"tblW" => Ok(XMLElement::TableWidth),
"tblInd" => Ok(XMLElement::TableIndent),
"tblBorders" => Ok(XMLElement::TableBorders),

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -17,6 +17,12 @@ export type TableCellPropertyJSON = {
hasNumbering: boolean;
};
export type TableRowPropertyJSON = {
gridAfter: number | null;
rowHeight: number | null;
widthAfter: number | null;
};
export type TableCellJSON = {
children: TableCellChildJSON[];
property: TableCellPropertyJSON;
@ -24,6 +30,7 @@ export type TableCellJSON = {
export type TableRowJSON = {
cells: TableCellJSON[];
property: TableRowPropertyJSON;
};
export type TablePropertyJSON = {

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,12 @@ describe("reader", () => {
test("should read lvlOverride docx", () => {
const buf = readFileSync("../fixtures/lvl_override/override.docx");
const json = w.readDocx(buf);
console.log(json);
expect(json).toMatchSnapshot();
});
test("should read gridAfter docx", () => {
const buf = readFileSync("../fixtures/grid_after/grid_after.docx");
const json = w.readDocx(buf);
expect(json).toMatchSnapshot();
});
});

Binary file not shown.