epiphany/node_modules/parse-srcset/tests/unit/ps.js
2023-12-09 22:19:03 -08:00

345 lines
21 KiB
JavaScript

define(function (require) {
var tdd = require('intern!tdd');
// Not really Chai but a Chai-compatible "assert" library for old IE.
var assert = require('intern/chai!assert');
// parse-srcset is an AMD module.
var parseSrcset = require('../../src/parse-srcset');
var he = require('tests/he');
tdd.suite('Parse Srcset', function() {
// Adapted from the W3C srcset conformance checker at:
// http://w3c-test.org/html/semantics/embedded-content/the-img-element/srcset/parse-a-srcset-attribute.html
var w3Ctests = [
{
groupName: "Splitting Loop",
testArray: [
{srcset: '', expect: '', desc: 'empty string'},
{srcset: ',' , expect: '', desc: 'single comma'},
{srcset: ',,,', expect: '', desc: 'three commas'},
{srcset: '		data:,a		1x		', expect: 'data:,a', desc: 'tabs'},
{srcset: '

data:,a

1x

', expect: 'data:,a', desc: 'line feeds'},
{srcset: 'data:,a1x', expect: 'data:,a1x', desc: 'line tab'},
{srcset: 'data:,a1x', expect: 'data:,a', desc: 'form feed U+000C'},
{srcset: '

data:,a

1x

', expect: 'data:,a', desc: 'carriage return U+000D'},
{srcset: 'data:,a1x', expect: 'data:,a1x', desc: 'shift out U+000E'},
{srcset: 'data:,a1x', expect: 'data:,a1x', desc: 'shift in U+000F' },
{srcset: 'data:,a1x', expect: 'data:,a1x', desc: 'data link escape U+0010' },
{srcset: 'data:,a', expect: 'data:,a', desc:'plain url'},
{srcset: 'data:,a ', expect: 'data:,a', desc:'trailing space'},
{srcset: 'data:,a ,', expect: 'data:,a', desc:'trailing space and comma'},
{srcset: 'data:,a,', expect: 'data:,a', desc:'trailing comma'},
{srcset: 'data:,a, ', expect: 'data:,a', desc:'trailing comma and space'},
{srcset: 'data:,a,,,', expect: 'data:,a', desc:'trailing three commas'},
{srcset: 'data:,a,, , ', expect: 'data:,a', desc:'trailing two commas space comma space'},
{srcset: ' data:,a', expect: 'data:,a', desc:'leading space'},
{srcset: ',,,data:,a', expect: 'data:,a', desc:'leading three commas'},
{srcset: ' , ,,data:,a', expect: 'data:,a', desc:'leading space comma space comma comma'},
{srcset: ' data:,a', expect: ' data:,a', desc:'leading non-breaking space'},
{srcset: 'data:,a ', expect: 'data:,a ', desc:'trailing non-breaking space'}
]
},
{
groupName: "Descriptor Tokenizer",
testArray: [
{srcset: 'data:,a 1x', expect: 'data:,a', desc: 'plain url with descriptor'},
{srcset: 'data:,a 1x ', expect: 'data:,a', desc: 'trailing space'},
{srcset: 'data:,a 1x,', expect: 'data:,a', desc: 'trailing comma'},
{srcset: 'data:,a ( , data:,b 1x, ), data:,c', expect: 'data:,c', desc: 'irregular parens 1'},
{srcset: 'data:,a ((( , data:,b 1x, ), data:,c', expect: 'data:,c', desc: 'irregular parens 2'},
{srcset: 'data:,a [ , data:,b 1x, ], data:,c', expect: 'data:,b', desc: 'brackets'},
{srcset: 'data:,a { , data:,b 1x, }, data:,c', expect: 'data:,b', desc: 'braces'},
{srcset: 'data:,a " , data:,b 1x, ", data:,c', expect: 'data:,b', desc: 'double quotes'},
{srcset: 'data:,a \\,data:;\\,b, data:,c', expect: 'data:;\\,b', desc: 'backslashes'},
{srcset: 'data:,a, data:,b (', expect: 'data:,a', desc: 'trailing unclosed paren'},
{srcset: 'data:,a, data:,b ( ', expect: 'data:,a', desc: 'unclosed paren space'},
{srcset: 'data:,a, data:,b (,', expect: 'data:,a', desc: 'unclosed paren comma'},
{srcset: 'data:,a, data:,b (x', expect: 'data:,a', desc: 'unclosed paren x'},
{srcset: 'data:,a, data:,b ()', expect: 'data:,a', desc: 'parens, no descriptor'},
{srcset: 'data:,a (, data:,b', expect: '', desc: 'unclosed paren'},
{srcset: 'data:,a /*, data:,b, data:,c */', expect: 'data:,b', desc: 'block comments'},
{srcset: 'data:,a //, data:,b', expect: 'data:,b', desc: 'double slash like a comment'}
]
},
{ groupName: "Descriptor Parser",
testArray : [
{srcset: 'data:,a foo', expect: '', desc: 'trailing foo'},
{srcset: 'data:,a foo foo', expect: '', desc: 'trailing foo foo'},
{srcset: 'data:,a foo 1x', expect: '', desc: 'trailing foo 1x'},
{srcset: 'data:,a foo 1x foo', expect: '', desc: 'trailing 1x foo'},
{srcset: 'data:,a foo 1w', expect: '', desc: 'trailing foo 1w'},
{srcset: 'data:,a foo 1w foo', expect: '', desc: 'trailing foo 1w foo'},
{srcset: 'data:,a 1x 1x', expect: '', desc: 'two density descriptors'},
{srcset: 'data:,a 1w 1w', expect: '', desc: 'two width descriptors'},
{srcset: 'data:,a 1h 1h', expect: '', desc: 'two height descriptors'},
{srcset: 'data:,a 1w 1x', expect: '', desc: 'width then density'},
{srcset: 'data:,a 1x 1w', expect: '', desc: 'density then width'},
{srcset: 'data:,a 1w 1h', expect: 'data:,a', desc: 'width then height'},
{srcset: 'data:,a 1h 1w', expect: 'data:,a', desc: 'height then width'},
{srcset: 'data:,a 1h 1x', expect: '', desc: 'height then density'},
{srcset: 'data:,a 1h 1w 1x', expect: '', desc: 'height then width then density'},
{srcset: 'data:,a 1x 1w 1h', expect: '', desc: 'density then width then height'},
{srcset: 'data:,a 1h foo', expect: '', desc: 'trailing 1h foo'},
{srcset: 'data:,a foo 1h', expect: '', desc: 'trailing foo 1h'},
{srcset: 'data:,a 0w', expect: '', desc: 'zero width'},
{srcset: 'data:,a -1w', expect: '', desc: 'negative width'},
{srcset: 'data:,a 1w -1w', expect: '', desc: 'positive width, negative width'},
{srcset: 'data:,a 1.0w', expect: '', desc: 'floating point width'},
{srcset: 'data:,a 1w 1.0w', expect: '', desc: 'integer width, floating point width'},
{srcset: 'data:,a 1e0w', expect: '', desc: 'exponent width'},
{srcset: 'data:,a 1w 1e0w', expect: '', desc: 'integer width, exponent width'},
{srcset: 'data:,a 1www', expect: '', desc: '1www'},
{srcset: 'data:,a 1w 1www', expect: '', desc: '1w 1www'},
{srcset: 'data:,a 1w +1w', expect: '', desc: '1w +1w'},
{srcset: 'data:,a 1W', expect: '', desc: 'capital W descriptor'},
{srcset: 'data:,a 1w 1W', expect: '', desc: 'lowercase w, capital W descriptors'},
{srcset: 'data:,a Infinityw', expect: '', desc: 'Infinityw'},
{srcset: 'data:,a 1w Infinityw', expect: '', desc: '1w Infinityw'},
{srcset: 'data:,a NaNw', expect: '', desc: 'Nanw'},
{srcset: 'data:,a 1w NaNw', expect: '', desc: '1w Nanw'},
{srcset: 'data:,a 0x1w', expect: '', desc: 'ox1w'},
{srcset: 'data:,a 1w', expect: '', desc: 'trailing U+0001'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+00A0'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+1680'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+2000'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+2001'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+2002'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+2003'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+2004'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+2005'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+2006'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+2007'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+2008'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+2009'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+200A'},
{srcset: 'data:,a 1‌w', expect: '', desc: 'trailing U+200C'},
{srcset: 'data:,a 1‍w', expect: '', desc: 'trailing U+200D'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+202F'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+205F'},
{srcset: 'data:,a 1 w', expect: '', desc: 'trailing U+3000'},
{srcset: 'data:,a 1w', expect: '', desc: 'trailing U+FEFF'},
{srcset: 'data:,a 1w' , expect: '', desc: 'leading U+0001'},
// {srcset: 'data:,a  1w' , expect: '', desc: 'leading U+00A0 width'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+1680'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+2000'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+2001'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+2002'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+2003'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+2004'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+2005'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+2006'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+2007'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+2008'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+2009'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+200A'},
{srcset: 'data:,a ‌1w', expect: '', desc: 'leading U+200C'},
{srcset: 'data:,a ‍1w', expect: '', desc: 'leading U+200D'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+202F'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+205F'},
{srcset: 'data:,a  1w', expect: '', desc: 'leading U+3000'},
{srcset: 'data:,a 1w', expect: '', desc: 'leading U+FEFF'},
{srcset: 'data:,a 0x', expect: 'data:,a', desc: 'zero density'},
{srcset: 'data:,a -0x' , expect: 'data:,a', desc: 'negative zero density'},
{srcset: 'data:,a 1x -0x', expect: '', desc: '1x -0x'},
{srcset: 'data:,a -1x', expect: '', desc: '-1x'},
{srcset: 'data:,a 1x -1x', expect: '', desc: '1x -1x'},
{srcset: 'data:,a 1e0x', expect: 'data:,a', desc: '1e0x'},
{srcset: 'data:,a 1E0x', expect: 'data:,a', desc: '1E0x'},
{srcset: 'data:,a 1e-1x', expect: 'data:,a', desc: '1e-1x'},
{srcset: 'data:,a 1.5e1x', expect: 'data:,a', desc: '1.5e1x'},
{srcset: 'data:,a -x', expect: '', desc: 'negative density with no digits'},
{srcset: 'data:,a .x', expect: '', desc: 'decimal density with no digits'},
{srcset: 'data:,a -.x', expect: '', desc: '-.x'},
{srcset: 'data:,a 1.x', expect: '', desc: '1.x'},
{srcset: 'data:,a .5x', expect: 'data:,a', desc: 'floating point density descriptor'},
{srcset: 'data:,a .5e1x', expect: 'data:,a', desc: '.5e1x'},
{srcset: 'data:,a 1x 1.5e1x', expect: '', desc: '1x 1.5e1x'},
{srcset: 'data:,a 1x 1e1.5x', expect: '', desc: '1x 1e1.5x'},
{srcset: 'data:,a 1.0x', expect: 'data:,a', desc: '1.0x'},
{srcset: 'data:,a 1x 1.0x', expect: '', desc: '1x 1.0x'},
{srcset: 'data:,a +1x', expect: '', desc: 'no plus sign allowed on floating point number'},
{srcset: 'data:,a 1X', expect: '', desc: 'Capital X descriptor'},
{srcset: 'data:,a Infinityx', expect: '', desc: 'Infinityx'},
{srcset: 'data:,a NaNx', expect: '', desc: 'NaNx'},
{srcset: 'data:,a 0x1x', expect: '', desc: '0X1x'},
{srcset: 'data:,a 0X1x', expect: '', desc: '1x'},
{srcset: 'data:,a 1x', expect: '', desc: 'trailing U+0001'},
{srcset: 'data:,a 1 x' , expect: '', desc: 'trailing U+00A0 density'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+1680'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+2000'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+2001'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+2002'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+2003'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+2004'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+2005'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+2006'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+2007'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+2008'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+2009'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+200A'},
{srcset: 'data:,a 1‌x', expect: '', desc: 'trailing U+200C'},
{srcset: 'data:,a 1‍x', expect: '', desc: 'trailing U+200D'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+202F'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+205F'},
{srcset: 'data:,a 1 x', expect: '', desc: 'trailing U+3000'},
{srcset: 'data:,a 1x', expect: '', desc: 'trailing U+FEFF'},
{srcset: 'data:,a 1x' , expect: '', desc: 'leading U+0001'},
{srcset: 'data:,a  1x' , expect: '', desc: 'leading U+00A0 density'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+1680'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+2000'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+2001'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+2002'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+2003'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+2004'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+2005'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+2006'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+2007'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+2008'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+2009'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+200A'},
{srcset: 'data:,a ‌1x', expect: '', desc: 'leading U+200C'},
{srcset: 'data:,a ‍1x', expect: '', desc: 'leading U+200D'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+202F'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+205F'},
{srcset: 'data:,a  1x', expect: '', desc: 'leading U+3000'},
{srcset: 'data:,a 1x', expect: '', desc: 'leading U+FEFF'},
{srcset: 'data:,a 1w 0h', expect: '', desc: '1w 0h'},
{srcset: 'data:,a 1w -1h', expect: '', desc: '1w -1h'},
{srcset: 'data:,a 1w 1.0h', expect: '', desc: '1w 1.0h'},
{srcset: 'data:,a 1w 1e0h', expect: '', desc: '1w 1e0h'},
{srcset: 'data:,a 1w 1hhh', expect: '', desc: '1w 1hhh'},
{srcset: 'data:,a 1w 1H', expect: '', desc: '1w 1H'},
{srcset: 'data:,a 1w Infinityh', expect: '', desc: '1w Infinityh'},
{srcset: 'data:,a 1w NaNh', expect: '', desc: '1w NaNh'},
{srcset: 'data:,a 0x1h', expect: '', desc: '0x1h'},
{srcset: 'data:,a 0X1h', expect: '', desc: '0X1h'},
{srcset: 'data:,a 1w 1h', expect: '', desc: 'trailing U+0001'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+00A0'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+1680'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+2000'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+2001'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+2002'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+2003'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+2004'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+2005'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+2006'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+2007'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+2008'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+2009'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+200A'},
{srcset: 'data:,a 1w 1‌h', expect: '', desc: 'trailing U+200C'},
{srcset: 'data:,a 1w 1‍h', expect: '', desc: 'trailing U+200D'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+202F'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+205F'},
{srcset: 'data:,a 1w 1 h', expect: '', desc: 'trailing U+3000'},
{srcset: 'data:,a 1w 1h', expect: '', desc: 'trailing U+FEFF'},
{srcset: 'data:,a 1w 1h', expect: '', desc: 'leading U+0001'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+00A0'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+1680'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+2000'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+2001'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+2002'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+2003'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+2004'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+2005'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+2006'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+2007'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+2008'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+2009'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+200A'},
{srcset: 'data:,a 1w ‌1h', expect: '', desc: 'leading U+200C'},
{srcset: 'data:,a 1w ‍1h', expect: '', desc: 'leading U+200D'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+202F'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+205F'},
{srcset: 'data:,a 1w  1h', expect: '', desc: 'leading U+3000'},
{srcset: 'data:,a 1w 1h', expect: '', desc: 'leading U+FEFF'}
]
}
];
// HTML Entities are much easier to troubleshoot in console.
he.encode.options.useNamedReferences = true;
function runTest(test) {
var origAttr = test.srcset;
var attrDecoded = he.decode(origAttr);
var parsed = parseSrcset(attrDecoded);
var firstCandidate = parsed[0];
var url = "";
var encodedUrl = "";
if (firstCandidate) {
url = firstCandidate.url;
}
// Must re-encode url prior to comparison with expected string.
if (url) {
encodedUrl = he.encode(url);
}
console.log("");
console.log(test.desc);
console.log("origAttr: '" + origAttr + "'");
console.log("attrDecoded: '" + attrDecoded + "'");
console.log("parsed: ", parsed);
console.log("url: '" + url + "'");
console.log("encodedUrl: '" + encodedUrl + "'");
tdd.test( test.desc , function() {
assert.strictEqual(encodedUrl, test.expect, "passed" );
});
}
function runTestGroup(testGroup) {
var j;
var testArray = testGroup.testArray;
// Group Tests
tdd.suite(testGroup.groupName, function() {
for (j = 0; j < testArray.length; j++) {
runTest(testArray[j]);
}
});
}
var i;
var w3CtestsLength = w3Ctests.length;
for (i = 0; i < w3CtestsLength; i++) {
runTestGroup(w3Ctests[i]);
}
// tdd.test('First Test', function () {
// var parsed = parseSrcset('data:,a 1x');
// var url = parsed[0].url;
//
// console.log("parsed: ", parsed);
// console.log("url: ", url);
//
// assert.strictEqual(parsed, parsed, 'should be');
//
// // assert.strictEqual(url, 'data:,a', 'should be');
// });
// assert.strictEqual(parseSrcset('data:,a 1x')[0], 'data:,a', 'plain url with descriptor');
// tdd.test('Second Test', function () {
// assert.strictEqual(5, 5, '5 is itself, right?');
// });
});
});