This fixes some issues with the CSS handling:
- The precedence was inverse. The UA stylesheet should have least
precedence, rather than highest, etc.
- Added support for 'combined' selector specs, like:
p, h1 { blah; }
- Fixed the alignment handling for BlockView.


This makes the JAPI pages look almost perfect now:

http://kennke.org/%7Eroman/japi3.png


2006-11-16  Roman Kennke  <[EMAIL PROTECTED]>

        * gnu/javax/swing/text/html/css/CSSParser.java
        (parseRuleset): Support 'combined' selectors.
        (main): Adapt callback for combined selectors support.
        * gnu/javax/swing/text/html/css/CSSParserCallback.java
        (startStatement): Take selector array as argument, to
        support combined selectors.
        * javax/swing/text/html/BlockView.java
        (calculateMinorAxisRequirements): Fetch and apply alignment.
        * javax/swing/text/html/StyleSheet.java
        (CSSStyle): Inverted the constants for correct precedence.
        (CSSStyleSheetParserCallback.styles): New field. Stores the
current
        styles.
        (CSSStyleSheetParserCallback.style): Removed.
        (CSSStyleSheetParserCallback.declaration): Update multiple
styles.
        (CSSStyleSheetParserCallback.end): Push multiple styles.
        (CSSStyleSheetParserCallback.start): Initialize multiple styles.
