You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ra...@apache.org on 2020/04/12 11:43:59 UTC

[cordova-common] branch master updated: fix(ConfigParser): ImageResources constructor (#142)

This is an automated email from the ASF dual-hosted git repository.

raphinesse pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-common.git


The following commit(s) were added to refs/heads/master by this push:
     new 0c03967  fix(ConfigParser): ImageResources constructor (#142)
0c03967 is described below

commit 0c03967ccf7826c884ee4e7826c621549da275dd
Author: Raphael von der GrĂ¼n <ra...@gmail.com>
AuthorDate: Sun Apr 12 13:43:53 2020 +0200

    fix(ConfigParser): ImageResources constructor (#142)
    
    The previous ImageResources constructor did not cover the case where a
    single integer argument is given to construct an empty array of the
    given length. This broke the `map` method since it uses that constructor
    variant.
    
    This commit fixes that situation by using a static factory method
    instead of overriding the native Array constructor.
---
 spec/ConfigParser/ConfigParser.spec.js |  6 ++++++
 src/ConfigParser/ConfigParser.js       | 21 +++++++++++++++------
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/spec/ConfigParser/ConfigParser.spec.js b/spec/ConfigParser/ConfigParser.spec.js
index 4649343..bc3382b 100644
--- a/spec/ConfigParser/ConfigParser.spec.js
+++ b/spec/ConfigParser/ConfigParser.spec.js
@@ -328,6 +328,12 @@ describe('config.xml parser', function () {
                 expect(cfg.getStaticResources('android', 'icon').getByDensity('mdpi')).toBeDefined();
                 expect(cfg.getStaticResources('android', 'icon').getByDensity('mdpi').src).toBe('logo-android.png');
             });
+
+            it('Test 042 : should return an array that has a working map method', function () {
+                expect(() =>
+                    cfg.getStaticResources(null, 'icon').map(x => x)
+                ).not.toThrow();
+            });
         });
 
         describe('file resources', function () {
diff --git a/src/ConfigParser/ConfigParser.js b/src/ConfigParser/ConfigParser.js
index 38893dc..795bc1b 100644
--- a/src/ConfigParser/ConfigParser.js
+++ b/src/ConfigParser/ConfigParser.js
@@ -201,7 +201,7 @@ class ConfigParser {
         const commonResources = this.doc.findall(resourceName)
             .map(elt => new ImageResource(normalizedAttrs(elt)));
 
-        return new ImageResources(...platformResources, ...commonResources);
+        return ImageResources.create(...platformResources, ...commonResources);
     }
 
     /**
@@ -580,13 +580,22 @@ class FileResource extends BaseResource {
 }
 
 class ImageResources extends Array {
-    constructor (...args) {
-        super(...args);
-
-        // The spread is necessary to avoid infinite recursion
-        this.defaultResource = [...this].filter(res =>
+    /**
+     * Creates an ImageResources instance with defaultResource property.
+     *
+     * It is easy to break native Array methods like `map` when carelessly
+     * overriding the array constructor, so it's safer to use this factory
+     * function for our needs instead.
+     *
+     * @param {...ImageResource} args - The entries of this array
+     * @return {ImageResources} An ImageResources instance with args as entries
+     */
+    static create (...args) {
+        const defaultResource = args.filter(res =>
             !res.width && !res.height && !res.density
         ).pop();
+
+        return Object.assign(new ImageResources(...args), { defaultResource });
     }
 
     /**


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org