WordprocessingML run/paragraph/table properties — a bag of typed fields (
<w:rPr>/<w:pPr>/<w:tblPr>).
Module docxProperties | Source packages/front/office/ooxml/src/docx/properties.js | Deps xml | Worker-safe yes
Maps the most common children of <w:rPr> (§17.3.2), <w:pPr> (§17.3.1) and <w:tblPr> (§17.4.59) onto a flat object. Children that are not modelled are preserved in _extras and re-emitted on write; the three bags read their _extras in the schema order their writer emits (CT_RPr / CT_PPr / CT_TblPr, by orderBySequence), so a source listing them out of order reads as its written form does. Toggle elements (<w:b/>, <w:i/>, <w:strike/>) follow the ECMA semantics: present without w:val = on, w:val="0"|"false"|"off" = off.
Resolve
const props = runtime.resolve('docxProperties');
// Returns: { parseRunProperties, renderRunProperties,
// parseParagraphProperties, renderParagraphProperties,
// parseTableProperties, renderTableProperties,
// readToggle, writeToggle, valOf, elVal,
// sequence, orderBySequence, sameElement, hydrateLossless }
API
| Method | Signature | Returns |
|---|---|---|
parseRunProperties |
(rPrEl) => RunProperties | undefined |
Typed bag. |
renderRunProperties |
(rPr) => element | null |
<w:rPr>, or null when empty. |
parseParagraphProperties |
(pPrEl) => ParagraphProperties | undefined |
Typed bag. |
renderParagraphProperties |
(pPr) => element | null |
<w:pPr>, or null. |
parseTableProperties |
(tblPrEl) => TableProperties | undefined |
Typed bag; undefined when the element has no element children. |
renderTableProperties |
(tblPr) => element | null |
<w:tblPr> (the typed children and the _extras in CT_TblPr order), or null when nothing would render. renderRunProperties and renderParagraphProperties order theirs by CT_RPr / CT_PPr the same way. |
readToggle / writeToggle |
boolean helpers | For element toggles. |
sameElement(a, b) |
(XmlNode, XmlNode) => boolean |
Whether two elements are the same: names, attributes (in any order) and children, an element's whitespace-only text left out, a childless element's w:val compared as a toggle (true / 1 / on / none = on, false / 0 / off = off). |
hydrateLossless(bag, typeBag, untypeBag) |
(bag, (bag) => bag, (bag) => bag) => bag |
Types a property bag's _extras element by element: each is typed alone by typeBag, written back by untypeBag and kept typed only when that gives the same element (sameElement) and its fields are not set yet; an element written back otherwise, an empty one, or a name the bag holds twice (unless it types into an array) stays in _extras as read, in place. The guard of the typing extensions (wmlRunFormatting, wmlParagraphFormatting, wmlTableProperties, wmlSettings). Mutates and returns bag. |
valOf / elVal |
w:val helpers |
Read / write the w:val attribute. |
sequence(names) |
(names: Array<string|string[]>) => seq |
Indexes an xsd:sequence of child names for orderBySequence; an entry that is an array names elements sharing one place (a choice inside the sequence). |
orderBySequence(children, seq) |
(XmlNode[], seq) => XmlNode[] |
A new array of children sorted by their place in seq, stably. An mc:AlternateContent takes the place of the element its mc:Fallback (else its first mc:Choice) holds; a name seq does not list goes before the sequence's trailing …Change entry when it has one, else last. The same code as ooxmlShared's (this module has no ooxmlShared dependency); docxStructure uses it to put the elements a read preserved in _extras back among the typed ones. |
Model
RunProperties := {
bold?, italic?, strike?, // boolean
underline?: 'single'|'double'|…,
underlineColor?: 'RRGGBB', // <w:u w:color>
underlineTheme?: ThemeLink, // <w:u>'s theme link
color?: 'RRGGBB', // hex without '#'
colorTheme?: ThemeLink, // <w:color>'s theme link, which Word applies
// before `color`: drop it when `color` changes
size?: number, // half-points (24 = 12pt)
font?: string, // <w:rFonts>: ascii, else hAnsi, else cs
fonts?: { hint?, ascii?, hAnsi?, eastAsia?, cs?,
asciiTheme?, hAnsiTheme?, eastAsiaTheme?, cstheme? },
// every <w:rFonts> attribute, as read
vertAlign?: 'superscript'|'subscript'|'baseline',
highlight?: string,
rStyle?: string, // styleId
_extras?: [xmlNode]
}
ThemeLink := { themeColor?, themeTint?, themeShade? } // the attributes, verbatim
ParagraphProperties := {
align?: 'left'|'center'|'right'|'both'|'distribute',
indent?: { left?, right?, firstLine?, hanging?, extraAttrs? }, // twips
spacing?: { before?, after?, line?, lineRule?, extraAttrs? }, // lineRule: 'auto' | 'exact' | 'atLeast', as read
pStyle?: string, // styleId
numPr?: { ilvl, numId },
rPr?: RunProperties,
_extras?: [xmlNode]
}
TableProperties := { // <w:tblPr> (§17.4.59)
style?: string, // <w:tblStyle w:val> (styleId)
width?: { w: number|string, // <w:tblW w:w w:type>; number when numeric
type: 'auto'|'dxa'|'pct'|'nil' }, // default 'auto'
borders?: TableBorders, // <w:tblBorders>
cellMargins?: TableCellMargins, // <w:tblCellMar> (§17.4.42)
_extras?: [xmlNode] // every other child, verbatim, in CT_TblPr order
}
TableCellMargins := { // edges render in schema order
top?: Width, start?: Width, left?: Width,
bottom?: Width, end?: Width, right?: Width,
_extras?: [xmlNode] // any other child of <w:tblCellMar>
}
Width := { w: number|string, // <w:top w:w w:type>; number when numeric
type: 'auto'|'dxa'|'pct'|'nil' } // default 'auto'
TableBorders := { // edges render in schema order
top?: Border, left?: Border, bottom?: Border,
right?: Border, insideH?: Border, insideV?: Border,
_extras?: [xmlNode] // any other child of <w:tblBorders>
}
Border := { val: string, sz?: number, space?: number, color?: string,
extraAttrs?: { [attrName: string]: string } }
// <w:top w:val w:sz w:space w:color ...> (§17.3.4)
// extraAttrs: every other attribute (w:themeColor,
// w:themeTint, w:themeShade, w:shadow, w:frame, ...),
// full names, source order; key omitted when empty
Examples
Bold red 14pt run
const props = runtime.resolve('docxProperties');
const rPrEl = props.renderRunProperties({
bold: true, color: 'CC0000', size: 28
});
// → <w:rPr><w:b/><w:color w:val="CC0000"/><w:sz w:val="28"/></w:rPr>
Centred paragraph with indentation
props.renderParagraphProperties({
align: 'center',
indent: { left: 720, firstLine: 360 },
pStyle: 'Heading1'
});
A bordered table
props.renderTableProperties({
style: 'TableGrid',
borders: { top: { val: 'single', sz: 4, space: 0, color: 'auto' },
insideH: { val: 'single', sz: 4, space: 0, color: 'auto' } }
});
// → <w:tblPr><w:tblStyle w:val="TableGrid"/><w:tblBorders>
// <w:top w:val="single" w:sz="4" w:space="0" w:color="auto"/>
// <w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/>
// </w:tblBorders></w:tblPr>
Default cell margins
props.renderTableProperties({
style: 'TableGrid',
cellMargins: { left: { w: 108, type: 'dxa' }, right: { w: 108, type: 'dxa' } }
});
// → <w:tblPr><w:tblStyle w:val="TableGrid"/><w:tblCellMar>
// <w:left w:w="108" w:type="dxa"/><w:right w:w="108" w:type="dxa"/>
// </w:tblCellMar></w:tblPr>
Coverage
- Typed: bold/italic/strike/underline/color/size/font/fonts/vertAlign/highlight/rStyle (run); align/indent/spacing/pStyle/numPr/rPr (paragraph).
w:spacingandw:indattributes:before,after,line,lineRuleandleft,right,firstLine,hangingare typed; every other attribute of the two elements (beforeAutospacing,afterAutospacing,beforeLines,afterLines,start,end,leftChars,firstLineChars, …) is kept verbatim inspacing.extraAttrs/indent.extraAttrs, source order, and written back after the typed ones, which it never overrides; an element holding only such attributes is kept. Editing a typed field leavesextraAttrsas read (for instance aw:startnext to an editedleft).- Run fonts:
fontis one name (w:ascii, elsew:hAnsi, elsew:cs); a model withoutfontswrites it to the three as<w:rFonts w:ascii w:hAnsi w:cs>. A read also givesfonts, every attribute of<w:rFonts>by its local name (a<w:rFonts>holding onlyw:hintgivesfontsand nofont), and the writer writesfontsback as it is whilefontstill equals the name it derives. A changedfontis written toascii,hAnsiandcs, and their theme attributes (asciiTheme,hAnsiTheme,cstheme) are left out — Word would otherwise apply the theme font over the edit; a removedfontremoves those six.eastAsia,eastAsiaThemeandhintare always kept; with no attribute left, no<w:rFonts>is written. - Typed (table):
tblStyle(style),tblW(width),tblBorders(borders, six edges; an empty<w:tblBorders/>has nothing to type and stays verbatim intblPr._extras, sobordersis absent for it),tblCellMar(cellMargins, six edges:top,start,left,bottom,end,right).w:tblGrid,w:trPr,w:tcBorders,w:tblLookandw:tblStylePrare not modelled; they stay verbatim in_extras(tblLookintblPr._extras); a table written with no grid gets one derived from its rows (see structure). _extras: everything else (kern,w:lang,w:shd,w:tabs,w:keepNext, …) — modelled by thewmlRunFormatting/wmlParagraphFormattingextras.
Notes
sizeis in half-points (the ECMA convention): 12pt =size: 24.coloris hex without#. The special value'auto'is allowed.align: 'both'means justified (OOXML terminology).Border.extraAttrscarries every attribute the four modelled fields do not (w:themeColor,w:themeTint,w:themeShade,w:shadow,w:frame, and anything else) verbatim, in source order, and re-emits them afterw:color, so every border attribute is re-emitted on write. AnextraAttrskey namingw:val/w:sz/w:space/w:coloris ignored on render; the four fields win.- A
Borderonly renders the attributes it carries:{ val: 'nil' }emits<w:top w:val="nil"/>and never anundefinedattribute.szis in eighths of a point. renderTablePropertiesfollows the file's_extras-last convention:tblBorders._extrasclose the borders element andcellMargins._extrasclose the margins element.tblPr._extrasfollow the typed children, split attblCellMar:tblLook,tblCaption,tblDescriptionandtblPrChangerender after it, every other extra before it. A schema-ordered<w:tblPr>(for exampletblLayoutthentblCellMarthentblLook) therefore keeps its order through a parse/render round trip.cellMarginsis a default for every cell of the table (or of every table using the style, when set on a table style);wis in twentieths of a point withtype: 'dxa'. Word's built-inTable Gridstyle usesleft/right108 dxa (0.19 cm).- Toggles:
bold: trueemits<w:b/>(now:val);bold: falseemits<w:b w:val="0"/>(an explicit override of an inherited style).
See also
- docx-structure — applies these props to runs/paragraphs.
- docx-styles — reuses them for
docDefaultsand styles. - docx-numbering — reuses them for
<w:lvl>rPr/pPr.