• Nick Carter's avatar
    Introduce JsReplace, for getting values from C++ to ExecuteScript scripts · 6a2260c4
    Nick Carter authored
    JsReplace(script, args...) works by replacing $1, $2, $3, etc in |script| with
    JS literal values constructed from |args|, similar to
    base::ReplaceStringPlaceholders, except that args can be a variadic mix of
    strings, ints, base::Values, GURLs and more.
    
    Unlike StringPrintf or manual concatenation, this utility will properly
    escape string content, even if it contains slashes or quotation marks.
    
    Example 1:
    
      GURL page_url("http://example.com");
      EXPECT_TRUE(ExecuteScript(
          shell(), JsReplace("window.open($1, '_blank');", page_url)));
    
    $1 is replaced with a double-quoted JS string literal: "http://example.com".
    Note that quotes around $1 are not required.
    
    Example 2:
    
      bool forced_reload = true;
      EXPECT_TRUE(ExecuteScript(
          shell(), JsReplace("window.location.reload($1);", forced_reload)));
    
    This becomes "window.location.reload(true);" -- because bool values are
    supported by base::Value. Numbers, lists, and dicts also work.
    
    Change-Id: I11041b6d2db299949266290b940e2d855522f2ea
    Reviewed-on: https://chromium-review.googlesource.com/1125098
    Commit-Queue: Nick Carter <nick@chromium.org>
    Reviewed-by: default avatarAlbert J. Wong <ajwong@chromium.org>
    Cr-Commit-Position: refs/heads/master@{#577262}
    6a2260c4
zoom_browsertest.cc 24.5 KB