Apply a text-highlight background color to a run of text.
Library verdicts: python-docx: — docxjs: pass
| Feature id | docx/highlight-text |
|---|---|
| Format | docx |
| Category | text-formatting |
| Spec | ecma-376-5-part-1 § 17.3.2.15 w:highlight |
| ID / part | Predicate | XPath | python-docx | docxjs |
|---|---|---|---|---|
highlight-element-presentword/document.xml | existThe run carrying the highlighted text must have a <w:highlight> child inside <w:rPr>. | //w:r[w:t[normalize-space()='Highlighted text']]/w:rPr/w:highlight | — | — |
highlight-val-is-yellowword/document.xml | equal = yelloww:val must equal 'yellow'. | //w:r[w:t[normalize-space()='Highlighted text']]/w:rPr/w:highlight/@w:val | — | — |
| ID | Predicate | Selector | python-docx | docxjs |
|---|---|---|---|---|
highlight-text-rendered | css_selector/ match-text = Highlighted textA DOM node with a yellow background (via background-color/background or <mark>) must contain the fixture text. | .docx-wrapper [style*='background-color: yellow'], .docx-wrapper [style*='background-color:yellow'], .docx-wrapper [style*='background: yellow'], .docx-wrapper [style*='background:yellow'], .docx-wrapper [style*='rgb(255, 255, 0)'], .docx-wrapper [style*='rgb(255,255,0)'], .docx-wrapper [style*='#ffff00'], .docx-wrapper [style*='#FFFF00'], .docx-wrapper mark | — | pass |
highlight-text-visible | computed_style = ^(rgb\(255,\s*255,\s*0\)|#ffff00|#FFFF00|yellow)$ style=background-colorThe computed background-color on the highlighted run must resolve to yellow. | .docx-wrapper :is(mark, span):not(:has(*)) | — | pass |
scripts/gen_highlight_text.py
#!/usr/bin/env python3
"""Generate ``fixtures/docx/highlight-text.docx``.
A minimal document exercising the ``w:highlight`` element. One
paragraph containing one run with yellow text-highlighting.
Satisfies the assertions in ``features/docx/highlight-text.json``.
"""
from __future__ import annotations
from pathlib import Path
from docx import Document
from docx.enum.text import WD_COLOR_INDEX
_REPO_ROOT = Path(__file__).resolve().parent.parent
_OUT = _REPO_ROOT / "fixtures" / "docx" / "highlight-text.docx"
def main() -> int:
doc = Document()
paragraph = doc.add_paragraph()
run = paragraph.add_run("Highlighted text")
run.font.highlight_color = WD_COLOR_INDEX.YELLOW
_OUT.parent.mkdir(parents=True, exist_ok=True)
doc.save(_OUT, reproducible=True)
print(_OUT)
return 0
if __name__ == "__main__":
raise SystemExit(main())
{
"$schema": "../manifest.schema.json",
"id": "docx/highlight-text",
"title": "Highlight text",
"format": "docx",
"category": "text-formatting",
"summary": "Apply a text-highlight background color to a run of text.",
"spec": {
"source": "ecma-376-5-part-1",
"clause": "17.3.2.15",
"element": "w:highlight",
"rnc_reference": "spec/ecma-376-5/part-1/rnc/WordprocessingML.rnc",
"xsd_reference": "spec/ecma-376-5/part-1/xsd/wml.xsd",
"notes": "<w:highlight w:val=\"yellow\"/> sets the text highlighting color. w:val is an enumerated ST_HighlightColor (black, blue, cyan, darkBlue, darkCyan, darkGray, darkGreen, darkMagenta, darkRed, darkYellow, green, lightGray, magenta, none, red, white, yellow). Distinct from <w:shd> (cell/run shading, which uses hex values)."
},
"fixtures": {
"machine": "docx/highlight-text",
"office": "docx/highlight-text"
},
"generator": {
"python": "scripts/gen_highlight_text.py"
},
"assertions": [
{
"id": "highlight-element-present",
"part": "word/document.xml",
"namespaces": {
"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
},
"xpath": "//w:r[w:t[normalize-space()='Highlighted text']]/w:rPr/w:highlight",
"must": "exist",
"description": "The run carrying the highlighted text must have a <w:highlight> child inside <w:rPr>."
},
{
"id": "highlight-val-is-yellow",
"part": "word/document.xml",
"namespaces": {
"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
},
"xpath": "//w:r[w:t[normalize-space()='Highlighted text']]/w:rPr/w:highlight/@w:val",
"must": "equal",
"value": "yellow",
"description": "w:val must equal 'yellow'."
}
],
"render_assertions": [
{
"id": "highlight-text-rendered",
"kind": "css_selector",
"selector": ".docx-wrapper [style*='background-color: yellow'], .docx-wrapper [style*='background-color:yellow'], .docx-wrapper [style*='background: yellow'], .docx-wrapper [style*='background:yellow'], .docx-wrapper [style*='rgb(255, 255, 0)'], .docx-wrapper [style*='rgb(255,255,0)'], .docx-wrapper [style*='#ffff00'], .docx-wrapper [style*='#FFFF00'], .docx-wrapper mark",
"must": "match-text",
"value": "Highlighted text",
"description": "A DOM node with a yellow background (via background-color/background or <mark>) must contain the fixture text."
},
{
"id": "highlight-text-visible",
"kind": "computed_style",
"selector": ".docx-wrapper :is(mark, span):not(:has(*))",
"style_property": "background-color",
"value": "^(rgb\\(255,\\s*255,\\s*0\\)|#ffff00|#FFFF00|yellow)$",
"description": "The computed background-color on the highlighted run must resolve to yellow."
}
]
}
