implement Content Controls #3271
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add comprehensive Content Controls implementation
What this PR does
This adds full support for Content Controls to the docx library. Content Controls are the interactive elements you see in Word documents - things like dropdown menus, date pickers, checkboxes, and text input fields that users can interact with.
Why we need this
Content Controls are essential for creating professional document templates and forms. They allow developers to build documents where users can fill in specific areas without breaking the document structure or formatting.
More importantly, Content Controls serve as reliable markers that plugins and external systems can use to identify and manipulate specific document sections. Unlike bookmarks or styles, Content Controls provide a robust tagging system that survives document editing.
What's included
I've implemented all five main types of Content Controls:
Block Content Control - For wrapping paragraphs and tables
Run Content Control - For inline text with rich formatting
Checkbox Content Control - Interactive checkboxes
Date Picker Content Control - Calendar date selection
Dropdown Content Control - Selection menus with predefined options
Each control supports the standard features you'd expect: custom appearance (bounding box, tags, or hidden), data binding to XML stores, locking mechanisms, and visual styling options like custom colors and placeholder text.
Plugin development example
Here's how a developer might use Content Controls to build a document processing plugin:
This approach gives plugin developers reliable anchor points in documents that won't break when users edit other parts of the document. The tags act as a stable API between the document template and the processing system.
Technical details
The implementation follows Microsoft's OOXML specification closely to ensure compatibility with Word 2010 and later versions. I've added comprehensive validation to prevent common issues that can corrupt Word documents, particularly around nesting controls and generating proper GUIDs.
All the code is fully typed with TypeScript - no more
any
types floating around. I fixed 125 ESLint errors in the process and added over 600 test cases covering edge cases, OOXML compliance, and error conditions.Documentation and examples
I've written extensive documentation including a complete implementation specification and usage guide with practical examples. There's also a demo file showing how to use each type of control, including plugin development patterns.
The API is designed to be intuitive. Here's what creating a simple block control looks like:
Testing
The implementation is thoroughly tested with particular attention to preventing Word document corruption. I've included tests for nesting validation, OOXML compliance, and all the edge cases I could think of.
This is a purely additive feature - existing code won't be affected at all.