Skip to main content

js

Function js 

Source
pub fn js<T: Serialize>(value: &T) -> Result<String, Error>
Expand description

@js($expr)’s JS-safe (not HTML-escaped) serialization - drops a Serialize value into inline JavaScript as a JSON.parse('...') call, mirroring Laravel’s Illuminate\Support\Js::from() two-layer mechanism faithfully:

  1. JSON-encode the value, then hex-escape <, >, &, ' in the result (JSON_HEX_TAG/JSON_HEX_AMP/JSON_HEX_APOS equivalents) - this is what makes it safe to embed inside an HTML attribute or a <script> block without e.g. a "</script>" string value breaking out of context.
  2. Wrap that hex-escaped string as JSON.parse('...'), JSON-re-encoding it (which escapes backslashes/newlines/quotes - including the \u... sequences step 1 just introduced, and the delimiting ' itself) and stripping the outer "..." quotes that encoding pass adds. What’s left is exactly the text that belongs between the single quotes of JSON.parse('...'), so the whole token can be spliced directly into surrounding source with no caller-added quoting.