Skip to content

Instantly share code, notes, and snippets.

@danschumann
Last active January 4, 2021 16:39
Show Gist options
  • Save danschumann/ae0b5bdcf2e1cd1f4b61 to your computer and use it in GitHub Desktop.
Save danschumann/ae0b5bdcf2e1cd1f4b61 to your computer and use it in GitHub Desktop.

Revisions

  1. danschumann revised this gist Aug 18, 2014. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions Step 1 -- Backend .js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,3 @@
    myObject = { ... }
    // This should cover all the bases for what kinds of text the object has.
    // We replace \ with \\ and " with \" so that it can be 1 string when it is printed in html
    // We are replacing because it needs to be passed to frontend through JSON.parse("...");
    // "{"mykey":"my\"val"}" -- pre-regex -- not parseable
    // "{\"mykey\":\"my\\\"val\"}" -- post-regex -- parseable
    // the first replace is for `my\"val`, since the second replace would cause that to break

    this.injectString = JSON.stringify( myObject ).replace(/\\/g, '\\\\').replace(/"/g, '\\\"')
  2. danschumann revised this gist Aug 18, 2014. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions Step 1 -- Backend .js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    myObject = { ... }
    // This should cover all the bases for what kinds of text the object has.
    // We replace \ with \\ and " with \" so that it can be 1 string when it is printed in html
    // We are replacing because it needs to be passed to frontend through JSON.parse("...");
    // "{"mykey":"my\"val"}" -- pre-regex -- not parseable
    // "{\"mykey\":\"my\\\"val\"}" -- post-regex -- parseable
    // the first replace is for `my\"val`, since the second replace would cause that to break

    this.injectString = JSON.stringify( myObject ).replace(/\\/g, '\\\\').replace(/"/g, '\\\"')
  3. danschumann revised this gist Aug 18, 2014. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
  4. danschumann revised this gist Aug 18, 2014. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  5. danschumann revised this gist Aug 18, 2014. 2 changed files with 1 addition and 1 deletion.
    File renamed without changes.
    2 changes: 1 addition & 1 deletion gistfile1.html → Step 2
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    We only worry about parsing now, since everything should have been properly escaped
    <script>
    window.groups = JSON.parse("<%- @injectString %>");
    window.myObjectFrontend = JSON.parse("<%- @injectString %>");
    </script>
  6. danschumann created this gist Aug 18, 2014.
    4 changes: 4 additions & 0 deletions gistfile1.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    We only worry about parsing now, since everything should have been properly escaped
    <script>
    window.groups = JSON.parse("<%- @injectString %>");
    </script>
    3 changes: 3 additions & 0 deletions gistfile2.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    myObject = { ... }
    // This should cover all the bases for what kinds of text the object has.
    this.injectString = JSON.stringify( myObject ).replace(/\\/g, '\\\\').replace(/"/g, '\\\"')