You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by ah...@apache.org on 2013/10/21 07:09:21 UTC

git commit: [flex-falcon] [refs/heads/develop] - Custom CSS for JS

Updated Branches:
  refs/heads/develop 577e7143b -> ee9a0d0bd


Custom CSS for JS


Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/ee9a0d0b
Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/ee9a0d0b
Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/ee9a0d0b

Branch: refs/heads/develop
Commit: ee9a0d0bd0296f170cf3ca020ccb2940326952f5
Parents: 577e714
Author: Alex Harui <ah...@apache.org>
Authored: Sun Oct 20 22:07:15 2013 -0700
Committer: Alex Harui <ah...@apache.org>
Committed: Sun Oct 20 22:08:49 2013 -0700

----------------------------------------------------------------------
 .../mxml/flexjs/MXMLFlexJSPublisher.java        |  32 ++-
 .../js/flexjs/JSCSSCompilationSession.java      | 228 +++++++++++++++++++
 .../internal/projects/FlexJSProject.java        |   1 +
 .../compiler/internal/targets/FlexJSTarget.java |   1 +
 4 files changed, 260 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/ee9a0d0b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/mxml/flexjs/MXMLFlexJSPublisher.java
----------------------------------------------------------------------
diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/mxml/flexjs/MXMLFlexJSPublisher.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/mxml/flexjs/MXMLFlexJSPublisher.java
index 296c2f9..3947e02 100644
--- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/mxml/flexjs/MXMLFlexJSPublisher.java
+++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/mxml/flexjs/MXMLFlexJSPublisher.java
@@ -130,6 +130,9 @@ public class MXMLFlexJSPublisher extends JSGoogPublisher implements
         final String projectReleaseJSFilePath = releaseDirPath
                 + File.separator + outputFileName;
 
+        appendExportSymbol(projectIntermediateJSFilePath, projectName);
+        appendEncodedCSS(projectIntermediateJSFilePath, projectName);
+
         // just copy base.js. All other goog files should get copied as the DepsWriter chases down goog.requires
         FileUtils.copyFile(new File(closureGoogSrcLibDirPath + File.separator + "base.js"), 
                 new File(closureGoogTgtLibDirPath + File.separator + "base.js"));
@@ -145,8 +148,6 @@ public class MXMLFlexJSPublisher extends JSGoogPublisher implements
             e.printStackTrace();
         }
         
-        appendExportSymbol(projectIntermediateJSFilePath, projectName);
-
         if (!isMarmotinniRun)
         {
             //for (String sdkJSLibSrcDirPath : sdkJSLibSrcDirPaths)
@@ -265,6 +266,33 @@ public class MXMLFlexJSPublisher extends JSGoogPublisher implements
         writeFile(path, appendString.toString(), true);
     }
 
