Cscott has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/78087


Change subject: Use Object.create(null) when making maps.
......................................................................

Use Object.create(null) when making maps.

Object.create(null) creates an object which doesn't inherit from Object. Thus:
'hasOwnProperty' in {} === true
but
'hasOwnProperty' in Object.create(null) === false

This makes it safer to use as a map, and with the 'in' operator.

Change-Id: I6c4947579d59f2b550a8bb2fe178de63bd554827
---
M js/lib/mediawiki.Util.js
1 file changed, 16 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Parsoid 
refs/changes/87/78087/1

diff --git a/js/lib/mediawiki.Util.js b/js/lib/mediawiki.Util.js
index 6adc9cb..6cbfad7 100644
--- a/js/lib/mediawiki.Util.js
+++ b/js/lib/mediawiki.Util.js
@@ -675,9 +675,9 @@
        KVtoHash: function ( kvs ) {
                if ( ! kvs ) {
                        console.warn( "Invalid kvs!: " + JSON.stringify( kvs, 
null, 2 ) );
-                       return {};
+                       return Object.create(null);
                }
-               var res = {};
+               var res = Object.create(null);
                for ( var i = 0, l = kvs.length; i < l; i++ ) {
                        var kv = kvs[i],
                                key = this.tokensToString( kv.k ).trim();
@@ -893,14 +893,27 @@
         }
     },
 
+       // Return a hash mapping all of the given elements of `a` to `true`.
+       // The result hash is created with `Object.create(null)`, so it doesn't
+       // inherit extra properties (like `hasOwnProperty`) from `Object`.
        arrayToHash: function(a) {
-               var h = {};
+               var h = Object.create(null); // No inherited methods
                for (var i = 0, n = a.length; i < n; i++) {
                        h[a[i]] = true;
                }
                return h;
        },
 
+       // Return a hash with the same own properties as `h`.
+       // The result hash is created with `Object.create(null)`, so it doesn't
+       // inherit extra properties (like `hasOwnProperty`) from `Object`, nor
+       // will it include any inherited properties from `h`.
+       safeHash: function(h) {
+               var r = Object.create(null);
+               Object.keys(h).forEach(function(k) { r[k] = h[k]; });
+               return r;
+       },
+
        extractExtBody: function(extName, extTagSrc) {
                var re = "<" + extName + "[^>]*/?>([\\s\\S]*)";
                return extTagSrc.replace(new RegExp(re, "mi"), function() {

-- 
To view, visit https://gerrit.wikimedia.org/r/78087
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6c4947579d59f2b550a8bb2fe178de63bd554827
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Parsoid
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to