Skip to content

Commit 77f5506

Browse files
authored
fix: support on/off boolean values in reader (#883)
* fix: support on/off boolean values in reader Handle Word on/off boolean attribute values in the reader and prefer w:val when parsing booleans so run properties are interpreted correctly. Made-with: Cursor * fix: use pinned TypeScript in wasm CI Run the wasm TypeScript build with the pnpm-managed compiler and install with a frozen lockfile so CI stays aligned with the pinned dependency versions. Made-with: Cursor
1 parent 20a7079 commit 77f5506

3 files changed

Lines changed: 62 additions & 6 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ jobs:
7070
- run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
7171
- run: rustup target add wasm32-unknown-unknown
7272
- run: npm install -g pnpm
73-
- run: pnpm install && pnpm wasm-pack:node && pnpm wasm-pack:dev && tsc -p tsconfig.node.json && pnpm test
73+
- run: pnpm install --frozen-lockfile && pnpm wasm-pack:node && pnpm wasm-pack:dev && pnpm exec tsc -p tsconfig.node.json && pnpm test
7474
- name: screenshot
7575
run: pnpm screenshot
7676
- uses: reg-viz/reg-actions@v3
Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
11
use crate::reader::OwnedAttribute;
22

33
pub fn is_false(v: &str) -> bool {
4-
v == "0" || v == "false"
4+
v == "0" || v.eq_ignore_ascii_case("false") || v.eq_ignore_ascii_case("off")
55
}
66

77
pub fn read_bool(attrs: &[OwnedAttribute]) -> bool {
8-
if let Some(v) = attrs.first() {
9-
if is_false(&v.value) {
10-
return false;
11-
}
8+
if let Some(v) = attrs
9+
.iter()
10+
.find(|attr| attr.name.local_name == "val")
11+
.or_else(|| attrs.first())
12+
{
13+
return !is_false(&v.value);
1214
}
1315
true
1416
}
17+
18+
#[cfg(test)]
19+
mod tests {
20+
use super::*;
21+
use crate::reader::OwnedName;
22+
23+
fn attr(local_name: &str, value: &str) -> OwnedAttribute {
24+
OwnedAttribute {
25+
name: OwnedName {
26+
local_name: local_name.to_string(),
27+
namespace: None,
28+
prefix: Some("w".to_string()),
29+
},
30+
value: value.to_string(),
31+
}
32+
}
33+
34+
#[test]
35+
fn test_is_false_supports_off() {
36+
assert!(is_false("off"));
37+
assert!(is_false("OFF"));
38+
}
39+
40+
#[test]
41+
fn test_read_bool_prefers_val_attribute() {
42+
let attrs = vec![attr("foo", "false"), attr("val", "on")];
43+
assert!(read_bool(&attrs));
44+
}
45+
}

docx-core/src/reader/run.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,31 @@ mod tests {
312312
);
313313
}
314314

315+
#[test]
316+
fn test_read_on_off_values() {
317+
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
318+
<w:r><w:rPr>
319+
<w:b w:val="off"/>
320+
<w:i w:val="on"/>
321+
</w:rPr></w:r>
322+
</w:document>"#;
323+
let mut parser = EventReader::new(c.as_bytes());
324+
let run = Run::read(&mut parser, &[]).unwrap();
325+
assert_eq!(
326+
run,
327+
Run {
328+
children: vec![],
329+
run_property: RunProperty {
330+
bold: Some(Bold::new().disable()),
331+
bold_cs: Some(BoldCs::new().disable()),
332+
italic: Some(Italic::new()),
333+
italic_cs: Some(ItalicCs::new()),
334+
..RunProperty::default()
335+
},
336+
}
337+
);
338+
}
339+
315340
#[test]
316341
fn test_read_fit_text() {
317342
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">

0 commit comments

Comments
 (0)