forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattrs.js
364 lines (325 loc) · 11.4 KB
/
attrs.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
var jsUrlsAllowed = false;
Blaze._allowJavascriptUrls = function () {
jsUrlsAllowed = true;
};
Blaze._javascriptUrlsAllowed = function () {
return jsUrlsAllowed;
};
// An AttributeHandler object is responsible for updating a particular attribute
// of a particular element. AttributeHandler subclasses implement
// browser-specific logic for dealing with particular attributes across
// different browsers.
//
// To define a new type of AttributeHandler, use
// `var FooHandler = AttributeHandler.extend({ update: function ... })`
// where the `update` function takes arguments `(element, oldValue, value)`.
// The `element` argument is always the same between calls to `update` on
// the same instance. `oldValue` and `value` are each either `null` or
// a Unicode string of the type that might be passed to the value argument
// of `setAttribute` (i.e. not an HTML string with character references).
// When an AttributeHandler is installed, an initial call to `update` is
// always made with `oldValue = null`. The `update` method can access
// `this.name` if the AttributeHandler class is a generic one that applies
// to multiple attribute names.
//
// AttributeHandlers can store custom properties on `this`, as long as they
// don't use the names `element`, `name`, `value`, and `oldValue`.
//
// AttributeHandlers can't influence how attributes appear in rendered HTML,
// only how they are updated after materialization as DOM.
AttributeHandler = function (name, value) {
this.name = name;
this.value = value;
};
Blaze._AttributeHandler = AttributeHandler;
AttributeHandler.prototype.update = function (element, oldValue, value) {
if (value === null) {
if (oldValue !== null)
element.removeAttribute(this.name);
} else {
element.setAttribute(this.name, value);
}
};
AttributeHandler.extend = function (options) {
var curType = this;
var subType = function AttributeHandlerSubtype(/*arguments*/) {
AttributeHandler.apply(this, arguments);
};
subType.prototype = new curType;
subType.extend = curType.extend;
if (options)
_.extend(subType.prototype, options);
return subType;
};
/// Apply the diff between the attributes of "oldValue" and "value" to "element."
//
// Each subclass must implement a parseValue method which takes a string
// as an input and returns a dict of attributes. The keys of the dict
// are unique identifiers (ie. css properties in the case of styles), and the
// values are the entire attribute which will be injected into the element.
//
// Extended below to support classes, SVG elements and styles.
var DiffingAttributeHandler = AttributeHandler.extend({
update: function (element, oldValue, value) {
if (!this.getCurrentValue || !this.setValue || !this.parseValue)
throw new Error("Missing methods in subclass of 'DiffingAttributeHandler'");
var oldAttrsMap = oldValue ? this.parseValue(oldValue) : {};
var newAttrsMap = value ? this.parseValue(value) : {};
// the current attributes on the element, which we will mutate.
var attrString = this.getCurrentValue(element);
var attrsMap = attrString ? this.parseValue(attrString) : {};
_.each(_.keys(oldAttrsMap), function (t) {
if (! (t in newAttrsMap))
delete attrsMap[t];
});
_.each(_.keys(newAttrsMap), function (t) {
attrsMap[t] = newAttrsMap[t];
});
this.setValue(element, _.values(attrsMap).join(' '));
}
});
var ClassHandler = DiffingAttributeHandler.extend({
// @param rawValue {String}
getCurrentValue: function (element) {
return element.className;
},
setValue: function (element, className) {
element.className = className;
},
parseValue: function (attrString) {
var tokens = {};
_.each(attrString.split(' '), function(token) {
if (token)
tokens[token] = token;
});
return tokens;
}
});
var SVGClassHandler = ClassHandler.extend({
getCurrentValue: function (element) {
return element.className.baseVal;
},
setValue: function (element, className) {
element.setAttribute('class', className);
}
});
var StyleHandler = DiffingAttributeHandler.extend({
getCurrentValue: function (element) {
return element.getAttribute('style');
},
setValue: function (element, style) {
if (style === '') {
element.removeAttribute('style');
} else {
element.setAttribute('style', style);
}
},
// Parse a string to produce a map from property to attribute string.
//
// Example:
// "color:red; foo:12px" produces a token {color: "color:red", foo:"foo:12px"}
parseValue: function (attrString) {
var tokens = {};
// Regex for parsing a css attribute declaration, taken from css-parse:
// https://github.com/reworkcss/css-parse/blob/7cef3658d0bba872cde05a85339034b187cb3397/index.js#L219
var regex = /(\*?[-#\/\*\\\w]+(?:\[[0-9a-z_-]+\])?)\s*:\s*(?:\'(?:\\\'|.)*?\'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+[;\s]*/g;
var match = regex.exec(attrString);
while (match) {
// match[0] = entire matching string
// match[1] = css property
// Prefix the token to prevent conflicts with existing properties.
// XXX No `String.trim` on Safari 4. Swap out $.trim if we want to
// remove strong dep on jquery.
tokens[' ' + match[1]] = match[0].trim ?
match[0].trim() : $.trim(match[0]);
match = regex.exec(attrString);
}
return tokens;
}
});
var BooleanHandler = AttributeHandler.extend({
update: function (element, oldValue, value) {
var name = this.name;
if (value == null) {
if (oldValue != null)
element[name] = false;
} else {
element[name] = true;
}
}
});
var DOMPropertyHandler = AttributeHandler.extend({
update: function (element, oldValue, value) {
var name = this.name;
if (value !== element[name])
element[name] = value;
}
});
// attributes of the type 'xlink:something' should be set using
// the correct namespace in order to work
var XlinkHandler = AttributeHandler.extend({
update: function(element, oldValue, value) {
var NS = 'http://www.w3.org/1999/xlink';
if (value === null) {
if (oldValue !== null)
element.removeAttributeNS(NS, this.name);
} else {
element.setAttributeNS(NS, this.name, this.value);
}
}
});
// cross-browser version of `instanceof SVGElement`
var isSVGElement = function (elem) {
return 'ownerSVGElement' in elem;
};
var isUrlAttribute = function (tagName, attrName) {
// Compiled from http://www.w3.org/TR/REC-html40/index/attributes.html
// and
// http://www.w3.org/html/wg/drafts/html/master/index.html#attributes-1
var urlAttrs = {
FORM: ['action'],
BODY: ['background'],
BLOCKQUOTE: ['cite'],
Q: ['cite'],
DEL: ['cite'],
INS: ['cite'],
OBJECT: ['classid', 'codebase', 'data', 'usemap'],
APPLET: ['codebase'],
A: ['href'],
AREA: ['href'],
LINK: ['href'],
BASE: ['href'],
IMG: ['longdesc', 'src', 'usemap'],
FRAME: ['longdesc', 'src'],
IFRAME: ['longdesc', 'src'],
HEAD: ['profile'],
SCRIPT: ['src'],
INPUT: ['src', 'usemap', 'formaction'],
BUTTON: ['formaction'],
BASE: ['href'],
MENUITEM: ['icon'],
HTML: ['manifest'],
VIDEO: ['poster']
};
if (attrName === 'itemid') {
return true;
}
var urlAttrNames = urlAttrs[tagName] || [];
return _.contains(urlAttrNames, attrName);
};
// To get the protocol for a URL, we let the browser normalize it for
// us, by setting it as the href for an anchor tag and then reading out
// the 'protocol' property.
if (Meteor.isClient) {
var anchorForNormalization = document.createElement('A');
}
var getUrlProtocol = function (url) {
if (Meteor.isClient) {
anchorForNormalization.href = url;
return (anchorForNormalization.protocol || "").toLowerCase();
} else {
throw new Error('getUrlProtocol not implemented on the server');
}
};
// UrlHandler is an attribute handler for all HTML attributes that take
// URL values. It disallows javascript: URLs, unless
// Blaze._allowJavascriptUrls() has been called. To detect javascript:
// urls, we set the attribute on a dummy anchor element and then read
// out the 'protocol' property of the attribute.
var origUpdate = AttributeHandler.prototype.update;
var UrlHandler = AttributeHandler.extend({
update: function (element, oldValue, value) {
var self = this;
var args = arguments;
if (Blaze._javascriptUrlsAllowed()) {
origUpdate.apply(self, args);
} else {
var isJavascriptProtocol = (getUrlProtocol(value) === "javascript:");
if (isJavascriptProtocol) {
Blaze._warn("URLs that use the 'javascript:' protocol are not " +
"allowed in URL attribute values. " +
"Call Blaze._allowJavascriptUrls() " +
"to enable them.");
origUpdate.apply(self, [element, oldValue, null]);
} else {
origUpdate.apply(self, args);
}
}
}
});
// XXX make it possible for users to register attribute handlers!
makeAttributeHandler = function (elem, name, value) {
// generally, use setAttribute but certain attributes need to be set
// by directly setting a JavaScript property on the DOM element.
if (name === 'class') {
if (isSVGElement(elem)) {
return new SVGClassHandler(name, value);
} else {
return new ClassHandler(name, value);
}
} else if (name === 'style') {
return new StyleHandler(name, value);
} else if ((elem.tagName === 'OPTION' && name === 'selected') ||
(elem.tagName === 'INPUT' && name === 'checked')) {
return new BooleanHandler(name, value);
} else if ((elem.tagName === 'TEXTAREA' || elem.tagName === 'INPUT')
&& name === 'value') {
// internally, TEXTAREAs tracks their value in the 'value'
// attribute just like INPUTs.
return new DOMPropertyHandler(name, value);
} else if (name.substring(0,6) === 'xlink:') {
return new XlinkHandler(name.substring(6), value);
} else if (isUrlAttribute(elem.tagName, name)) {
return new UrlHandler(name, value);
} else {
return new AttributeHandler(name, value);
}
// XXX will need one for 'style' on IE, though modern browsers
// seem to handle setAttribute ok.
};
ElementAttributesUpdater = function (elem) {
this.elem = elem;
this.handlers = {};
};
// Update attributes on `elem` to the dictionary `attrs`, whose
// values are strings.
ElementAttributesUpdater.prototype.update = function(newAttrs) {
var elem = this.elem;
var handlers = this.handlers;
for (var k in handlers) {
if (! _.has(newAttrs, k)) {
// remove attributes (and handlers) for attribute names
// that don't exist as keys of `newAttrs` and so won't
// be visited when traversing it. (Attributes that
// exist in the `newAttrs` object but are `null`
// are handled later.)
var handler = handlers[k];
var oldValue = handler.value;
handler.value = null;
handler.update(elem, oldValue, null);
delete handlers[k];
}
}
for (var k in newAttrs) {
var handler = null;
var oldValue;
var value = newAttrs[k];
if (! _.has(handlers, k)) {
if (value !== null) {
// make new handler
handler = makeAttributeHandler(elem, k, value);
handlers[k] = handler;
oldValue = null;
}
} else {
handler = handlers[k];
oldValue = handler.value;
}
if (oldValue !== value) {
handler.value = value;
handler.update(elem, oldValue, value);
if (value === null)
delete handlers[k];
}
}
};