Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Chart.min.js has broken template function

Open mattbasta opened this issue 11 years ago • 4 comments

It seems the minification on Chart.min.js has caused the template method to break. Here's the original:

new Function("obj",
    "var p=[],print=function(){p.push.apply(p,arguments);};" +

    // Introduce the data as local variables using with(){}
    "with(obj){p.push('" +

    // Convert the template into pure JavaScript
    str
        .replace(/[\r\t\n]/g, " ")
        .split("<%").join("\t")
        .replace(/((^|%>)[^\t]*)'/g, "$1\r")
        .replace(/\t=(.*?)%>/g, "',$1,'")
        .split("\t").join("');")
        .split("%>").join("p.push('")
        .split("\r").join("\\'") +
    "');}return p.join('');"
);

and here's the minified version:

new Function("obj", "var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('" + t.replace(/[\r\t\n]/g, " ").split("<%").join("  ").replace(/((^|%>)[^\t]*)'/g, "$1\r").replace(/\t=(.*?)%>/g, "',$1,'").split(" ").join("');").split("%>").join("p.push('").split("\r").join("\\'") + "');}return p.join('');")

That's really hard to read, but the difference is in the second split/join:

// Original:
.split("<%").join("\t")
// and
.split("\t").join("');")

// Minified:
.split("<%").join("  ")
// and
.split(" ").join("');")

Somewhere along the way, the tab character that the minifier tried to replace the \t with got transformed into a few spaces. That means that invalid JS gets generated by the template function (the tab is required in order to build the templated string), causing the script to fail.

Update: This looks like an issue with UglifyJS

mattbasta avatar Jul 05 '15 15:07 mattbasta

Just want to confirm this bug as I have also run into it. Console outputs Uncaught SyntaxError: Unexpected string.

Following @mattbasta instructions of replacing two spaces with \t does resolve the issue. Compiling using grunt-contrib-uglify (^0.9.1) works properly with the expected output. Might be an issue with the gulp-uglify module version, if I have a chance I'll fork and play around with recompiling.

shampine avatar Jul 14 '15 22:07 shampine

There's actually a second \t very nearby that also needs to be updated. I'll update the issue when I get home.

On Tue, Jul 14, 2015, 3:48 PM Patrick Shampine [email protected] wrote:

Just want to confirm this bug as I have also run into it. Console outputs Uncaught SyntaxError: Unexpected string.

Following @mattbasta https://github.com/mattbasta instructions of replacing two spaces with \t does resolve the issue. Compiling using grunt-contrib-uglify works properly with the expected output. Might be an issue with the gulp-uglify module version, if I have a chance I'll fork and play around with recompiling.

— Reply to this email directly or view it on GitHub https://github.com/nnnick/Chart.js/issues/1280#issuecomment-121415209.

mattbasta avatar Jul 15 '15 01:07 mattbasta

Here's a compile with that snippit via gulp-uglify v1.2.0 (current is 0.2.1), it still appears to be incorrect as the join is empty.

gulp-uglify 1.2.0

new Function("obj",
  "var p=[],print=function(){p.push.apply(p,arguments);};

  with(obj){p.push('"+
    t
      .replace(/[\r\t\n]/g," ")
      .split("<%").join(" ")
      .replace(/((^|%>)[^\t]*)'/g,"$1\r")
      .replace(/\t=(.*?)%>/g,"',$1,'")
      .split(" ").join("');")
      .split("%>").join("p.push('")
      .split("\r").join("\\'")+
    "');}return p.join('');"
)

shampine avatar Jul 20 '15 17:07 shampine

Hello, today I am closing all issues that are only affecting version 1 of Chart.js as WONTFIX.

If this issue does affect version 2 as well, I apologize for the error. Please create a test case against Chart.js 2 using on of the below websites and we will be happy to reopen the issue and update its classification:

  • http://jsbin.com/
  • http://jsfiddle.net/
  • http://codepen.io/pen/
  • Premade template: http://codepen.io/pen?template=JXVYzq

fulldecent avatar Jun 27 '16 17:06 fulldecent