Forum

Methodology

Toolbox

Platform

Community

desktop
touch-pad
touch-phone

strings

This block provides helpers for manipulating string data.

Overview

Elements of the block

Element Usage Description
escape JS A set of methods for escaping XML and HTML control characters.

Properties and methods of the block elements

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.

Public block technologies

The block is implemented in:

  • vanilla.js

Description

Elements of the block

escape element

This element provides an object with a set of methods for escaping XML and HTML control characters.

Properties and methods of the object

xml method

Use 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 method

Use for escaping HTML control characters. It is a synonym of the xml method.

attr method

Use 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) + '"/>';
}

strings__escape

A set of string escaping functions

Object methods:

xml(str):String
description
Escape string to use in XML
parameters
str
String
html(str):String
description
Escape string to use in HTML
parameters
str
String
attr(str):String
description
Escape string to use in attributes
parameters
str
String