Paste From Word Plugin
- Features
- askBeforePasteFromWord
- processPasteFromWord
- defaultActionOnPasteFromWord
- pasteFromWordActionList
- Basic Word Paste Configuration
- Always Clean Word HTML
- Always Keep Word Formatting
- Custom Word Paste Options
- Disable Word Detection
- Use Default Paste Action
- With Beautify HTML Integration
- Word Detection
- Processing Flow
- Insert Modes
- Word Cleaning Process
- Style Application
- Beautify HTML Event
- beautifyHTML
- :processHTML
- Edge Cases
- Notes
- Typical Use Case
- Classes
Processes pasting HTML content from Microsoft Word and Excel documents. This plugin detects Word/Excel-formatted HTML, prompts user for insertion method, and cleans up Microsoft Office-specific markup and styles.
Features
- Automatic detection of Word/Excel HTML
- User prompt for insertion method (keep formatting, clean, or plain text)
- Word-specific HTML cleaning
- Excel paste support
- Style preservation or stripping options
- Custom action list for Word paste
- Default action configuration
- Beautify HTML integration
- Tag stripping for plain text mode
- Seamless integration with paste plugin
askBeforePasteFromWord
Type: boolean
Default: true
When true
, shows a dialog asking the user how to insert content when Word/Excel HTML is detected. Options defined in pasteFromWordActionList
.
Example:
const editor = Jodit.make('#editor', {
askBeforePasteFromWord: true
});
processPasteFromWord
Type: boolean
Default: true
When true
, enables processing of HTML fragments copied from Microsoft Word/Excel. When false
, Word HTML is treated as regular HTML.
Example:
const editor = Jodit.make('#editor', {
processPasteFromWord: true
});
defaultActionOnPasteFromWord
Type: InsertMode | null
Default: null
Default insert method when pasting from Word/Excel and askBeforePasteFromWord
is false
. When null
, falls back to defaultActionOnPaste
. Possible values:
INSERT_AS_HTML
: Keep Word formattingINSERT_AS_TEXT
: Clean Word markupINSERT_ONLY_TEXT
: Insert only plain textnull
: UsedefaultActionOnPaste
instead
Example:
const editor = Jodit.make('#editor', {
askBeforePasteFromWord: false,
defaultActionOnPasteFromWord: Jodit.constants.INSERT_AS_TEXT
});
pasteFromWordActionList
Type: IUIOption[]
Default:
[
{ value: INSERT_AS_HTML, text: 'Keep' },
{ value: INSERT_AS_TEXT, text: 'Clean' },
{ value: INSERT_ONLY_TEXT, text: 'Insert only Text' }
]
Options displayed to user when pasting from Word/Excel (if askBeforePasteFromWord
is true
). Each option has:
value
: Insert mode constanttext
: Display text for the option
Example:
const editor = Jodit.make('#editor', {
pasteFromWordActionList: [
{ value: Jodit.constants.INSERT_AS_HTML, text: 'Keep Formatting' },
{ value: Jodit.constants.INSERT_AS_TEXT, text: 'Clean Markup' },
{ value: Jodit.constants.INSERT_ONLY_TEXT, text: 'Plain Text' }
]
});
Basic Word Paste Configuration
const editor = Jodit.make('#editor', {
askBeforePasteFromWord: true,
processPasteFromWord: true
});
// Paste from Word
// User will be prompted with insertion options
Always Clean Word HTML
const editor = Jodit.make('#editor', {
askBeforePasteFromWord: false,
defaultActionOnPasteFromWord: Jodit.constants.INSERT_AS_TEXT
});
// Word content is automatically cleaned
Always Keep Word Formatting
const editor = Jodit.make('#editor', {
askBeforePasteFromWord: false,
defaultActionOnPasteFromWord: Jodit.constants.INSERT_AS_HTML
});
// Word formatting is preserved
Custom Word Paste Options
const editor = Jodit.make('#editor', {
askBeforePasteFromWord: true,
pasteFromWordActionList: [
{ value: Jodit.constants.INSERT_AS_HTML, text: 'Keep All Formatting' },
{ value: Jodit.constants.INSERT_AS_TEXT, text: 'Remove Word Markup' }
]
});
Disable Word Detection
const editor = Jodit.make('#editor', {
processPasteFromWord: false
});
// Word HTML treated as regular HTML
Use Default Paste Action
const editor = Jodit.make('#editor', {
askBeforePasteFromWord: false,
defaultActionOnPasteFromWord: null, // Use defaultActionOnPaste
defaultActionOnPaste: Jodit.constants.INSERT_AS_TEXT
});
With Beautify HTML Integration
const editor = Jodit.make('#editor', {
askBeforePasteFromWord: false,
defaultActionOnPasteFromWord: Jodit.constants.INSERT_AS_HTML,
events: {
beautifyHTML: (html) => {
// Custom HTML beautification
return html.replace(/\s+/g, ' ').trim();
}
}
});
Word Detection
The plugin uses isHtmlFromWord()
helper to detect Word/Excel HTML by checking for:
- Microsoft Office XML namespaces
- Word-specific class names (
MsoNormal
,MsoListParagraph
, etc.) - Office-specific meta tags
- Conditional comments
- VML markup
Processing Flow
- Paste Event: User pastes content
- Word Detection: Plugin checks if HTML is from Word/Excel
- Processing Decision:
- If
processPasteFromWord
isfalse
: Skip Word processing - If not Word HTML: Skip Word processing
- If
askBeforePasteFromWord
istrue
: Show dialog - If
askBeforePasteFromWord
isfalse
: UsedefaultActionOnPasteFromWord
ordefaultActionOnPaste
- If
- Content Processing: Apply selected insertion mode
- Insertion: Insert processed HTML into editor
Insert Modes
INSERT_AS_HTML (Keep formatting):
- Apply Styles: Calls
applyStyles()
to convert inline styles - Beautify: Fires
beautifyHTML
event for custom processing - Result: Preserves Word formatting with minimal cleanup
INSERT_AS_TEXT (Clean markup):
- Clean From Word: Calls
cleanFromWord()
to remove Word-specific markup - Result: Removes most Word cruft while keeping basic structure
INSERT_ONLY_TEXT (Plain text):
- Clean From Word: Calls
cleanFromWord()
first - Strip Tags: Calls
stripTags()
to remove all HTML - Result: Only plain text content
Word Cleaning Process
The cleanFromWord()
function:
- Removes XML namespaces and VML
- Strips Word-specific class names
- Removes conditional comments
- Cleans up mso-* CSS properties
- Normalizes whitespace
- Removes empty elements
- Converts Word list markup to standard HTML lists
- Strips proprietary Word attributes
Style Application
The applyStyles()
function:
- Converts inline Word styles to clean CSS
- Normalizes font names
- Simplifies color values
- Removes redundant properties
- Preserves meaningful formatting
Beautify HTML Event
When using INSERT_AS_HTML
mode, the plugin fires beautifyHTML
event:
- Allows custom HTML post-processing
- Receives cleaned HTML as parameter
- Should return processed HTML string
- Useful for additional formatting rules
beautifyHTML
Fired when inserting Word HTML with INSERT_AS_HTML
mode. Allows custom HTML beautification/processing before insertion.
Parameters:
html
(string): The HTML to beautify
Returns: Processed HTML string
Example:
editor.e.on('beautifyHTML', (html) => {
// Remove all empty paragraphs
return html.replace(/<p>\s*<\/p>/g, '');
});
:processHTML
Internal event watched by plugin via @watch(':processHTML')
decorator. Fired by paste plugin to process pasted HTML.
Parameters:
e
: Paste eventtext
: Pasted HTML stringtexts
: Pasted data object
Returns: true
if Word HTML was processed, false
otherwise
Edge Cases
-
Not Word HTML: If HTML doesn't match Word patterns, plugin does nothing (returns
false
) -
Disabled Processing: If
processPasteFromWord
isfalse
, all Word HTML treated as regular HTML -
Null Default Action: If
defaultActionOnPasteFromWord
isnull
, usesdefaultActionOnPaste
instead -
Empty Content: Empty Word HTML is inserted as empty content
-
Mixed Content: If paste contains Word HTML + other content, only Word HTML is specially processed
-
Excel HTML: Excel-generated HTML is also detected and processed
-
Dialog Cancellation: If user cancels Word paste dialog, content is not inserted
-
Nested Lists: Word lists are converted to standard HTML lists during cleaning
-
Inline Styles: Word inline styles are either preserved (INSERT_AS_HTML) or removed (INSERT_AS_TEXT)
-
Beautify Return: If
beautifyHTML
event returns non-string, original HTML is used
Notes
- Plugin requires 'paste' plugin (declared via
static requires = ['paste']
) - Plugin is class-based, extends
Plugin
base class - Word detection is reliable for content copied directly from Word/Excel
- The
@watch(':processHTML')
decorator hooks into paste plugin's processing pipeline - Cleaning process removes Microsoft Office-specific markup while preserving content
- The
INSERT_AS_HTML
mode applies cleaner styles viaapplyStyles()
- The
INSERT_AS_TEXT
mode usescleanFromWord()
for thorough cleanup - The
INSERT_ONLY_TEXT
mode strips all tags after cleaning - The plugin integrates seamlessly with the paste plugin's workflow
- Dialog message explains that content is from Word/Excel
- Default action list uses simpler labels than general paste actions
- The
isHtmlFromWord()
helper checks multiple Word/Excel indicators - Style preservation (INSERT_AS_HTML) still removes problematic Word markup
- Plugin does not process Word documents pasted as files (only HTML clipboard content)
- The
beautifyHTML
event allows custom post-processing without overriding core logic - Empty methods
afterInit()
andbeforeDestruct()
are placeholders for future use - Plugin returns
true
fromprocessWordHTML()
to prevent further paste processing - The paste plugin respects the plugin's return value and stops processing when
true
Typical Use Case
Users often copy content from Microsoft Word or Excel and paste it into the editor. This content contains:
- Excessive inline styles
- Microsoft Office XML namespaces
- Proprietary attributes
- VML graphics markup
- Conditional comments
- Empty spans and divs
The plugin:
- Detects this Word/Excel HTML automatically
- Prompts user for desired insertion method
- Cleans up the markup appropriately
- Inserts clean, standards-compliant HTML
This improves editor content quality and reduces bloat from Office applications.