Skip to content

Commit

Permalink
Change goog.fs.sliceBlob() to call slice() using the new spec.
Browse files Browse the repository at this point in the history
IE10 implements slice() according to the new spec.
FF and Chrome also have bugs to remove their respective vendor
prefixes:
https://bugzilla.mozilla.org/show_bug.cgi?id=725289
https://bugs.webkit.org/show_bug.cgi?id=78111

R=nweiz,azzie
DELTA=121  (104 added, 5 deleted, 12 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=5103


git-svn-id: http://closure-library.googlecode.com/svn/trunk@2023 0b95b8e8-c90f-11de-9d4f-f947ee5921c8
  • Loading branch information
andyehou@google.com committed Jul 2, 2012
1 parent 31d8b40 commit 42ba039
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 17 deletions.
2 changes: 1 addition & 1 deletion closure/goog/deps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 26 additions & 16 deletions closure/goog/fs/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ goog.require('goog.events');
goog.require('goog.fs.Error');
goog.require('goog.fs.FileReader');
goog.require('goog.fs.FileSystem');
goog.require('goog.userAgent');


/**
Expand Down Expand Up @@ -216,23 +217,32 @@ goog.fs.sliceBlob = function(blob, start, opt_end) {
// and http://hg.mozilla.org/mozilla-central/rev/dae833f4d934
return blob.mozSlice(start, opt_end);
} else if (blob.slice) {
// This is the original specification. Negative indices are not accepted,
// only range end is clamped and range end specification is obligatory.
// See http://www.w3.org/TR/2009/WD-FileAPI-20091117/, this will be
// replaced by http://dev.w3.org/2006/webapi/FileAPI/ in the future.
if (start < 0) {
start += blob.size;
// Old versions of Firefox and Chrome use the original specification.
// Negative indices are not accepted, only range end is clamped and
// range end specification is obligatory.
// See http://www.w3.org/TR/2009/WD-FileAPI-20091117/
if ((goog.userAgent.GECKO && !goog.userAgent.isVersion('13.0')) ||
(goog.userAgent.WEBKIT && !goog.userAgent.isVersion('537.1'))) {
if (start < 0) {
start += blob.size;
}
if (start < 0) {
start = 0;
}
if (opt_end < 0) {
opt_end += blob.size;
}
if (opt_end < start) {
opt_end = start;
}
return blob.slice(start, opt_end - start);
}
if (start < 0) {
start = 0;
}
if (opt_end < 0) {
opt_end += blob.size;
}
if (opt_end < start) {
opt_end = start;
}
return blob.slice(start, opt_end - start);
// IE and the latest versions of Firefox and Chrome use the new
// specification. Natively accepts negative indices, clamping to the blob
// range and range end is optional.
// See http://dev.w3.org/2006/webapi/FileAPI/
return blob.slice(start, opt_end);
}
return null;
};

88 changes: 88 additions & 0 deletions closure/goog/fs/fs_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,94 @@
}


function testSliceBlob() {
// A mock blob object whose slice returns the parameters it was called with.
var blob = {
'size': 10,
'slice': function(start, end) {
return [start, end];
}
};

// Simulate Firefox 13 that implements the new slice.
var tmpStubs = new goog.testing.PropertyReplacer();
tmpStubs.set(goog.userAgent, 'GECKO', true);
tmpStubs.set(goog.userAgent, 'WEBKIT', false);
tmpStubs.set(goog.userAgent, 'IE', false);
tmpStubs.set(goog.userAgent, 'VERSION', '13.0');
tmpStubs.set(goog.userAgent, 'isVersionCache_', {});

// Expect slice to be called with no change to parameters
assertArrayEquals([2, 10], goog.fs.sliceBlob(blob, 2));
assertArrayEquals([-2, 10], goog.fs.sliceBlob(blob, -2));
assertArrayEquals([3, 6], goog.fs.sliceBlob(blob, 3, 6));
assertArrayEquals([3, -6], goog.fs.sliceBlob(blob, 3, -6));

// Simulate IE 10 that implements the new slice.
var tmpStubs = new goog.testing.PropertyReplacer();
tmpStubs.set(goog.userAgent, 'GECKO', false);
tmpStubs.set(goog.userAgent, 'WEBKIT', false);
tmpStubs.set(goog.userAgent, 'IE', true);
tmpStubs.set(goog.userAgent, 'VERSION', '10.0');
tmpStubs.set(goog.userAgent, 'isVersionCache_', {});

// Expect slice to be called with no change to parameters
assertArrayEquals([2, 10], goog.fs.sliceBlob(blob, 2));
assertArrayEquals([-2, 10], goog.fs.sliceBlob(blob, -2));
assertArrayEquals([3, 6], goog.fs.sliceBlob(blob, 3, 6));
assertArrayEquals([3, -6], goog.fs.sliceBlob(blob, 3, -6));

// Simulate Firefox 4 that implements the old slice.
tmpStubs.set(goog.userAgent, 'GECKO', true);
tmpStubs.set(goog.userAgent, 'WEBKIT', false);
tmpStubs.set(goog.userAgent, 'IE', false);
tmpStubs.set(goog.userAgent, 'VERSION', '2.0');
tmpStubs.set(goog.userAgent, 'isVersionCache_', {});

// Expect slice to be called with transformed parameters.
assertArrayEquals([2, 8], goog.fs.sliceBlob(blob, 2));
assertArrayEquals([8, 2], goog.fs.sliceBlob(blob, -2));
assertArrayEquals([3, 3], goog.fs.sliceBlob(blob, 3, 6));
assertArrayEquals([3, 1], goog.fs.sliceBlob(blob, 3, -6));

// Simulate Firefox 5 that implements mozSlice (new spec).
delete blob.slice
blob.mozSlice = function(start, end) {
return ['moz', start, end];
}
tmpStubs.set(goog.userAgent, 'GECKO', true);
tmpStubs.set(goog.userAgent, 'WEBKIT', false);
tmpStubs.set(goog.userAgent, 'IE', false);
tmpStubs.set(goog.userAgent, 'VERSION', '5.0');
tmpStubs.set(goog.userAgent, 'isVersionCache_', {});

// Expect mozSlice to be called with no change to parameters.
assertArrayEquals(['moz', 2, 10], goog.fs.sliceBlob(blob, 2));
assertArrayEquals(['moz', -2, 10], goog.fs.sliceBlob(blob, -2));
assertArrayEquals(['moz', 3, 6], goog.fs.sliceBlob(blob, 3, 6));
assertArrayEquals(['moz', 3, -6], goog.fs.sliceBlob(blob, 3, -6));

// Simulate Chrome 20 that implements webkitSlice (new spec).
delete blob.mozSlice;
blob.webkitSlice = function(start, end) {
return ['webkit', start, end];
}
tmpStubs.set(goog.userAgent, 'GECKO', false);
tmpStubs.set(goog.userAgent, 'WEBKIT', true);
tmpStubs.set(goog.userAgent, 'IE', false);
tmpStubs.set(goog.userAgent, 'VERSION', '536.10');
tmpStubs.set(goog.userAgent, 'isVersionCache_', {});

// Expect webkitSlice to be called with no change to parameters.
assertArrayEquals(['webkit', 2, 10], goog.fs.sliceBlob(blob, 2));
assertArrayEquals(['webkit', -2, 10], goog.fs.sliceBlob(blob, -2));
assertArrayEquals(['webkit', 3, 6], goog.fs.sliceBlob(blob, 3, 6));
assertArrayEquals(['webkit', 3, -6], goog.fs.sliceBlob(blob, 3, -6));

tmpStubs.reset();
}


function loadTestDir() {
return deferredFs.branch().addCallback(function(fs) {
return fs.getRoot().getDirectory(
Expand Down

0 comments on commit 42ba039

Please sign in to comment.