Fix reader error (#52)

* fix: cell reader error

* fix: table indent

* fix: error

* 0.0.51
main
bokuweb 2020-03-10 12:02:51 +09:00 committed by GitHub
parent 6f54e5e493
commit 55655ab4b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 50 additions and 17 deletions

View File

@ -3,7 +3,7 @@ use std::fs::*;
use std::io::Read;
pub fn main() {
let mut file = File::open("./insert.docx").unwrap();
let mut file = File::open("./1/1.docx").unwrap();
let mut buf = vec![];
file.read_to_end(&mut buf).unwrap();
dbg!(read_docx(&buf).unwrap().json());

View File

@ -37,7 +37,7 @@ impl Table {
self
}
pub fn indent(mut self, v: usize) -> Table {
pub fn indent(mut self, v: i32) -> Table {
self.property = self.property.indent(v);
self
}

View File

@ -7,12 +7,12 @@ use crate::xml_builder::*;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TableIndent {
width: usize,
width: i32,
width_type: WidthType,
}
impl TableIndent {
pub fn new(width: usize, width_type: WidthType) -> TableIndent {
pub fn new(width: i32, width_type: WidthType) -> TableIndent {
TableIndent { width, width_type }
}
}

View File

@ -32,7 +32,7 @@ impl TableProperty {
Default::default()
}
pub fn indent(mut self, v: usize) -> TableProperty {
pub fn indent(mut self, v: i32) -> TableProperty {
self.indent = Some(TableIndent::new(v, WidthType::DXA));
self
}

View File

@ -6,7 +6,7 @@ use crate::types::*;
use super::super::errors::*;
pub fn read_width(attrs: &[OwnedAttribute]) -> Result<(usize, WidthType), ReaderError> {
pub fn read_width(attrs: &[OwnedAttribute]) -> Result<(isize, WidthType), ReaderError> {
let mut w = 0;
let mut width_type = WidthType::Auto;
for a in attrs {
@ -14,7 +14,7 @@ pub fn read_width(attrs: &[OwnedAttribute]) -> Result<(usize, WidthType), Reader
if local_name == "type" {
width_type = WidthType::from_str(&a.value)?;
} else if local_name == "w" {
w = usize::from_str(&a.value)?;
w = isize::from_str(&a.value)?;
}
}
Ok((w, width_type))

View File

@ -25,7 +25,7 @@ impl ElementReader for Table {
}
XMLElement::TableWidth => {
let (w, width_type) = read_width(&attributes)?;
t = t.width(w, width_type);
t = t.width(w as usize, width_type);
continue;
}
XMLElement::Justification => {
@ -33,7 +33,7 @@ impl ElementReader for Table {
}
XMLElement::TableIndent => {
let (w, _) = read_width(&attributes)?;
t = t.indent(w);
t = t.indent(w as i32);
continue;
}
XMLElement::TableBorders => {
@ -44,7 +44,7 @@ impl ElementReader for Table {
}
XMLElement::GridCol => {
let (w, _) = read_width(&attributes)?;
grid_col.push(w);
grid_col.push(w as usize);
}
_ => {}
}

View File

@ -45,13 +45,19 @@ impl ElementReader for TableCell {
cell = cell.width(w, width_type);
}
XMLElement::TableGridSpan => {
cell = cell
.grid_span(usize::from_str(&attributes[0].value)?)
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(
&attributes[0].value,
&a.value,
)?);
} else {
// Treat as a continue without attribute
cell = cell.vertical_merge(VMergeType::Continue)
}
}
XMLElement::TableCellBorders => {
// TODO: Support table cell borders later
@ -126,4 +132,31 @@ mod tests {
.vertical_merge(VMergeType::Restart),
);
}
#[test]
fn test_read_no_attr_vmerge() {
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:tc>
<w:tcPr>
<w:tcW w:w="6425" w:type="dxa"/>
<w:vMerge />
<w:shd w:fill="auto" w:val="clear"/>
</w:tcPr>
<w:p>
<w:r>
<w:rPr></w:rPr>
</w:r>
</w:p>
</w:tc>
</w:document>"#;
let mut parser = EventReader::new(c.as_bytes());
let cell = TableCell::read(&mut parser, &[]).unwrap();
assert_eq!(
cell,
TableCell::new()
.add_paragraph(Paragraph::new().add_run(Run::new()))
.width(6425, WidthType::DXA)
.vertical_merge(VMergeType::Continue),
);
}
}

View File

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

View File

@ -29,7 +29,7 @@ impl Table {
self
}
pub fn indent(mut self, v: usize) -> Table {
pub fn indent(mut self, v: i32) -> Table {
self.0 = self.0.indent(v);
self
}