WordprocessingML structural elements — runs, hyperlinks, paragraphs, tables, sections, SDTs.
Module docxStructure | Source packages/front/office/ooxml/src/docx/structure.js | Deps xml, docxProperties, docxDrawing, ooxmlMath | Worker-safe yes
Every structural element has a parse<Name> / render<Name> pair that round-trips its XML. Unknown children of a container land in _extras. The model is uniform: every node carries a type and a children (or body / cells / rows).
Resolve
const s = runtime.resolve('docxStructure');
// Returns: { parseRun, renderRun,
// parseHyperlink, renderHyperlink,
// parseParagraph, renderParagraph,
// parseTable, renderTable,
// parseRow, renderRow,
// parseCell, renderCell,
// parseSection, renderSection,
// parseSdt, renderSdt,
// parseSdtProperties, renderSdtProperties,
// parseBody, renderBodyChildren }
API
| Method | Signature | Returns |
|---|---|---|
parseRun / renderRun |
(<w:r>) / (run) |
Run with typed rPr + children. |
parseHyperlink / renderHyperlink |
symmetric | <w:hyperlink>. |
parseParagraph / renderParagraph |
<w:p> |
Full paragraph. |
parseTable / renderTable |
<w:tbl> |
Table with a typed tblPr and rows. |
parseRow / renderRow |
<w:tr> |
Row with cells. |
parseCell / renderCell |
<w:tc> |
Cell with children. |
parseSection / renderSection |
<w:sectPr> |
Section properties. pageSize (w, h) and pageMargin are in twips: a strict universal measure (612pt, 2.54cm, 1in, 6pc) is converted on read, so the section is written back in integers. pageSize.orient, and pageSize.code (the w:code paper code, a number). An empty <w:sectPr/> reads as {} and is written back as the empty element; no sectPr writes none. |
parseSdt / renderSdt |
<w:sdt> |
Structured Document Tag (templating). |
parseSdtProperties / renderSdtProperties |
<w:sdtPr> |
SDT properties. |
parseBody |
(<w:body>) => {body, sectPr, extras} |
Decomposed body. |
renderBodyChildren |
(doc) => element[] |
<w:body> children (with sectPr last). |
Model
document := { type:'document', body:[paragraph|table|blockSdt|oMathPara|marker],
sectPr?, background?,
_extras? } // any other body child, verbatim, before sectPr
background := { color?, themeColor?, themeTint?, themeShade?, // <w:background>
_extras?: [xmlNode] } // its v:background / drawing, verbatim
paragraph := { type:'paragraph', pPr?, children:[inline], paraId?, textId?, _extras? }
inline := run | hyperlink | sdt | fldSimple | oMath | revision | marker
marker := bookmarkStart | bookmarkEnd | commentRangeStart | commentRangeEnd
| rangeMarker // inline, or between blocks (body, cell,
// a block SDT's content)
bookmarkStart := { type:'bookmarkStart', id, name?, colFirst?, colLast?: number,
displacedByCustomXml? }
bookmarkEnd | commentRangeStart | commentRangeEnd
:= { type, id, displacedByCustomXml? }
rangeMarker := { type:'rangeMarker', name, attrs } // range markup with no type
// of its own: moveFrom/moveTo/customXml…RangeStart|End,
// permStart, permEnd, proofErr; attrs verbatim
revision := { type:'ins'|'del'|'moveFrom'|'moveTo', id, author?, date?,
children:[inline], // typed like a paragraph's
_extras?: [xmlNode] } // any other child, verbatim
run := { type:'run', rPr?, children:[textRun|breakRun|tabRun|drawing|…] }
textRun := { type:'text', value: string }
breakRun := { type:'break', kind?: 'page'|'column'|'line'|'textWrapping', clear?: string } // clear: w:clear
tabRun := { type:'tab' }
delText := { type:'delText', value: string } // inside <w:del>
commentReference := { type:'commentReference', id }
footnoteReference := { type:'footnoteReference', id, customMarkFollows?: boolean } // endnoteReference alike
fldChar := { type:'fldChar', kind:'begin'|'separate'|'end', dirty?, fldLock?,
_extras?: [xmlNode] } // <w:ffData> / <w:fldData> / <w:numberingChange>, verbatim
instrText := { type:'instrText', value: string }
footnoteReference / endnoteReference / softHyphen / noBreakHyphen — same shape
hyperlink := { type:'hyperlink', rId?, anchor?, target?, external?,
tgtFrame?, tooltip?, docLocation?, // strings, verbatim
history?: boolean, // w:history (ST_OnOff), written 1 / 0
children:[inline], // typed like a paragraph's
_extras?: [xmlNode] } // any other child, verbatim
// target / external: set by docx.read when the rId resolves in
// the node's part, consumed by docx.write (see docx.md § Notes)
smartTag := { type:'smartTag',
attrs: { [name]: string }, // w:uri, w:element, as read
smartTagPr?: xmlNode, // <w:smartTagPr>, verbatim
children:[inline], // typed like a paragraph's
_extras?: [xmlNode] } // any other child, verbatim
// a w:smartTag (§ 17.5.1.9) among a paragraph's, a revision's,
// a content control's or a smart tag's inlines; written back in
// its place, w:smartTagPr first
table := { type:'table',
tblPr?: TableProperties, // <w:tblPr>: style, width, borders, cellMargins
// (see docxProperties; other tblPr
// children live in tblPr._extras)
_extras?: [xmlNode], // <w:tblGrid> (and anything else), verbatim
rows:[{ type:'row', sdt?:[SdtWrapper], paraId?, textId?,
cells:[{ type:'cell', sdt?:[SdtWrapper],
children:[paragraph|table|blockSdt|marker],
_extras?: [xmlNode] }] }] }
SdtWrapper := { group: number, // unique within the row / table
properties?: SdtProperties,
_extras?: [xmlNode], // <w:sdtEndPr>, verbatim
_contentExtras?: [xmlNode] } // other content children, verbatim
sdt := { type:'sdt', properties?: SdtProperties,
children:[inline], // inline, in a paragraph: typed as a paragraph's
_extras?: [xmlNode], // <w:sdtEndPr>, verbatim
_contentExtras?: [xmlNode] } // other content children (w:proofErr), verbatim
blockSdt := { type:'blockSdt', properties?: SdtProperties,
children:[paragraph|table|blockSdt], // block, in the body or a cell
_extras?: [xmlNode] } // <w:sdtEndPr>, verbatim
SdtProperties := { alias?, tag?, id?: number, showingPlcHdr?: boolean,
dataBinding?: { xpath?, prefixMappings?, storeItemID? },
kind?: 'text'|'richText'|'picture'|'dropDownList'|'comboBox'
|'date'|'checkbox'|'repeatingSection'|'repeatingSectionItem',
sectionTitle?: string, // repeatingSection only
doNotAllowInsertDeleteSection?: true, // repeatingSection only
_kindNode?: xmlNode, // dropDownList / comboBox / date / checkbox, verbatim
_extras?: [xmlNode] } // any other <w:sdtPr> child, verbatim
Examples
Build a rich paragraph
const s = runtime.resolve('docxStructure');
const para = {
type: 'paragraph',
pPr: { align: 'center' },
children: [
{ type: 'run', rPr: { bold: true },
children: [{ type: 'text', value: 'Title' }] },
{ type: 'run', children: [{ type: 'break', kind: 'line' }] },
{ type: 'run', children: [{ type: 'text', value: 'subtitle' }] }
]
};
s.renderParagraph(para);
Parse a body
const root = xml.parse('<w:body>…</w:body>');
const { body, sectPr, extras } = s.parseBody(root);
// body: Array<paragraph|table|…>, sectPr: SectionProperties|undefined
Notes
- Word's paragraph and row ids: a paragraph's and a table row's
w14:paraId/w14:textIdread asparaId/textId(strings) when the read keeps thew14namespace (docx.read'sextensions: 'keep'; the default read drops it, so no field), and are written back on thew:p/w:tr— a paragraph holding only a display equation (oMathPara) keeps them too.docx.writewrites eachparaIdonce per part: a value an earlier paragraph or row of the part carries (a paragraph a consumer copied) is left off the later one (itstextIdis written). New paragraphs get none (Word assigns them).withParaIds(fn)sets that once-per-part scope around a render. - Tracked changes:
<w:ins>,<w:del>,<w:moveFrom>and<w:moveTo>arerevisionnodes, in place among the paragraph's children (ECMA-376 Part 1 § 17.13.5). A revision's children are typed exactly as a paragraph's — runs, hyperlinks, inline SDTs, simple fields, math, bookmark and comment-range markers and nested revisions — and any other child (<w:smartTag>,<w:customXml>, …) stays verbatim in the revision's own_extras, written after its typed children. Nothing inside a revision is dropped.delTextstays distinct fromtext, so the two are never merged by accident. The*PrChangesnapshots are the opt-inwmlTrackedChanges' concern; the move range markers are typed here (see Range markup). - Hyperlinks: a
<w:hyperlink>holds a paragraph's content (CT_Hyperlink, EG_PContent): its children are typed exactly as a paragraph's — runs, revisions, bookmark and comment-range markers, range markers, inline SDTs, simple fields, math — in place, and any other child (<w:smartTag>, …) stays verbatim in the hyperlink's own_extras, written after its typed children. A consumer that walks a hyperlink'schildrenmust type-check them, as for a paragraph's: they are not only runs. Onlyr:idandw:anchorare typed among its attributes. - Field characters: a
<w:fldChar>reads as{ type:'fldChar', kind }(w:fldCharType), withdirtyandfldLock(w:fldLockon, ECMA-376ST_OnOff) when set; its child — the<w:ffData>of a legacy form field (text input, check box, drop-down), a<w:fldData>, or the<w:numberingChange>of a tracked numbering change — stays verbatim in the node's_extrasand is written back inside the element. - Range markup: the twelve move and custom XML revision range markers (
w:moveFromRangeStart/End,w:moveToRangeStart/End,w:customXml{Ins,Del,MoveFrom,MoveTo}RangeStart/End), the permission ranges (w:permStart/w:permEnd) and the proofing marks (w:proofErr) read as onerangeMarkernode (namethe local name,attrsverbatim) at their place — among a paragraph's, a revision's or a hyperlink's inline children, in the body, in a block SDT's content or in a table cell — and are written back there. - Bookmarks and comment ranges:
w:bookmarkStart/w:bookmarkEndandw:commentRangeStart/w:commentRangeEndread as their typed nodes wherever they stand — inline, and between blocks in the body (of the main document and of the story parts), in a block SDT's content and in a table cell — and are written back there. Besidesid(and a bookmark'sname) they keepdisplacedByCustomXml(next/prev) and a table-column bookmark'scolFirst/colLast(numbers). A consumer that walks the body or a cell's children must type-check them: they are not only blocks. Markers between rows or between cells stay verbatim in the table's or the row's_extras, written after its rows or cells. A body child the model does not type is kept indocument._extras, a block SDT content child in its_contentExtras, both verbatim. - Empty properties: an empty
<w:rPr/>or<w:pPr/>(and an empty paragraph-mark<w:rPr/>) reads as an empty object and is written back as the empty element; a run or paragraph with norPr/pPrwrites none. commentReference/footnoteReference/endnoteReferencecarry only anid; the content lives in the linked part.- Page background:
<w:background>, before<w:body>in the main part, reads asdocument.background(docx.read): itsw:color,w:themeColor,w:themeTintandw:themeShadeas strings, its children (a VML<v:background>with its fill, a drawing) verbatim in_extras.docx.writeputs it back before the body, the root declaring the namespaces its children use; nobackgroundwrites none. Word shows it only when the settings ask (w:displayBackgroundShape, carried with the settings). - Content controls around cells and rows: a row-level
<w:sdt>(CT_SdtCell) wraps cells, a table-level one (CT_SdtRow) wraps rows. Their cells (rows) are read in place among the row's cells (the table's rows), as ordinarycell(row) nodes, each carryingsdt: the controls around it, outermost first. A wrapper is{ group, properties?, _extras?, _contentExtras? }:groupis unique within the row (the table), numbered in the order the controls open, and the items of one control share it;propertiesis the<w:sdtPr>read as a block control's;_extraskeeps the control's other children (<w:sdtEndPr>) and_contentExtrasany content child that is neither a cell (row) nor a control, both verbatim. The write puts consecutive items sharing a control back inside one<w:sdt>(its properties,_extras, then<w:sdtContent>with the items, the inner controls and_contentExtras), so every cell keeps its column. A control that wraps no cell (row) stays verbatim in the row's (table's)_extras. A consumer that walksrow.cellssees every cell; one that ignoressdtreads the content without the control. A cell is written with an empty<w:p/>only when it holds no block-level content, typed or kept verbatim (w:p,w:tbl,w:sdt,w:customXml,w:altChunk): ECMA-376 requires some. - Tables:
<w:tblPr>is typed astable.tblPr(style,width,borders) and rendered first, then the grid, then the other_extras, then the rows.CT_Tblrequires<w:tblPr>and<w:tblGrid>: a table with notblPr(typed or preserved) renders an empty<w:tblPr/>, and a table with no preserved<w:tblGrid>renders one derived from its rows — one<w:gridCol>per column of the widest row (a cell counts itsgridSpan), withw:wonly when a span-free row as wide as the grid gives every cell adxawidth.<w:tblGrid>and each row's<w:trPr>are NOT typed here: they stay verbatim in_extras(table._extras/row._extras); cell borders (<w:tcBorders>) stay in the cell'stcPr._extras. The opt-inwmlTablePropertiespromotes those. oMathParanodes are delegated toooxmlMath. Block math: the schema allows<m:oMathPara>only inside a<w:p>, sorenderBodyChildrenwrites a body-leveloMathParanode as a<w:p>with no<w:pPr>holding only that<m:oMathPara>, as Word does.parseBodyreads that shape back as the body-level node: a<w:p>with no<w:pPr>whose only element child is one<m:oMathPara>(a bare body-level<m:oMathPara>reads the same way). Any other paragraph holding an<m:oMathPara>— with a<w:pPr>, a run or a bookmark beside it — stays aparagraphwith the<m:oMathPara>in its_extras, and so does one inside a table cell or a block SDT. The same applies to the story parts (headers, footers, notes, comments), which shareparseBody/renderBodyChildren.- Drawings are delegated to
docxDrawing. - SDTs (Structured Document Tags) type the
alias,tag,id,showingPlcHdranddataBindingchildren of<w:sdtPr>plus thekindelement; anything else (<w:placeholder>,<w:rPr>,<w:lock>, …) stays verbatim inproperties._extras— see templating. - Repeating sections are a Word 2012 extension ([MS-DOCX] §2.5.1.10, §2.5.1.11, §2.5.3.8), not ECMA-376 elements.
parseSdtPropertiesreads<w15:repeatingSection>askind: 'repeatingSection', its<w15:sectionTitle w:val>child assectionTitleand its<w15:doNotAllowInsertDeleteSection>child (on/off: an absentw:val,1,trueoronmean on) asdoNotAllowInsertDeleteSection: true;<w15:repeatingSectionItem/>reads askind: 'repeatingSectionItem'. The legacy main-namespace form (<w:repeatingSection w:sectionTitle="…"/>,<w:repeatingSectionItem/>) is still read.renderSdtPropertieswrites thew15elements only, title before the lock; declaring thew15namespace on the part root is the caller's job (docx.writedoes it forword/document.xml).
See also
- docx — top-level orchestrator.
- docx-properties — delegated formatting.
- math — inline and block math.