Class: BaseString

BaseString

Common base class for all messages that might require HTML escaping. Each of the three api hooks -- api.S, api.US, and api.msg -- produce BaseString objects, and are used just like console.log(). See the example below.


new BaseString(value)

Parameters:
Name Type Description
value string
Example
// Fail a test case with some known html formatting:
api.fail(api.S('<strong>Should be emphasized in the failure report</strong>'));

// Fail a test case with some user data that may need escaping:
const userData = '<script>alert("HACKED!");</script>';
api.fail(api.US('Your data', userData, 'should not have appeared here.'));

// All the api formatting functions assume the input needs escaping:
// --> api.msg() is an alias for api.US()
api.fail(api.msg('Your data', formatJavascript(userData), 'is bad!'));

// You can use backtick expansion -- with care! -- but you lose the raw() information.
// You must also mark it as Safe, to avoid double-escaping the substituted contents:
api.fail(api.S(`Here is a ${formatString('B&B')} element`));

// That's because toString() always yields the *escaped* string:
api.US('B&B').toString() === 'B&amp;B';

// You can compare the original version using raw():
api.US('B&B').raw() === 'B&B';

Methods


raw()

Returns:

The original string value, with no HTML escaping, useful for comparisons and validation.

Type
string

replace(expression, replacement)

Replaces the raw version of this string value using the expression and replacement provided.

Parameters:
Name Type Description
expression string | Regex
replacement string
Returns:
Type
BaseString

toJSON()

This method is an alias of toString()-- returns an HTML-safe representation of our string value.

Returns:
Type
string

toString()

Returns an HTML-safe representation of our string value.

Returns:
Type
string