~didrocks/+junk/face-detection-15.04

« back to all changes in this revision

Viewing changes to facedetection/www/bower_components/prism/components/prism-core.js

  • Committer: Didier Roche
  • Date: 2016-05-10 23:09:11 UTC
  • Revision ID: didier.roche@canonical.com-20160510230911-c7xr490zrj3yrzxd
New version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var _self = (typeof window !== 'undefined')
 
2
        ? window   // if in browser
 
3
        : (
 
4
                (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
 
5
                ? self // if in worker
 
6
                : {}   // if in node js
 
7
        );
 
8
 
 
9
/**
 
10
 * Prism: Lightweight, robust, elegant syntax highlighting
 
11
 * MIT license http://www.opensource.org/licenses/mit-license.php/
 
12
 * @author Lea Verou http://lea.verou.me
 
13
 */
 
14
 
 
15
var Prism = (function(){
 
16
 
 
17
// Private helper vars
 
18
var lang = /\blang(?:uage)?-(\w+)\b/i;
 
19
var uniqueId = 0;
 
20
 
 
21
var _ = _self.Prism = {
 
22
        util: {
 
23
                encode: function (tokens) {
 
24
                        if (tokens instanceof Token) {
 
25
                                return new Token(tokens.type, _.util.encode(tokens.content), tokens.alias);
 
26
                        } else if (_.util.type(tokens) === 'Array') {
 
27
                                return tokens.map(_.util.encode);
 
28
                        } else {
 
29
                                return tokens.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' ');
 
30
                        }
 
31
                },
 
32
 
 
33
                type: function (o) {
 
34
                        return Object.prototype.toString.call(o).match(/\[object (\w+)\]/)[1];
 
35
                },
 
36
 
 
37
                objId: function (obj) {
 
38
                        if (!obj['__id']) {
 
39
                                Object.defineProperty(obj, '__id', { value: ++uniqueId });
 
40
                        }
 
41
                        return obj['__id'];
 
42
                },
 
43
 
 
44
                // Deep clone a language definition (e.g. to extend it)
 
45
                clone: function (o) {
 
46
                        var type = _.util.type(o);
 
47
 
 
48
                        switch (type) {
 
49
                                case 'Object':
 
50
                                        var clone = {};
 
51
 
 
52
                                        for (var key in o) {
 
53
                                                if (o.hasOwnProperty(key)) {
 
54
                                                        clone[key] = _.util.clone(o[key]);
 
55
                                                }
 
56
                                        }
 
57
 
 
58
                                        return clone;
 
59
 
 
60
                                case 'Array':
 
61
                                        // Check for existence for IE8
 
62
                                        return o.map && o.map(function(v) { return _.util.clone(v); });
 
63
                        }
 
64
 
 
65
                        return o;
 
66
                }
 
67
        },
 
68
 
 
69
        languages: {
 
70
                extend: function (id, redef) {
 
71
                        var lang = _.util.clone(_.languages[id]);
 
72
 
 
73
                        for (var key in redef) {
 
74
                                lang[key] = redef[key];
 
75
                        }
 
76
 
 
77
                        return lang;
 
78
                },
 
79
 
 
80
                /**
 
81
                 * Insert a token before another token in a language literal
 
82
                 * As this needs to recreate the object (we cannot actually insert before keys in object literals),
 
83
                 * we cannot just provide an object, we need anobject and a key.
 
84
                 * @param inside The key (or language id) of the parent
 
85
                 * @param before The key to insert before. If not provided, the function appends instead.
 
86
                 * @param insert Object with the key/value pairs to insert
 
87
                 * @param root The object that contains `inside`. If equal to Prism.languages, it can be omitted.
 
88
                 */
 
89
                insertBefore: function (inside, before, insert, root) {
 
90
                        root = root || _.languages;
 
91
                        var grammar = root[inside];
 
92
                        
 
93
                        if (arguments.length == 2) {
 
94
                                insert = arguments[1];
 
95
                                
 
96
                                for (var newToken in insert) {
 
97
                                        if (insert.hasOwnProperty(newToken)) {
 
98
                                                grammar[newToken] = insert[newToken];
 
99
                                        }
 
100
                                }
 
101
                                
 
102
                                return grammar;
 
103
                        }
 
104
                        
 
105
                        var ret = {};
 
106
 
 
107
                        for (var token in grammar) {
 
108
 
 
109
                                if (grammar.hasOwnProperty(token)) {
 
110
 
 
111
                                        if (token == before) {
 
112
 
 
113
                                                for (var newToken in insert) {
 
114
 
 
115
                                                        if (insert.hasOwnProperty(newToken)) {
 
116
                                                                ret[newToken] = insert[newToken];
 
117
                                                        }
 
118
                                                }
 
119
                                        }
 
120
 
 
121
                                        ret[token] = grammar[token];
 
122
                                }
 
123
                        }
 
124
                        
 
125
                        // Update references in other language definitions
 
126
                        _.languages.DFS(_.languages, function(key, value) {
 
127
                                if (value === root[inside] && key != inside) {
 
128
                                        this[key] = ret;
 
129
                                }
 
130
                        });
 
131
 
 
132
                        return root[inside] = ret;
 
133
                },
 
134
 
 
135
                // Traverse a language definition with Depth First Search
 
136
                DFS: function(o, callback, type, visited) {
 
137
                        visited = visited || {};
 
138
                        for (var i in o) {
 
139
                                if (o.hasOwnProperty(i)) {
 
140
                                        callback.call(o, i, o[i], type || i);
 
141
 
 
142
                                        if (_.util.type(o[i]) === 'Object' && !visited[_.util.objId(o[i])]) {
 
143
                                                visited[_.util.objId(o[i])] = true;
 
144
                                                _.languages.DFS(o[i], callback, null, visited);
 
145
                                        }
 
146
                                        else if (_.util.type(o[i]) === 'Array' && !visited[_.util.objId(o[i])]) {
 
147
                                                visited[_.util.objId(o[i])] = true;
 
148
                                                _.languages.DFS(o[i], callback, i, visited);
 
149
                                        }
 
150
                                }
 
151
                        }
 
152
                }
 
153
        },
 
154
        plugins: {},
 
155
        
 
156
        highlightAll: function(async, callback) {
 
157
                var elements = document.querySelectorAll('code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code');
 
158
 
 
159
                for (var i=0, element; element = elements[i++];) {
 
160
                        _.highlightElement(element, async === true, callback);
 
161
                }
 
162
        },
 
163
 
 
164
        highlightElement: function(element, async, callback) {
 
165
                // Find language
 
166
                var language, grammar, parent = element;
 
167
 
 
168
                while (parent && !lang.test(parent.className)) {
 
169
                        parent = parent.parentNode;
 
170
                }
 
171
 
 
172
                if (parent) {
 
173
                        language = (parent.className.match(lang) || [,''])[1];
 
174
                        grammar = _.languages[language];
 
175
                }
 
176
 
 
177
                // Set language on the element, if not present
 
178
                element.className = element.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
 
179
 
 
180
                // Set language on the parent, for styling
 
181
                parent = element.parentNode;
 
182
 
 
183
                if (/pre/i.test(parent.nodeName)) {
 
184
                        parent.className = parent.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
 
185
                }
 
186
 
 
187
                var code = element.textContent;
 
188
 
 
189
                var env = {
 
190
                        element: element,
 
191
                        language: language,
 
192
                        grammar: grammar,
 
193
                        code: code
 
194
                };
 
195
 
 
196
                if (!code || !grammar) {
 
197
                        _.hooks.run('complete', env);
 
198
                        return;
 
199
                }
 
200
 
 
201
                _.hooks.run('before-highlight', env);
 
202
 
 
203
                if (async && _self.Worker) {
 
204
                        var worker = new Worker(_.filename);
 
205
 
 
206
                        worker.onmessage = function(evt) {
 
207
                                env.highlightedCode = evt.data;
 
208
 
 
209
                                _.hooks.run('before-insert', env);
 
210
 
 
211
                                env.element.innerHTML = env.highlightedCode;
 
212
 
 
213
                                callback && callback.call(env.element);
 
214
                                _.hooks.run('after-highlight', env);
 
215
                                _.hooks.run('complete', env);
 
216
                        };
 
217
 
 
218
                        worker.postMessage(JSON.stringify({
 
219
                                language: env.language,
 
220
                                code: env.code,
 
221
                                immediateClose: true
 
222
                        }));
 
223
                }
 
224
                else {
 
225
                        env.highlightedCode = _.highlight(env.code, env.grammar, env.language);
 
226
 
 
227
                        _.hooks.run('before-insert', env);
 
228
 
 
229
                        env.element.innerHTML = env.highlightedCode;
 
230
 
 
231
                        callback && callback.call(element);
 
232
 
 
233
                        _.hooks.run('after-highlight', env);
 
234
                        _.hooks.run('complete', env);
 
235
                }
 
236
        },
 
237
 
 
238
        highlight: function (text, grammar, language) {
 
239
                var tokens = _.tokenize(text, grammar);
 
240
                return Token.stringify(_.util.encode(tokens), language);
 
241
        },
 
242
 
 
243
        tokenize: function(text, grammar, language) {
 
244
                var Token = _.Token;
 
245
 
 
246
                var strarr = [text];
 
247
 
 
248
                var rest = grammar.rest;
 
249
 
 
250
                if (rest) {
 
251
                        for (var token in rest) {
 
252
                                grammar[token] = rest[token];
 
253
                        }
 
254
 
 
255
                        delete grammar.rest;
 
256
                }
 
257
 
 
258
                tokenloop: for (var token in grammar) {
 
259
                        if(!grammar.hasOwnProperty(token) || !grammar[token]) {
 
260
                                continue;
 
261
                        }
 
262
 
 
263
                        var patterns = grammar[token];
 
264
                        patterns = (_.util.type(patterns) === "Array") ? patterns : [patterns];
 
265
 
 
266
                        for (var j = 0; j < patterns.length; ++j) {
 
267
                                var pattern = patterns[j],
 
268
                                        inside = pattern.inside,
 
269
                                        lookbehind = !!pattern.lookbehind,
 
270
                                        lookbehindLength = 0,
 
271
                                        alias = pattern.alias;
 
272
 
 
273
                                pattern = pattern.pattern || pattern;
 
274
 
 
275
                                for (var i=0; i<strarr.length; i++) { // Don’t cache length as it changes during the loop
 
276
 
 
277
                                        var str = strarr[i];
 
278
 
 
279
                                        if (strarr.length > text.length) {
 
280
                                                // Something went terribly wrong, ABORT, ABORT!
 
281
                                                break tokenloop;
 
282
                                        }
 
283
 
 
284
                                        if (str instanceof Token) {
 
285
                                                continue;
 
286
                                        }
 
287
 
 
288
                                        pattern.lastIndex = 0;
 
289
 
 
290
                                        var match = pattern.exec(str);
 
291
 
 
292
                                        if (match) {
 
293
                                                if(lookbehind) {
 
294
                                                        lookbehindLength = match[1].length;
 
295
                                                }
 
296
 
 
297
                                                var from = match.index - 1 + lookbehindLength,
 
298
                                                        match = match[0].slice(lookbehindLength),
 
299
                                                        len = match.length,
 
300
                                                        to = from + len,
 
301
                                                        before = str.slice(0, from + 1),
 
302
                                                        after = str.slice(to + 1);
 
303
 
 
304
                                                var args = [i, 1];
 
305
 
 
306
                                                if (before) {
 
307
                                                        args.push(before);
 
308
                                                }
 
309
 
 
310
                                                var wrapped = new Token(token, inside? _.tokenize(match, inside) : match, alias);
 
311
 
 
312
                                                args.push(wrapped);
 
313
 
 
314
                                                if (after) {
 
315
                                                        args.push(after);
 
316
                                                }
 
317
 
 
318
                                                Array.prototype.splice.apply(strarr, args);
 
319
                                        }
 
320
                                }
 
321
                        }
 
322
                }
 
323
 
 
324
                return strarr;
 
325
        },
 
326
 
 
327
        hooks: {
 
328
                all: {},
 
329
 
 
330
                add: function (name, callback) {
 
331
                        var hooks = _.hooks.all;
 
332
 
 
333
                        hooks[name] = hooks[name] || [];
 
334
 
 
335
                        hooks[name].push(callback);
 
336
                },
 
337
 
 
338
                run: function (name, env) {
 
339
                        var callbacks = _.hooks.all[name];
 
340
 
 
341
                        if (!callbacks || !callbacks.length) {
 
342
                                return;
 
343
                        }
 
344
 
 
345
                        for (var i=0, callback; callback = callbacks[i++];) {
 
346
                                callback(env);
 
347
                        }
 
348
                }
 
349
        }
 
350
};
 
351
 
 
352
var Token = _.Token = function(type, content, alias) {
 
353
        this.type = type;
 
354
        this.content = content;
 
355
        this.alias = alias;
 
356
};
 
357
 
 
358
Token.stringify = function(o, language, parent) {
 
359
        if (typeof o == 'string') {
 
360
                return o;
 
361
        }
 
362
 
 
363
        if (_.util.type(o) === 'Array') {
 
364
                return o.map(function(element) {
 
365
                        return Token.stringify(element, language, o);
 
366
                }).join('');
 
367
        }
 
368
 
 
369
        var env = {
 
370
                type: o.type,
 
371
                content: Token.stringify(o.content, language, parent),
 
372
                tag: 'span',
 
373
                classes: ['token', o.type],
 
374
                attributes: {},
 
375
                language: language,
 
376
                parent: parent
 
377
        };
 
378
 
 
379
        if (env.type == 'comment') {
 
380
                env.attributes['spellcheck'] = 'true';
 
381
        }
 
382
 
 
383
        if (o.alias) {
 
384
                var aliases = _.util.type(o.alias) === 'Array' ? o.alias : [o.alias];
 
385
                Array.prototype.push.apply(env.classes, aliases);
 
386
        }
 
387
 
 
388
        _.hooks.run('wrap', env);
 
389
 
 
390
        var attributes = '';
 
391
 
 
392
        for (var name in env.attributes) {
 
393
                attributes += (attributes ? ' ' : '') + name + '="' + (env.attributes[name] || '') + '"';
 
394
        }
 
395
 
 
396
        return '<' + env.tag + ' class="' + env.classes.join(' ') + '" ' + attributes + '>' + env.content + '</' + env.tag + '>';
 
397
 
 
398
};
 
399
 
 
400
if (!_self.document) {
 
401
        if (!_self.addEventListener) {
 
402
                // in Node.js
 
403
                return _self.Prism;
 
404
        }
 
405
        // In worker
 
406
        _self.addEventListener('message', function(evt) {
 
407
                var message = JSON.parse(evt.data),
 
408
                    lang = message.language,
 
409
                    code = message.code,
 
410
                    immediateClose = message.immediateClose;
 
411
 
 
412
                _self.postMessage(_.highlight(code, _.languages[lang], lang));
 
413
                if (immediateClose) {
 
414
                        _self.close();
 
415
                }
 
416
        }, false);
 
417
 
 
418
        return _self.Prism;
 
419
}
 
420
 
 
421
//Get current script and highlight
 
422
var script = document.currentScript || [].slice.call(document.getElementsByTagName("script")).pop();
 
423
 
 
424
if (script) {
 
425
        _.filename = script.src;
 
426
 
 
427
        if (document.addEventListener && !script.hasAttribute('data-manual')) {
 
428
                document.addEventListener('DOMContentLoaded', _.highlightAll);
 
429
        }
 
430
}
 
431
 
 
432
return _self.Prism;
 
433
 
 
434
})();
 
435
 
 
436
if (typeof module !== 'undefined' && module.exports) {
 
437
        module.exports = Prism;
 
438
}
 
439
 
 
440
// hack for components to work correctly in node.js
 
441
if (typeof global !== 'undefined') {
 
442
        global.Prism = Prism;
 
443
}