# all files / lighthouse-core/lib/ manifest-parser.js

78.85% Statements 82/104
60.98% Branches 25/41
93.75% Functions 15/16
79.21% Lines 80/101

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300

    1×

    1×

    1×

    1×
    399×
    399×

    399×
    136×

    263×
    6×

    263×

    399×

    1×

    77×

    1×
    84×

    84×
    72×

    12×
    12×

    12×

    1×
    42×

    1×
    42×

    1×

    42×

    1×
    42×

    42×

    42×

    1×
    42×

    42×

    42×

    1×

    35×
    35×

    35×

    35×

    35×
    35×
    30×
    43×
    30×

    35×

    1×
    42×
    42×

    42×
    19×

    23×

    35×

    23×

    1×

    1×
    42×
    42×

    42×
    42×

    1×
    42×
    42×
    42×

    42×

    42×

    42×

    42×

    1×
    42×

    1×
    42×

    1×
    43×

    43×
    43×

    1×

    42×

    42×

    1×
     /**
     * @license
     * Copyright 2016 Google Inc. All Rights Reserved.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    'use strict';

    const validateColor = require('./web-inspector').Color.parse;

    const ALLOWED_DISPLAY_VALUES = [
      'fullscreen',
      'standalone',
      'minimal-ui',
      'browser'
    ];

    const ALLOWED_ORIENTATION_VALUES = [
      'any',
      'natural',
      'landscape',
      'portrait',
      'portrait-primary',
      'portrait-secondary',
      'landscape-primary',
      'landscape-secondary'
    ];

    function parseString(raw, trim) {
      let value;
      let debugString;

      if (typeof raw === 'string') {
        value = trim ? raw.trim() : raw;
      } else {
        if (raw !== undefined) {
          debugString = 'ERROR: expected a string.';
        }
        value = undefined;
      }

      return {
        raw,
        value,
        debugString
      };
    }

    function parseURL(raw) {
      // TODO: resolve url using baseURL
      // var baseURL = args.baseURL;
      // new URL(parseString(raw).value, baseURL);
      return parseString(raw, true);
    }

    function parseColor(raw) {
      const color = parseString(raw);

      // Finished if color missing or not a string.
      if (color.value === undefined) {
        return color;
      }

      // Use DevTools's color parser to check CSS3 Color parsing.
      const validatedColor = validateColor(color.raw);
      Iif (!validatedColor) {
        color.value = undefined;
        color.debugString = 'ERROR: color parsing failed.';
      }

      return color;
    }

    function parseName(jsonInput) {
      return parseString(jsonInput.name, true);
    }

    function parseShortName(jsonInput) {
      return parseString(jsonInput.short_name, true);
    }

    function parseStartUrl(jsonInput) {
      // TODO: parse url using manifest_url as a base (missing).
      // start_url must be same-origin as Document of the top-level browsing context.
      return parseURL(jsonInput.start_url);
    }

    function parseDisplay(jsonInput) {
      let display = parseString(jsonInput.display, true);

      Iif (display.value && ALLOWED_DISPLAY_VALUES.indexOf(display.value.toLowerCase()) === -1) {
        display.value = undefined;
        display.debugString = 'ERROR: \'display\' has an invalid value, will be ignored.';
      }

      return display;
    }

    function parseOrientation(jsonInput) {
      let orientation = parseString(jsonInput.orientation, true);

      Iif (orientation.value &&
          ALLOWED_ORIENTATION_VALUES.indexOf(orientation.value.toLowerCase()) === -1) {
        orientation.value = undefined;
        orientation.debugString = 'ERROR: \'orientation\' has an invalid value, will be ignored.';
      }

      return orientation;
    }

    function parseIcon(raw) {
      // TODO: pass manifest url as base.
      let src = parseURL(raw.src);
      let type = parseString(raw.type, true);

      let density = {
        raw: raw.density,
        value: 1,
        debugString: undefined
      };
      Iif (density.raw !== undefined) {
        density.value = parseFloat(density.raw);
        if (isNaN(density.value) || !isFinite(density.value) || density.value <= 0) {
          density.value = 1;
          density.debugString = 'ERROR: icon density cannot be NaN, +∞, or less than or equal to +0.';
        }
      }

      let sizes = parseString(raw.sizes);
      if (sizes.value !== undefined) {
        let set = new Set();
        sizes.value.trim().split(/\s+/).forEach(size => set.add(size.toLowerCase()));
        sizes.value = set.size > 0 ? Array.from(set) : undefined;
      }

      return {
        raw,
        value: {
          src,
          type,
          density,
          sizes
        },
        debugString: undefined
      };
    }

    function parseIcons(jsonInput) {
      const raw = jsonInput.icons;
      let value;

      if (raw === undefined) {
        return {
          raw,
          value,
          debugString: undefined
        };
      }

      Iif (!Array.isArray(raw)) {
        return {
          raw,
          value,
          debugString: 'ERROR: \'icons\' expected to be an array but is not.'
        };
      }

      // TODO(bckenny): spec says to skip icons missing `src`. Warn instead?
      value = raw.filter(icon => !!icon.src).map(parseIcon);

      return {
        raw,
        value,
        debugString: undefined
      };
    }

    function parseApplication(raw) {
      let platform = parseString(raw.platform, true);
      let id = parseString(raw.id, true);
      // TODO: pass manfiest url as base.
      let url = parseURL(raw.url);

      return {
        raw,
        value: {
          platform,
          id,
          url
        },
        debugString: undefined
      };
    }

    function parseRelatedApplications(jsonInput) {
      const raw = jsonInput.related_applications;
      let value;

      Eif (raw === undefined) {
        return {
          raw,
          value,
          debugString: undefined
        };
      }

      if (!Array.isArray(raw)) {
        return {
          raw,
          value,
          debugString: 'ERROR: \'related_applications\' expected to be an array but is not.'
        };
      }

      // TODO(bckenny): spec says to skip apps missing `platform`. Warn instead?
      value = raw.filter(application => !!application.platform).map(parseApplication);

      return {
        raw,
        value,
        debugString: undefined
      };
    }

    function parsePreferRelatedApplications(jsonInput) {
      const raw = jsonInput.prefer_related_applications;
      let value;
      let debugString;

      Iif (typeof raw === 'boolean') {
        value = raw;
      } else {
        Iif (raw !== undefined) {
          debugString = 'ERROR: \'prefer_related_applications\' expected to be a boolean.';
        }
        value = undefined;
      }

      return {
        raw,
        value,
        debugString
      };
    }

    function parseThemeColor(jsonInput) {
      return parseColor(jsonInput.theme_color);
    }

    function parseBackgroundColor(jsonInput) {
      return parseColor(jsonInput.background_color);
    }

    function parse(string) {
      let jsonInput;

      try {
        jsonInput = JSON.parse(string);
      } catch (e) {
        return {
          raw: string,
          value: undefined,
          debugString: 'ERROR: file isn\'t valid JSON: ' + e
        };
      }

      /* eslint-disable camelcase */
      let manifest = {
        name: parseName(jsonInput),
        short_name: parseShortName(jsonInput),
        start_url: parseStartUrl(jsonInput),
        display: parseDisplay(jsonInput),
        orientation: parseOrientation(jsonInput),
        icons: parseIcons(jsonInput),
        related_applications: parseRelatedApplications(jsonInput),
        prefer_related_applications: parsePreferRelatedApplications(jsonInput),
        theme_color: parseThemeColor(jsonInput),
        background_color: parseBackgroundColor(jsonInput)
      };
      /* eslint-enable camelcase */

      return {
        raw: string,
        value: manifest,
        debugString: undefined
      };
    }

    module.exports = parse;