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

Side by Side Diff: third_party/polymer/v1_0/css_strip_prefixes.js

Issue 1633603004: Add script to remove prefixed CSS from third_party/polymer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Process CSS files too. Created 4 years, 10 months 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
« no previous file with comments | « third_party/polymer/v1_0/components-chromium/paper-tooltip/paper-tooltip.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /**
2 * @fileoverview Script to remove CSS prefixed rules that don't apply to
3 * Chromium.
4 */
5 var fs = require('fs');
6 var glob = require('glob').sync;
7 var path = require('path');
8
9
10 /**
11 * Strips CSS rules/attributes from the file in-place.
12 * @param {string} filename The filename to process.
13 */
14 function processFile(filename) {
15 var fileContents = fs.readFileSync(filename, {'encoding': 'utf-8'});
16 var lines = fileContents.split('\n');
17 var processedFileContents = reconstructFile(lines, findLinesToRemove(lines));
18 fs.writeFileSync(filename, processedFileContents);
19 console.log('processedFile', filename);
20 }
21
22
23 /**
24 * @return {!Array<string>} lines
25 * @return {!Array<number>} The lines numbers that need to be removed in
26 * ascinding order.
27 */
28 function findLinesToRemove(lines) {
29 // Regex used to detect CSS attributes or even entire CSS rules to be removed.
30 var prefixRegex = new RegExp(/-webkit-|-moz-|-ms-/);
Dan Beam 2016/01/26 17:31:45 new RegExp(/regexLiteral/) makes very little sense
31
32 // Regexes used to remove entire CSS rules that can span multiple lines.
33 var cssRulesToRemove = [
34 new RegExp(/@-webkit-keyframes/),
Dan Beam 2016/01/26 17:31:45 why are you removing this?
35 new RegExp(/-moz-placeholder/),
36 new RegExp(/-ms-input-placeholder/),
37 ];
38
39 // Regexes used to preserve a CSS rule that can span multiple lines.
40 var cssRulesToPreserve = [
41 new RegExp(/-webkit-input-placeholder/),
42 ];
43
44 var openingBracketRegex = new RegExp(/{/);
45 var closingBracketRegex = new RegExp(/}/);
46
47 // Whether currently visited line is within a CSS rule that should be removed.
48 var inCssRuleToRemove = false;
49 // Whether currently visited line is within a CSS rule that should be
50 // preserved.
51 var inCssRuleToPreserve = false;
52 // Current number of nested-levels with respect to curly braces.
53 var levelCounter = 0;
54
55 return lines.reduce(function(soFar, line, index) {
56 if (inCssRuleToRemove || inCssRuleToPreserve) {
57 if (openingBracketRegex.test(line)) {
58 levelCounter++;
59 }
60 if (closingBracketRegex.test(line)) {
61 levelCounter--;
62 }
63
64 if (!inCssRuleToPreserve) {
65 soFar.push(index);
66 }
67
68 if (levelCounter == 0) {
69 inCssRuleToRemove = false;
70 inCssRuleToPreserve = false;
71 }
72 } else if (prefixRegex.test(line)) {
73 inCssRuleToRemove = cssRulesToRemove.some(
74 function(regex) { return regex.test(line); });
75 inCssRuleToPreserve = cssRulesToPreserve.some(
76 function(regex) { return regex.test(line); });
77
78 if (inCssRuleToRemove || inCssRuleToPreserve) {
79 levelCounter++;
80 if (inCssRuleToRemove) {
81 soFar.push(index);
82 }
83 } else {
84 soFar.push(index);
85 }
86 }
87 return soFar;
88 }, []);
Dan Beam 2016/01/26 17:31:45 I have no idea what this code does, frankly, but t
89 }
90
91
92 /**
93 * Generates the final processed file.
94 * @param {!Array<string>} lines The lines to be process.
95 * @param {!Array<number>} linesToRemove Indices of the lines to be removed in
96 * ascending order.
97 * @return {string} The contents of the processed file.
98 */
99 function reconstructFile(lines, linesToRemove) {
100 var matchWhiteSpaceRegex = /\s+/;
101 // Process line numbers in descinding order, such that the array can be
102 // modified in-place.
103 linesToRemove.reverse().forEach(function(lineIndex) {
104 lines.splice(lineIndex, 1);
105 });
106 return lines.join('\n');
107 }
108
109
110 var srcDir = path.resolve(path.join(__dirname, 'components-chromium'));
111 var htmlFiles = glob(srcDir + '/**/*.html');
112 var cssFiles = glob(srcDir + '/**/*.css');
113 htmlFiles.concat(cssFiles).forEach(processFile);
114 console.log('DONE');
OLDNEW
« no previous file with comments | « third_party/polymer/v1_0/components-chromium/paper-tooltip/paper-tooltip.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698