ngx-editor
Version:
WYSIWYG Editor for Angular Applications
50 lines (49 loc) • 4.32 kB
JavaScript
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/**
* @record
*/
function IMaxLengthValidatorOptions() { }
/** @type {?|undefined} */
IMaxLengthValidatorOptions.prototype.excludeLineBreaks;
/** @type {?|undefined} */
IMaxLengthValidatorOptions.prototype.concatWhiteSpaces;
/** @type {?|undefined} */
IMaxLengthValidatorOptions.prototype.excludeWhiteSpaces;
/**
* @param {?} maxlength
* @param {?=} options
* @return {?}
*/
export function MaxLengthValidator(maxlength, options) {
return (control) => {
/** @type {?} */
const parsedDocument = new DOMParser().parseFromString(control.value, 'text/html');
/** @type {?} */
let innerText = parsedDocument.body.innerText || '';
// replace all linebreaks
if (options.excludeLineBreaks) {
innerText = innerText.replace(/(\r\n\t|\n|\r\t)/gm, '');
}
// concat multiple whitespaces into a single whitespace
if (options.concatWhiteSpaces) {
innerText = innerText.replace(/(\s\s+)/gm, ' ');
}
// remove all whitespaces
if (options.excludeWhiteSpaces) {
innerText = innerText.replace(/(\s)/gm, '');
}
if (innerText.length > maxlength) {
return {
ngxEditor: {
allowedLength: maxlength,
textLength: innerText.length
}
};
}
return null;
};
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWF4bGVuZ3RoLXZhbGlkYXRvci5qcyIsInNvdXJjZVJvb3QiOiJuZzovL25neC1lZGl0b3IvIiwic291cmNlcyI6WyJhcHAvbmd4LWVkaXRvci92YWxpZGF0b3JzL21heGxlbmd0aC12YWxpZGF0b3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7OztBQVFBLE1BQU0sNkJBQTZCLFNBQWlCLEVBQUUsT0FBb0M7SUFDeEYsT0FBTyxDQUFDLE9BQXdCLEVBQWlDLEVBQUU7O1FBQ2pFLE1BQU0sY0FBYyxHQUFHLElBQUksU0FB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