From 78bfb5cb1cc72164e73cf7919501fa22ba05445d Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Mon, 26 Nov 2012 12:36:46 +0200 Subject: [PATCH] Keep block order when unnesting (#10309) Change-Id: I5a7e6b74a55176799e85c943a128d9aab0db0876 --- .../com/vaadin/sass/internal/ScssStylesheet.java | 7 ++++++- .../sass/internal/visitor/BlockNodeHandler.java | 13 +++++++------ theme-compiler/tests/resources/css/nesting.css | 8 ++++++++ theme-compiler/tests/resources/scss/nesting.scss | 12 ++++++++++++ .../src/com/vaadin/sass/testcases/scss/Nesting.java | 4 +++- 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/theme-compiler/src/com/vaadin/sass/internal/ScssStylesheet.java b/theme-compiler/src/com/vaadin/sass/internal/ScssStylesheet.java index af061d8e95..2a5d617a6b 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/ScssStylesheet.java +++ b/theme-compiler/src/com/vaadin/sass/internal/ScssStylesheet.java @@ -235,9 +235,14 @@ public class ScssStylesheet extends Node { variables.clear(); variables.putAll(variableScope); + // clean up insert point so that processing of the next block will + // insert after that block + lastNodeAdded.remove(originalParent); + // has the node been removed from its parent? if (originalParent != null) { - return !originalParent.getChildren().contains(node); + boolean removed = !originalParent.getChildren().contains(node); + return removed; } else { return false; } diff --git a/theme-compiler/src/com/vaadin/sass/internal/visitor/BlockNodeHandler.java b/theme-compiler/src/com/vaadin/sass/internal/visitor/BlockNodeHandler.java index cb736ba558..fc34e32f7a 100644 --- a/theme-compiler/src/com/vaadin/sass/internal/visitor/BlockNodeHandler.java +++ b/theme-compiler/src/com/vaadin/sass/internal/visitor/BlockNodeHandler.java @@ -98,14 +98,15 @@ public class BlockNodeHandler { } node.setSelectorList(newList); Node oldParent = node.getParentNode(); + HashMap lastNodeAdded = ScssStylesheet.getLastNodeAdded(); - if (lastNodeAdded.get(oldParent) != null) { - oldParent.getParentNode().appendChild(node, - lastNodeAdded.get(oldParent)); - } else { - oldParent.getParentNode().appendChild(node, oldParent); + Node lastAdded = lastNodeAdded.get(oldParent.getParentNode()); + if (lastAdded == null) { + lastAdded = oldParent; } - lastNodeAdded.put(oldParent, node); + oldParent.getParentNode().appendChild(node, lastAdded); + + lastNodeAdded.put(oldParent.getParentNode(), node); } } diff --git a/theme-compiler/tests/resources/css/nesting.css b/theme-compiler/tests/resources/css/nesting.css index b3861a0131..e1cdf3a579 100644 --- a/theme-compiler/tests/resources/css/nesting.css +++ b/theme-compiler/tests/resources/css/nesting.css @@ -44,4 +44,12 @@ .main .second.third .fourth { color: black; +} + +.root .first-block .nested { + order: first; +} + +.root .last-block { + order: last; } \ No newline at end of file diff --git a/theme-compiler/tests/resources/scss/nesting.scss b/theme-compiler/tests/resources/scss/nesting.scss index 0336c9e86d..1fefe0dde0 100644 --- a/theme-compiler/tests/resources/scss/nesting.scss +++ b/theme-compiler/tests/resources/scss/nesting.scss @@ -37,4 +37,16 @@ color: black; } } +} + +.root { + .first-block { + .nested { + order: first; + } + } + + .last-block { + order: last; + } } \ No newline at end of file diff --git a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/Nesting.java b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/Nesting.java index 109182f608..bdc6e63391 100644 --- a/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/Nesting.java +++ b/theme-compiler/tests/src/com/vaadin/sass/testcases/scss/Nesting.java @@ -42,7 +42,7 @@ public class Nesting extends AbstractTestBase { parser.setDocumentHandler(handler); parser.parseStyleSheet(getClass().getResource(scss).getPath()); ScssStylesheet root = handler.getStyleSheet(); - Assert.assertEquals(5, root.getChildren().size()); + Assert.assertEquals(6, root.getChildren().size()); BlockNode blockNode0 = (BlockNode) root.getChildren().get(0); Assert.assertEquals(2, blockNode0.getChildren().size()); @@ -78,6 +78,8 @@ public class Nesting extends AbstractTestBase { BlockNode nestednestedBlockInBlock4 = (BlockNode) nestedBlockInBlock3 .getChildren().get(1); Assert.assertEquals(1, nestednestedBlockInBlock4.getChildren().size()); + + // the parsing of the last block is not checked in detail } @Test -- 2.39.5