diff options
author | John Ahlroos <john@vaadin.com> | 2013-05-06 12:40:18 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2013-05-06 12:51:59 +0000 |
commit | 5388dce3783a05b9d2959cd5303bf13edcbf5d81 (patch) | |
tree | f50b80bcd0821e175cafbaa186bf653b5f5d9107 /theme-compiler | |
parent | d09b586ecee2c178882c917546d6d6cb6600fa85 (diff) | |
download | vaadin-framework-5388dce3783a05b9d2959cd5303bf13edcbf5d81.tar.gz vaadin-framework-5388dce3783a05b9d2959cd5303bf13edcbf5d81.zip |
Fixed IllegalSyntaxException when using spaces in path #11782
Change-Id: I105b2835d44c94f00b847f342fd0a6e0ef571e97
Diffstat (limited to 'theme-compiler')
-rw-r--r-- | theme-compiler/src/com/vaadin/sass/internal/resolver/VaadinResolver.java | 62 | ||||
-rw-r--r-- | theme-compiler/tests/src/com/vaadin/sass/resolvers/VaadinResolverTest.java | 83 |
2 files changed, 131 insertions, 14 deletions
diff --git a/theme-compiler/src/com/vaadin/sass/internal/resolver/VaadinResolver.java b/theme-compiler/src/com/vaadin/sass/internal/resolver/VaadinResolver.java index d6480f3e2c..fec16a54c8 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/resolver/VaadinResolver.java +++ b/theme-compiler/src/com/vaadin/sass/internal/resolver/VaadinResolver.java @@ -16,8 +16,7 @@ package com.vaadin.sass.internal.resolver; import java.io.File; -import java.net.URI; -import java.net.URISyntaxException; +import java.util.Stack; import org.w3c.css.sac.InputSource; @@ -26,18 +25,8 @@ public class VaadinResolver implements ScssStylesheetResolver { @Override public InputSource resolve(String identifier) { - /* - * Normalize classpath so ../../ segments are resolved - */ - try { - // Ensure only "/" is used, also in Windows - identifier = identifier.replace(File.separatorChar, '/'); - // Resolve "foo/../bar" -> "bar" - identifier = new URI(identifier).normalize().getPath(); - } catch (URISyntaxException e) { - // No worries, continuing with the unnormalized path and hope for - // the best - } + // Remove extra "." and ".." + identifier = normalize(identifier); InputSource source = null; @@ -53,4 +42,49 @@ public class VaadinResolver implements ScssStylesheetResolver { return source; } + + /** + * Normalizes "." and ".." from the path string where parent path segments + * can be removed. Preserve leading "..". + * + * @param path + * A relative or absolute file path + * @return The normalized path + */ + private static String normalize(String path) { + + // Ensure only "/" is used, also in Windows + path = path.replace(File.separatorChar, '/'); + + // Split into segments + String[] segments = path.split("/"); + Stack<String> result = new Stack<String>(); + + // Replace '.' and '..' segments + for (int i = 0; i < segments.length; i++) { + if (segments[i].equals(".")) { + // Segments marked '.' are ignored + + } else if (segments[i].equals("..") && !result.isEmpty() + && !result.lastElement().equals("..")) { + // If segment is ".." then remove the previous iff the previous + // element is not a ".." and the result stack is not empty + result.pop(); + } else { + // Other segments are just added to the stack + result.push(segments[i]); + } + } + + // Reconstruct path + StringBuilder pathBuilder = new StringBuilder(); + for (int i = 0; i < result.size(); i++) { + if (i > 0) { + pathBuilder.append("/"); + } + pathBuilder.append(result.get(i)); + } + return pathBuilder.toString(); + } + } diff --git a/theme-compiler/tests/src/com/vaadin/sass/resolvers/VaadinResolverTest.java b/theme-compiler/tests/src/com/vaadin/sass/resolvers/VaadinResolverTest.java new file mode 100644 index 0000000000..59b49888c2 --- /dev/null +++ b/theme-compiler/tests/src/com/vaadin/sass/resolvers/VaadinResolverTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +/** + * + */ +package com.vaadin.sass.resolvers; + +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +import java.lang.reflect.Method; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.sass.internal.resolver.VaadinResolver; + +public class VaadinResolverTest { + + @Test + public void testPathNormalization() throws Exception { + + VaadinResolver resolver = new VaadinResolver(); + + Method normalizeMethod = VaadinResolver.class.getDeclaredMethod( + "normalize", String.class); + normalizeMethod.setAccessible(true); + + String identifier, result; + + identifier = "a/b/../../../a b/b.scss"; + result = (String) normalizeMethod.invoke(resolver, identifier); + Assert.assertEquals("../a b/b.scss", result); + + identifier = "./a/b/../c/d/.././e.scss"; + result = (String) normalizeMethod.invoke(resolver, identifier); + Assert.assertEquals("a/c/e.scss", result); + + identifier = "/äåäåäääå/:;:;:;/???????/- -/e.scss"; + result = (String) normalizeMethod.invoke(resolver, identifier); + Assert.assertEquals("/äåäåäääå/:;:;:;/???????/- -/e.scss", result); + + identifier = "."; + result = (String) normalizeMethod.invoke(resolver, identifier); + Assert.assertEquals("", result); + + identifier = "../.."; + result = (String) normalizeMethod.invoke(resolver, identifier); + Assert.assertEquals("../..", result); + + identifier = "./../a.scss"; + result = (String) normalizeMethod.invoke(resolver, identifier); + Assert.assertEquals("../a.scss", result); + } + +} |