Example #1
0
  private void processStyleSheet(VirtualFile cssFile) {
    implicitIncludes.add(cssFile);

    try {
      FontManager fontManager = configuration.getFontsConfiguration().getTopLevelManager();
      StyleSheet styleSheet = new StyleSheet();
      styleSheet.checkDeprecation(configuration.showDeprecationWarnings());
      styleSheet.parse(
          cssFile.getName(), cssFile.getInputStream(), ThreadLocalToolkit.getLogger(), fontManager);

      extractStyles(styleSheet, false);
    } catch (Exception exception) {
      CompilerMessage m = new ParseError(exception.getLocalizedMessage());
      m.setPath(cssFile.getName());
      ThreadLocalToolkit.log(m);
    }
  }
Example #2
0
  public void extractStyles(StyleSheet styleSheet, boolean local) throws Exception {
    RuleList sheetRules = styleSheet.getCssRules();

    if (sheetRules != null) {
      //    aggregate rules by selector

      Iterator ruleIterator = sheetRules.iterator();
      while (ruleIterator.hasNext()) {
        Rule rule = (Rule) ruleIterator.next();

        if (rule instanceof StyleRule) {
          // for each selector in this rule
          SelectorList selectors = ((StyleRule) rule).getSelectorList();
          int nSelectors = selectors.getLength();
          for (int i = 0; i < nSelectors; i++) {
            Selector selector = selectors.item(i);
            int lineNumber = rule.getStyle().getLineNumber();

            if (selector instanceof AbstractSelector) {
              lineNumber = ((AbstractSelector) selector).getLineNumber();
            }

            if (selector.getSelectorType() == Selector.SAC_CONDITIONAL_SELECTOR) {
              Condition condition = ((ConditionalSelector) selector).getCondition();

              if (condition.getConditionType() == Condition.SAC_CLASS_CONDITION) {
                String name = ((AttributeCondition) condition).getValue();
                assert name != null : "parsed CSS class selector name is null";

                addStyleClassSelector(name, rule, lineNumber);
              } else {
                ConditionTypeNotSupported conditionTypeNotSupported =
                    new ConditionTypeNotSupported(
                        compilationUnit.getSource().getName(), lineNumber, condition.toString());
                ThreadLocalToolkit.log(conditionTypeNotSupported);
              }
            } else if (selector.getSelectorType() == Selector.SAC_ELEMENT_NODE_SELECTOR) {
              //    pick up type selectors only from root (application)
              if (compilationUnit.isRoot()) {
                String name = ((ElementSelector) selector).getLocalName();

                // Batik seems to generate an empty element
                // selector when @charset, so filter those out.
                if (name != null) {
                  addStyleTypeSelector(name, rule, local, lineNumber);
                }
              } else {
                // [preilly] This restriction should be removed once the
                // app model supports encapsulation of CSS styles.
                ComponentTypeSelectorsNotSupported componentTypeSelectorsNotSupported =
                    new ComponentTypeSelectorsNotSupported(
                        compilationUnit.getSource().getName(), lineNumber, selector.toString());
                ThreadLocalToolkit.log(componentTypeSelectorsNotSupported);
              }
            } else {
              SelectorTypeNotSupported selectorTypeNotSupported =
                  new SelectorTypeNotSupported(
                      compilationUnit.getSource().getName(), lineNumber, selector.toString());
              ThreadLocalToolkit.log(selectorTypeNotSupported);
            }
          }
        } else if (rule instanceof FontFaceRule) {
          addFontFaceRule((FontFaceRule) rule);
        }
      }
    }
  }