Grammalecte  Check-in [51af80e942]

Overview
Comment:[core] [js] avoid avoid functions overriding in common objects prototypes
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | core
Files: files | file ages | folders
SHA3-256: 51af80e9429d234c73a79e11c9b9c0768f7870797abbec3f20bfefdce8c64343
User & Date: olr on 2017-04-28 15:00:22
Original Comment: [js] avoid avoid functions overriding in common objects prototypes
Other Links: manifest | tags
Context
2017-04-30
12:23
[fr] confusion son / sont check-in: c6acd80ef5 user: olr tags: fr, trunk
2017-04-28
15:00
[core] [js] avoid avoid functions overriding in common objects prototypes check-in: 51af80e942 user: olr tags: core, trunk
2017-04-27
11:24
[fr] faux positif: si nécessaire check-in: b32cdfdbe2 user: olr tags: fr, trunk
Changes

Modified gc_core/js/jsex_map.js from [975706110f] to [181b814a6e].

     1      1   
     2      2   // Map
     3      3   
     4         -Map.prototype._shallowCopy = function () {
     5         -    let oNewMap = new Map();
     6         -    for (let [key, val] of this.entries()) {
     7         -        oNewMap.set(key, val);
            4  +if (Map.prototype.__grammalecte__ === undefined) {
            5  +    Map.prototype._shallowCopy = function () {
            6  +        let oNewMap = new Map();
            7  +        for (let [key, val] of this.entries()) {
            8  +            oNewMap.set(key, val);
            9  +        }
           10  +        return oNewMap;
     8     11       }
     9         -    return oNewMap;
    10         -}
    11     12   
    12         -Map.prototype._get = function (key, defaultValue) {
    13         -    let res = this.get(key);
    14         -    if (res !== undefined) {
    15         -        return res;
           13  +    Map.prototype._get = function (key, defaultValue) {
           14  +        let res = this.get(key);
           15  +        if (res !== undefined) {
           16  +            return res;
           17  +        }
           18  +        return defaultValue;
    16     19       }
    17         -    return defaultValue;
    18         -}
    19     20   
    20         -Map.prototype._toString = function () {
    21         -    // Default .toString() gives nothing useful
    22         -    let sRes = "{ ";
    23         -    for (let [k, v] of this.entries()) {
    24         -        sRes += (typeof k === "string") ? '"' + k + '": ' : k.toString() + ": ";
    25         -        sRes += (typeof v === "string") ? '"' + v + '", ' : v.toString() + ", ";
           21  +    Map.prototype._toString = function () {
           22  +        // Default .toString() gives nothing useful
           23  +        let sRes = "{ ";
           24  +        for (let [k, v] of this.entries()) {
           25  +            sRes += (typeof k === "string") ? '"' + k + '": ' : k.toString() + ": ";
           26  +            sRes += (typeof v === "string") ? '"' + v + '", ' : v.toString() + ", ";
           27  +        }
           28  +        sRes = sRes.slice(0, -2) + " }"
           29  +        return sRes;
    26     30       }
    27         -    sRes = sRes.slice(0, -2) + " }"
    28         -    return sRes;
    29         -}
    30     31   
    31         -Map.prototype._update = function (dDict) {
    32         -    for (let [k, v] of dDict.entries()) {
    33         -        this.set(k, v);
    34         -    }
    35         -}
    36         -
    37         -Map.prototype._updateOnlyExistingKeys = function (dDict) {
    38         -    for (let [k, v] of dDict.entries()) {
    39         -        if (this.has(k)){
           32  +    Map.prototype._update = function (dDict) {
           33  +        for (let [k, v] of dDict.entries()) {
    40     34               this.set(k, v);
    41     35           }
    42     36       }
           37  +
           38  +    Map.prototype._updateOnlyExistingKeys = function (dDict) {
           39  +        for (let [k, v] of dDict.entries()) {
           40  +            if (this.has(k)){
           41  +                this.set(k, v);
           42  +            }
           43  +        }
           44  +    }
           45  +
           46  +    Map.prototype.__grammalecte__ = true;
    43     47   }

Modified gc_core/js/jsex_regex.js from [35ff67872c] to [de380ed036].

     1      1   
     2      2   // regex
     3      3   
     4         -RegExp.prototype._exec2 = function (sText, aGroupsPos, aNegLookBefore=null) {
     5         -    let m;
     6         -    while ((m = this.exec(sText)) !== null) {
     7         -        // we have to iterate over sText here too
     8         -        // because first match doesn’t imply it’s a valid match according to negative lookbefore assertions,
     9         -        // and even if first match is finally invalid, it doesn’t mean the following eligible matchs would be invalid too.
    10         -        if (aNegLookBefore !== null) {
    11         -            // check negative look before assertions
    12         -            if ( !aNegLookBefore.some(sRegEx  =>  (RegExp.leftContext.search(sRegEx) >= 0)) ) {
            4  +if (RegExp.prototype.__grammalecte__ === undefined) {
            5  +    RegExp.prototype._exec2 = function (sText, aGroupsPos, aNegLookBefore=null) {
            6  +        let m;
            7  +        while ((m = this.exec(sText)) !== null) {
            8  +            // we have to iterate over sText here too
            9  +            // because first match doesn’t imply it’s a valid match according to negative lookbefore assertions,
           10  +            // and even if first match is finally invalid, it doesn’t mean the following eligible matchs would be invalid too.
           11  +            if (aNegLookBefore !== null) {
           12  +                // check negative look before assertions
           13  +                if ( !aNegLookBefore.some(sRegEx  =>  (RegExp.leftContext.search(sRegEx) >= 0)) ) {
           14  +                    break;
           15  +                }
           16  +            } else {
    13     17                   break;
    14     18               }
    15         -        } else {
    16         -            break;
    17     19           }
    18         -    }
    19         -    if (m === null) {
    20         -        return null;
    21         -    }
           20  +        if (m === null) {
           21  +            return null;
           22  +        }
    22     23   
    23         -    let codePos;
    24         -    let iPos = 0;
    25         -    m.start = [m.index];
    26         -    m.end = [this.lastIndex];
    27         -    if (m.length > 1) {
    28         -        // there is subgroup(s)
    29         -        if (aGroupsPos !== null) {
    30         -            // aGroupsPos is defined
    31         -            for (let i = 1; i <= m.length-1; i++) {
    32         -                codePos = aGroupsPos[i-1];
    33         -                if (typeof codePos === "number") {
    34         -                    // position as a number
    35         -                    m.start.push(m.index + codePos);
    36         -                    m.end.push(m.index + codePos + m[i].length);
    37         -                } else if (codePos === "$") {
    38         -                    // at the end of the pattern
    39         -                    m.start.push(this.lastIndex - m[i].length);
    40         -                    m.end.push(this.lastIndex);
    41         -                } else if (codePos === "w") {
    42         -                    // word in the middle of the pattern
    43         -                    iPos = m[0].search("[ ~’,()«»“”]"+m[i]+"[ ,’~()«»“”]") + 1 + m.index
           24  +        let codePos;
           25  +        let iPos = 0;
           26  +        m.start = [m.index];
           27  +        m.end = [this.lastIndex];
           28  +        if (m.length > 1) {
           29  +            // there is subgroup(s)
           30  +            if (aGroupsPos !== null) {
           31  +                // aGroupsPos is defined
           32  +                for (let i = 1; i <= m.length-1; i++) {
           33  +                    codePos = aGroupsPos[i-1];
           34  +                    if (typeof codePos === "number") {
           35  +                        // position as a number
           36  +                        m.start.push(m.index + codePos);
           37  +                        m.end.push(m.index + codePos + m[i].length);
           38  +                    } else if (codePos === "$") {
           39  +                        // at the end of the pattern
           40  +                        m.start.push(this.lastIndex - m[i].length);
           41  +                        m.end.push(this.lastIndex);
           42  +                    } else if (codePos === "w") {
           43  +                        // word in the middle of the pattern
           44  +                        iPos = m[0].search("[ ~’,()«»“”]"+m[i]+"[ ,’~()«»“”]") + 1 + m.index
           45  +                        m.start.push(iPos);
           46  +                        m.end.push(iPos + m[i].length)
           47  +                    } else if (codePos === "*") {
           48  +                        // anywhere
           49  +                        iPos = m[0].indexOf(m[i]) + m.index;
           50  +                        m.start.push(iPos);
           51  +                        m.end.push(iPos + m[i].length)
           52  +                    } else if (codePos === "**") {
           53  +                        // anywhere after previous group
           54  +                        iPos = m[0].indexOf(m[i], m.end[i-1]-m.index) + m.index;
           55  +                        m.start.push(iPos);
           56  +                        m.end.push(iPos + m[i].length)
           57  +                    } else if (codePos.startsWith(">")) {
           58  +                        // >x:_
           59  +                        // todo: look in substring x
           60  +                        iPos = m[0].indexOf(m[i]) + m.index;
           61  +                        m.start.push(iPos);
           62  +                        m.end.push(iPos + m[i].length);
           63  +                    } else {
           64  +                        console.error("# Error: unknown positioning code in regex [" + this.source + "], for group[" + i.toString() +"], code: [" + codePos + "]");
           65  +                    }
           66  +                }
           67  +            } else {
           68  +                // no aGroupsPos
           69  +                for (let subm of m.slice(1)) {
           70  +                    iPos = m[0].indexOf(subm) + m.index;
    44     71                       m.start.push(iPos);
    45         -                    m.end.push(iPos + m[i].length)
    46         -                } else if (codePos === "*") {
    47         -                    // anywhere
    48         -                    iPos = m[0].indexOf(m[i]) + m.index;
    49         -                    m.start.push(iPos);
    50         -                    m.end.push(iPos + m[i].length)
    51         -                } else if (codePos === "**") {
    52         -                    // anywhere after previous group
    53         -                    iPos = m[0].indexOf(m[i], m.end[i-1]-m.index) + m.index;
    54         -                    m.start.push(iPos);
    55         -                    m.end.push(iPos + m[i].length)
    56         -                } else if (codePos.startsWith(">")) {
    57         -                    // >x:_
    58         -                    // todo: look in substring x
    59         -                    iPos = m[0].indexOf(m[i]) + m.index;
    60         -                    m.start.push(iPos);
    61         -                    m.end.push(iPos + m[i].length);
    62         -                } else {
    63         -                    console.error("# Error: unknown positioning code in regex [" + this.source + "], for group[" + i.toString() +"], code: [" + codePos + "]");
           72  +                    m.end.push(iPos + subm.length);
    64     73                   }
    65     74               }
    66         -        } else {
    67         -            // no aGroupsPos
    68         -            for (let subm of m.slice(1)) {
    69         -                iPos = m[0].indexOf(subm) + m.index;
    70         -                m.start.push(iPos);
    71         -                m.end.push(iPos + subm.length);
    72         -            }
    73     75           }
           76  +        return m;
    74     77       }
    75         -    return m;
           78  +
           79  +    RegExp.prototype.__grammalecte__ = true;
    76     80   }

Modified gc_core/js/jsex_string.js from [c771aa921e] to [adf06fbda5].

     1      1   
     2      2   // String
     3      3   
     4         -String.prototype._count = function (sSearch, bOverlapping) {
     5         -    // http://jsperf.com/string-ocurrence-split-vs-match/8
     6         -    if (sSearch.length <= 0) {
     7         -        return this.length + 1;
            4  +if (String.prototype.__grammalecte__ === undefined) {
            5  +    String.prototype._count = function (sSearch, bOverlapping) {
            6  +        // http://jsperf.com/string-ocurrence-split-vs-match/8
            7  +        if (sSearch.length <= 0) {
            8  +            return this.length + 1;
            9  +        }
           10  +        let nOccur = 0;
           11  +        let iPos = 0;
           12  +        let nStep = (bOverlapping) ? 1 : sSearch.length;
           13  +        while ((iPos = this.indexOf(sSearch, iPos)) >= 0) {
           14  +            nOccur++;
           15  +            iPos += nStep;
           16  +        }
           17  +        return nOccur;
           18  +    }
           19  +    String.prototype._isDigit = function () {
           20  +        return (this.search(/^[0-9⁰¹²³⁴⁵⁶⁷⁸⁹]+$/) !== -1);
           21  +    }
           22  +    String.prototype._isLowerCase = function () {
           23  +        return (this.search(/^[a-zà-öø-ÿ0-9-]+$/) !== -1);
           24  +    }
           25  +    String.prototype._isUpperCase = function () {
           26  +        return (this.search(/^[A-ZÀ-ÖØ-ߌ0-9-]+$/) !== -1);
           27  +    }
           28  +    String.prototype._isTitle = function () {
           29  +        return (this.search(/^[A-ZÀ-ÖØ-ߌ][a-zà-öø-ÿ'’-]+$/) !== -1);
           30  +    }
           31  +    String.prototype._toCapitalize = function () {
           32  +        return this.slice(0,1).toUpperCase() + this.slice(1).toLowerCase();
           33  +    }
           34  +    String.prototype._expand = function (oMatch) {
           35  +        let sNew = this;
           36  +        for (let i = 0; i < oMatch.length ; i++) {
           37  +            let z = new RegExp("\\\\"+parseInt(i), "g");
           38  +            sNew = sNew.replace(z, oMatch[i]);
           39  +        }
           40  +        return sNew;
           41  +    }
           42  +    String.prototype._trimRight = function (sChars) {
           43  +        let z = new RegExp("["+sChars+"]+$");
           44  +        return this.replace(z, "");
     8     45       }
     9         -    let nOccur = 0;
    10         -    let iPos = 0;
    11         -    let nStep = (bOverlapping) ? 1 : sSearch.length;
    12         -    while ((iPos = this.indexOf(sSearch, iPos)) >= 0) {
    13         -        nOccur++;
    14         -        iPos += nStep;
           46  +    String.prototype._trimLeft = function (sChars) {
           47  +        let z = new RegExp("^["+sChars+"]+");
           48  +        return this.replace(z, "");
    15     49       }
    16         -    return nOccur;
    17         -}
    18         -String.prototype._isDigit = function () {
    19         -    return (this.search(/^[0-9⁰¹²³⁴⁵⁶⁷⁸⁹]+$/) !== -1);
    20         -}
    21         -String.prototype._isLowerCase = function () {
    22         -    return (this.search(/^[a-zà-öø-ÿ0-9-]+$/) !== -1);
    23         -}
    24         -String.prototype._isUpperCase = function () {
    25         -    return (this.search(/^[A-ZÀ-ÖØ-ߌ0-9-]+$/) !== -1);
    26         -}
    27         -String.prototype._isTitle = function () {
    28         -    return (this.search(/^[A-ZÀ-ÖØ-ߌ][a-zà-öø-ÿ'’-]+$/) !== -1);
    29         -}
    30         -String.prototype._toCapitalize = function () {
    31         -    return this.slice(0,1).toUpperCase() + this.slice(1).toLowerCase();
    32         -}
    33         -String.prototype._expand = function (oMatch) {
    34         -    let sNew = this;
    35         -    for (let i = 0; i < oMatch.length ; i++) {
    36         -        let z = new RegExp("\\\\"+parseInt(i), "g");
    37         -        sNew = sNew.replace(z, oMatch[i]);
           50  +    String.prototype._trim = function (sChars) {
           51  +        let z1 = new RegExp("^["+sChars+"]+");
           52  +        let z2 = new RegExp("["+sChars+"]+$");
           53  +        return this.replace(z1, "").replace(z2, "");
    38     54       }
    39         -    return sNew;
    40         -}
    41         -String.prototype._trimRight = function (sChars) {
    42         -    let z = new RegExp("["+sChars+"]+$");
    43         -    return this.replace(z, "");
    44         -}
    45         -String.prototype._trimLeft = function (sChars) {
    46         -    let z = new RegExp("^["+sChars+"]+");
    47         -    return this.replace(z, "");
    48         -}
    49         -String.prototype._trim = function (sChars) {
    50         -    let z1 = new RegExp("^["+sChars+"]+");
    51         -    let z2 = new RegExp("["+sChars+"]+$");
    52         -    return this.replace(z1, "").replace(z2, "");
           55  +
           56  +    String.prototype.__grammalecte__ = true;
    53     57   }