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

Side by Side Diff: lib/src/prism/plugins/previewer-gradient/prism-previewer-gradient.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
3 if (
4 typeof self !== 'undefined' && !self.Prism ||
5 typeof global !== 'undefined' && !global.Prism
6 ) {
7 return;
8 }
9
10 var languages = {
11 'css': true,
12 'less': true,
13 'sass': [
14 {
15 lang: 'sass',
16 before: 'punctuation',
17 inside: 'inside',
18 root: Prism.languages.sass && Prism.languages.sa ss['variable-line']
19 },
20 {
21 lang: 'sass',
22 before: 'punctuation',
23 inside: 'inside',
24 root: Prism.languages.sass && Prism.languages.sa ss['property-line']
25 }
26 ],
27 'scss': true,
28 'stylus': [
29 {
30 lang: 'stylus',
31 before: 'func',
32 inside: 'rest',
33 root: Prism.languages.stylus && Prism.languages. stylus['property-declaration'].inside
34 },
35 {
36 lang: 'stylus',
37 before: 'func',
38 inside: 'rest',
39 root: Prism.languages.stylus && Prism.languages. stylus['variable-declaration'].inside
40 }
41 ]
42 };
43
44 Prism.hooks.add('before-highlight', function (env) {
45 if (env.language && languages[env.language] && !languages[env.la nguage].initialized) {
46 var lang = languages[env.language];
47 if (Prism.util.type(lang) !== 'Array') {
48 lang = [lang];
49 }
50 lang.forEach(function(lang) {
51 var before, inside, root, skip;
52 if (lang === true) {
53 // Insert before color previewer if it e xists
54 before = Prism.plugins.Previewer && Pris m.plugins.Previewer.byType['color'] ? 'color' : 'important';
55 inside = env.language;
56 lang = env.language;
57 } else {
58 before = lang.before || 'important';
59 inside = lang.inside || lang.lang;
60 root = lang.root || Prism.languages;
61 skip = lang.skip;
62 lang = env.language;
63 }
64
65 if (!skip && Prism.languages[lang]) {
66 Prism.languages.insertBefore(inside, bef ore, {
67 'gradient': {
68 pattern: /(?:\b|\B-[a-z] {1,10}-)(?:repeating-)?(?:linear|radial)-gradient\((?:(?:rgb|hsl)a?\(.+?\)|[^\)] )+\)/gi,
69 inside: {
70 'function': /[\w -]+(?=\()/,
71 'punctuation': / [(),]/
72 }
73 }
74 }, root);
75 env.grammar = Prism.languages[lang];
76
77 languages[env.language] = {initialized: true};
78 }
79 });
80 }
81 });
82
83 // Stores already processed gradients so that we don't
84 // make the conversion every time the previewer is shown
85 var cache = {};
86
87 /**
88 * Returns a W3C-valid linear gradient
89 * @param {string} prefix Vendor prefix if any ("-moz-", "-webkit-", etc .)
90 * @param {string} func Gradient function name ("linear-gradient")
91 * @param {string[]} values Array of the gradient function parameters ([ "0deg", "red 0%", "blue 100%"])
92 */
93 var convertToW3CLinearGradient = function(prefix, func, values) {
94 // Default value for angle
95 var angle = '180deg';
96
97 if (/^(?:-?\d*\.?\d+(?:deg|rad)|to\b|top|right|bottom|left)/.tes t(values[0])) {
98 angle = values.shift();
99 if (angle.indexOf('to ') < 0) {
100 // Angle uses old keywords
101 // W3C syntax uses "to" + opposite keywords
102 if (angle.indexOf('top') >= 0) {
103 if (angle.indexOf('left') >= 0) {
104 angle = 'to bottom right';
105 } else if (angle.indexOf('right') >= 0) {
106 angle = 'to bottom left';
107 } else {
108 angle = 'to bottom';
109 }
110 } else if (angle.indexOf('bottom') >= 0) {
111 if (angle.indexOf('left') >= 0) {
112 angle = 'to top right';
113 } else if (angle.indexOf('right') >= 0) {
114 angle = 'to top left';
115 } else {
116 angle = 'to top';
117 }
118 } else if (angle.indexOf('left') >= 0) {
119 angle = 'to right';
120 } else if (angle.indexOf('right') >= 0) {
121 angle = 'to left';
122 } else if (prefix) {
123 // Angle is shifted by 90deg in prefixed gradients
124 if (angle.indexOf('deg') >= 0) {
125 angle = (90 - parseFloat(angle)) + 'deg';
126 } else if (angle.indexOf('rad') >= 0) {
127 angle = (Math.PI / 2 - parseFloa t(angle)) + 'rad';
128 }
129 }
130 }
131 }
132
133 return func + '(' + angle + ',' + values.join(',') + ')';
134 };
135
136 /**
137 * Returns a W3C-valid radial gradient
138 * @param {string} prefix Vendor prefix if any ("-moz-", "-webkit-", etc .)
139 * @param {string} func Gradient function name ("linear-gradient")
140 * @param {string[]} values Array of the gradient function parameters ([ "0deg", "red 0%", "blue 100%"])
141 */
142 var convertToW3CRadialGradient = function(prefix, func, values) {
143 if (values[0].indexOf('at') < 0) {
144 // Looks like old syntax
145
146 // Default values
147 var position = 'center';
148 var shape = 'ellipse';
149 var size = 'farthest-corner';
150
151 if (/\bcenter|top|right|bottom|left\b|^\d+/.test(values[ 0])) {
152 // Found a position
153 // Remove angle value, if any
154 position = values.shift().replace(/\s*-?\d+(?:ra d|deg)\s*/, '');
155 }
156 if (/\bcircle|ellipse|closest|farthest|contain|cover\b/. test(values[0])) {
157 // Found a shape and/or size
158 var shapeSizeParts = values.shift().split(/\s+/) ;
159 if (shapeSizeParts[0] && (shapeSizeParts[0] === 'circle' || shapeSizeParts[0] === 'ellipse')) {
160 shape = shapeSizeParts.shift();
161 }
162 if (shapeSizeParts[0]) {
163 size = shapeSizeParts.shift();
164 }
165
166 // Old keywords are converted to their synonyms
167 if (size === 'cover') {
168 size = 'farthest-corner';
169 } else if (size === 'contain') {
170 size = 'clothest-side';
171 }
172 }
173
174 return func + '(' + shape + ' ' + size + ' at ' + positi on + ',' + values.join(',') + ')';
175 }
176 return func + '(' + values.join(',') + ')';
177 };
178
179 /**
180 * Converts a gradient to a W3C-valid one
181 * Does not support old webkit syntax (-webkit-gradient(linear...) and - webkit-gradient(radial...))
182 * @param {string} gradient The CSS gradient
183 */
184 var convertToW3CGradient = function(gradient) {
185 if (cache[gradient]) {
186 return cache[gradient];
187 }
188 var parts = gradient.match(/^(\b|\B-[a-z]{1,10}-)((?:repeating-) ?(?:linear|radial)-gradient)/);
189 // "", "-moz-", etc.
190 var prefix = parts && parts[1];
191 // "linear-gradient", "radial-gradient", etc.
192 var func = parts && parts[2];
193
194 var values = gradient.replace(/^(?:\b|\B-[a-z]{1,10}-)(?:repeati ng-)?(?:linear|radial)-gradient\(|\)$/g, '').split(/\s*,\s*/);
195
196 if (func.indexOf('linear') >= 0) {
197 return cache[gradient] = convertToW3CLinearGradient(pref ix, func, values);
198 } else if (func.indexOf('radial') >= 0) {
199 return cache[gradient] = convertToW3CRadialGradient(pref ix, func, values);
200 }
201 return cache[gradient] = func + '(' + values.join(',') + ')';
202 };
203
204
205
206 if (Prism.plugins.Previewer) {
207 new Prism.plugins.Previewer('gradient', function(value) {
208 this.firstChild.style.backgroundImage = '';
209 this.firstChild.style.backgroundImage = convertToW3CGrad ient(value);
210 return !!this.firstChild.style.backgroundImage;
211 }, '*', function () {
212 this._elt.innerHTML = '<div></div>';
213 });
214 }
215
216 }());
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698