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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/polymer/v1_0/css_strip_prefixes.js
diff --git a/third_party/polymer/v1_0/css_strip_prefixes.js b/third_party/polymer/v1_0/css_strip_prefixes.js
new file mode 100644
index 0000000000000000000000000000000000000000..c8c30fe4dcc68e1b3ed6ea2a5b00db10f43243aa
--- /dev/null
+++ b/third_party/polymer/v1_0/css_strip_prefixes.js
@@ -0,0 +1,101 @@
+/**
+ * @fileoverview Script to remove CSS prefixed rules that don't apply to
+ * Chromium.
+ */
+var fs = require('fs');
+var glob = require('glob').sync;
+var path = require('path');
+/**
+ * Strips CSS rules/attributes from the file in-place.
+ * @param {string} filename The filename to process.
+ */
+function processFile(filename) {
+ var fileContents = fs.readFileSync(filename, {'encoding': 'utf-8'});
+ var lines = fileContents.split('\n');
+ var processedFileContents = reconstructFile(lines, findLinesToRemove(lines));
+ fs.writeFileSync(filename, processedFileContents);
+ console.log('processedFile', filename);
+}
+/**
+ * @return {!Array<string>} lines
+ * @return {!Array<number>} The lines numbers that need to be removed in
+ * ascinding order.
+ */
+function findLinesToRemove(lines) {
+ // Regex used to detect CSS attributes or even entire CSS rules to be removed.
+ var prefixRegex = new RegExp(/-webkit-|-moz-|-ms-/);
+ // Regexes used to remove entire CSS rules that can span multiple lines.
+ var cssRulesToRemove = [
+ new RegExp(/@-webkit-keyframes/),
+ new RegExp(/-moz-placeholder/),
+ new RegExp(/-ms-input-placeholder/),
+ ];
+ // Regexes used to preserve a CSS rule that can span multiple lines.
+ var cssRulesToPreserve = [
+ new RegExp(/-webkit-appearance/),
+ new RegExp(/-webkit-font-smoothing/),
+ new RegExp(/-webkit-overflow-scrolling/),
+ new RegExp(/-webkit-tap-highlight/),
+ ];
+ var openingBracketRegex = new RegExp(/{/);
+ var closingBracketRegex = new RegExp(/}/);
+ // Whether currently visited line is within a CSS rule that should be removed.
+ var inCssRuleToRemove = false;
+ // Whether currently visited line is within a CSS rule that should be
+ // preserved.
+ var inCssRuleToPreserve = false;
+ // Current number of nested-levels with respect to curly braces.
+ var levelCounter = 0;
+ return lines.reduce(function(soFar, line, index) {
+ if (inCssRuleToRemove || inCssRuleToPreserve) {
+ if (openingBracketRegex.test(line)) {
+ levelCounter++;
+ }
+ if (closingBracketRegex.test(line)) {
+ levelCounter--;
+ }
+ if (!inCssRuleToPreserve) {
+ soFar.push(index);
+ }
+ if (levelCounter == 0) {
+ inCssRuleToRemove = false;
+ inCssRuleToPreserve = false;
+ }
+ } else if (prefixRegex.test(line)) {
+ inCssRuleToRemove = cssRulesToRemove.some(
+ function(regex) { return regex.test(line); });
+ inCssRuleToPreserve = cssRulesToPreserve.some(
+ function(regex) { return regex.test(line); });
+ if (inCssRuleToRemove || inCssRuleToPreserve) {
+ levelCounter++;
+ if (inCssRuleToRemove) {
+ soFar.push(index);
+ }
+ } else {
+ soFar.push(index);
+ }
+ }
+ return soFar;
+ }, []);
+}
+/**
+ * Generates the final processed file.
+ * @param {!Array<string>} lines The lines to be process.
+ * @param {!Array<number>} linesToRemove Indices of the lines to be removed in
+ * ascending order.
+ * @return {string} The contents of the processed file.
+ */
+function reconstructFile(lines, linesToRemove) {
+ var matchWhiteSpaceRegex = /\s+/;
+ // Process line numbers in descinding order, such that the array can be
+ // modified in-place.
+ linesToRemove.reverse().forEach(function(lineIndex) {
+ lines.splice(lineIndex, 1);
+ });
+ return lines.join('\n');
+}
+var srcDir = path.resolve(path.join(__dirname, 'components-chromium'));
+var htmlFiles = glob(srcDir + '/**/*.html');
+var cssFiles = glob(srcDir + '/**/*.css');
+htmlFiles.concat(cssFiles).forEach(processFile);
+console.log('DONE');
« 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