@@ -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,9 +213,9 @@ export class CharacterCount extends ConfigurableComponent {
212213 */
213214 updateCount ( text ) {
214215 const { $textarea } = this
215- let { countType } = this . config
216+ let { countFunction , countType } = this . config
216217
217- const countFunction =
218+ countFunction ?? =
218219 countType === 'words'
219220 ? CharacterCount . countFunctions . words
220221 : CharacterCount . countFunctions . characters
@@ -518,6 +519,7 @@ export class CharacterCount extends ConfigurableComponent {
518519 maxlength : { type : 'number' } ,
519520 threshold : { type : 'number' } ,
520521 countType : { type : 'string' } ,
522+ countFunction : { type : 'function' } ,
521523 textareaDescriptionClass : { type : 'string' } ,
522524 visibleCountMessageClass : { type : 'string' } ,
523525 screenReaderCountMessageClass : { type : 'string' } ,
@@ -564,6 +566,7 @@ export function initCharacterCounts(options) {
564566 * @property {number } [threshold=0] - The percentage value of the limit at which point the count message is displayed.
565567 * If this attribute is set, the count message will be hidden by default.
566568 * @property {'characters' | 'graphemes' | 'words' } countType - The count type (`"characters"`, `"graphemes"` or `"words"`) used to count the text.
569+ * @property {(text: string, segmenter?: Intl.Segmenter | null) => number } [countFunction] - Custom character or word counting function.
567570 * @property {string } textareaDescriptionClass - Textarea description class
568571 * @property {string } visibleCountMessageClass - Visible count message class
569572 * @property {string } screenReaderCountMessageClass - Screen reader count message class
0 commit comments