forked from googlearchive/code-prettify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_prettify.js
More file actions
251 lines (225 loc) · 9.55 KB
/
Copy pathrun_prettify.js
File metadata and controls
251 lines (225 loc) · 9.55 KB
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
// Copyright (C) 2013 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Looks at query parameters to decide which language handlers and style-sheets
// to load.
// Query Parameter Format Effect Default
// +------------------+---------------+------------------------------+--------+
// | autorun= | true | false | If true then prettyPrint() | "true" |
// | | | is called on page load. | |
// +------------------+---------------+------------------------------+--------+
// | lang= | language name | Loads the language handler | Can |
// | | | named "lang-<NAME>.js". | appear |
// | | | See available handlers at | many |
// | | | https://github.com/google/ | times. |
// | | | code-prettify/tree/master/ | |
// | | | src | |
// +------------------+---------------+------------------------------+--------+
// | skin= | skin name | Loads the skin stylesheet | none. |
// | | | named "<NAME>.css". | |
// | | | https://cdn.rawgit.com/ | |
// | | | google/code-prettify/master/ | |
// | | | styles/index.html | |
// +------------------+---------------+------------------------------+--------+
// | callback= | JS identifier | When "prettyPrint" finishes | none |
// | | | window.exports[js_ident] is | |
// | | | called. | |
// | | | The callback must be under | |
// | | | exports to reduce the risk | |
// | | | of XSS via query parameter | |
// | | | injection. | |
// +------------------+---------------+------------------------------+--------+
// Exmaples
// .../prettify.js?lang=css&skin=sunburst
// 1. Loads the CSS language handler which can be used to prettify CSS
// stylesheets, HTML <style> element bodies and style="..." attributes
// values.
// 2. Loads the sunburst.css stylesheet instead of the default prettify.css
// stylesheet.
// A gallery of stylesheets is available at
// https://cdn.rawgit.com/google/code-prettify/master/styles/index.html
// 3. Since autorun=false is not specified, calls prettyPrint() on page load.
(function () {
"use strict";
var win = window;
var setTimeout = win.setTimeout;
var doc = document;
var root = doc.documentElement;
var head = doc['head'] || doc.getElementsByTagName("head")[0] || root;
// From http://javascript.nwbox.com/ContentLoaded/contentloaded.js
// Author: Diego Perini (diego.perini at gmail.com)
// Summary: cross-browser wrapper for DOMContentLoaded
// Updated: 20101020
// License: MIT
// Version: 1.2
function contentLoaded(callback) {
var addEventListener = doc['addEventListener'];
var done = false, top = true,
add = addEventListener ? 'addEventListener' : 'attachEvent',
rem = addEventListener ? 'removeEventListener' : 'detachEvent',
pre = addEventListener ? '' : 'on',
init = function(e) {
if (e.type == 'readystatechange' && doc.readyState != 'complete') {
return;
}
(e.type == 'load' ? win : doc)[rem](pre + e.type, init, false);
if (!done && (done = true)) { callback.call(win, e.type || e); }
},
poll = function() {
try {
root.doScroll('left');
} catch(e) {
setTimeout(poll, 50);
return;
}
init('poll');
};
if (doc.readyState == 'complete') {
callback.call(win, 'lazy');
} else {
if (doc.createEventObject && root.doScroll) {
try { top = !win.frameElement; } catch(e) { }
if (top) { poll(); }
}
doc[add](pre + 'DOMContentLoaded', init, false);
doc[add](pre + 'readystatechange', init, false);
win[add](pre + 'load', init, false);
}
}
// Given a list of URLs to stylesheets, loads the first that loads without
// triggering an error event.
function loadStylesheetsFallingBack(stylesheets) {
var n = stylesheets.length;
function load(i) {
if (i === n) { return; }
var link = doc.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
if (i + 1 < n) {
// http://pieisgood.org/test/script-link-events/ indicates that many
// versions of IE do not support onerror on <link>s, though
// http://msdn.microsoft.com/en-us/library/ie/ms535848(v=vs.85).aspx
// indicates that recent IEs do support error.
link.error = link.onerror = function () { load(i + 1); };
}
link.href = stylesheets[i];
head.appendChild(link);
}
load(0);
}
var scriptQuery = '';
// Look for the <script> node that loads this script to get its parameters.
// This starts looking at the end instead of just considering the last
// because deferred and async scripts run out of order.
// If the script is loaded twice, then this will run in reverse order.
var scripts = doc.getElementsByTagName('script');
for (var i = scripts.length; --i >= 0;) {
var script = scripts[i];
var match = script.src.match(
/^[^?#]*\/run_prettify\.js(\?[^#]*)?(?:#.*)?$/);
if (match) {
scriptQuery = match[1] || '';
// Remove the script from the DOM so that multiple runs at least run
// multiple times even if parameter sets are interpreted in reverse
// order.
script.parentNode.removeChild(script);
break;
}
}
// Pull parameters into local variables.
var autorun = true;
var langs = [];
var skins = [];
var callbacks = [];
scriptQuery.replace(
/[?&]([^&=]+)=([^&]+)/g,
function (_, name, value) {
value = decodeURIComponent(value);
name = decodeURIComponent(name);
if (name == 'autorun') { autorun = !/^[0fn]/i.test(value); } else
if (name == 'lang') { langs.push(value); } else
if (name == 'skin') { skins.push(value); } else
if (name == 'callback') { callbacks.push(value); }
});
// Use https to avoid mixed content warnings in client pages and to
// prevent a MITM from rewrite prettify mid-flight.
// This only works if this script is loaded via https : something
// over which we exercise no control.
var LOADER_BASE_URL =
'https://cdn.rawgit.com/google/code-prettify/master/loader';
for (var i = 0, n = langs.length; i < n; ++i) (function (lang) {
var script = doc.createElement("script");
// Excerpted from jQuery.ajaxTransport("script") to fire events when
// a script is finished loading.
// Attach handlers for each script
script.onload = script.onerror = script.onreadystatechange = function () {
if (script && (
!script.readyState || /loaded|complete/.test(script.readyState))) {
// Handle memory leak in IE
script.onerror = script.onload = script.onreadystatechange = null;
--pendingLanguages;
checkPendingLanguages();
// Remove the script
if (script.parentNode) {
script.parentNode.removeChild(script);
}
script = null;
}
};
script.type = 'text/javascript';
script.src = LOADER_BASE_URL
+ '/lang-' + encodeURIComponent(langs[i]) + '.js';
// Circumvent IE6 bugs with base elements (#2709 and #4378) by prepending
head.insertBefore(script, head.firstChild);
})(langs[i]);
var pendingLanguages = langs.length;
function checkPendingLanguages() {
if (!pendingLanguages) {
setTimeout(onLangsLoaded, 0);
}
}
var skinUrls = [];
for (var i = 0, n = skins.length; i < n; ++i) {
skinUrls.push(LOADER_BASE_URL
+ '/skins/' + encodeURIComponent(skins[i]) + '.css');
}
skinUrls.push(LOADER_BASE_URL + '/prettify.css');
loadStylesheetsFallingBack(skinUrls);
var prettyPrint = (function () {
include("prettify.js");
return prettyPrint;
})();
// If this script is deferred or async and the document is already
// loaded we need to wait for language handlers to load before performing
// any autorun.
function onLangsLoaded() {
if (autorun) {
contentLoaded(
function () {
var n = callbacks.length;
var callback = n ? function () {
for (var i = 0; i < n; ++i) {
(function (i) {
setTimeout(
function () {
win['exports'][callbacks[i]].apply(win, arguments);
}, 0);
})(i);
}
} : void 0;
prettyPrint(callback);
});
}
}
checkPendingLanguages();
}());