/**
 * Copyright 2015-2016, Google, Inc.
 * 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 url = require('url');

/**
 * Class representing a Web App Manifest
 */
class Manifest {
  constructor(manifestUrl, jsonManifest) {
    this.url = manifestUrl;
    this.raw = JSON.stringify(jsonManifest);
    this.name = jsonManifest.name;
    this.shortName = jsonManifest.short_name;
    this.description = jsonManifest.description;
    this.startUrl = jsonManifest.start_url;
    this.backgroundColor = jsonManifest.background_color;
    this.icons = jsonManifest.icons;
    this.scope = jsonManifest.scope;
  }

  getBestIcon() {
    function getIconSize(icon) {
      if (!icon.sizes) {
        return 0;
      }
      return parseInt(icon.sizes.substring(0, icon.sizes.indexOf('x')), 10);
    }

    if (!this.icons) {
      return null;
    }

    let bestIcon;
    let bestIconSize;

    this.icons.forEach(icon => {
      if (!bestIcon) {
        bestIcon = icon;
        bestIconSize = getIconSize(icon);
        return;
      }

      const iconSize = getIconSize(icon);
      if (iconSize > bestIconSize) {
        bestIcon = icon;
        bestIconSize = iconSize;
      }
    });

    return bestIcon;
  }

  /** Gets the Url for the largest icon in the Manifest */
  getBestIconUrl() {
    let bestIcon = this.getBestIcon();
    if (!bestIcon || !bestIcon.src) {
      return '';
    }
    return url.resolve(this.url, bestIcon.src);
  }
}

module.exports = Manifest;

@@ -1413,34 +1413,36 @@
     
-this.icons.forEach(
+for (let icon of this.
 icon
- =%3E
+s)
  %7B%0A 
@@ -1534,24 +1534,8 @@
 n);%0A
-        return;%0A
     
@@ -1688,16 +1688,158 @@
   %7D%0A
+%0A
     
-%7D);%0A
+  // We can return 128 and 144 even if there are bigger ones.%0A      if (iconSize == 128 %7C%7C iconSize == 144) %7B%0A        return icon;%0A      %7D%0A    %7D;
 %0A   

Fix for #512