-
Notifications
You must be signed in to change notification settings - Fork 1
/
angular-xregexp.js
133 lines (113 loc) · 4.69 KB
/
angular-xregexp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* angular-xregexp.js / v1.0.0 / (c) 2016 Cosimo Meli / MIT Licence */
'format amd';
/* global define */
(function () {
'use strict';
function requireXRegExp() {
try {
return require('xregexp'); // Using nw.js or browserify?
} catch (e) {
throw new Error('Please install XRegExp via npm. Please reference to: https://github.com/cosimomeli/angular-xregexp');
}
}
function angularXRegExp(angular, XRegExp) {
if (typeof XRegExp === 'undefined') {
if (typeof require === 'function') {
XRegExp = requireXRegExp();
} else {
throw new Error('XRegExp cannot be found by angular-xregexp! Please reference to: https://github.com/cosimomeli/angular-xregexp');
}
}
var NODE_TYPE_TEXT = 3;
/**
* @returns {string} Returns the string representation of the element.
*/
function startingTag(element) {
element = angular.element(element).clone();
try {
// turns out IE does not let you set .html() on elements which
// are not allowed to have children. So we just ignore it.
element.empty();
} catch (e) {
}
var elemHtml = angular.element('<div>').append(element).html();
try {
return element[0].nodeType === NODE_TYPE_TEXT ? angular.lowercase(elemHtml) :
elemHtml.match(/^(<[^>]+>)/)[1].replace(/^<([\w\-]+)/, function (match, nodeName) {
return '<' + angular.lowercase(nodeName);
});
} catch (e) {
return angular.lowercase(elemHtml);
}
}
/**
* New link function to replace the ngPattern one
*/
function newLink(scope, elm, attr, ctrl) {
if (!ctrl) {
return;
}
function toXRegExp(regex) {
var REGEX_STRING_REGEXP = /^\/(.+)\/([a-z]*)$/;
if (regex instanceof RegExp) {
regex = regex.toString();
}
var match = regex.match(REGEX_STRING_REGEXP);
if (match) {
return new XRegExp(match[1], match[2]);
}
}
var regexp, patternExp = toXRegExp(attr.ngPattern || attr.pattern);
attr.$observe('pattern', function (regex) {
if (angular.isString(regex) && regex.length > 0) {
regex = new XRegExp('^' + regex + '$');
} else if (regex instanceof RegExp) {
regex = toXRegExp(regex);
}
if (regex && !regex.test) {
throw angular.$$minErr('ngPattern')('noregexp',
'Expected {0} to be a RegExp but was {1}. Element: {2}', patternExp,
regex, startingTag(elm));
}
regexp = regex || undefined;
ctrl.$validate();
});
ctrl.$validators.pattern = function (modelValue, viewValue) {
// HTML5 pattern constraint validates the input value, so we validate the viewValue
return ctrl.$isEmpty(viewValue) || angular.isUndefined(regexp) || regexp.test(viewValue);
};
}
/**
* @ngdoc overview
* @name angularXRegExp
*
* @description
* angularXRegExp module provides XRegExp functionality for angular.js apps.
*/
angular.module('angularXRegExp', [])
/**
* @ngdoc object
* @name angularXRegExp.config
*/
.config(['$provide', function ($provide) {
$provide.decorator('ngPatternDirective', ['$delegate', function ($delegate) {
//Replace ngPattern's link function
$delegate[0].compile = function () {
return function () {
newLink.apply(this, arguments);
};
};
return $delegate;
}]);
}]);
return 'angularXRegExp';
}
var isElectron = window && window.process && window.process.type;
if (typeof define === 'function' && define.amd) {
define(['angular', 'xregexp'], angularXRegExp);
} else if (typeof module !== 'undefined' && module && module.exports && (typeof require === 'function') && !isElectron) {
module.exports = angularXRegExp(require('angular'), require('xregexp'));
} else {
angularXRegExp(angular, (typeof global !== 'undefined' ? global : window).XRegExp);
}
})();