From 58ac9b8cc9f6706d4fcdf3e2354ffcb5976f46b2 Mon Sep 17 00:00:00 2001 From: lepipele Date: Wed, 4 Apr 2018 14:22:40 -0500 Subject: [PATCH 1/4] Adding support for CSS Variables Ref: https://github.com/xoofx/NUglify/issues/17 All previous tests passed --- src/NUglify/Css/CssParser.cs | 7 +++++++ src/NUglify/Css/CssScanner.cs | 1 + 2 files changed, 8 insertions(+) diff --git a/src/NUglify/Css/CssParser.cs b/src/NUglify/Css/CssParser.cs index 6a4652ac..58a73490 100644 --- a/src/NUglify/Css/CssParser.cs +++ b/src/NUglify/Css/CssParser.cs @@ -4085,6 +4085,13 @@ private bool Append(object obj, TokenType tokenType) // for identifiers, if the first character is a hyphen or an underscore, then it's a prefix // and we want to look at the next character for nmstart. firstIndex = text[0] == '_' || text[0] == '-' ? 1 : 0; + + // CSS variables start with double dash -- + if (text.StartsWith("--") && text.Length >= 3) + { + firstIndex = 2; + } + if (firstIndex < text.Length) { // the only valid non-escaped first characters are A-Z (and a-z) diff --git a/src/NUglify/Css/CssScanner.cs b/src/NUglify/Css/CssScanner.cs index 6e0dd6f6..ba54d147 100644 --- a/src/NUglify/Css/CssScanner.cs +++ b/src/NUglify/Css/CssScanner.cs @@ -1604,6 +1604,7 @@ private string GetNmStart() { if (IsNonAscii(m_currentChar) || (m_currentChar == '_') + || (m_currentChar == '-') // CSS variables start with double dash || ('a' <= m_currentChar && m_currentChar <= 'z') || ('A' <= m_currentChar && m_currentChar <= 'Z')) { From 080fa0c2603814897a3d02c1d09da2644f83ee2e Mon Sep 17 00:00:00 2001 From: lepipele Date: Wed, 4 Apr 2018 14:23:17 -0500 Subject: [PATCH 2/4] Adding Nunit3TestAdapter so Visual Studio can auto detect tests --- src/NUglify.Tests/project.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NUglify.Tests/project.json b/src/NUglify.Tests/project.json index 1d29a5b6..1159bcaa 100644 --- a/src/NUglify.Tests/project.json +++ b/src/NUglify.Tests/project.json @@ -18,6 +18,7 @@ } }, "dependencies": { - "NUnit": "3.5.0" + "NUnit": "3.5.0", + "NUnit3TestAdapter": "3.5.0" } } \ No newline at end of file From eb53f9f7263321b27e5a6c15a96d6014060a2172 Mon Sep 17 00:00:00 2001 From: lepipele Date: Wed, 4 Apr 2018 14:24:08 -0500 Subject: [PATCH 3/4] Added new test that checks if CSS Variables are uglified correctly --- src/NUglify.Tests/Css/Selectors.cs | 6 ++++ src/NUglify.Tests/NUglify.Tests.csproj | 6 ++++ .../Selectors/Bootstrap4CssVariables.css | 1 + .../Selectors/Bootstrap4CssVariables.css | 30 +++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 src/NUglify.Tests/TestData/CSS/Expected/Selectors/Bootstrap4CssVariables.css create mode 100644 src/NUglify.Tests/TestData/CSS/Input/Selectors/Bootstrap4CssVariables.css diff --git a/src/NUglify.Tests/Css/Selectors.cs b/src/NUglify.Tests/Css/Selectors.cs index 01a43b7e..182b5b7e 100644 --- a/src/NUglify.Tests/Css/Selectors.cs +++ b/src/NUglify.Tests/Css/Selectors.cs @@ -126,5 +126,11 @@ public void Any() { TestHelper.Instance.RunTest(); } + + [Test] + public void Bootstrap4CssVariables() + { + TestHelper.Instance.RunTest(); + } } } diff --git a/src/NUglify.Tests/NUglify.Tests.csproj b/src/NUglify.Tests/NUglify.Tests.csproj index 12e6aeae..829ac230 100644 --- a/src/NUglify.Tests/NUglify.Tests.csproj +++ b/src/NUglify.Tests/NUglify.Tests.csproj @@ -375,9 +375,15 @@ PreserveNewest + + PreserveNewest + PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/src/NUglify.Tests/TestData/CSS/Expected/Selectors/Bootstrap4CssVariables.css b/src/NUglify.Tests/TestData/CSS/Expected/Selectors/Bootstrap4CssVariables.css new file mode 100644 index 00000000..a767c0a1 --- /dev/null +++ b/src/NUglify.Tests/TestData/CSS/Expected/Selectors/Bootstrap4CssVariables.css @@ -0,0 +1 @@ +:root{--blue:#007bff;--indigo:#6610f2;--purple:#6f42c1;--pink:#e83e8c;--red:#dc3545;--orange:#fd7e14;--yellow:#ffc107;--green:#28a745;--teal:#20c997;--cyan:#17a2b8;--white:#fff;--gray:#6c757d;--gray-dark:#343a40;--primary:#007bff;--secondary:#6c757d;--success:#28a745;--info:#17a2b8;--warning:#ffc107;--danger:#dc3545;--light:#f8f9fa;--dark:#343a40;--breakpoint-xs:0;--breakpoint-sm:576px;--breakpoint-md:768px;--breakpoint-lg:992px;--breakpoint-xl:1200px;--font-family-sans-serif:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";--font-family-monospace:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace} \ No newline at end of file diff --git a/src/NUglify.Tests/TestData/CSS/Input/Selectors/Bootstrap4CssVariables.css b/src/NUglify.Tests/TestData/CSS/Input/Selectors/Bootstrap4CssVariables.css new file mode 100644 index 00000000..875baab5 --- /dev/null +++ b/src/NUglify.Tests/TestData/CSS/Input/Selectors/Bootstrap4CssVariables.css @@ -0,0 +1,30 @@ +:root { + --blue: #007bff; + --indigo: #6610f2; + --purple: #6f42c1; + --pink: #e83e8c; + --red: #dc3545; + --orange: #fd7e14; + --yellow: #ffc107; + --green: #28a745; + --teal: #20c997; + --cyan: #17a2b8; + --white: #fff; + --gray: #6c757d; + --gray-dark: #343a40; + --primary: #007bff; + --secondary: #6c757d; + --success: #28a745; + --info: #17a2b8; + --warning: #ffc107; + --danger: #dc3545; + --light: #f8f9fa; + --dark: #343a40; + --breakpoint-xs: 0; + --breakpoint-sm: 576px; + --breakpoint-md: 768px; + --breakpoint-lg: 992px; + --breakpoint-xl: 1200px; + --font-family-sans-serif: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; + --font-family-monospace: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; +} From 58d388e0a6bd261d0bbc2eacc84b74785c2b5f09 Mon Sep 17 00:00:00 2001 From: lepipele Date: Wed, 4 Apr 2018 16:24:38 -0500 Subject: [PATCH 4/4] Clean code doesn't need comments --- src/NUglify/Css/CssParser.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NUglify/Css/CssParser.cs b/src/NUglify/Css/CssParser.cs index 58a73490..52a804e9 100644 --- a/src/NUglify/Css/CssParser.cs +++ b/src/NUglify/Css/CssParser.cs @@ -4086,8 +4086,8 @@ private bool Append(object obj, TokenType tokenType) // and we want to look at the next character for nmstart. firstIndex = text[0] == '_' || text[0] == '-' ? 1 : 0; - // CSS variables start with double dash -- - if (text.StartsWith("--") && text.Length >= 3) + var isCssVariable = text.StartsWith("--") && text.Length >= 3; + if (isCssVariable) { firstIndex = 2; }