From 821ebf49d898ba5f0e2f2e386de242b3dbcbe2e1 Mon Sep 17 00:00:00 2001 From: Haijian Wang Date: Tue, 19 Feb 2013 11:17:51 +0200 Subject: [PATCH] more than expected params in mixin throws IOB exception. (Ticket #10988) Change-Id: I6451c94a4f733b002165983f92c8577dac4904f6 --- .../internal/visitor/MixinNodeHandler.java | 31 ++++++++++++++++--- .../resources/scss/mixin-extra-params.scss | 4 +++ .../testcases/scss/MixinExtraParameters.java | 25 +++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 theme-compiler/tests/resources/scss/mixin-extra-params.scss create mode 100644 theme-compiler/tests/src/com/vaadin/sass/testcases/scss/MixinExtraParameters.java diff --git a/theme-compiler/src/com/vaadin/sass/internal/visitor/MixinNodeHandler.java b/theme-compiler/src/com/vaadin/sass/internal/visitor/MixinNodeHandler.java index 8a62e3abf8..5aa90151b9 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/visitor/MixinNodeHandler.java +++ b/theme-compiler/src/com/vaadin/sass/internal/visitor/MixinNodeHandler.java @@ -108,18 +108,39 @@ public class MixinNodeHandler { } } } - - int i = 0; - for (final LexicalUnitImpl unit : remainingUnits) { + checkExtraParameters(mixinNode, remainingNodes.size(), + remainingUnits.size()); + for (int i = 0; i < remainingNodes.size() + && i < remainingUnits.size(); i++) { + LexicalUnitImpl unit = remainingUnits.get(i); remainingNodes.get(i).setExpr( (LexicalUnitImpl) DeepCopy.copy(unit)); - i++; } - } } + protected static void checkExtraParameters(MixinNode mixinNode, + int remainingNodesSize, int remainingUnitsSize) { + if (remainingUnitsSize > remainingNodesSize) { + String fileName = null; + Node root = mixinNode.getParentNode(); + while (root != null && !(root instanceof ScssStylesheet)) { + root = root.getParentNode(); + } + if (root != null) { + fileName = ((ScssStylesheet) root).getFileName(); + } + StringBuilder builder = new StringBuilder(); + builder.append("More parameters than expected, in Mixin ").append( + mixinNode.getName()); + if (fileName != null) { + builder.append(", in file ").append(fileName); + } + throw new RuntimeException(builder.toString()); + } + } + private static void replaceChildVariables(MixinDefNode mixinDef, Node node) { for (final Node child : node.getChildren()) { replaceChildVariables(mixinDef, child); diff --git a/theme-compiler/tests/resources/scss/mixin-extra-params.scss b/theme-compiler/tests/resources/scss/mixin-extra-params.scss new file mode 100644 index 0000000000..af376adf90 --- /dev/null +++ b/theme-compiler/tests/resources/scss/mixin-extra-params.scss @@ -0,0 +1,4 @@ +@mixin test ($p1) { + color: $p1; +} +@include test(foo, bar); \ No newline at end of file diff --git a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/MixinExtraParameters.java b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/MixinExtraParameters.java new file mode 100644 index 0000000000..79c2ec366a --- /dev/null +++ b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/MixinExtraParameters.java @@ -0,0 +1,25 @@ +package com.vaadin.sass.testcases.scss; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.sass.AbstractTestBase; +import com.vaadin.sass.internal.ScssStylesheet; + +public class MixinExtraParameters extends AbstractTestBase { + String scss = "/scss/mixin-extra-params.scss"; + + @Test + public void testCompiler() { + ScssStylesheet sheet; + try { + sheet = getStyleSheet(scss); + sheet.compile(); + } catch (RuntimeException e) { + Assert.assertEquals(e.getMessage(), + "More parameters than expected, in Mixin test"); + } catch (Exception e) { + Assert.fail(); + } + } +} -- 2.39.5