Read table cell prop in style (#264)

* fix: read table cell props in style

* fix: spec
main
bokuweb 2021-03-25 15:30:33 +09:00 committed by GitHub
parent ef612ee1ff
commit 19e8708057
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
42 changed files with 420 additions and 116 deletions

View File

@ -5,7 +5,7 @@ use crate::types::*;
use crate::xml_builder::*;
use crate::StyleType;
use super::{BasedOn, Name, Next, ParagraphProperty, QFormat, RunProperty, TableProperty};
use super::*;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
@ -16,6 +16,7 @@ pub struct Style {
pub run_property: RunProperty,
pub paragraph_property: ParagraphProperty,
pub table_property: TableProperty,
pub table_cell_property: TableCellProperty,
pub based_on: Option<BasedOn>,
}
@ -31,6 +32,7 @@ impl Default for Style {
run_property: rpr,
paragraph_property: ppr,
table_property: TableProperty::new(),
table_cell_property: TableCellProperty::new(),
based_on: None,
}
}
@ -113,6 +115,11 @@ impl Style {
self.table_property = p;
self
}
pub fn table_cell_property(mut self, p: TableCellProperty) -> Self {
self.table_cell_property = p;
self
}
}
impl BuildXML for Style {

View File

@ -15,6 +15,7 @@ mod drawing;
mod errors;
mod from_xml;
mod ignore;
mod table_cell_property;
mod insert;
mod level;
mod level_override;

View File

@ -62,6 +62,11 @@ impl ElementReader for Style {
style = style.table_property(p);
}
}
XMLElement::TableCellProperty => {
if let Ok(p) = TableCellProperty::read(r, &attributes) {
style = style.table_cell_property(p);
}
}
_ => {}
}
}

View File

