Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2000-2013 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.sass.internal.tree;
  17. import java.util.ArrayList;
  18. import java.util.regex.Pattern;
  19. import com.vaadin.sass.internal.ScssStylesheet;
  20. import com.vaadin.sass.internal.expression.ArithmeticExpressionEvaluator;
  21. import com.vaadin.sass.internal.parser.LexicalUnitImpl;
  22. import com.vaadin.sass.internal.util.StringUtil;
  23. public class RuleNode extends Node implements IVariableNode {
  24. private static final long serialVersionUID = 6653493127869037022L;
  25. String variable;
  26. LexicalUnitImpl value;
  27. String comment;
  28. private boolean important;
  29. public RuleNode(String variable, LexicalUnitImpl value, boolean important,
  30. String comment) {
  31. this.variable = variable;
  32. this.value = value;
  33. this.important = important;
  34. this.comment = comment;
  35. }
  36. public String getVariable() {
  37. return variable;
  38. }
  39. public void setVariable(String variable) {
  40. this.variable = variable;
  41. }
  42. public LexicalUnitImpl getValue() {
  43. return value;
  44. }
  45. public void setValue(LexicalUnitImpl value) {
  46. this.value = value;
  47. }
  48. @Override
  49. public String toString() {
  50. StringBuilder builder = new StringBuilder();
  51. builder.append(variable).append(": ").append(value.toString());
  52. builder.append(important ? " !important;" : ";");
  53. if (comment != null) {
  54. builder.append(comment);
  55. }
  56. return builder.toString();
  57. }
  58. public boolean isImportant() {
  59. return important;
  60. }
  61. public void setImportant(boolean important) {
  62. this.important = important;
  63. }
  64. public String getComment() {
  65. return comment;
  66. }
  67. public void setComment(String comment) {
  68. this.comment = comment;
  69. }
  70. @Override
  71. public void replaceVariables(ArrayList<VariableNode> variables) {
  72. for (final VariableNode node : variables) {
  73. String interpolation = "#{$" + node.getName() + "}";
  74. if (variable != null && variable.contains(interpolation)) {
  75. variable = variable.replaceAll(Pattern.quote(interpolation),
  76. node.getExpr().unquotedString());
  77. }
  78. if (value.getLexicalUnitType() == LexicalUnitImpl.SAC_FUNCTION) {
  79. if (value.getParameters() != null) {
  80. if (StringUtil.containsVariable(value.getParameters()
  81. .toString(), node.getName())) {
  82. LexicalUnitImpl param = value.getParameters();
  83. while (param != null) {
  84. if (param.getLexicalUnitType() == LexicalUnitImpl.SCSS_VARIABLE
  85. && param.getValue().toString()
  86. .equals(node.getName())) {
  87. param.replaceValue(node.getExpr());
  88. }
  89. param = param.getNextLexicalUnit();
  90. }
  91. }
  92. }
  93. } else if (value.getStringValue() != null
  94. && value.getStringValue().contains(interpolation)) {
  95. LexicalUnitImpl current = value;
  96. while (current != null) {
  97. if (current.getValue().toString().contains(interpolation)) {
  98. current.setStringValue(current
  99. .getValue()
  100. .toString()
  101. .replaceAll(Pattern.quote(interpolation),
  102. node.getExpr().unquotedString()));
  103. }
  104. current = current.getNextLexicalUnit();
  105. }
  106. } else {
  107. LexicalUnitImpl current = value;
  108. while (current != null) {
  109. if (current.getLexicalUnitType() == LexicalUnitImpl.SCSS_VARIABLE
  110. && current.getValue().toString()
  111. .equals(node.getName())) {
  112. current.replaceValue(node.getExpr());
  113. }
  114. current = current.getNextLexicalUnit();
  115. }
  116. }
  117. }
  118. }
  119. @Override
  120. public void traverse() {
  121. /*
  122. * "replaceVariables(ScssStylesheet.getVariables());" seems duplicated
  123. * and can be extracted out of if, but it is not.
  124. * containsArithmeticalOperator must be called before replaceVariables.
  125. * Because for the "/" operator, it needs to see if its predecessor or
  126. * successor is a Variable or not, to determine it is an arithmetic
  127. * operator.
  128. */
  129. if (ArithmeticExpressionEvaluator.get().containsArithmeticalOperator(
  130. value)) {
  131. replaceVariables(ScssStylesheet.getVariables());
  132. value = ArithmeticExpressionEvaluator.get().evaluate(value);
  133. } else {
  134. replaceVariables(ScssStylesheet.getVariables());
  135. }
  136. }
  137. }