Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 31beb55

Browse files
committed
Merge pull request #3593 from adobe/randy/match-gradient-in-list
Quick View: match gradient in list
2 parents a069792 + 9cc3e0f commit 31beb55

File tree

3 files changed

+83
-50
lines changed

3 files changed

+83
-50
lines changed

src/extensions/default/QuickView/main.js

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -169,52 +169,61 @@ define(function (require, exports, module) {
169169

170170

171171
// Color & gradient preview provider --------------------------------------
172-
172+
173173
function colorAndGradientPreviewProvider(editor, pos, token, line) {
174-
var cm = editor._codeMirror;
175-
176-
// Check for gradient
177-
var gradientRegEx = /-webkit-gradient\([^;]*;?|(-moz-|-ms-|-o-|-webkit-|\s)(linear-gradient\([^;]*);?|(-moz-|-ms-|-o-|-webkit-)(radial-gradient\([^;]*);?/,
178-
gradientMatch = line.match(gradientRegEx),
179-
prefix = "",
180-
colorValue;
181-
182-
if (gradientMatch) {
183-
if (gradientMatch[0].indexOf("@") !== -1) {
184-
// If the gradient match has "@" in it, it is most likely a less or
185-
// sass variable. Ignore it since it won't be displayed correctly.
186-
gradientMatch = null;
187174

188-
} else if (gradientMatch[0].indexOf("to ") !== -1) {
189-
// If the gradient match has "to " in it, it's most likely the new gradient
190-
// syntax which is not supported until Chrome 26, so we can't yet preview it
191-
gradientMatch = null;
192-
}
193-
}
194-
195-
// If it was a linear-gradient or radial-gradient variant, prefix with
196-
// "-webkit-" so it shows up correctly in Brackets.
197-
if (gradientMatch && gradientMatch[0].indexOf("-webkit-gradient") !== 0) {
198-
prefix = "-webkit-";
199-
}
200-
201-
// For prefixed gradients, use the non-prefixed value as the color value.
202-
// "-webkit-" will be added before this value
203-
if (gradientMatch) {
204-
if (gradientMatch[2]) {
205-
colorValue = gradientMatch[2]; // linear gradiant
206-
} else if (gradientMatch[4]) {
207-
colorValue = gradientMatch[4]; // radial gradiant
175+
// Check for gradient. -webkit-gradient() can have parens in parameters
176+
// nested 2 levels. Other gradients can only nest 1 level.
177+
var gradientRegEx = /-webkit-gradient\((?:[^\(]*?(?:\((?:[^\(]*?(?:\([^\)]*?\))*?)*?\))*?)*?\)|(?:(?:-moz-|-ms-|-o-|-webkit-|\s)(linear-gradient)|(?:-moz-|-ms-|-o-|-webkit-)(radial-gradient))(\((?:[^\)]*?(?:\([^\)]*?\))*?)*?\))/gi;
178+
179+
function execGradientMatch(line) {
180+
var gradientMatch = gradientRegEx.exec(line),
181+
prefix = "",
182+
colorValue;
183+
184+
if (gradientMatch) {
185+
if (gradientMatch[0].indexOf("@") !== -1) {
186+
// If the gradient match has "@" in it, it is most likely a less or
187+
// sass variable. Ignore it since it won't be displayed correctly.
188+
gradientMatch = null;
189+
190+
} else if (gradientMatch[0].indexOf("to ") !== -1) {
191+
// If the gradient match has "to " in it, it's most likely the new gradient
192+
// syntax which is not supported until Chrome 26, so we can't yet preview it
193+
gradientMatch = null;
194+
195+
} else {
196+
// If it was a linear-gradient or radial-gradient variant, prefix with
197+
// "-webkit-" so it shows up correctly in Brackets.
198+
if (gradientMatch[0].indexOf("-webkit-gradient") !== 0) {
199+
prefix = "-webkit-";
200+
}
201+
202+
// For prefixed gradients, use the non-prefixed value as the color value.
203+
// "-webkit-" will be added before this value
204+
if (gradientMatch[1]) {
205+
colorValue = gradientMatch[1] + gradientMatch[3]; // linear gradiant
206+
} else if (gradientMatch[2]) {
207+
colorValue = gradientMatch[2] + gradientMatch[3]; // radial gradiant
208+
}
209+
}
208210
}
211+
212+
return {
213+
match: gradientMatch,
214+
prefix: prefix,
215+
colorValue: colorValue
216+
};
209217
}
210218

211-
// Check for color
212219
var colorRegEx = /#[a-f0-9]{6}\b|#[a-f0-9]{3}\b|\brgb\(\s*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b\s*,\s*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b\s*,\s*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b\s*\)|\brgb\(\s*([0-9]{1,2}%|100%)\s*,\s*([0-9]{1,2}%|100%)\s*,\s*([0-9]{1,2}%|100%)\s*\)|\brgba\(\s*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b\s*,\s*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b\s*,\s*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b\s*,\s*(1|1\.0|0|0?\.[0-9]{1,3})\s*\)|\brgba\(\s*([0-9]{1,2}%|100%)\s*,\s*([0-9]{1,2}%|100%)\s*,\s*([0-9]{1,2}%|100%)\s*,\s*(1|1\.0|0|0?\.[0-9]{1,3})\s*\)|\bhsl\(\s*([0-9]{1,3})\b\s*,\s*([0-9]{1,2}|100)\b%\s*,\s*([0-9]{1,2}|100)\b%\s*\)|\bhsla\(\s*([0-9]{1,3})\b\s*,\s*([0-9]{1,2}|100)\b%\s*,\s*([0-9]{1,2}|100)\b%\s*,\s*(1|1\.0|0|0?\.[0-9]{1,3})\s*\)|\baliceblue\b|\bantiquewhite\b|\baqua\b|\baquamarine\b|\bazure\b|\bbeige\b|\bbisque\b|\bblack\b|\bblanchedalmond\b|\bblue\b|\bblueviolet\b|\bbrown\b|\bburlywood\b|\bcadetblue\b|\bchartreuse\b|\bchocolate\b|\bcoral\b|\bcornflowerblue\b|\bcornsilk\b|\bcrimson\b|\bcyan\b|\bdarkblue\b|\bdarkcyan\b|\bdarkgoldenrod\b|\bdarkgray\b|\bdarkgreen\b|\bdarkgrey\b|\bdarkkhaki\b|\bdarkmagenta\b|\bdarkolivegreen\b|\bdarkorange\b|\bdarkorchid\b|\bdarkred\b|\bdarksalmon\b|\bdarkseagreen\b|\bdarkslateblue\b|\bdarkslategray\b|\bdarkslategrey\b|\bdarkturquoise\b|\bdarkviolet\b|\bdeeppink\b|\bdeepskyblue\b|\bdimgray\b|\bdimgrey\b|\bdodgerblue\b|\bfirebrick\b|\bfloralwhite\b|\bforestgreen\b|\bfuchsia\b|\bgainsboro\b|\bghostwhite\b|\bgold\b|\bgoldenrod\b|\bgray\b|\bgreen\b|\bgreenyellow\b|\bgrey\b|\bhoneydew\b|\bhotpink\b|\bindianred\b|\bindigo\b|\bivory\b|\bkhaki\b|\blavender\b|\blavenderblush\b|\blawngreen\b|\blemonchiffon\b|\blightblue\b|\blightcoral\b|\blightcyan\b|\blightgoldenrodyellow\b|\blightgray\b|\blightgreen\b|\blightgrey\b|\blightpink\b|\blightsalmon\b|\blightseagreen\b|\blightskyblue\b|\blightslategray\b|\blightslategrey\b|\blightsteelblue\b|\blightyellow\b|\blime\b|\blimegreen\b|\blinen\b|\bmagenta\b|\bmaroon\b|\bmediumaquamarine\b|\bmediumblue\b|\bmediumorchid\b|\bmediumpurple\b|\bmediumseagreen\b|\bmediumslateblue\b|\bmediumspringgreen\b|\bmediumturquoise\b|\bmediumvioletred\b|\bmidnightblue\b|\bmintcream\b|\bmistyrose\b|\bmoccasin\b|\bnavajowhite\b|\bnavy\b|\boldlace\b|\bolive\b|\bolivedrab\b|\borange\b|\borangered\b|\borchid\b|\bpalegoldenrod\b|\bpalegreen\b|\bpaleturquoise\b|\bpalevioletred\b|\bpapayawhip\b|\bpeachpuff\b|\bperu\b|\bpink\b|\bplum\b|\bpowderblue\b|\bpurple\b|\bred\b|\brosybrown\b|\broyalblue\b|\bsaddlebrown\b|\bsalmon\b|\bsandybrown\b|\bseagreen\b|\bseashell\b|\bsienna\b|\bsilver\b|\bskyblue\b|\bslateblue\b|\bslategray\b|\bslategrey\b|\bsnow\b|\bspringgreen\b|\bsteelblue\b|\btan\b|\bteal\b|\bthistle\b|\btomato\b|\bturquoise\b|\bviolet\b|\bwheat\b|\bwhite\b|\bwhitesmoke\b|\byellow\b|\byellowgreen\b/gi,
213-
match = gradientMatch || colorRegEx.exec(line);
220+
gradientMatch = execGradientMatch(line),
221+
match = gradientMatch.match || colorRegEx.exec(line),
222+
cm = editor._codeMirror;
214223

215224
while (match) {
216225
if (pos.ch >= match.index && pos.ch <= match.index + match[0].length) {
217-
var previewCSS = prefix + (colorValue || match[0]);
226+
var previewCSS = gradientMatch.prefix + (gradientMatch.colorValue || match[0]);
218227
var preview = "<div class='color-swatch' style='background:" + previewCSS + "'>" +
219228
"</div>";
220229
var startPos = {line: pos.line, ch: match.index},
@@ -234,7 +243,12 @@ define(function (require, exports, module) {
234243
_previewCSS: previewCSS
235244
};
236245
}
237-
match = colorRegEx.exec(line);
246+
247+
// Get next match
248+
if (gradientMatch.match) {
249+
gradientMatch = execGradientMatch(line);
250+
}
251+
match = gradientMatch.match || colorRegEx.exec(line);
238252
}
239253

240254
return null;

src/extensions/default/QuickView/unittest-files/test.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ background: -ms-linear-gradient(top, #d2dfed 0%,#c8d7eb 26%,#bed0ea 51%,#a6c0e3
8888
/* From css3please.com */
8989
.box_gradient {
9090
background-color: #333;
91-
background-image: -webkit-gradient(linear, left top, left bottom, from(#333), to(#CCC)); /* Chrome, Safari 4+ */
91+
background-image: -webkit-gradient(linear, left top, left bottom, from(rgb(51,51,51)), to(rgb(204,204,204))); /* Chrome, Safari 4+ */
9292
background-image: -webkit-linear-gradient(top, #333, #CCC); /* Chrome 10-25, iOS 5+, Safari 5.1+ */
9393
background-image: -moz-linear-gradient(top, #333, #CCC); /* Firefox 3.6-15 */
9494
background-image: -o-linear-gradient(top, #333, #CCC); /* Opera 11.10-12.00 */
@@ -130,6 +130,13 @@ background: -ms-linear-gradient(top, #d2dfed 0%,#c8d7eb 26%,#bed0ea 51%,#a6c0e3
130130
background-image: repeating-radial-gradient(red, blue 20px, red 40px);
131131
}
132132

133+
/* lea.verou.me/css3patterns */
134+
.gradient_pattern {
135+
background:
136+
linear-gradient(63deg, #999 23%, transparent 23%) 7px 0,
137+
linear-gradient(63deg, transparent 74%, #999 78%), linear-gradient(63deg, transparent 0%, #999 38%, #999 58%, transparent 100%), #444;
138+
}
139+
133140
.images {
134141
background: url(img/grabber_color-well.png);
135142
background: url("img/Color.png");

src/extensions/default/QuickView/unittests.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,15 @@ define(function (require, exports, module) {
183183
it("Should show linear gradient preview for those with vendor prefix", function () {
184184
runs(function () {
185185
var expectedGradient1 = "-webkit-linear-gradient(top, #d2dfed 0%, #c8d7eb 26%, #bed0ea 51%, #a6c0e3 51%, #afc7e8 62%, #bad0ef 75%, #99b5db 88%, #799bc8 100%)",
186-
expectedGradient2 = "-webkit-gradient(linear, left top, left bottom, color-stop(0%,#d2dfed), color-stop(26%,#c8d7eb), color-stop(51%,#bed0ea), color-stop(51%,#a6c0e3), color-stop(62%,#afc7e8), color-stop(75%,#bad0ef), color-stop(88%,#99b5db), color-stop(100%,#799bc8));",
187-
expectedGradient3 = "-webkit-linear-gradient(top, #d2dfed 0%,#c8d7eb 26%,#bed0ea 51%,#a6c0e3 51%,#afc7e8 62%,#bad0ef 75%,#99b5db 88%,#799bc8 100%)";
186+
expectedGradient2 = "-webkit-gradient(linear, left top, left bottom, color-stop(0%,#d2dfed), color-stop(26%,#c8d7eb), color-stop(51%,#bed0ea), color-stop(51%,#a6c0e3), color-stop(62%,#afc7e8), color-stop(75%,#bad0ef), color-stop(88%,#99b5db), color-stop(100%,#799bc8))",
187+
expectedGradient3 = "-webkit-linear-gradient(top, #d2dfed 0%,#c8d7eb 26%,#bed0ea 51%,#a6c0e3 51%,#afc7e8 62%,#bad0ef 75%,#99b5db 88%,#799bc8 100%)",
188+
expectedGradient4 = "-webkit-gradient(linear, left top, left bottom, from(rgb(51,51,51)), to(rgb(204,204,204)))";
188189
checkGradientAtPos(expectedGradient1, 80, 36); // -moz- prefix gets stripped
189190
checkGradientAtPos(expectedGradient2, 81, 36); // Old webkit syntax
190191
checkGradientAtPos(expectedGradient3, 82, 36); // -webkit- prefix gets stripped
191192
checkGradientAtPos(expectedGradient3, 83, 36); // -o- prefix gets stripped
192193
checkGradientAtPos(expectedGradient3, 84, 36); // -ms- prefix gets stripped
194+
checkGradientAtPos(expectedGradient4, 90, 36); // test parameters with 2 levels of nested parens
193195
});
194196
});
195197

@@ -213,7 +215,7 @@ define(function (require, exports, module) {
213215

214216
it("Should show radial gradient preview for those with vendor prefix syntax", function () {
215217
runs(function () {
216-
var expectedGradient1 = "-webkit-gradient(radial, center center, 0, center center, 141, from(black), to(white), color-stop(25%, blue), color-stop(40%, green), color-stop(60%, red), color-stop(80%, purple));",
218+
var expectedGradient1 = "-webkit-gradient(radial, center center, 0, center center, 141, from(black), to(white), color-stop(25%, blue), color-stop(40%, green), color-stop(60%, red), color-stop(80%, purple))",
217219
expectedGradient2 = "-webkit-radial-gradient(center center, circle contain, black 0%, blue 25%, green 40%, red 60%, purple 80%, white 100%)";
218220
checkGradientAtPos(expectedGradient1, 110, 93); // old webkit syntax
219221
checkGradientAtPos(expectedGradient2, 111, 36); // -webkit- prefix preserved
@@ -261,6 +263,16 @@ define(function (require, exports, module) {
261263
});
262264
});
263265

266+
it("Should show comma-separated gradients", function () {
267+
runs(function () {
268+
// line ending in comma
269+
checkGradientAtPos("-webkit-linear-gradient(63deg, #999 23%, transparent 23%)", 135, 50);
270+
271+
// multiple gradients on a line
272+
checkGradientAtPos("-webkit-linear-gradient(63deg, transparent 74%, #999 78%)", 136, 50);
273+
checkGradientAtPos("-webkit-linear-gradient(63deg, transparent 0%, #999 38%, #999 58%, transparent 100%)", 136, 100);
274+
});
275+
});
264276
});
265277

266278
describe("Quick view display", function () {
@@ -346,24 +358,24 @@ define(function (require, exports, module) {
346358
describe("Quick view images", function () {
347359
it("Should show image preview for file path inside url()", function () {
348360
runs(function () {
349-
checkImagePathAtPos("img/grabber_color-well.png", 133, 26);
350-
checkImagePathAtPos("img/Color.png", 134, 26);
351-
checkImagePathAtPos("img/DancingPeaks.gif", 135, 26);
352-
checkImagePathAtPos("img/Example.svg", 136, 26);
361+
checkImagePathAtPos("img/grabber_color-well.png", 140, 26);
362+
checkImagePathAtPos("img/Color.png", 141, 26);
363+
checkImagePathAtPos("img/DancingPeaks.gif", 142, 26);
364+
checkImagePathAtPos("img/Example.svg", 143, 26);
353365
});
354366
});
355367

356368
it("Should show image preview for urls with http/https", function () {
357369
runs(function () {
358-
checkImagePathAtPos("https://raw.github.com/gruehle/HoverPreview/master/screenshots/Image.png", 138, 26);
370+
checkImagePathAtPos("https://raw.github.com/gruehle/HoverPreview/master/screenshots/Image.png", 145, 26);
359371
});
360372
});
361373

362374
it("Should show image preview for file path inside single or double quotes", function () {
363375
runs(function () {
364-
checkImagePathAtPos("img/med_hero.jpg", 140, 26);
365-
checkImagePathAtPos("img/Gradient.png", 141, 26);
366-
checkImagePathAtPos("Lake_mapourika_NZ.jpeg", 142, 26);
376+
checkImagePathAtPos("img/med_hero.jpg", 147, 26);
377+
checkImagePathAtPos("img/Gradient.png", 148, 26);
378+
checkImagePathAtPos("Lake_mapourika_NZ.jpeg", 149, 26);
367379
});
368380

369381
// This must be in the last spec in the suite.

0 commit comments

Comments
 (0)