Skip to content

Commit

Permalink
Import options reference #126
Browse files Browse the repository at this point in the history
  • Loading branch information
meri committed Feb 17, 2014
1 parent 84da0c9 commit bcfb4ae
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends ASTCssNode> childs = ruleSet.getChilds();
childs.removeAll(ruleSet.getSelectors());
for (ASTCssNode kid : childs) {
manipulator.setTreeSilentness(kid, false);
}
}

private Selector constructNewSelector(Selector extending, Selector possibleTarget) {
Expand All @@ -106,17 +119,22 @@ private Selector constructNewSelector(Selector extending, Selector possibleTarge
List<Extend> 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: {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/github/sommeri/less4j/utils/CssPrinter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.sommeri.less4j.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -860,6 +861,7 @@ private boolean appendNumberExpression(NumberExpression node) {
}

public void appendSelectors(List<Selector> 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(" ");
Expand All @@ -875,6 +877,15 @@ public void appendSelectors(List<Selector> selectors) {
}
}

private <T extends ASTCssNode> List<T> filterSilent(List<T> nodes) {
List<T> result = new ArrayList<T>();
for (T t : nodes) {
if (!t.isSilent())
result.add(t);
}
return result;
}

private boolean isEmptySelector(Selector selector) {
if (selector.isCombined())
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.test-c {
color: red;
}
.test-c {
background-color: green;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//This one tests reference chaining
@import (reference) "import/extend-lessjs-1851-fileB.less";

.test-c {
&:extend(.test-b all);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.mainExtendingReference {
imported-as: reference;
}
.mainRuleset {
main: ruleset;
}
.mainExtendingReference {
main-extending: in main;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@import (reference) "import/import-as-reference-extend.less";
.mainRuleset {
main: ruleset;
}

.mainExtendingReference:extend(.importedAsReference) {
main-extending: in main;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down Expand Up @@ -48,8 +44,7 @@
}
.y {
pulled-in: yes;
}
/* comment pulled in */
} /* comment pulled in */
.visible {
extend: test;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test-a {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import (reference) "extend-lessjs-1851-fileA.less";

.test-b {
background-color: green;
&:extend(.test-a all);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.importedAsReference {
imported-as: reference;
}
.unusedAndReference:extend(.mainRuleset) {
unused-and: reference;
}

0 comments on commit bcfb4ae

Please sign in to comment.