ソースを参照

first attempt to generate masaktemplate from activetests

Robin Herbots 12 年 前
コミット
ce7ef92406
1 ファイル変更51 行追加61 行削除
  1. 51 61
      js/jquery.inputmask.js

+ 51 - 61
js/jquery.inputmask.js

@@ -34,43 +34,6 @@
             var ms = [];
             var ms = [];
             var genmasks = []; //used to keep track of the masks that where processed, to avoid duplicates
             var genmasks = []; //used to keep track of the masks that where processed, to avoid duplicates
             var maskTokens = [];
             var maskTokens = [];
-            function getMaskTemplate(mask) {
-                if (opts.numericInput) {
-                    mask = mask.split('').reverse().join('');
-                }
-                var escaped = false, outCount = 0, greedy = opts.greedy, repeat = opts.repeat;
-                if (repeat == "*") greedy = false;
-                //if (greedy == true && opts.placeholder == "") opts.placeholder = " ";
-                if (mask.length == 1 && greedy == false && repeat != 0) {
-                    opts.placeholder = "";
-                } //hide placeholder with single non-greedy mask
-                var singleMask = $.map(mask.split(""), function (element, index) {
-                    var outElem = [];
-                    if (element == opts.escapeChar) {
-                        escaped = true;
-                    } else if ((element != opts.optionalmarker.start && element != opts.optionalmarker.end) || escaped) {
-                        var maskdef = opts.definitions[element];
-                        if (maskdef && !escaped) {
-                            for (var i = 0; i < maskdef.cardinality; i++) {
-                                outElem.push(opts.placeholder.charAt((outCount + i) % opts.placeholder.length));
-                            }
-                        } else {
-                            outElem.push(element);
-                            escaped = false;
-                        }
-                        outCount += outElem.length;
-                        return outElem;
-                    }
-                });
-
-                //allocate repetitions
-                var repeatedMask = singleMask.slice();
-                for (var i = 1; i < repeat && greedy; i++) {
-                    repeatedMask = repeatedMask.concat(singleMask.slice());
-                }
-
-                return { "mask": repeatedMask, "repeat": repeat, "greedy": greedy };
-            }
             function analyseMask(mask) {
             function analyseMask(mask) {
                 if (opts.numericInput) {
                 if (opts.numericInput) {
                     mask = mask.split('').reverse().join('');
                     mask = mask.split('').reverse().join('');
@@ -232,39 +195,37 @@
             }
             }
             function generateMask(maskPrefix, maskPart, metadata) {
             function generateMask(maskPrefix, maskPart, metadata) {
                 var maskParts = splitFirstOptionalEndPart(maskPart);
                 var maskParts = splitFirstOptionalEndPart(maskPart);
-                var newMask, maskTemplate;
+                var newMask;
 
 
                 var masks = splitFirstOptionalStartPart(maskParts[0]);
                 var masks = splitFirstOptionalStartPart(maskParts[0]);
                 if (masks.length > 1) {
                 if (masks.length > 1) {
                     newMask = maskPrefix + masks[0] + markOptional(masks[1]) + (maskParts.length > 1 ? maskParts[1] : "");
                     newMask = maskPrefix + masks[0] + markOptional(masks[1]) + (maskParts.length > 1 ? maskParts[1] : "");
                     if ($.inArray(newMask, genmasks) == -1 && newMask != "") {
                     if ($.inArray(newMask, genmasks) == -1 && newMask != "") {
                         genmasks.push(newMask);
                         genmasks.push(newMask);
-                        maskTemplate = getMaskTemplate(newMask);
                         ms.push({
                         ms.push({
                             "mask": newMask,
                             "mask": newMask,
                             "maskToken": analyseMask(newMask),
                             "maskToken": analyseMask(newMask),
-                            "_buffer": maskTemplate["mask"],
-                            "buffer": maskTemplate["mask"].slice(),
+                            "_buffer": undefined,
+                            "buffer": undefined,
                             "tests": {},
                             "tests": {},
                             "lastValidPosition": -1,
                             "lastValidPosition": -1,
-                            "greedy": maskTemplate["greedy"],
-                            "repeat": maskTemplate["repeat"],
+                            "greedy": undefined,
+                            "repeat": undefined,
                             "metadata": metadata
                             "metadata": metadata
                         });
                         });
                     }
                     }
                     newMask = maskPrefix + masks[0] + (maskParts.length > 1 ? maskParts[1] : "");
                     newMask = maskPrefix + masks[0] + (maskParts.length > 1 ? maskParts[1] : "");
                     if ($.inArray(newMask, genmasks) == -1 && newMask != "") {
                     if ($.inArray(newMask, genmasks) == -1 && newMask != "") {
                         genmasks.push(newMask);
                         genmasks.push(newMask);
-                        maskTemplate = getMaskTemplate(newMask);
                         ms.push({
                         ms.push({
                             "mask": newMask,
                             "mask": newMask,
                             "maskToken": analyseMask(newMask),
                             "maskToken": analyseMask(newMask),
-                            "_buffer": maskTemplate["mask"],
-                            "buffer": maskTemplate["mask"].slice(),
+                            "_buffer": undefined,
+                            "buffer": undefined,
                             "tests": {},
                             "tests": {},
                             "lastValidPosition": -1,
                             "lastValidPosition": -1,
-                            "greedy": maskTemplate["greedy"],
-                            "repeat": maskTemplate["repeat"],
+                            "greedy": undefined,
+                            "repeat": undefined,
                             "metadata": metadata
                             "metadata": metadata
                         });
                         });
                     }
                     }
@@ -279,7 +240,6 @@
                     newMask = maskPrefix + maskParts;
                     newMask = maskPrefix + maskParts;
                     if ($.inArray(newMask, genmasks) == -1 && newMask != "") {
                     if ($.inArray(newMask, genmasks) == -1 && newMask != "") {
                         genmasks.push(newMask);
                         genmasks.push(newMask);
-                        maskTemplate = getMaskTemplate(newMask);
                         ms.push({
                         ms.push({
                             "mask": newMask,
                             "mask": newMask,
                             "maskToken": analyseMask(newMask),
                             "maskToken": analyseMask(newMask),
@@ -287,12 +247,12 @@
                             //the buffer can be build upon these validPositions array
                             //the buffer can be build upon these validPositions array
                             //further validation start as from this index
                             //further validation start as from this index
                             "validPositions": [],
                             "validPositions": [],
-                            "_buffer": maskTemplate["mask"],
-                            "buffer": maskTemplate["mask"].slice(),
+                            "_buffer": undefined,
+                            "buffer": undefined,
                             "tests": {},
                             "tests": {},
                             "lastValidPosition": -1,
                             "lastValidPosition": -1,
-                            "greedy": maskTemplate["greedy"],
-                            "repeat": maskTemplate["repeat"],
+                            "greedy": undefined,
+                            "repeat": undefined,
                             "metadata": metadata
                             "metadata": metadata
                         });
                         });
                     }
                     }
@@ -300,6 +260,8 @@
 
 
             }
             }
 
 
+            if (opts.repeat == "*") opts.greedy = false;
+
             if ($.isFunction(opts.mask)) { //allow mask to be a preprocessing fn - should return a valid mask
             if ($.isFunction(opts.mask)) { //allow mask to be a preprocessing fn - should return a valid mask
                 opts.mask = opts.mask.call(this, opts);
                 opts.mask = opts.mask.call(this, opts);
             }
             }
@@ -312,6 +274,10 @@
                     }
                     }
                 });
                 });
             } else {
             } else {
+                if (opts.mask.length == 1 && opts.greedy == false && opts.repeat != 0) {
+                    opts.placeholder = "";
+                } //hide placeholder with single non-greedy mask
+
                 generateMask("", opts.mask.toString());
                 generateMask("", opts.mask.toString());
             }
             }
 
 
