Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(465)

Side by Side Diff: lib/src/prism/plugins/jsonp-highlight/prism-jsonp-highlight.js

Issue 1418513006: update elements and fix some bugs (Closed) Base URL: git@github.com:dart-lang/polymer_elements.git@master
Patch Set: code review updates Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 (function() {
2 if ( !self.Prism || !self.document || !document.querySelectorAll ) retur n;
3
4 var adapters = [];
5 function registerAdapter(adapter) {
6 if (typeof adapter === "function" && !getAdapter(adapter)) {
7 adapters.push(adapter);
8 }
9 }
10 function getAdapter(adapter) {
11 if (typeof adapter === "function") {
12 return adapters.filter(function(fn) { return fn.valueOf( ) === adapter.valueOf()})[0];
13 }
14 else if (typeof adapter === "string" && adapter.length > 0) {
15 return adapters.filter(function(fn) { return fn.name === adapter})[0];
16 }
17 return null;
18 }
19 function removeAdapter(adapter) {
20 if (typeof adapter === "string")
21 adapter = getAdapter(adapter);
22 if (typeof adapter === "function") {
23 var index = adapters.indexOf(adapter);
24 if (index >=0) {
25 adapters.splice(index,1);
26 }
27 }
28 }
29
30 Prism.plugins.jsonphighlight = {
31 registerAdapter: registerAdapter,
32 removeAdapter: removeAdapter,
33 highlight: highlight
34 };
35 registerAdapter(function github(rsp, el) {
36 if ( rsp && rsp.meta && rsp.data ) {
37 if ( rsp.meta.status && rsp.meta.status >= 400 ) {
38 return "Error: " + ( rsp.data.message || rsp.met a.status );
39 }
40 else if ( typeof(rsp.data.content) === "string" ) {
41 return typeof(atob) === "function"
42 ? atob(rsp.data.content.replace(/\s/g, " "))
43 : "Your browser cannot decode base64";
44 }
45 }
46 return null;
47 });
48 registerAdapter(function gist(rsp, el) {
49 if ( rsp && rsp.meta && rsp.data && rsp.data.files ) {
50 if ( rsp.meta.status && rsp.meta.status >= 400 ) {
51 return "Error: " + ( rsp.data.message || rsp.met a.status );
52 }
53 else {
54 var filename = el.getAttribute("data-filename");
55 if (filename == null) {
56 // Maybe in the future we can somehow re nder all files
57 // But the standard <script> include for gists does that nicely already,
58 // so that might be getting beyond the s cope of this plugin
59 for (var key in rsp.data.files) {
60 if (rsp.data.files.hasOwnPropert y(key)) {
61 filename = key;
62 break;
63 }
64 }
65 }
66 if (rsp.data.files[filename] !== undefined) {
67 return rsp.data.files[filename].content;
68 }
69 else {
70 return "Error: unknown or missing gist f ile " + filename;
71 }
72 }
73 }
74 return null;
75 });
76 registerAdapter(function bitbucket(rsp, el) {
77 return rsp && rsp.node && typeof(rsp.data) === "string"
78 ? rsp.data
79 : null;
80 });
81
82 var jsonpcb = 0,
83 loadstr = "Loading…";
84
85 function highlight() {
86 Array.prototype.slice.call(document.querySelectorAll("pre[data-j sonp]")).forEach(function(pre) {
87 pre.textContent = "";
88
89 var code = document.createElement("code");
90 code.textContent = loadstr;
91 pre.appendChild(code);
92
93 var adapterfn = pre.getAttribute("data-adapter");
94 var adapter = null;
95 if ( adapterfn ) {
96 if ( typeof(window[adapterfn]) === "function" ) {
97 adapter = window[adapterfn];
98 }
99 else {
100 code.textContent = "JSONP adapter functi on '" + adapterfn + "' doesn't exist";
101 return;
102 }
103 }
104
105 var cb = "prismjsonp" + ( jsonpcb++ );
106
107 var uri = document.createElement("a");
108 var src = uri.href = pre.getAttribute("data-jsonp");
109 uri.href += ( uri.search ? "&" : "?" ) + ( pre.getAttrib ute("data-callback") || "callback" ) + "=" + cb;
110
111 var timeout = setTimeout(function() {
112 // we could clean up window[cb], but if the requ est finally succeeds, keeping it around is a good thing
113 if ( code.textContent === loadstr )
114 code.textContent = "Timeout loading '" + src + "'";
115 }, 5000);
116
117 var script = document.createElement("script");
118 script.src = uri.href;
119
120 window[cb] = function(rsp) {
121 document.head.removeChild(script);
122 clearTimeout(timeout);
123 delete window[cb];
124
125 var data = "";
126
127 if ( adapter ) {
128 data = adapter(rsp, pre);
129 }
130 else {
131 for ( var p in adapters ) {
132 data = adapters[p](rsp, pre);
133 if ( data !== null ) break;
134 }
135 }
136
137 if (data === null) {
138 code.textContent = "Cannot parse respons e (perhaps you need an adapter function?)";
139 }
140 else {
141 code.textContent = data;
142 Prism.highlightElement(code);
143 }
144 };
145
146 document.head.appendChild(script);
147 });
148 }
149
150 highlight();
151 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698