Skip to main content

encode_script_to_vec

Function encode_script_to_vec 

Source
pub fn encode_script_to_vec<S: AsRef<str>>(
    text: S,
    output: &mut Vec<u8>,
) -> &[u8] ⓘ
Expand description

Write text used in the <script> element to a mutable Vec<u8> reference and return the encoded data slice.

This appends to output and returns only the new data slice.

The following substring is escaped:

  • </script> => <\/script>
  • <!-- => <\!--

</script at the end of the text is also escaped, because the text after it can end the element. Pass the whole element text in one call; separate calls do not share parsing state. This scans text without parsing JavaScript and may change its meaning. Existing escapes are kept, so encoding and decoding may not return the original text. This is not a JSON encoder; the \! escape is not valid JSON. For JSON, first serialize the data as JSON, then replace each literal < with \u003C. Use a quoted text encoder when inserting raw text into a JavaScript string.