@@ -331,7 +297,19 @@
                 $el, chromeValueOnInput;
                 $el, chromeValueOnInput;
 
 
             //maskset helperfunctions
             //maskset helperfunctions
+            function getMaskTemplate() {
+                var maskTemplate = [];
+
+                var pos = 0, test;
 
 
+                do {
+                    test = getActiveTests(pos);
+                    maskTemplate.push(test["fn"] == null ? test["def"] : opts.placeholder.charAt(pos % opts.placeholder.length));
+                    pos++;
+                } while (test["fn"] != null || (test["fn"] == null && test["def"] != "") && opts.repeat != "*"); //fixme
+
+                return { "mask": maskTemplate, "repeat": opts.repeat, "greedy": opts.greedy };
+            }
             function getActiveMaskSet() {
             function getActiveMaskSet() {
                 return masksets[activeMasksetIndex];
                 return masksets[activeMasksetIndex];
             }
             }
@@ -353,10 +331,10 @@
                             } else if (match.isOptional) {
                             } else if (match.isOptional) {
                                 match = ResolveTestFromToken(match, ndxInitializer, loopNdx, quantifierRecurse);
                                 match = ResolveTestFromToken(match, ndxInitializer, loopNdx, quantifierRecurse);
                                 if (match) return match;
                                 if (match) return match;
-                            } else if (match.isQuantifier) {
+                            } else if (match.isQuantifier && quantifierRecurse !== true) {
                                 var qt = match;
                                 var qt = match;
-                                for (var qndx = ndxInitializer.length > 0 ? ndxInitializer.shift() : 0; qndx < isNaN(qt.quantifier.max) ? qndx + 1 : qt.quantifier.max; qndx++) {
-                                    //console.log("qt loop for " + pos);
+                                for (var qndx = (ndxInitializer.length > 0 && quantifierRecurse !== true) ? ndxInitializer.shift() : 0; qndx < (isNaN(qt.quantifier.max) ? qndx + 1 : qt.quantifier.max) ; qndx++) {
+                                    console.log(qndx + " loop for " + pos);
                                     match = handleMatch(maskToken.matches[maskToken.matches.indexOf(qt) - 1], [qndx].concat(loopNdx), true);
                                     match = handleMatch(maskToken.matches[maskToken.matches.indexOf(qt) - 1], [qndx].concat(loopNdx), true);
                                     if (match) {
                                     if (match) {
                                         return match;
                                         return match;
@@ -371,10 +349,12 @@
                         } else testPos++;
                         } else testPos++;
                     }
                     }
 
 
-                    for (var tndx = ndxInitializer.length > 0 ? ndxInitializer.shift() : 0; tndx < maskToken.matches.length; tndx++) {
-                        var match = handleMatch(maskToken.matches[tndx], [tndx].concat(loopNdx));
-                        if (match && testPos == pos) {
-                            return match;
+                    for (var tndx = (ndxInitializer.length > 0 && quantifierRecurse !== true ? ndxInitializer.shift() : 0) ; tndx < maskToken.matches.length ; tndx++) {
+                        if (maskToken.matches[tndx]["isQuantifier"] !== true) {
+                            var match = handleMatch(maskToken.matches[tndx], [tndx].concat(loopNdx), quantifierRecurse);
+                            if (match && testPos == pos) {
+                                return match;
+                            }
                         }
                         }
                     }
                     }
                 }
                 }
