Expand description
javascript contextual output encoders.
provides four encoding contexts:
for_javascript— universal encoder, safe in HTML attributes, script blocks, and standalone .js filesfor_javascript_attribute— optimized for HTML event attributes (e.g.,onclick="...")for_javascript_block— optimized for<script>blocksfor_javascript_source— optimized for standalone .js / JSON files
§security notes
- none of these encoders encode the grave accent (
`). never embed untrusted data directly inside ES2015+ template literals. instead, encode the data into a regular javascript string variable, then reference that variable from the template literal. - these encoders are for string literal contexts only. they cannot make arbitrary javascript expressions, variable names, or property accessors safe.
for_javascript_blockandfor_javascript_sourceuse backslash escapes for quotes (\",\') which are not safe in HTML attribute contexts.for_javascript_attributedoes not escape/and is not safe in<script>blocks where</script>could appear.
Functions§
- for_
javascript - encodes
inputfor safe embedding in a javascript string literal. - for_
javascript_ attribute - encodes
inputfor safe embedding in a javascript string literal inside an HTML event attribute (e.g.,onclick="..."). - for_
javascript_ block - encodes
inputfor safe embedding in a javascript string literal inside an HTML<script>block. - for_
javascript_ source - encodes
inputfor safe embedding in a javascript string literal in a standalone .js or JSON file. - write_
javascript - writes the javascript-encoded form of
inputtoout. - write_
javascript_ attribute - writes the javascript-attribute-encoded form of
inputtoout. - write_
javascript_ block - writes the javascript-block-encoded form of
inputtoout. - write_
javascript_ source - writes the javascript-source-encoded form of
inputtoout.