-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplastik-url-validator.html
executable file
·142 lines (138 loc) · 3.86 KB
/
plastik-url-validator.html
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
134
135
136
137
138
139
140
141
142
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-validator-behavior/iron-validator-behavior.html">
<script src="../validator-js/validator.js"></script>
<script>
/**
* `plastik-url-validator` checks whether or not any input is a valid URL.
*
* This functionality is provided by the [validator.js](https://github.com/chriso/validator.js)
* library, a dependency of this element.
*
* ### Example
*
* ```html
* <input is="iron-input" bind-value="{{value}}" validator="plastik-url-validator">
*
* <plastik-url-validator protocol-optional allow-protocol-relative-urls>
* </plastik-url-validator>
* ```
*
* @demo demo/index.html
* @polymerBehavior
*/
Polymer({
is: 'plastik-url-validator',
properties: {
/**
* Gets or sets which protocols to allow.
*
* @attribute protocols
* @type {array}
*/
protocols: {
type: Array,
value: [ 'http', 'https' ]
},
/**
* Gets or sets whether or not a TLD is optional.
*
* @attribute requireTld
* @type {boolean}
*/
tldOptional: {
type: Boolean,
value: false
},
/**
* Gets or sets whether or not a protocol is optional. If true, `protocols` has
* no effect. If false, `allowProtocolRelativeUrls` has no effect.
*
* @attribute requireProtocol
* @type {boolean}
*/
protocolOptional: {
type: Boolean,
value: false
},
/**
* Gets or sets whether or not underscores are allowed.
*
* @attribute allowUnderscores
* @type {boolean}
*/
allowUnderscores: {
type: Boolean,
value: false
},
/**
* Gets or sets a list of hosts to whitelist.
*
* @attribute hostWhitelist
* @type {array}
*/
hostWhitelist: {
type: Array,
value: []
},
/**
* Gets or sets a list of hosts to blacklist.
*
* @attribute hostBlacklist
* @type {array}
*/
hostBlacklist: {
type: Array,
value: []
},
/**
* Gets or sets whether or not to allow trailing dots.
*
* @attribute: allowTrailingDot
* @type {boolean}
*/
allowTrailingDot: {
type: Boolean,
value: false
},
/**
* Gets or sets whether or not to allow protocol relative URLs.
*
* @attribute allowProtocolRelativeUrls
* @type {boolean}
*/
allowProtocolRelativeUrls: {
type: Boolean,
value: false
}
},
behaviors: [
Polymer.IronValidatorBehavior
],
/**
* Validates input and returns whether or not it is a valid URL given
* the options set on the validator.
*
* @method validate
* @param {string} input The input to validate.
*/
validate: function(input) {
if (input === null || typeof input === 'undefined') {
input = '';
} else if (typeof input !== 'string') {
console.warn((typeof input) + ' passed to URL validator.');
return false;
}
var options = {
protocols: this.protocols ? (this.protocols.length ? this.protocols : undefined) : undefined,
require_tld: !this.tldOptional,
require_protocol: !this.protocolOptional,
allow_underscores: this.allowUnderscores,
host_whitelist: this.hostWhitelist ? (this.hostWhitelist.length ? this.hostWhitelist : undefined) : undefined,
host_blacklist: this.hostBlacklist ? (this.hostBlacklist.length ? this.hostBlacklist : undefined) : undefined,
allow_trailing_dot: this.allowTrailingDot,
allow_protocol_relative_urls: this.allowProtocolRelativeUrls
};
return validator.isURL(input, options);
}
});
</script>