@@ -397,7 +377,7 @@
                     if (match && testPos == pos) {
                     if (match && testPos == pos) {
                         testLocator.push(mtndx);
                         testLocator.push(mtndx);
                         getActiveMaskSet()['tests'][pos] = { "match": match, "locator": testLocator.reverse() };
                         getActiveMaskSet()['tests'][pos] = { "match": match, "locator": testLocator.reverse() };
-                        //console.log(pos + " - " + testLocator);
+                        console.log(pos + " - " + testLocator);
                         return match;
                         return match;
                     }
                     }
                 }
                 }
@@ -406,10 +386,20 @@
             }
             }
 
 
             function getActiveBufferTemplate() {
             function getActiveBufferTemplate() {
+                if (getActiveMaskSet()['_buffer'] == undefined) {
+                    //generate template
+                    var maskTemplate = getMaskTemplate();
+                    getActiveMaskSet()["_buffer"] = maskTemplate["mask"];
+                    getActiveMaskSet()["greedy"] = maskTemplate["greedy"];
+                    getActiveMaskSet()["repeat"] = maskTemplate["repeat"];
+                }
                 return getActiveMaskSet()['_buffer'];
                 return getActiveMaskSet()['_buffer'];
             }
             }
 
 
             function getActiveBuffer() {
             function getActiveBuffer() {
+                if (getActiveMaskSet()['buffer'] == undefined) {
+                    getActiveMaskSet()['buffer'] = getActiveBufferTemplate().slice();
+                }
                 return getActiveMaskSet()['buffer'];
                 return getActiveMaskSet()['buffer'];
             }
             }