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

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: add missing files Created 3 years, 9 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 * Strips CSS rules/attributes from the file in-place.
10 * @param {string} filename The filename to process.
11 */
12 function processFile(filename) {
13 var fileContents = fs.readFileSync(filename, {'encoding': 'utf-8'});
14 var lines = fileContents.split('\n');
15 var processedFileContents = reconstructFile(lines, findLinesToRemove(lines));
16 fs.writeFileSync(filename, processedFileContents);
17 console.log('processedFile', filename);
18 }
19 /**
20 * @return {!Array<string>} lines
21 * @return {!Array<number>} The lines numbers that need to be removed in
22 * ascinding order.
23 */
24 function findLinesToRemove(lines) {
25 // Regex used to detect CSS attributes or even entire CSS rules to be removed.
26 var prefixRegex = new RegExp(/-webkit-|-moz-|-ms-/);
27 // Regexes used to remove entire CSS rules that can span multiple lines.
28 var cssRulesToRemove = [
29 new RegExp(/@-webkit-keyframes/),
30 new RegExp(/-moz-placeholder/),
31 new RegExp(/-ms-input-placeholder/),
32 ];
33 // Regexes used to preserve a CSS rule that can span multiple lines.
34 var cssRulesToPreserve = [
35 new RegExp(/-webkit-appearance/),
36 new RegExp(/-webkit-font-smoothing/),
37 new RegExp(/-webkit-overflow-scrolling/),
38 new RegExp(/-webkit-tap-highlight/),
39 ];
40 var openingBracketRegex = new RegExp(/{/);
41 var closingBracketRegex = new RegExp(/}/);
42 // Whether currently visited line is within a CSS rule that should be removed.
43 var inCssRuleToRemove = false;
44 // Whether currently visited line is within a CSS rule that should be
45 // preserved.
46 var inCssRuleToPreserve = false;
47 // Current number of nested-levels with respect to curly braces.
48 var levelCounter = 0;
49 return lines.reduce(function(soFar, line, index) {
50 if (inCssRuleToRemove || inCssRuleToPreserve) {
51 if (openingBracketRegex.test(line)) {
52 levelCounter++;
53 }
54 if (closingBracketRegex.test(line)) {
55 levelCounter--;
56 }
57 if (!inCssRuleToPreserve) {
58 soFar.push(index);
59 }
60 if (levelCounter == 0) {
61 inCssRuleToRemove = false;
62 inCssRuleToPreserve = false;
63 }
64 } else if (prefixRegex.test(line)) {
65 inCssRuleToRemove = cssRulesToRemove.some(
66 function(regex) { return regex.test(line); });
67 inCssRuleToPreserve = cssRulesToPreserve.some(
68 function(regex) { return regex.test(line); });
69 if (inCssRuleToRemove || inCssRuleToPreserve) {
70 levelCounter++;
71 if (inCssRuleToRemove) {
72 soFar.push(index);
73 }
74 } else {
75 soFar.push(index);
76 }
77 }
78 return soFar;
79 }, []);
80 }
81 /**
82 * Generates the final processed file.
83 * @param {!Array<string>} lines The lines to be process.
84 * @param {!Array<number>} linesToRemove Indices of the lines to be removed in
85 * ascending order.
86 * @return {string} The contents of the processed file.
87 */
88 function reconstructFile(lines, linesToRemove) {
89 var matchWhiteSpaceRegex = /\s+/;
90 // Process line numbers in descinding order, such that the array can be
91 // modified in-place.
92 linesToRemove.reverse().forEach(function(lineIndex) {
93 lines.splice(lineIndex, 1);
94 });
95 return lines.join('\n');
96 }
97 var srcDir = path.resolve(path.join(__dirname, 'components-chromium'));
98 var htmlFiles = glob(srcDir + '/**/*.html');
99 var cssFiles = glob(srcDir + '/**/*.css');
100 htmlFiles.concat(cssFiles).forEach(processFile);
101 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