Skip to main content

Module javascript

Module javascript 

Source
Expand description

javascript contextual output encoders.

provides five encoding contexts:

§security notes

  • the string literal encoders (for_javascript, for_javascript_attribute, for_javascript_block, for_javascript_source) do not encode the grave accent (`). do not use them to embed data inside template literals — use for_js_template instead.
  • these encoders are for string/template literal contexts only. they cannot make arbitrary javascript expressions, variable names, or property accessors safe.
  • for_javascript_block and for_javascript_source use backslash escapes for quotes (\", \') which are not safe in HTML attribute contexts.
  • for_javascript_attribute does not escape / and is not safe in <script> blocks where </script> could appear.

Functions§

for_javascript
encodes input for safe embedding in a javascript string literal.
for_javascript_attribute
encodes input for safe embedding in a javascript string literal inside an HTML event attribute (e.g., onclick="...").
for_javascript_block
encodes input for safe embedding in a javascript string literal inside an HTML <script> block.
for_javascript_source
encodes input for safe embedding in a javascript string literal in a standalone .js or JSON file.
for_js_template
encodes input for safe embedding inside an ES6 template literal (`...`).
write_javascript
writes the javascript-encoded form of input to out.
write_javascript_attribute
writes the javascript-attribute-encoded form of input to out.
write_javascript_block
writes the javascript-block-encoded form of input to out.
write_javascript_source
writes the javascript-source-encoded form of input to out.
write_js_template
writes the template-literal-encoded form of input to out.