Limit Plugin
- Features
- limitWords
- limitChars
- limitHTML
- Word Limit Only
- Character Limit Only
- Both Limits
- With Event Handling
- Prevent Paste Beyond Limit
- Limit Checking
- Word Counting
- Character Counting
- Strict vs Non-Strict Comparison
- Command Key Handling
- Paste Prevention
- Change Prevention
- limit.limit
- denyWords.limit
- denyChars.limit
- denyPaste.limit
- Edge Cases
- Notes
- Classes
Provides character and word count limits for the Jodit editor. This plugin restricts the amount of content users can enter by setting maximum word or character counts, with options to include or exclude HTML markup in the count.
Features
- Limit word count
- Limit character count
- Optional HTML markup inclusion in limits
- Prevents keyboard input beyond limits
- Prevents paste operations beyond limits
- Restores content on paste if limit exceeded
- Events for limit detection and denial
- Respects command keys (Ctrl/Cmd shortcuts)
- Automatic change prevention
limitWords
Type: false | number
Default: false
Limits the number of words in the editor. Words are separated by spaces. When limitHTML
is false
, HTML tags and attributes are not counted. When limitHTML
is true
, tags and attributes are included in word count.
Example:
const editor = Jodit.make('#editor', {
limitWords: 100
});
limitChars
Type: false | number
Default: false
Limits the number of characters in the editor. When limitHTML
is false
, only text content characters are counted (spaces and special characters are filtered out, HTML tags and attributes are ignored). When limitHTML
is true
, the entire HTML string is counted.
Example:
const editor = Jodit.make('#editor', {
limitChars: 500
});
limitHTML
Type: false
Default: false
Controls what content is counted against limits. When false
, only text content is counted (HTML tags and attributes excluded). When true
, the full HTML markup is included in counts.
Important: The type is false
, not boolean
. This option can only be set to false
(default) and cannot be changed to true
based on the type definition.
Example:
const editor = Jodit.make('#editor', {
limitChars: 500,
limitHTML: false // Count only text content
});
Word Limit Only
const editor = Jodit.make('#editor', {
limitWords: 100
});
Character Limit Only
const editor = Jodit.make('#editor', {
limitChars: 400
});
Both Limits
const editor = Jodit.make('#editor', {
limitWords: 100,
limitChars: 500
});
With Event Handling
const editor = Jodit.make('#editor', {
limitWords: 100
});
editor.e.on('limit.limit', editor.async.debounce(() => {
editor.message.error('Limit reached!');
}, 300));
editor.e.on('denyWords.limit', () => {
console.log('Word limit exceeded');
});
editor.e.on('denyChars.limit', () => {
console.log('Character limit exceeded');
});
editor.e.on('denyPaste.limit', () => {
editor.message.info('Cannot paste: would exceed limit');
});
Prevent Paste Beyond Limit
const editor = Jodit.make('#editor', {
limitChars: 200
});
// Paste operation will be reverted if it exceeds limit
editor.e.on('denyPaste.limit', () => {
alert('Pasted content exceeds character limit');
});
Limit Checking
The plugin monitors several events:
beforePaste
: Saves snapshot before paste operationkeydown/keyup/beforeEnter
: Checks limits before allowing key presschange
: Validates limits on any content change, reverts if exceededafterPaste
: Validates pasted content, restores snapshot if limit exceeded
Word Counting
Words are counted by:
- Content Selection: Uses
jodit.text
(whenlimitHTML
isfalse
) orjodit.value
(whenlimitHTML
istrue
) - Cleaning: Removes invisible spaces using
INVISIBLE_SPACE_REG_EXP
- Splitting: Splits text by
SPACE_REG_EXP
(spaces and whitespace) - Filtering: Filters out empty strings
- Counting: Returns array length as word count
Character Counting
Characters are counted by:
- Word Array: Gets words using word counting logic
- Joining: Joins words without separators (
words.join('')
) - Length: Returns string length as character count
This means spaces between words are NOT counted as characters (they are removed during word splitting).
Strict vs Non-Strict Comparison
The plugin uses two comparison modes:
- Non-Strict (for keypress/beforeEnter):
count >= limit
- prevents reaching the limit - Strict (for paste/change):
count > limit
- allows reaching exact limit
Command Key Handling
The plugin does NOT prevent input when:
- Command keys are pressed (arrows, function keys, etc.)
- Ctrl or Cmd modifier is held (allows shortcuts like Ctrl+C, Ctrl+V)
- Meta key is held
This allows navigation and clipboard operations even when limit is reached.
Paste Prevention
When paste would exceed limit:
- Snapshot: Content snapshot is taken before paste (
beforePaste
event) - Paste Occurs: Content is pasted normally
- Validation:
afterPaste
checks if limit exceeded - Restore: If limit exceeded, snapshot is restored (paste is reverted)
- Event:
denyPaste.limit
event is fired
Change Prevention
On content changes:
- New Value: Gets new editor content
- Old Value: Receives previous content from event parameter
- Check: Validates if new content exceeds limit (strict mode)
- Revert: If exceeded, sets
jodit.value = oldValue
limit.limit
Fired whenever any limit is reached.
Example:
editor.e.on('limit.limit', () => {
console.log('A limit was reached');
});
denyWords.limit
Fired when word limit is exceeded.
Example:
editor.e.on('denyWords.limit', () => {
editor.message.error('Word limit exceeded!');
});
denyChars.limit
Fired when character limit is exceeded.
Example:
editor.e.on('denyChars.limit', () => {
editor.message.error('Character limit exceeded!');
});
denyPaste.limit
Fired when paste operation would exceed limits.
Example:
editor.e.on('denyPaste.limit', () => {
editor.message.info('Cannot paste: would exceed limits');
});
Edge Cases
-
Command Keys: Navigation keys, function keys, and Ctrl/Cmd shortcuts are always allowed even at limit
-
Exact Limit: Users can reach exact limit (e.g., type 100 words when limit is 100) but cannot exceed it
-
Paste Behavior: Paste is allowed to complete, then reverted if it exceeds limit (not prevented before paste)
-
Both Limits: When both
limitWords
andlimitChars
are set, BOTH must be satisfied -
HTML Counting: When
limitHTML
isfalse
, HTML tags like<strong>
,<p>
, etc. are not counted -
Space Handling: Spaces between words are NOT counted as characters (removed during processing)
-
Invisible Spaces: Special invisible space characters are filtered out before counting
-
Change Event: External changes (via API calls) are also validated and can be reverted
-
False Values: Setting limit to
false
disables that limit (not the same as setting to 0)
Notes
- Both word and character limits can be active simultaneously
- Command keys and keyboard shortcuts (Ctrl/Cmd) are never blocked
- Character count excludes spaces between words
- Word count filters out empty strings from splitting
- Paste operations are allowed to complete before being validated and potentially reverted
- The plugin uses history snapshots for paste reversal
- Event handlers should use debouncing to avoid excessive notifications
- The
limitHTML
type isfalse
only, notboolean
- verify the actual implementation - Plugin uses namespaced events (
.limit
) for clean removal - All event listeners are cleaned up on editor destruction