+    private void appendEncodedCSS(String path, String projectName)
+            throws IOException
+    {
+        StringBuilder appendString = new StringBuilder();
+        appendString.append("\n\n");
+        appendString.append(projectName);
+        appendString.append(".prototype.cssData = [");
+        String s = project.cssEncoding;
+        int reqidx = s.indexOf("goog.require");
+        if (reqidx != -1)
+        {
+            String reqs = s.substring(reqidx);
+            s = s.substring(0, reqidx - 1);
+            String fileData = readCode(new File(path));
+            reqidx = fileData.indexOf("goog.require");
+            String after = fileData.substring(reqidx);
+            String before = fileData.substring(0, reqidx - 1);
+            s = before + reqs + after + appendString.toString() + s;
+            writeFile(path, s, false);
+        }
+        else
+        {
+            appendString.append(s);
+            writeFile(path, appendString.toString(), true);
+        }
+    }
+        
     protected String readCode(File file)
     {
         String code = "";

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/ee9a0d0b/compiler.jx/src/org/apache/flex/compiler/internal/driver/js/flexjs/JSCSSCompilationSession.java
----------------------------------------------------------------------
diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/driver/js/flexjs/JSCSSCompilationSession.java b/compiler.jx/src/org/apache/flex/compiler/internal/driver/js/flexjs/JSCSSCompilationSession.java
index af2612b..6e1e4ec 100644
--- a/compiler.jx/src/org/apache/flex/compiler/internal/driver/js/flexjs/JSCSSCompilationSession.java
+++ b/compiler.jx/src/org/apache/flex/compiler/internal/driver/js/flexjs/JSCSSCompilationSession.java
@@ -1,14 +1,53 @@
 package org.apache.flex.compiler.internal.driver.js.flexjs;
 
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.flex.compiler.constants.IASLanguageConstants;
 import org.apache.flex.compiler.css.ICSSDocument;
+import org.apache.flex.compiler.css.ICSSMediaQueryCondition;
+import org.apache.flex.compiler.css.ICSSProperty;
+import org.apache.flex.compiler.css.ICSSPropertyValue;
 import org.apache.flex.compiler.css.ICSSRule;
+import org.apache.flex.compiler.css.ICSSSelector;
+import org.apache.flex.compiler.css.ICSSSelectorCondition;
+import org.apache.flex.compiler.definitions.references.IResolvedQualifiersReference;
+import org.apache.flex.compiler.definitions.references.ReferenceFactory;
+import org.apache.flex.compiler.internal.css.CSSArrayPropertyValue;
+import org.apache.flex.compiler.internal.css.CSSColorPropertyValue;
+import org.apache.flex.compiler.internal.css.CSSFunctionCallPropertyValue;
+import org.apache.flex.compiler.internal.css.CSSKeywordPropertyValue;
+import org.apache.flex.compiler.internal.css.CSSNumberPropertyValue;
+import org.apache.flex.compiler.internal.css.CSSPropertyValue;
+import org.apache.flex.compiler.internal.css.CSSRgbColorPropertyValue;
+import org.apache.flex.compiler.internal.css.CSSStringPropertyValue;
 import org.apache.flex.compiler.internal.css.codegen.CSSCompilationSession;
+import org.apache.flex.compiler.problems.CSSCodeGenProblem;
+import org.apache.flex.compiler.problems.ICompilerProblem;
+import org.apache.flex.compiler.projects.ICompilerProject;
 
 import com.google.common.collect.ImmutableList;
 
 public class JSCSSCompilationSession extends CSSCompilationSession
 {
 
+    private ArrayList<String> requires;
+    
+    public String getEncodedCSS(ICompilerProject project, final Collection<ICompilerProblem> problems)
+    {
+        final ICSSDocument css = synthesisNormalizedCSS();
+        StringBuilder sb = new StringBuilder();
+        requires = new ArrayList<String>();
+        encodeCSS(css, sb, project, problems);
+        sb.append("];\n");
+        for (String r : requires)
+        {
+            sb.append("goog.require('" + r + "');\n");
+        }
+
+        return sb.toString();        
+    }
+    
     public String emitCSS()
     {
         final ICSSDocument css = synthesisNormalizedCSS();
@@ -26,4 +65,193 @@ public class JSCSSCompilationSession extends CSSCompilationSession
             sb.append("\n\n");
         }
     }
+    
+    private void encodeCSS(ICSSDocument css, StringBuilder sb,
+            ICompilerProject project, final Collection<ICompilerProblem> problems)
+    {
+        ImmutableList<ICSSRule> rules = css.getRules();
+        boolean skipcomma = true;
+        for (ICSSRule rule : rules)
+        {
+            String s = encodeRule(rule, project, problems);
+            if (s != null)
+            {
+                if (skipcomma)
+                    skipcomma = false;
+                else
+                    sb.append(",\n");
+                sb.append(s);
+            }
+        }
+    }
+    
+    private String encodeRule(ICSSRule rule,
+            ICompilerProject project, final Collection<ICompilerProblem> problems)
+    {
+        final StringBuilder result = new StringBuilder();
+
+        ImmutableList<ICSSMediaQueryCondition> mqlist = rule.getMediaQueryConditions();
+        int n = mqlist.size();
+        if (n > 0)
+        {
+            if (mqlist.get(0).toString().equals("-flex-flash"))
+                return null;
+            
+            result.append(n);
+            
+            for (ICSSMediaQueryCondition mqcond : mqlist)
+            {
+                result.append(",\n");
+                result.append("\"" + mqcond.toString() + "\"");
+            }
+        }
+        else
+            result.append(n);
+
+        result.append(",\n");
+
+        ImmutableList<ICSSSelector> slist = rule.getSelectorGroup();
+        result.append(slist.size());
+
+        for (ICSSSelector sel : slist)
+        {
+            result.append(",\n");
+            String selName = this.resolvedSelectors.get(sel);
+            if (selName == null || selName.equals("null"))
+                result.append("\"" + sel.toString() + "\"");
+            else
+            {
+                ImmutableList<ICSSSelectorCondition> conds = sel.getConditions();
+                for (ICSSSelectorCondition cond : conds)
+                    selName += cond.toString();
+                result.append("\"" + selName + "\"");
+            }
+        }
+        result.append(",\n");
+        
+        ImmutableList<ICSSProperty> plist = rule.getProperties();
+        result.append(plist.size());
+        
+        for (final ICSSProperty prop : plist)
+        {
+            result.append(",\n");
+            result.append("\"" + prop.getName() + "\"");
+            result.append(",\n");
+            ICSSPropertyValue value = prop.getValue();
+            if (value instanceof CSSArrayPropertyValue)
+            {
+                ImmutableList<? extends ICSSPropertyValue> values = ((CSSArrayPropertyValue)value).getElements();
+                result.append("[");
+                boolean firstone = true;
+                for (ICSSPropertyValue val : values)
+                {
+                    if (firstone)
+                        firstone = false;
+                    else
+                        result.append(", ");
+                    if (val instanceof CSSStringPropertyValue)
+                    {
+                        result.append("\"" + ((CSSStringPropertyValue)val).getValue() + "\"");
+                    }
+                    else if (val instanceof CSSColorPropertyValue)
+                    {
+                        result.append(new Integer(((CSSColorPropertyValue)val).getColorAsInt()));
+                    }
+                    else if (val instanceof CSSRgbColorPropertyValue)
+                    {
+                        result.append(new Integer(((CSSRgbColorPropertyValue)val).getColorAsInt()));
+                    }
+                    else if (val instanceof CSSKeywordPropertyValue)
+                    {
+                        CSSKeywordPropertyValue keywordValue = (CSSKeywordPropertyValue)val;
+                        String keywordString = keywordValue.getKeyword();
+                        if (IASLanguageConstants.TRUE.equals(keywordString))
+                            result.append("true");
+                        else if (IASLanguageConstants.FALSE.equals(keywordString))
+                            result.append("false");
+                        else
+                            result.append("\"" + ((CSSKeywordPropertyValue)val).getKeyword() + "\"");
+                    }
+                    else if (val instanceof CSSNumberPropertyValue)
+                    {
+                        result.append(new Double(((CSSNumberPropertyValue)val).getNumber().doubleValue()));
+                    }
+                    else
+                    {
+                        result.append("unexpected value type: " + val.toString());
+                    }
+                }
+                result.append("]");
+            }
+            else if (value instanceof CSSStringPropertyValue)
+            {
+                result.append("\"" + ((CSSStringPropertyValue)value).getValue() + "\"");
+            }
+            else if (value instanceof CSSColorPropertyValue)
+            {
+                result.append(new Integer(((CSSColorPropertyValue)value).getColorAsInt()));
+            }
+            else if (value instanceof CSSRgbColorPropertyValue)
+            {
+                result.append(new Integer(((CSSRgbColorPropertyValue)value).getColorAsInt()));
+            }
+            else if (value instanceof CSSKeywordPropertyValue)
+            {
+                CSSKeywordPropertyValue keywordValue = (CSSKeywordPropertyValue)value;
+                String keywordString = keywordValue.getKeyword();
+                if (IASLanguageConstants.TRUE.equals(keywordString))
+                    result.append("true");
+                else if (IASLanguageConstants.FALSE.equals(keywordString))
+                    result.append("false");
+                else
+                    result.append("\"" + ((CSSKeywordPropertyValue)value).getKeyword() + "\"");
+            }
+            else if (value instanceof CSSNumberPropertyValue)
+            {
+                result.append(new Double(((CSSNumberPropertyValue)value).getNumber().doubleValue()));
+            }
+            else if (value instanceof CSSFunctionCallPropertyValue)
+            {
+                final CSSFunctionCallPropertyValue functionCall = (CSSFunctionCallPropertyValue)value;
+                if ("ClassReference".equals(functionCall.name))
+                {
+                    final String className = CSSFunctionCallPropertyValue.getSingleArgumentFromRaw(functionCall.rawArguments);
+                    if ("null".equals(className))
+                    {
+                        // ClassReference(null) resets the property's class reference.
+                        result.append("null");
+                    }
+                    else
+                    {
+                        final IResolvedQualifiersReference reference = ReferenceFactory.packageQualifiedReference(project.getWorkspace(), className);
+                        result.append(className);
+                        requires.add(className);
+                    }
+                }
+                else if ("url".equals(functionCall.name))
+                {
+                    final String urlString = CSSFunctionCallPropertyValue.getSingleArgumentFromRaw(functionCall.rawArguments);
+                    result.append("\"" + urlString + "\"");
+                }
+                else if ("PropertyReference".equals(functionCall.name))
+                {
+                    // TODO: implement me
+                }
+                else if ("Embed".equals(functionCall.name))
+                {
+                    final ICompilerProblem e = new CSSCodeGenProblem(
+                            new IllegalStateException("Unable to find compilation unit for " + functionCall));
+                    problems.add(e);
+                }
+                else
+                {
+                    assert false : "CSS parser bug: unexpected function call property value: " + functionCall;
+                    throw new IllegalStateException("Unexpected function call property value: " + functionCall);
+                }
+            }
+        }
+
+        return result.toString();
+
+    }
 }

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/ee9a0d0b/compiler.jx/src/org/apache/flex/compiler/internal/projects/FlexJSProject.java
----------------------------------------------------------------------
diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/projects/FlexJSProject.java b/compiler.jx/src/org/apache/flex/compiler/internal/projects/FlexJSProject.java
index eb1adfe..0a69124 100644
--- a/compiler.jx/src/org/apache/flex/compiler/internal/projects/FlexJSProject.java
+++ b/compiler.jx/src/org/apache/flex/compiler/internal/projects/FlexJSProject.java
@@ -53,6 +53,7 @@ public class FlexJSProject extends FlexProject
 
     public ICompilationUnit mainCU;
     public String cssDocument;
+    public String cssEncoding;
 
     @Override
     public void addDependency(ICompilationUnit from, ICompilationUnit to,

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/ee9a0d0b/compiler.jx/src/org/apache/flex/compiler/internal/targets/FlexJSTarget.java
----------------------------------------------------------------------
diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/targets/FlexJSTarget.java b/compiler.jx/src/org/apache/flex/compiler/internal/targets/FlexJSTarget.java
index 3485f7b..ddc9173 100644
--- a/compiler.jx/src/org/apache/flex/compiler/internal/targets/FlexJSTarget.java
+++ b/compiler.jx/src/org/apache/flex/compiler/internal/targets/FlexJSTarget.java
@@ -225,6 +225,7 @@ public class FlexJSTarget extends JSTarget implements IJSTarget
         try
         {
             flexProject.cssDocument = cssCompilationSession.emitCSS();
+            flexProject.cssEncoding = cssCompilationSession.getEncodedCSS(flexProject, problems);
         }
         catch (Exception e)
         {