@@ -61,6 +61,7 @@ export class CharacterCount extends ConfigurableComponent {
6161 const {
6262 i18n,
6363 maxlength,
64+ countFunction,
6465 countType,
6566 screenReaderCountMessageClass,
6667 textareaDescriptionClass,
@@ -72,9 +73,9 @@ export class CharacterCount extends ConfigurableComponent {
7273 locale : closestAttributeValue ( this . $root , 'lang' )
7374 } )
7475
75- if ( 'Segmenter' in Intl && countType === 'graphemes' ) {
76+ if ( 'Segmenter' in Intl && ( countType === 'graphemes' || ! ! countFunction ) ) {
7677 this . segmenter = new Intl . Segmenter ( this . i18n . locale , {
77- granularity : 'grapheme'
78+ granularity : countType === 'words' ? 'word' : 'grapheme'
7879 } )
7980 }
8081
@@ -212,10 +213,11 @@ export class CharacterCount extends ConfigurableComponent {
212213 */
213214 updateCount ( text ) {
214215 const { $textarea, countFunctions } = this
215- const { countType } = this . config
216+ let { countType, countFunction } = this . config
216217
217218 text ??= $textarea . value
218- this . length = countFunctions [ countType ] . call ( this , text )
219+ countFunction ??= countFunctions [ countType ]
220+ this . length = countFunction . call ( this , text )
219221 }
220222
221223 /**
@@ -519,6 +521,7 @@ export class CharacterCount extends ConfigurableComponent {
519521 maxlength : { type : 'number' } ,
520522 threshold : { type : 'number' } ,
521523 countType : { type : 'string' } ,
524+ countFunction : { type : 'function' } ,
522525 textareaDescriptionClass : { type : 'string' } ,
523526 visibleCountMessageClass : { type : 'string' } ,
524527 screenReaderCountMessageClass : { type : 'string' } ,
@@ -565,6 +568,7 @@ export function initCharacterCounts(options) {
565568 * @property {number } [threshold=0] - The percentage value of the limit at which point the count message is displayed.
566569 * If this attribute is set, the count message will be hidden by default.
567570 * @property {'characters' | 'graphemes' | 'words' } countType - The count type (`"characters"`, `"graphemes"` or `"words"`) used to count the text.
571+ * @property {(this: CharacterCount, text: string) => number } [countFunction] - Custom character or word counting function.
568572 * @property {string } textareaDescriptionClass - Textarea description class
569573 * @property {string } visibleCountMessageClass - Visible count message class
570574 * @property {string } screenReaderCountMessageClass - Screen reader count message class
0 commit comments