Common API
Contents
This API provides helper functions.
Usage
Add the following to your build.gradle
file:
dependencies {
include "com.enonic.xp:lib-common:${xpVersion}"
}
Add the import
statement to your controller:
import commonLib from '/lib/xp/common';
You are now ready to use the API.
Functions
sanitize
Transforms a text string so that it can be safely used in cases where the range of accepted characters is restricted.
Some usage examples for sanitizing are: content or node name, principal name, URL or a file system path etc.
The following changes will be applied to the input text:
-
Convert characters to lowercase (according to the rules of the default locale)
-
Replace punctuation symbols and blank spaces with the hyphen character ('-')
-
Remove some unsafe and invisible Unicode characters
-
Strip duplicated hyphen characters
-
Remove diacritic characters
-
Map letters to the English alphabet (ASCII encoding)
Parameters
Name | Kind | Details |
---|---|---|
text |
string |
Text to sanitize |
Returns
string : The sanitized text
Example
import {sanitize} from '/lib/xp/common';
const sanitizedText = sanitize("Piña CØLADÆ <script>alert('hi!');</script>");
"pina-coladae-script-alerthi-script"