docx/superscript-text

Vertically align a run's glyphs above the baseline at a reduced size.

Library verdicts: python-docx: — docxjs: pass

Metadata

Feature iddocx/superscript-text
Formatdocx
Categorytext-formatting
Spececma-376-5-part-1 § 17.3.2.42 w:vertAlign

XPath assertions

ID / partPredicateXPathpython-docxdocxjs
superscript-element-present
word/document.xml
exist
The run carrying the superscript text must have a <w:vertAlign> child inside <w:rPr>.
//w:r[w:t[normalize-space()='Superscript text']]/w:rPr/w:vertAlign
superscript-val-is-superscript
word/document.xml
equal = superscript
w:val must equal 'superscript'.
//w:r[w:t[normalize-space()='Superscript text']]/w:rPr/w:vertAlign/@w:val

Render assertions

IDPredicateSelectorpython-docxdocxjs
superscript-text-renderedcss_selector/ match-text = Superscript text
A DOM node marked superscript (via <sup> or vertical-align: super) must contain the fixture text.
.docx-wrapper sup, .docx-wrapper [style*='vertical-align: super'], .docx-wrapper [style*='vertical-align:super']pass
superscript-text-visiblecomputed_style = ^super(?:$|\b) style=vertical-align
The computed vertical-align on the superscript run must be super (or a super-offset length).
.docx-wrapper :is(sup, span):not(:has(*))pass

Generator source

scripts/gen_superscript_text.py

#!/usr/bin/env python3
"""Generate ``fixtures/docx/superscript-text.docx``.

A minimal document exercising ``w:vertAlign w:val="superscript"``.
One paragraph containing one superscript run.
Satisfies the assertions in ``features/docx/superscript-text.json``.
"""

from __future__ import annotations

from pathlib import Path

from docx import Document

_REPO_ROOT = Path(__file__).resolve().parent.parent
_OUT = _REPO_ROOT / "fixtures" / "docx" / "superscript-text.docx"


def main() -> int:
    doc = Document()
    paragraph = doc.add_paragraph()
    run = paragraph.add_run("Superscript text")
    run.font.superscript = True
    _OUT.parent.mkdir(parents=True, exist_ok=True)
    doc.save(_OUT, reproducible=True)
    print(_OUT)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

Manifest (expanded)

{
  "$schema": "../manifest.schema.json",
  "id": "docx/superscript-text",
  "title": "Superscript text",
  "format": "docx",
  "category": "text-formatting",
  "summary": "Vertically align a run's glyphs above the baseline at a reduced size.",
  "spec": {
    "source": "ecma-376-5-part-1",
    "clause": "17.3.2.42",
    "element": "w:vertAlign",
    "rnc_reference": "spec/ecma-376-5/part-1/rnc/WordprocessingML.rnc",
    "xsd_reference": "spec/ecma-376-5/part-1/xsd/wml.xsd",
    "notes": "<w:vertAlign w:val=\"superscript\"/> raises the run above the baseline and reduces its size. Valid w:val values are 'baseline' (default), 'superscript', 'subscript'. Unlike the toggle properties (w:b, w:i), w:val is required."
  },
  "fixtures": {
    "machine": "docx/superscript-text",
    "office": "docx/superscript-text"
  },
  "generator": {
    "python": "scripts/gen_superscript_text.py"
  },
  "assertions": [
    {
      "id": "superscript-element-present",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:r[w:t[normalize-space()='Superscript text']]/w:rPr/w:vertAlign",
      "must": "exist",
      "description": "The run carrying the superscript text must have a <w:vertAlign> child inside <w:rPr>."
    },
    {
      "id": "superscript-val-is-superscript",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:r[w:t[normalize-space()='Superscript text']]/w:rPr/w:vertAlign/@w:val",
      "must": "equal",
      "value": "superscript",
      "description": "w:val must equal 'superscript'."
    }
  ],
  "render_assertions": [
    {
      "id": "superscript-text-rendered",
      "kind": "css_selector",
      "selector": ".docx-wrapper sup, .docx-wrapper [style*='vertical-align: super'], .docx-wrapper [style*='vertical-align:super']",
      "must": "match-text",
      "value": "Superscript text",
      "description": "A DOM node marked superscript (via <sup> or vertical-align: super) must contain the fixture text."
    },
    {
      "id": "superscript-text-visible",
      "kind": "computed_style",
      "selector": ".docx-wrapper :is(sup, span):not(:has(*))",
      "style_property": "vertical-align",
      "value": "^super(?:$|\\b)",
      "description": "The computed vertical-align on the superscript run must be super (or a super-offset length)."
    }
  ]
}

Fixture

Download superscript-text.docx (18.6 KB)

Reference preview

Reference (machine, page 1 PNG)

docx/superscript-text page 1 reference

Reference PDF


Download PDF Open in PDF.js

Spec notes

<w:vertAlign w:val="superscript"/> raises the run above the baseline and reduces its size. Valid w:val values are 'baseline' (default), 'superscript', 'subscript'. Unlike the toggle properties (w:b, w:i), w:val is required.