| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e7cf2ef commit 05d73ac
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,8 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | 4 | ArrayFrom, | |
| 5 | + MathMax, | ||
| 6 | + MathMin, | ||
| 5 | 7 | ObjectDefineProperty, | |
| 6 | 8 | ObjectSetPrototypeOf, | |
| 7 | 9 | PromiseResolve, | |
@@ -43,21 +45,21 @@ const { | |||
| 43 | 45 | codes: { | |
| 44 | 46 | ERR_INVALID_ARG_TYPE, | |
| 45 | 47 | ERR_BUFFER_TOO_LARGE, | |
| 46 | - ERR_OUT_OF_RANGE, | ||
| 47 | 48 | } | |
| 48 | 49 | } = require('internal/errors'); | |
| 49 | 50 | ||
| 50 | 51 | const { | |
| 51 | 52 | validateObject, | |
| 52 | 53 | validateString, | |
| 53 | - validateUint32, | ||
| 54 | 54 | isUint32, | |
| 55 | 55 | } = require('internal/validators'); | |
| 56 | 56 | ||
| 57 | 57 | const kHandle = Symbol('kHandle'); | |
| 58 | 58 | const kType = Symbol('kType'); | |
| 59 | 59 | const kLength = Symbol('kLength'); | |
| 60 | 60 | ||
| 61 | + const disallowedTypeCharacters = /[^\u{0020}-\u{007E}]/u; | ||
| 62 | + | ||
| 61 | 63 | let Buffer; | |
| 62 | 64 | ||
| 63 | 65 | function lazyBuffer() { | |
@@ -141,7 +143,7 @@ class Blob extends JSTransferable { | |||
| 141 | 143 | super(); | |
| 142 | 144 | this[kHandle] = createBlob(sources_, length); | |
| 143 | 145 | this[kLength] = length; | |
| 144 | - this[kType] = RegExpPrototypeTest(/[^\u{0020}-\u{007E}]/u, type) ? | ||
| 146 | + this[kType] = RegExpPrototypeTest(disallowedTypeCharacters, type) ? | ||
| 145 | 147 | '' : StringPrototypeToLowerCase(type); | |
| 146 | 148 | } | |
| 147 | 149 | ||
@@ -180,18 +182,32 @@ class Blob extends JSTransferable { | |||
| 180 | 182 | ||
| 181 | 183 | get size() { return this[kLength]; } | |
| 182 | 184 | ||
| 183 | - slice(start = 0, end = (this[kLength]), type = this[kType]) { | ||
| 184 | - validateUint32(start, 'start'); | ||
| 185 | - if (end < 0) end = this[kLength] + end; | ||
| 186 | - validateUint32(end, 'end'); | ||
| 187 | - validateString(type, 'type'); | ||
| 188 | - if (end < start) | ||
| 189 | - throw new ERR_OUT_OF_RANGE('end', 'greater than start', end); | ||
| 190 | - if (end > this[kLength]) | ||
| 191 | - throw new ERR_OUT_OF_RANGE('end', 'less than or equal to length', end); | ||
| 185 | + slice(start = 0, end = this[kLength], contentType = '') { | ||
| 186 | + if (start < 0) { | ||
| 187 | + start = MathMax(this[kLength] + start, 0); | ||
| 188 | + } else { | ||
| 189 | + start = MathMin(start, this[kLength]); | ||
| 190 | + } | ||
| 191 | + start |= 0; | ||
| 192 | + | ||
| 193 | + if (end < 0) { | ||
| 194 | + end = MathMax(this[kLength] + end, 0); | ||
| 195 | + } else { | ||
| 196 | + end = MathMin(end, this[kLength]); | ||
| 197 | + } | ||
| 198 | + end |= 0; | ||
| 199 | + | ||
| 200 | + contentType = `${contentType}`; | ||
| 201 | + if (RegExpPrototypeTest(disallowedTypeCharacters, contentType)) { | ||
| 202 | + contentType = ''; | ||
| 203 | + } else { | ||
| 204 | + contentType = StringPrototypeToLowerCase(contentType); | ||
| 205 | + } | ||
| 206 | + | ||
| 207 | + const span = MathMax(end - start, 0); | ||
| 208 | + | ||
| 192 | 209 | return new InternalBlob( | |
| 193 | - this[kHandle].slice(start, end), | ||
| 194 | - end - start, type); | ||
| 210 | + this[kHandle].slice(start, start + span), span, contentType); | ||
| 195 | 211 | } | |
| 196 | 212 | ||
| 197 | 213 | async arrayBuffer() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,53 @@ | |||
| 1 | + // META: title=Blob constructor | ||
| 2 | + // META: script=../support/Blob.js | ||
| 3 | + 'use strict'; | ||
| 4 | + | ||
| 5 | + var test_error = { | ||
| 6 | + name: "test", | ||
| 7 | + message: "test error", | ||
| 8 | + }; | ||
| 9 | + | ||
| 10 | + test(function() { | ||
| 11 | + var args = [ | ||
| 12 | + document.createElement("div"), | ||
| 13 | + window, | ||
| 14 | + ]; | ||
| 15 | + args.forEach(function(arg) { | ||
| 16 | + assert_throws_js(TypeError, function() { | ||
| 17 | + new Blob(arg); | ||
| 18 | + }, "Should throw for argument " + format_value(arg) + "."); | ||
| 19 | + }); | ||
| 20 | + }, "Passing platform objects for blobParts should throw a TypeError."); | ||
| 21 | + | ||
| 22 | + test(function() { | ||
| 23 | + var element = document.createElement("div"); | ||
| 24 | + element.appendChild(document.createElement("div")); | ||
| 25 | + element.appendChild(document.createElement("p")); | ||
| 26 | + var list = element.children; | ||
| 27 | + Object.defineProperty(list, "length", { | ||
| 28 | + get: function() { throw test_error; } | ||
| 29 | + }); | ||
| 30 | + assert_throws_exactly(test_error, function() { | ||
| 31 | + new Blob(list); | ||
| 32 | + }); | ||
| 33 | + }, "A platform object that supports indexed properties should be treated as a sequence for the blobParts argument (overwritten 'length'.)"); | ||
| 34 | + | ||
| 35 | + test_blob(function() { | ||
| 36 | + var select = document.createElement("select"); | ||
| 37 | + select.appendChild(document.createElement("option")); | ||
| 38 | + return new Blob(select); | ||
| 39 | + }, { | ||
| 40 | + expected: "[object HTMLOptionElement]", | ||
| 41 | + type: "", | ||
| 42 | + desc: "Passing an platform object that supports indexed properties as the blobParts array should work (select)." | ||
| 43 | + }); | ||
| 44 | + | ||
| 45 | + test_blob(function() { | ||
| 46 | + var elm = document.createElement("div"); | ||
| 47 | + elm.setAttribute("foo", "bar"); | ||
| 48 | + return new Blob(elm.attributes); | ||
| 49 | + }, { | ||
| 50 | + expected: "[object Attr]", | ||
| 51 | + type: "", | ||
| 52 | + desc: "Passing an platform object that supports indexed properties as the blobParts array should work (attributes)." | ||
| 53 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments