diff --git a/src/main/java/com/github/sommeri/less4j/core/compiler/selectors/ExtendsSolver.java b/src/main/java/com/github/sommeri/less4j/core/compiler/selectors/ExtendsSolver.java index d9b1327f..58261bc6 100644 --- a/src/main/java/com/github/sommeri/less4j/core/compiler/selectors/ExtendsSolver.java +++ b/src/main/java/com/github/sommeri/less4j/core/compiler/selectors/ExtendsSolver.java @@ -97,6 +97,19 @@ private ASTCssNode findOwnerNode(ASTCssNode extendingSelector) { private void addSelector(RuleSet ruleSet, Selector selector) { selector.setParent(ruleSet); ruleSet.addSelector(selector); + setVisibility(ruleSet, selector); + } + + private void setVisibility(RuleSet ruleSet, Selector newSelector) { + if (newSelector.isSilent() || !ruleSet.isSilent()) + return ; + ruleSet.setSilent(false); + + List childs = ruleSet.getChilds(); + childs.removeAll(ruleSet.getSelectors()); + for (ASTCssNode kid : childs) { + manipulator.setTreeSilentness(kid, false); + } } private Selector constructNewSelector(Selector extending, Selector possibleTarget) { @@ -106,17 +119,22 @@ private Selector constructNewSelector(Selector extending, Selector possibleTarge List allExtends = extending.getExtend(); for (Extend extend : allExtends) { if (!extend.isAll() && comparator.equals(possibleTarget, extend.getTarget())) - return extending.clone(); + return setNewSelectorVisibility(extend, extending.clone()); if (extend.isAll()) { Selector addSelector = comparator.replaceInside(extend.getTarget(), possibleTarget, extend.getParentAsSelector()); if (addSelector!=null) - return addSelector; + return setNewSelectorVisibility(extend, addSelector); } } return null; } + private Selector setNewSelectorVisibility(Extend extend, Selector newSelector) { + manipulator.setTreeSilentness(newSelector, extend.isSilent()); + return newSelector; + } + private void collectRulesets(ASTCssNode node) { switch (node.getType()) { case RULE_SET: { diff --git a/src/main/java/com/github/sommeri/less4j/utils/CssPrinter.java b/src/main/java/com/github/sommeri/less4j/utils/CssPrinter.java index a1b02b3b..6d103993 100644 --- a/src/main/java/com/github/sommeri/less4j/utils/CssPrinter.java +++ b/src/main/java/com/github/sommeri/less4j/utils/CssPrinter.java @@ -1,5 +1,6 @@ package com.github.sommeri.less4j.utils; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -860,6 +861,7 @@ private boolean appendNumberExpression(NumberExpression node) { } public void appendSelectors(List selectors) { + selectors = filterSilent(selectors); // follow less.js formatting in special case - only one empty selector if (selectors.size() == 1 && isEmptySelector(selectors.get(0))) { cssOnly.append(" "); @@ -875,6 +877,15 @@ public void appendSelectors(List selectors) { } } + private List filterSilent(List nodes) { + List result = new ArrayList(); + for (T t : nodes) { + if (!t.isSilent()) + result.add(t); + } + return result; + } + private boolean isEmptySelector(Selector selector) { if (selector.isCombined()) return false; diff --git a/src/test/resources/compile-basic-features/import/import-option-reference-extend-lessjs-1851.css b/src/test/resources/compile-basic-features/import/import-option-reference-extend-lessjs-1851.css new file mode 100644 index 00000000..36f3949e --- /dev/null +++ b/src/test/resources/compile-basic-features/import/import-option-reference-extend-lessjs-1851.css @@ -0,0 +1,6 @@ +.test-c { + color: red; +} +.test-c { + background-color: green; +} \ No newline at end of file diff --git a/src/test/resources/compile-basic-features/import/import-option-reference-extend-lessjs-1851.less b/src/test/resources/compile-basic-features/import/import-option-reference-extend-lessjs-1851.less new file mode 100644 index 00000000..08dac3d4 --- /dev/null +++ b/src/test/resources/compile-basic-features/import/import-option-reference-extend-lessjs-1851.less @@ -0,0 +1,6 @@ +//This one tests reference chaining +@import (reference) "import/extend-lessjs-1851-fileB.less"; + +.test-c { + &:extend(.test-b all); +} diff --git a/src/test/resources/compile-basic-features/import/import-options-reference-extend.css b/src/test/resources/compile-basic-features/import/import-options-reference-extend.css new file mode 100644 index 00000000..f70a4c32 --- /dev/null +++ b/src/test/resources/compile-basic-features/import/import-options-reference-extend.css @@ -0,0 +1,9 @@ +.mainExtendingReference { + imported-as: reference; +} +.mainRuleset { + main: ruleset; +} +.mainExtendingReference { + main-extending: in main; +} \ No newline at end of file diff --git a/src/test/resources/compile-basic-features/import/import-options-reference-extend.less b/src/test/resources/compile-basic-features/import/import-options-reference-extend.less new file mode 100644 index 00000000..eb6fd240 --- /dev/null +++ b/src/test/resources/compile-basic-features/import/import-options-reference-extend.less @@ -0,0 +1,8 @@ +@import (reference) "import/import-as-reference-extend.less"; +.mainRuleset { + main: ruleset; +} + +.mainExtendingReference:extend(.importedAsReference) { + main-extending: in main; +} \ No newline at end of file diff --git a/src/test/resources/compile-basic-features/import/import-reference-lessjs.css b/src/test/resources/compile-basic-features/import/import-options-reference-lessjs.css similarity index 79% rename from src/test/resources/compile-basic-features/import/import-reference-lessjs.css rename to src/test/resources/compile-basic-features/import/import-options-reference-lessjs.css index c2d055a7..f8433bce 100644 --- a/src/test/resources/compile-basic-features/import/import-reference-lessjs.css +++ b/src/test/resources/compile-basic-features/import/import-options-reference-lessjs.css @@ -1,7 +1,3 @@ -/* - The media statement above is invalid (no selector) - We should ban invalid media queries with properties and no selector? -*/ .visible { color: red; } @@ -48,8 +44,7 @@ } .y { pulled-in: yes; -} -/* comment pulled in */ +} /* comment pulled in */ .visible { extend: test; } diff --git a/src/test/resources/compile-basic-features/import/import-options-reference-lessjs.less b/src/test/resources/compile-basic-features/import/import-options-reference-lessjs.less new file mode 100644 index 00000000..c83c691d --- /dev/null +++ b/src/test/resources/compile-basic-features/import/import-options-reference-lessjs.less @@ -0,0 +1,18 @@ +//@import (reference) url("reference-lessjs/import-once.less"); +//@import (reference) url("reference-lessjs/css-3.less"); +@import (reference) url("reference-lessjs/media.less"); +/* + The media statement above is invalid (no selector) + We should ban invalid media queries with properties and no selector? +*/ +@import (reference) url("reference-lessjs/import-reference.less"); + +.b { + .z(); +} + +.zz(); + +.visible:extend(.z all) { + extend: test; +} \ No newline at end of file diff --git a/src/test/resources/compile-basic-features/import/import/extend-lessjs-1851-fileA.less b/src/test/resources/compile-basic-features/import/import/extend-lessjs-1851-fileA.less new file mode 100644 index 00000000..1a036187 --- /dev/null +++ b/src/test/resources/compile-basic-features/import/import/extend-lessjs-1851-fileA.less @@ -0,0 +1,3 @@ +.test-a { + color: red; +} \ No newline at end of file diff --git a/src/test/resources/compile-basic-features/import/import/extend-lessjs-1851-fileB.less b/src/test/resources/compile-basic-features/import/import/extend-lessjs-1851-fileB.less new file mode 100644 index 00000000..659cfd72 --- /dev/null +++ b/src/test/resources/compile-basic-features/import/import/extend-lessjs-1851-fileB.less @@ -0,0 +1,6 @@ +@import (reference) "extend-lessjs-1851-fileA.less"; + +.test-b { + background-color: green; + &:extend(.test-a all); +} \ No newline at end of file diff --git a/src/test/resources/compile-basic-features/import/import/import-as-reference-extend.less b/src/test/resources/compile-basic-features/import/import/import-as-reference-extend.less new file mode 100644 index 00000000..a9518b9a --- /dev/null +++ b/src/test/resources/compile-basic-features/import/import/import-as-reference-extend.less @@ -0,0 +1,6 @@ +.importedAsReference { + imported-as: reference; +} +.unusedAndReference:extend(.mainRuleset) { + unused-and: reference; +} \ No newline at end of file