/Roman
Index: gnu/javax/swing/text/html/css/CSSParser.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/javax/swing/text/html/css/CSSParser.java,v
retrieving revision 1.2
diff -u -1 -5 -r1.2 CSSParser.java
--- gnu/javax/swing/text/html/css/CSSParser.java	9 Nov 2006 16:31:30 -0000	1.2
+++ gnu/javax/swing/text/html/css/CSSParser.java	16 Nov 2006 16:06:28 -0000
@@ -33,30 +33,31 @@
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
 
 package gnu.javax.swing.text.html.css;
 
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.util.StringTokenizer;
 
 /**
  * A parser for CSS stylesheets.
  *
  * This parser is based on the simple CSS grammar describe in
  *
  * http://www.w3.org/TR/CSS21/syndata.html .
  *
  * @author Roman Kennke ([EMAIL PROTECTED])
  */
 // TODO: Maybe use more restrictive grammar:
 // http://www.w3.org/TR/CSS21/grammar.html#q1
 public class CSSParser
 {
 
@@ -146,31 +147,39 @@
   }
 
   /**
    * Parses a CSS rule set.
    *
    * @return <code>true</code> if the ruleset could be parsed successfully,
    *         <code>false</code> otherwise
    * 
    * @throws IOException if an IO or parse error occurs
    */
   private boolean parseRuleset()
     throws IOException
   {
     StringBuilder selector = new StringBuilder();
     parseSelector(selector);
-    callback.startStatement(new Selector(selector.toString()));
+    StringTokenizer selSplitter =
+      new StringTokenizer(selector.toString(), ",");
+    Selector[] sels = new Selector[selSplitter.countTokens()];
+    for (int i = 0; selSplitter.hasMoreTokens(); i++)
+      {
+        String sel = selSplitter.nextToken().trim();
+        sels[i] = new Selector(sel);
+      }
+    callback.startStatement(sels);
     // Read any number of whitespace.
     int token;
     do
       {
         token = readToken();
       } while (token == CSSScanner.S);
     boolean ret = true;
 
     if (token == CSSScanner.CURLY_LEFT)
       {
         // Read any number of whitespace.
         do
           {
             token = readToken();
           } while (token == CSSScanner.S);
@@ -446,33 +455,41 @@
         InputStream in;
         if (args.length > 0)
           {
             File file = new File(args[0]);
             in = new FileInputStream(file);
           }
         else
           {
             String name = "/javax/swing/text/html/default.css";
             in = CSSScanner.class.getResourceAsStream(name);
           }
         BufferedInputStream bin = new BufferedInputStream(in);
         InputStreamReader r = new InputStreamReader(bin);
         CSSParserCallback cb = new CSSParserCallback()
         {
-          public void startStatement(Selector selector)
+          public void startStatement(Selector[] selector)
           {
-            System.out.println("startStatement: " + selector);
+            System.out.print("startStatement: ");
+            for (int i = 0; i < selector.length; i++)
+              {
+                System.out.print(selector[i]);
+                if (i < selector.length - 1)
+                  System.out.print(',');
+                else
+                  System.out.println();
+              }
           }
           public void endStatement()
           {
             System.out.println("endStatement");
           }
           public void declaration(String property, String value)
           {
             System.out.println("declaration: " + property + ", " + value);
           }
         };
         CSSParser p = new CSSParser(r, cb);
         p.parse();
       }
     catch (IOException ex)
       {
Index: gnu/javax/swing/text/html/css/CSSParserCallback.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/javax/swing/text/html/css/CSSParserCallback.java,v
retrieving revision 1.2
diff -u -1 -5 -r1.2 CSSParserCallback.java
--- gnu/javax/swing/text/html/css/CSSParserCallback.java	9 Nov 2006 16:31:30 -0000	1.2
+++ gnu/javax/swing/text/html/css/CSSParserCallback.java	16 Nov 2006 16:06:28 -0000
@@ -50,31 +50,31 @@
   /**
    * Signals the beginning of a statement.
    *
    * A CSS statement is build up like follows:
    * <pre>
    * <selector> {
    *   ... declarations...
    * }
    * </pre>
    *
    * After startStatement(), the callback will receive zero to n callbacks
    * to declaration, followed by an endStatement() call.
    *
    * @param selector the selector of the statement.
    */
-  void startStatement(Selector selector);
+  void startStatement(Selector[] selector);
 
   /**
    * Signals the end of a statement.
    */
   void endStatement();
 
   /**
    * Signals the parsing of one declaration, which defines a mapping
    * from a property to a value.
    *
    * @param property the property
    * @param value the value
    */
   void declaration(String property, String value);
 
Index: javax/swing/text/html/BlockView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/BlockView.java,v
retrieving revision 1.5
diff -u -1 -5 -r1.5 BlockView.java
--- javax/swing/text/html/BlockView.java	9 Nov 2006 16:39:33 -0000	1.5
+++ javax/swing/text/html/BlockView.java	16 Nov 2006 16:06:28 -0000
@@ -159,30 +159,46 @@
     if (setCSSSpan(r, axis))
       {
         // If we have set the span from CSS, then we need to adjust
         // the margins.
         SizeRequirements parent = super.calculateMinorAxisRequirements(axis,
                                                                        null);
         int margin = axis == X_AXIS ? getLeftInset() + getRightInset()
                                     : getTopInset() + getBottomInset();
         r.minimum -= margin;
         r.preferred -= margin;
         r.maximum -= margin;
         constrainSize(axis, r, parent);
       }
     else
       r = super.calculateMinorAxisRequirements(axis, r);
+
+    // Apply text alignment if appropriate.
+    if (axis == X_AXIS)
+      {
+        Object o = getAttributes().getAttribute(CSS.Attribute.TEXT_ALIGN);
+        if (o != null)
+          {
+            String al = o.toString().trim();
+            if (al.equals("center"))
+              r.alignment = 0.5f;
+            else if (al.equals("right"))
+              r.alignment = 1.0f;
+            else
+              r.alignment = 0.0f;
+          }
+      }
     return r;
   }
 
   /**
    * Sets the span on the SizeRequirements object according to the
    * according CSS span value, when it is set.
    * 
    * @param r the size requirements
    * @param axis the axis
    *
    * @return <code>true</code> when the CSS span has been set,
    *         <code>false</code> otherwise
    */
   private boolean setCSSSpan(SizeRequirements r, int axis)
   {
Index: javax/swing/text/html/StyleSheet.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/StyleSheet.java,v
retrieving revision 1.15
diff -u -1 -5 -r1.15 StyleSheet.java
--- javax/swing/text/html/StyleSheet.java	15 Nov 2006 11:03:00 -0000	1.15
+++ javax/swing/text/html/StyleSheet.java	16 Nov 2006 16:06:28 -0000
@@ -95,100 +95,107 @@
  *
  * @author Lillian Angel ([EMAIL PROTECTED])
  */
 public class StyleSheet extends StyleContext
 {
 
   /**
    * Parses CSS stylesheets using the parser in gnu/javax/swing/html/css.
    *
    * This is package private to avoid accessor methods.
    */
   class CSSStyleSheetParserCallback
     implements CSSParserCallback
   {
     /**
-     * The current style.
+     * The current styles.
      */
-    private CSSStyle style;
+    private CSSStyle[] styles;
 
     /**
      * The precedence of the stylesheet to be parsed.
      */
     private int precedence;
 
     /**
      * Creates a new CSS parser. This parser parses a CSS stylesheet with
      * the specified precedence.
      *
      * @param prec the precedence, according to the constants defined in
      *        CSSStyle
      */
     CSSStyleSheetParserCallback(int prec)
     {
       precedence = prec;
     }
 
     /**
      * Called at the beginning of a statement.
      *
      * @param sel the selector
      */
-    public void startStatement(Selector sel)
+    public void startStatement(Selector[] sel)
     {
-      style = new CSSStyle(precedence, sel);
+      styles = new CSSStyle[sel.length];
+      for (int i = 0; i < sel.length; i++)
+        styles[i] = new CSSStyle(precedence, sel[i]);
     }
 
     /**
      * Called at the end of a statement.
      */
     public void endStatement()
     {
-      css.add(style);
-      style = null;
+      for (int i = 0; i < styles.length; i++)
+        css.add(styles[i]);
+      styles = null;
     }
 
     /**
      * Called when a declaration is parsed.
      *
      * @param property the property
      * @param value the value
      */
     public void declaration(String property, String value)
     {
       CSS.Attribute cssAtt = CSS.getAttribute(property);
       Object val = CSS.getValue(cssAtt, value);
-      CSS.addInternal(style, cssAtt, value);
-      if (cssAtt != null)
-        style.addAttribute(cssAtt, val);
+      for (int i = 0; i < styles.length; i++)
+        {
+          CSSStyle style = styles[i];
+          CSS.addInternal(style, cssAtt, value);
+          if (cssAtt != null)
+            style.addAttribute(cssAtt, val);
+        }
     }
 
   }
 
   /**
    * Represents a style that is defined by a CSS rule.
    */
   private class CSSStyle
     extends SimpleAttributeSet
     implements Style, Comparable
   {
 
-    static final int PREC_UA = 400000;
-    static final int PREC_NORM = 300000;
+    static final int PREC_UA = 0;
+    static final int PREC_NORM = 100000;
     static final int PREC_AUTHOR_NORMAL = 200000;
-    static final int PREC_AUTHOR_IMPORTANT = 100000;
-    static final int PREC_USER_IMPORTANT = 0;
+    static final int PREC_AUTHOR_IMPORTANT = 300000;
+    static final int PREC_USER_IMPORTANT = 400000;
 
     /**
      * The priority of this style when matching CSS selectors.
      */
     private int precedence;
 
     /**
      * The selector for this rule.
      *
      * This is package private to avoid accessor methods.
      */
     Selector selector;
 
     CSSStyle(int prec, Selector sel)
     {

Reply via email to