@ -5,7 +5,6 @@ use xml::attribute::OwnedAttribute;
use xml::reader::{EventReader, XmlEvent};
use super::*;
use crate::types::*;
impl ElementReader for TableCell {
fn read<R: Read>(r: &mut EventReader<R>, _: &[OwnedAttribute]) -> Result<Self, ReaderError> {
@ -25,86 +24,12 @@ impl ElementReader for TableCell {
cell = cell.add_paragraph(p);
continue;
}
XMLElement::TableCellProperty => loop {
let e = r.next();
match e {
Ok(XmlEvent::StartElement {
attributes, name, ..
}) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
ignore::ignore_element(
e.clone(),
XMLElement::TableCellPropertyChange,
r,
);
match e {
XMLElement::TableCellWidth => {
let mut w = 0;
let mut width_type = WidthType::Auto;
for a in attributes {
let local_name = &a.name.local_name;
if local_name == "type" {
width_type = WidthType::from_str(&a.value)?;
} else if local_name == "w" {
w = usize::from_str(&a.value)?;
}
}
cell = cell.width(w, width_type);
}
XMLElement::TableGridSpan => {
if let Some(a) = &attributes.get(0) {
cell = cell.grid_span(usize::from_str(&a.value)?)
}
}
XMLElement::TableVMerge => {
if let Some(a) = &attributes.get(0) {
cell = cell.vertical_merge(VMergeType::from_str(
&a.value,
)?);
} else {
// Treat as a continue without attribute
cell = cell.vertical_merge(VMergeType::Continue)
}
}
XMLElement::VAlign => {
if let Some(a) = &attributes.get(0) {
cell = cell.vertical_align(VAlignType::from_str(
&a.value,
)?);
}
}
XMLElement::Shading => {
if let Ok(shd) = Shading::read(r, &attributes) {
cell = cell.shading(shd);
}
}
XMLElement::TextDirection => {
if let Some(a) = &attributes.get(0) {
if let Ok(v) = TextDirectionType::from_str(&a.value)
{
cell = cell.text_direction(v);
}
}
}
XMLElement::TableCellBorders => {
let borders = TableCellBorders::read(r, &attributes)?;
cell = cell.set_borders(borders);
}
_ => {}
}
}
Ok(XmlEvent::EndElement { name, .. }) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
if e == XMLElement::TableCellProperty {
break;
}
}
Err(_) => return Err(ReaderError::XMLReadError),
_ => {}
XMLElement::TableCellProperty => {
if let Ok(p) = TableCellProperty::read(r, &attributes) {
cell.property = p;
}
},
continue;
}
_ => {}
}
}
@ -125,6 +50,7 @@ impl ElementReader for TableCell {
mod tests {
use super::*;
use crate::types::*;
#[cfg(test)]
use pretty_assertions::assert_eq;

View File

@ -0,0 +1,84 @@
use std::io::Read;
use std::str::FromStr;
use xml::attribute::OwnedAttribute;
use xml::reader::{EventReader, XmlEvent};
use super::*;
use crate::types::*;
impl ElementReader for TableCellProperty {
fn read<R: Read>(r: &mut EventReader<R>, _: &[OwnedAttribute]) -> Result<Self, ReaderError> {
let mut property = TableCellProperty::new();
loop {
let e = r.next();
match e {
Ok(XmlEvent::StartElement {
attributes, name, ..
}) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
ignore::ignore_element(e.clone(), XMLElement::TableCellPropertyChange, r);
match e {
XMLElement::TableCellWidth => {
let mut w = 0;
let mut width_type = WidthType::Auto;
for a in attributes {
let local_name = &a.name.local_name;
if local_name == "type" {
width_type = WidthType::from_str(&a.value)?;
} else if local_name == "w" {
w = usize::from_str(&a.value)?;
}
}
property = property.width(w, width_type);
}
XMLElement::TableGridSpan => {
if let Some(a) = &attributes.get(0) {
property = property.grid_span(usize::from_str(&a.value)?)
}
}
XMLElement::TableVMerge => {
if let Some(a) = &attributes.get(0) {
property = property.vertical_merge(VMergeType::from_str(&a.value)?);
} else {
// Treat as a continue without attribute
property = property.vertical_merge(VMergeType::Continue)
}
}
XMLElement::VAlign => {
if let Some(a) = &attributes.get(0) {
property = property.vertical_align(VAlignType::from_str(&a.value)?);
}
}
XMLElement::Shading => {
if let Ok(shd) = Shading::read(r, &attributes) {
property = property.shading(shd);
}
}
XMLElement::TextDirection => {
if let Some(a) = &attributes.get(0) {
if let Ok(v) = TextDirectionType::from_str(&a.value) {
property = property.text_direction(v);
}
}
}
XMLElement::TableCellBorders => {
let borders = TableCellBorders::read(r, &attributes)?;
property = property.set_borders(borders);
}
_ => {}
}
}
Ok(XmlEvent::EndElement { name, .. }) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
if e == XMLElement::TableCellProperty {
return Ok(property);
}
}
Err(_) => return Err(ReaderError::XMLReadError),
_ => {}
}
}
}
}

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

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

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

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

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,7 @@
import { RunPropertyJSON } from "./run";
import { ParagraphPropertyJSON } from "./paragraph";
import { TablePropertyJSON } from "./table";
import { TableCellPropertyJSON } from "..";
export type StyleJSON = {
styleId: string;
@ -9,6 +10,7 @@ export type StyleJSON = {
runProperty: RunPropertyJSON;
paragraphProperty: ParagraphPropertyJSON;
tableProperty: TablePropertyJSON;
tableCellProperty: TableCellPropertyJSON;
basedOn: string | null;
};

View File

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

View File

@ -751,6 +751,15 @@ Object {
},
"styleId": "Normal",
"styleType": "paragraph",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -2973,6 +2982,15 @@ Object {
},
"styleId": "a",
"styleType": "paragraph",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -3076,6 +3094,15 @@ Object {
},
"styleId": "a0",
"styleType": "character",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -3179,6 +3206,15 @@ Object {
},
"styleId": "a1",
"styleType": "table",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": null,
@ -3246,6 +3282,15 @@ Object {
},
"styleId": "a2",
"styleType": "numbering",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -5355,6 +5400,15 @@ Object {
},
"styleId": "a",
"styleType": "paragraph",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -5458,6 +5512,15 @@ Object {
},
"styleId": "a0",
"styleType": "character",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -5561,6 +5624,15 @@ Object {
},
"styleId": "a1",
"styleType": "table",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": null,
@ -5628,6 +5700,15 @@ Object {
},
"styleId": "a2",
"styleType": "numbering",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -5736,6 +5817,15 @@ Object {
},
"styleId": "a3",
"styleType": "paragraph",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -8428,6 +8518,15 @@ Object {
},
"styleId": "a",
"styleType": "paragraph",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -8531,6 +8630,15 @@ Object {
},
"styleId": "a0",
"styleType": "character",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -8634,6 +8742,15 @@ Object {
},
"styleId": "a1",
"styleType": "table",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": null,
@ -8701,6 +8818,15 @@ Object {
},
"styleId": "a2",
"styleType": "numbering",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -8804,6 +8930,15 @@ Object {
},
"styleId": "a3",
"styleType": "paragraph",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -8907,6 +9042,15 @@ Object {
},
"styleId": "a4",
"styleType": "character",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -9010,6 +9154,15 @@ Object {
},
"styleId": "a5",
"styleType": "character",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -9113,6 +9266,15 @@ Object {
},
"styleId": "a6",
"styleType": "paragraph",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -9216,6 +9378,15 @@ Object {
},
"styleId": "a7",
"styleType": "character",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -9319,6 +9490,15 @@ Object {
},
"styleId": "a8",
"styleType": "table",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -9422,6 +9602,15 @@ Object {
},
"styleId": "a9",
"styleType": "paragraph",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -10327,6 +10516,15 @@ Object {
},
"styleId": "a",
"styleType": "paragraph",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -10430,6 +10628,15 @@ Object {
},
"styleId": "a0",
"styleType": "character",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -10533,6 +10740,15 @@ Object {
},
"styleId": "a1",
"styleType": "table",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -10624,6 +10840,15 @@ Object {
},
"styleId": "a2",
"styleType": "numbering",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -10727,6 +10952,15 @@ Object {
},
"styleId": "a3",
"styleType": "table",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -11351,6 +11585,15 @@ Object {
},
"styleId": "a",
"styleType": "paragraph",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -11454,6 +11697,15 @@ Object {
},
"styleId": "a0",
"styleType": "character",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -11557,6 +11809,15 @@ Object {
},
"styleId": "a1",
"styleType": "table",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": null,
@ -11624,6 +11885,15 @@ Object {
},
"styleId": "a2",
"styleType": "numbering",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {
@ -11727,6 +11997,15 @@ Object {
},
"styleId": "a3",
"styleType": "table",
"tableCellProperty": Object {
"borders": null,
"gridSpan": null,
"shading": null,
"textDirection": null,
"verticalAlign": null,
"verticalMerge": null,
"width": null,
},
"tableProperty": Object {
"borders": Object {
"bottom": Object {