( multiSizeDataStr, primaryWidth, primaryHeight, multiSizeValidation, isFluidPrimary = false )
| 39 | * @return {?Array<!Array<number>>} An array of dimensions. |
| 40 | */ |
| 41 | export function getMultiSizeDimensions( |
| 42 | multiSizeDataStr, |
| 43 | primaryWidth, |
| 44 | primaryHeight, |
| 45 | multiSizeValidation, |
| 46 | isFluidPrimary = false |
| 47 | ) { |
| 48 | const dimensions = []; |
| 49 | const arrayOfSizeStrs = multiSizeDataStr.split(','); |
| 50 | |
| 51 | for (let i = 0; i < arrayOfSizeStrs.length; i++) { |
| 52 | const sizeStr = arrayOfSizeStrs[i]; |
| 53 | if (sizeStr.toLowerCase() == 'fluid') { |
| 54 | // Fluid dummy sizes should be appended to the front of the request |
| 55 | // parameter, so they must be handled elsewhere. |
| 56 | continue; |
| 57 | } |
| 58 | const size = sizeStr.split('x'); |
| 59 | |
| 60 | // Make sure that each size is specified in the form WxH. |
| 61 | if (size.length != 2) { |
| 62 | user().error('AMP-AD', `Invalid multi-size data format '${sizeStr}'.`); |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | const width = Number(size[0]); |
| 67 | const height = Number(size[1]); |
| 68 | |
| 69 | // Make sure that both dimensions given are positive numbers. |
| 70 | if ( |
| 71 | !validateDimensions( |
| 72 | width, |
| 73 | height, |
| 74 | (w) => isNaN(w) || w <= 0, |
| 75 | (h) => isNaN(h) || h <= 0, |
| 76 | (badParams) => |
| 77 | badParams |
| 78 | .map( |
| 79 | (badParam) => |
| 80 | `Invalid ${badParam.dim} of ${badParam.val} ` + |
| 81 | 'given for secondary size.' |
| 82 | ) |
| 83 | .join(' ') |
| 84 | ) |
| 85 | ) { |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | // Check that secondary size is not larger than primary size. |
| 90 | if ( |
| 91 | !isFluidPrimary && |
| 92 | !validateDimensions( |
| 93 | width, |
| 94 | height, |
| 95 | (w) => w > primaryWidth, |
| 96 | (h) => h > primaryHeight, |
| 97 | (badParams) => |
| 98 | badParams |
no test coverage detected