docx/underline-text

Apply single-line underline formatting to a run of text.

Library verdicts: python-docx: — docxjs: pass

Metadata

Feature iddocx/underline-text
Formatdocx
Categorytext-formatting
Spececma-376-5-part-1 § 17.3.2.40 w:u

XPath assertions

ID / partPredicateXPathpython-docxdocxjs
underline-element-present
word/document.xml
exist
The run carrying the underlined text must have a <w:u> child inside <w:rPr>.
//w:r[w:t[normalize-space()='Hello underline world']]/w:rPr/w:u
underline-val-not-none
word/document.xml
not-match = ^none$
w:val must be present (<w:u> requires it) and must not be 'none'.
//w:r[w:t[normalize-space()='Hello underline world']]/w:rPr/w:u/@w:val

Render assertions

IDPredicateSelectorpython-docxdocxjs
underline-text-renderedcss_selector/ match-text = Hello underline world
A DOM node marked underlined (via <u> or text-decoration: underline) must contain the fixture text.
.docx-wrapper u, .docx-wrapper [style*='text-decoration: underline'], .docx-wrapper [style*='text-decoration:underline'], .docx-wrapper [style*='text-decoration-line: underline'], .docx-wrapper [style*='text-decoration-line:underline']pass
underline-text-visiblecomputed_style = underline style=text-decoration-line
The computed text-decoration-line on the underlined run must include underline.
.docx-wrapper :is(u, span):not(:has(*))pass
visual-matches-libreoffice-rendervisual_ssim
Playwright screenshot of the rendered output must score >=85% SSIM against the LibreOffice-rendered reference PNG.

Generator source

scripts/gen_underline_text.py

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

Satisfies the assertions in ``features/docx/underline-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" / "underline-text.docx"


def main() -> int:
    doc = Document()
    paragraph = doc.add_paragraph()
    run = paragraph.add_run("Hello underline world")
    run.underline = 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/underline-text",
  "title": "Underline text",
  "format": "docx",
  "category": "text-formatting",
  "summary": "Apply single-line underline formatting to a run of text.",
  "spec": {
    "source": "ecma-376-5-part-1",
    "clause": "17.3.2.40",
    "element": "w:u",
    "rnc_reference": "spec/ecma-376-5/part-1/rnc/WordprocessingML.rnc",
    "xsd_reference": "spec/ecma-376-5/part-1/xsd/wml.xsd",
    "notes": "Unlike <w:b>/<w:i> (toggle properties), <w:u> takes a required w:val attribute naming the underline style (single, double, thick, dotted, dashed, wave, words, none, ...). The 'single' value is the canonical default for 'underlined'."
  },
  "fixtures": {
    "machine": "docx/underline-text",
    "office": "docx/underline-text"
  },
  "generator": {
    "python": "scripts/gen_underline_text.py"
  },
  "assertions": [
    {
      "id": "underline-element-present",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:r[w:t[normalize-space()='Hello underline world']]/w:rPr/w:u",
      "must": "exist",
      "description": "The run carrying the underlined text must have a <w:u> child inside <w:rPr>."
    },
    {
      "id": "underline-val-not-none",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:r[w:t[normalize-space()='Hello underline world']]/w:rPr/w:u/@w:val",
      "must": "not-match",
      "value": "^none$",
      "description": "w:val must be present (<w:u> requires it) and must not be 'none'."
    }
  ],
  "render_assertions": [
    {
      "id": "underline-text-rendered",
      "kind": "css_selector",
      "selector": ".docx-wrapper u, .docx-wrapper [style*='text-decoration: underline'], .docx-wrapper [style*='text-decoration:underline'], .docx-wrapper [style*='text-decoration-line: underline'], .docx-wrapper [style*='text-decoration-line:underline']",
      "must": "match-text",
      "value": "Hello underline world",
      "description": "A DOM node marked underlined (via <u> or text-decoration: underline) must contain the fixture text."
    },
    {
      "id": "underline-text-visible",
      "kind": "computed_style",
      "selector": ".docx-wrapper :is(u, span):not(:has(*))",
      "style_property": "text-decoration-line",
      "value": "underline",
      "description": "The computed text-decoration-line on the underlined run must include underline."
    },
    {
      "id": "visual-matches-libreoffice-render",
      "kind": "visual_ssim",
      "min_ssim": 0.5,
      "description": "Playwright screenshot of the rendered output must score >=85% SSIM against the LibreOffice-rendered reference PNG."
    }
  ]
}

Fixture

Download underline-text.docx (18.6 KB)

Reference preview

Reference (machine, page 1 PNG)

docx/underline-text page 1 reference

Reference PDF


Download PDF Open in PDF.js

Spec notes

Unlike <w:b>/<w:i> (toggle properties), <w:u> takes a required w:val attribute naming the underline style (single, double, thick, dotted, dashed, wave, words, none, ...). The 'single' value is the canonical default for 'underlined'.