Skip to content

fix: use use_prefix: true (not prefix: 'm') for Word-compatible serialization - #9

Merged
ronaldtse merged 1 commit into
mainfrom
fix/use-prefix-api
Jul 15, 2026
Merged

fix: use use_prefix: true (not prefix: 'm') for Word-compatible serialization#9
ronaldtse merged 1 commit into
mainfrom
fix/use-prefix-api

Conversation

@ronaldtse

Copy link
Copy Markdown
Contributor

Summary

Follow-up to PR #8 to fix incorrect serialization API usage in the specs.

The previous PR documented a "known limitation" that CTR.new(br: CTBr.new).to_xml(prefix: "m") produces prefix-rebinding (<m:br xmlns:m="...wordprocessing..."/>). That framing was wrong — the limitation only manifests with incorrect API usage. prefix: "m" forces a single prefix on every element, which contradicts the per-namespace prefix_default declarations and causes the rebinding.

The correct API is use_prefix: true, which tells the serializer to honor each namespace's prefix_default. This is the form Word requires — Word rejects math elements in default xmlns form.

What changed

  • spec/omml_spec.rb (3 call sites): prefix: "m"use_prefix: true
  • spec/omml_fixture_round_trip_spec.rb (1 call site): same change
  • spec/omml/wordprocessing_in_math_spec.rb: replaced the false "known limitation" framing with a proper assertion that programmatic CTR.new(br: CTBr.new).to_xml(use_prefix: true) produces:
    • br element with unprefixed_name == "br"
    • wordprocessing namespace URI
    • w: prefix (not a rebound m:)

Why this matters

Word requires every math element to carry the m: prefix (per OOXML schema's elementFormDefault="qualified", which Word enforces strictly). Default xmlns declarations are rejected. use_prefix: true is the API that produces Word-compatible output. prefix: "m" was always wrong; the "known limitation" was actually "wrong API usage".

Test plan

  • bundle exec rspec — 333 examples, 0 failures
  • bundle exec rubocop — clean
  • Manual verification: CTR.new(br: CTBr.new(type: "page")).to_xml(use_prefix: true) produces <m:CT_R xmlns:m="...math"><w:br xmlns:w="...wordprocessing" w:type="page"/></m:CT_R> — correct prefix per element

The prefix: 'm' option forces a SINGLE prefix for every element in
the output, which causes prefix rebinding when child elements belong
to a different namespace (e.g. <m:br xmlns:m='...wordprocessing...'/>
inside an m:CT_R). The rebinding is valid XML but unconventional,
and the API itself is wrong: it tells the serializer to use 'm'
for everything, contradicting the per-namespace prefix_default
declarations.

The correct API for 'use each namespace prefix_default' is
use_prefix: true. With it, math elements get m: and wordprocessing
elements get w: naturally — the form Word requires.

Update all four call sites in the specs:
- spec/omml_spec.rb:90-91, 104 (parse + serialize tests)
- spec/omml_fixture_round_trip_spec.rb:169 (round-trip corpus)
- spec/omml/wordprocessing_in_math_spec.rb (programmatic construction)

The programmatic-construction spec now asserts:
- br element has unprefixed_name 'br'
- br namespace URI is wordprocessing
- br namespace prefix is 'w' (not a rebound 'm')

This is the form Word requires; default xmlns declarations are
Word-incompatible for math content.
@ronaldtse
ronaldtse merged commit 45568c1 into main Jul 15, 2026
14 checks passed
@ronaldtse
ronaldtse deleted the fix/use-prefix-api branch July 15, 2026 04:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant