From fb2335d13d62bacc4d908e0c4f9ec34c56a32360 Mon Sep 17 00:00:00 2001 From: Jeff Booher Date: Wed, 21 Aug 2013 14:38:17 -0700 Subject: [PATCH 01/19] first tests passing --- src/language/CSSUtils.js | 12 ++++++++++++ test/spec/CSSUtils-test.js | 14 +++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/language/CSSUtils.js b/src/language/CSSUtils.js index a331e798008..7001c36c192 100644 --- a/src/language/CSSUtils.js +++ b/src/language/CSSUtils.js @@ -1115,9 +1115,21 @@ define(function (require, exports, module) { return _stripAtRules(selector); } + function extractAllNamedFlows(text) { + var namedFlowRegEx = /(?:flow\-into\: *)([a-zA-Z0-9_\-]+)(?: *;)/gi, + matches; + + matches = namedFlowRegEx.exec(text); + if (matches && matches.length > 1) { + return matches.slice(1); + } + return []; + } + exports._findAllMatchingSelectorsInText = _findAllMatchingSelectorsInText; // For testing only exports.findMatchingRules = findMatchingRules; exports.extractAllSelectors = extractAllSelectors; + exports.extractAllNamedFlows = extractAllNamedFlows; exports.findSelectorAtDocumentPos = findSelectorAtDocumentPos; exports.SELECTOR = SELECTOR; diff --git a/test/spec/CSSUtils-test.js b/test/spec/CSSUtils-test.js index f8df31facfc..a04f6f081bd 100644 --- a/test/spec/CSSUtils-test.js +++ b/test/spec/CSSUtils-test.js @@ -41,7 +41,8 @@ define(function (require, exports, module) { offsetsCssFileEntry = new NativeFileSystem.FileEntry(testPath + "/offsets.css"), bootstrapCssFileEntry = new NativeFileSystem.FileEntry(testPath + "/bootstrap.css"), escapesCssFileEntry = new NativeFileSystem.FileEntry(testPath + "/escaped-identifiers.css"), - embeddedHtmlFileEntry = new NativeFileSystem.FileEntry(testPath + "/embedded.html"); + embeddedHtmlFileEntry = new NativeFileSystem.FileEntry(testPath + "/embedded.html"), + cssRegionsFileEntry = new NativeFileSystem.FileEntry(testPath + "/regions.css"); var contextTestCss = require("text!spec/CSSUtils-test-files/contexts.css"), selectorPositionsTestCss = require("text!spec/CSSUtils-test-files/selector-positions.css"); @@ -1922,4 +1923,15 @@ define(function (require, exports, module) { }); }); }); + + describe("CSS Regions", function () { + beforeEach(function () { + init(this, cssRegionsFileEntry); + }); + + it("should find named flows", function () { + expect(CSSUtils.extractAllNamedFlows(this.fileContent).length).toNotBe(0); + }); + + }); }); From e10a0b1c3e5dd782c3193e6ebc5c05fbce5c3820 Mon Sep 17 00:00:00 2001 From: Jeff Booher Date: Wed, 21 Aug 2013 14:42:15 -0700 Subject: [PATCH 02/19] add another test --- test/spec/CSSUtils-test-files/regions.css | 7 +++++++ test/spec/CSSUtils-test.js | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 test/spec/CSSUtils-test-files/regions.css diff --git a/test/spec/CSSUtils-test-files/regions.css b/test/spec/CSSUtils-test-files/regions.css new file mode 100644 index 00000000000..f7668803bc5 --- /dev/null +++ b/test/spec/CSSUtils-test-files/regions.css @@ -0,0 +1,7 @@ +article.content { + flow-into: main; +} + +section.layout > div { + flow-from: main; +} diff --git a/test/spec/CSSUtils-test.js b/test/spec/CSSUtils-test.js index a04f6f081bd..5261ee5942b 100644 --- a/test/spec/CSSUtils-test.js +++ b/test/spec/CSSUtils-test.js @@ -1930,7 +1930,9 @@ define(function (require, exports, module) { }); it("should find named flows", function () { - expect(CSSUtils.extractAllNamedFlows(this.fileContent).length).toNotBe(0); + var namedFlows = CSSUtils.extractAllNamedFlows(this.fileContent); + expect(namedFlows.length).toBe(1); + expect(namedFlows[0]).toBe("main"); }); }); From 941b8a5522fef0bf84c817f98d2ec764bc92f8dc Mon Sep 17 00:00:00 2001 From: Jeff Booher Date: Wed, 21 Aug 2013 15:25:53 -0700 Subject: [PATCH 03/19] multiple named-flows --- src/language/CSSUtils.js | 17 ++++++++++++----- test/spec/CSSUtils-test-files/regions.css | 9 +++++++++ test/spec/CSSUtils-test.js | 3 ++- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/language/CSSUtils.js b/src/language/CSSUtils.js index 7001c36c192..99f6ce54a7d 100644 --- a/src/language/CSSUtils.js +++ b/src/language/CSSUtils.js @@ -1117,13 +1117,20 @@ define(function (require, exports, module) { function extractAllNamedFlows(text) { var namedFlowRegEx = /(?:flow\-into\: *)([a-zA-Z0-9_\-]+)(?: *;)/gi, + result = [], matches; - matches = namedFlowRegEx.exec(text); - if (matches && matches.length > 1) { - return matches.slice(1); - } - return []; + matches = text.match(namedFlowRegEx) || []; + + matches.forEach(function (match) { + var nameRegEx = /(?:flow\-into\: *)([a-zA-Z0-9_\-]+)(?: *;)/i, + thisMatch = nameRegEx.exec(match); + if (thisMatch && thisMatch.length === 2) { + result.push(thisMatch[1]); + } + }); + + return result; } exports._findAllMatchingSelectorsInText = _findAllMatchingSelectorsInText; // For testing only diff --git a/test/spec/CSSUtils-test-files/regions.css b/test/spec/CSSUtils-test-files/regions.css index f7668803bc5..6e380c51e1b 100644 --- a/test/spec/CSSUtils-test-files/regions.css +++ b/test/spec/CSSUtils-test-files/regions.css @@ -5,3 +5,12 @@ article.content { section.layout > div { flow-from: main; } + + +#jeff.content { + flow-into: jeff; +} + +#jeff.layout > div { + flow-from: jeff; +} diff --git a/test/spec/CSSUtils-test.js b/test/spec/CSSUtils-test.js index 5261ee5942b..3c1bcce6e69 100644 --- a/test/spec/CSSUtils-test.js +++ b/test/spec/CSSUtils-test.js @@ -1931,8 +1931,9 @@ define(function (require, exports, module) { it("should find named flows", function () { var namedFlows = CSSUtils.extractAllNamedFlows(this.fileContent); - expect(namedFlows.length).toBe(1); + expect(namedFlows.length).toBe(2); expect(namedFlows[0]).toBe("main"); + expect(namedFlows[1]).toBe("jeff"); }); }); From e4ab107d3ae2a3343019eb6fe36926029d2722f8 Mon Sep 17 00:00:00 2001 From: Jeff Booher Date: Wed, 21 Aug 2013 15:58:21 -0700 Subject: [PATCH 04/19] exclude strings and comments from results --- src/language/CSSUtils.js | 21 ++++++++++++++++++++ test/spec/CSSUtils-test-files/regions.css | 24 +++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/language/CSSUtils.js b/src/language/CSSUtils.js index 99f6ce54a7d..a0b9d28be3f 100644 --- a/src/language/CSSUtils.js +++ b/src/language/CSSUtils.js @@ -1115,13 +1115,34 @@ define(function (require, exports, module) { return _stripAtRules(selector); } + // From http://stackoverflow.com/questions/4402220/regex-to-minimize-css + function _minimize(_content) { + var content = _content; + content = content.replace(/\/\*(?:(?!\*\/)[\s\S])*\*\/|[\r\n\t]+/g, ''); + // now all comments, newlines and tabs have been removed + content = content.replace(/ {2,}/g, ' '); + // now there are no more than single adjacent spaces left + // now unnecessary: content = content.replace( /(\s)+\./g, ' .' ); + content = content.replace(/ ([{:}]) /g, '$1'); + content = content.replace(/([;,]) /g, '$1'); + content = content.replace(/ !/g, '!'); + return content; + } + function extractAllNamedFlows(text) { var namedFlowRegEx = /(?:flow\-into\: *)([a-zA-Z0-9_\-]+)(?: *;)/gi, result = [], matches; + // Minimize the CSS so that strings and comments + // do not match results + text = _minimize(text); + + // Find the lines that match. This will return an array of + // matched css properties (flow-into: junk;) matches = text.match(namedFlowRegEx) || []; + // Parse the matches to extract the name of the flow matches.forEach(function (match) { var nameRegEx = /(?:flow\-into\: *)([a-zA-Z0-9_\-]+)(?: *;)/i, thisMatch = nameRegEx.exec(match); diff --git a/test/spec/CSSUtils-test-files/regions.css b/test/spec/CSSUtils-test-files/regions.css index 6e380c51e1b..552b2907860 100644 --- a/test/spec/CSSUtils-test-files/regions.css +++ b/test/spec/CSSUtils-test-files/regions.css @@ -14,3 +14,27 @@ section.layout > div { #jeff.layout > div { flow-from: jeff; } + +/* +p.content { + flow-into: carter; +} + +p.layout > div { + flow-from: carter; +} +*/ + +div { + content: "/* p.content { flow-into: carter; } p.layout > div { flow-from: carter; } */" +} + +/* +div.content { + content: "flow-into: martin;" +} + +div.layout > div { + content: "flow-from: martin;" +} +*/ From 47cced39f1cbf7a3359ffe7b413866c7c0192092 Mon Sep 17 00:00:00 2001 From: Jeff Booher Date: Wed, 21 Aug 2013 16:52:04 -0700 Subject: [PATCH 05/19] exclude strings, previous commit handled everything except strings --- src/language/CSSUtils.js | 17 ++++++++++++++++- test/spec/CSSUtils-test-files/regions.css | 10 +++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/language/CSSUtils.js b/src/language/CSSUtils.js index a0b9d28be3f..f058100a38e 100644 --- a/src/language/CSSUtils.js +++ b/src/language/CSSUtils.js @@ -1115,6 +1115,7 @@ define(function (require, exports, module) { return _stripAtRules(selector); } + // Essentially minifies CSS by removing all newlines, tabs and comments // From http://stackoverflow.com/questions/4402220/regex-to-minimize-css function _minimize(_content) { var content = _content; @@ -1129,6 +1130,20 @@ define(function (require, exports, module) { return content; } + // Essentially minifies CSS by removing all newlines, tabs and comments + // From http://stackoverflow.com/questions/4402220/regex-to-minimize-css + function _reduceStrings(_content) { + return _content.replace(/[^\\]\"(.*)[^\\]\"|[^\\]\'(.*)[^\\]\'+/g, ''); + } + + /** + * Extracts all nemd flow instances + * @param {!String} text to extract from + * @param {?Document} htmlDocument An HTML file for context (so we can search