This block provides helpers for manipulating string data.
Element | Usage | Description |
---|---|---|
escape | JS |
A set of methods for escaping XML and HTML control characters. |
Element | Name | Type or return value | Description |
---|---|---|---|
escape | xml(str {String} ) |
String |
Use for escaping XML control characters. |
html(str {String} ) |
String |
Use for escaping HTML control characters. | |
attr(str {String} ) |
String |
Use for escaping control characters in HTML and XML attributes. |
The block is implemented in:
vanilla.js
escape
elementThis element provides an object with a set of methods for escaping XML and HTML control characters.
xml
methodUse for escaping XML control characters. Processes the symbols &
, <
, >
.
Accepted arguments:
str {String}
– String to process. Required argument.Return value: String
. The string with escaped control characters.
html
methodUse for escaping HTML control characters. It is a synonym of the xml
method.
attr
methodUse for escaping control characters in HTML and XML attributes. Processes the control characters "
, \
, '
, &
, <
, >
.
Accepted arguments:
str {String}
– String to process. Required argument.Return value: String
. The string with escaped control characters.
For example, in the common.blocks/select
block in the bem-components
library, strings__escape
is used for escaping control characters in the value
property of an HTML element:
_createControlHTML : function(name, val) {
// Using string concatenation to not depend on template engines
return '<input ' +
'type="hidden" ' +
'name="' + name + '" ' +
'class="' + this.buildClass('control') + '" ' +
'value="' + escape.attr(typeof val === 'object'? JSON.stringify(val) : val) + '"/>';
}