7030332: Default borders in tables looks incorrect JEditorPane
authorrupashka
Mon, 29 Aug 2011 16:25:04 +0400
changeset 10320 fb28887eb9f2
parent 10319 dbbded066362
child 10321 64f7ee2f31dd
child 10409 560001064c49
7030332: Default borders in tables looks incorrect JEditorPane Reviewed-by: peterz
jdk/src/share/classes/javax/swing/text/html/CSS.java
jdk/src/share/classes/javax/swing/text/html/TableView.java
jdk/test/javax/swing/text/html/TableView/7030332/bug7030332.html
jdk/test/javax/swing/text/html/TableView/7030332/bug7030332.java
jdk/test/javax/swing/text/html/TableView/7030332/sample0.png
jdk/test/javax/swing/text/html/TableView/7030332/sample1.png
jdk/test/javax/swing/text/html/TableView/7030332/sample2.png
jdk/test/javax/swing/text/html/TableView/7030332/sample3.png
jdk/test/javax/swing/text/html/TableView/7030332/sample4.png
--- a/jdk/src/share/classes/javax/swing/text/html/CSS.java	Tue Aug 23 17:56:14 2011 +0400
+++ b/jdk/src/share/classes/javax/swing/text/html/CSS.java	Mon Aug 29 16:25:04 2011 +0400
@@ -806,15 +806,11 @@
             // translate border width into the cells, if it has non-zero value.
             AttributeSet tableAttr = elem.getParentElement().
                                      getParentElement().getAttributes();
-            int borderWidth;
-            try {
-                borderWidth = Integer.parseInt(
-                    (String) tableAttr.getAttribute(HTML.Attribute.BORDER));
-            } catch (NumberFormatException e) {
-                borderWidth = 0;
-            }
+
+            int borderWidth = getTableBorder(tableAttr);
             if (borderWidth > 0) {
-                translateAttribute(HTML.Attribute.BORDER, tableAttr, cssAttrSet);
+                // If table contains the BORDER attribute cells should have border width equals 1
+                translateAttribute(HTML.Attribute.BORDER, "1", cssAttrSet);
             }
             String pad = (String)tableAttr.getAttribute(HTML.Attribute.CELLPADDING);
             if (pad != null) {
@@ -850,6 +846,21 @@
         return cssAttrSet;
     }
 
+    private static int getTableBorder(AttributeSet tableAttr) {
+        String borderValue = (String) tableAttr.getAttribute(HTML.Attribute.BORDER);
+
+        if (borderValue == HTML.NULL_ATTRIBUTE_VALUE || "".equals(borderValue)) {
+            // Some browsers accept <TABLE BORDER> and <TABLE BORDER=""> with the same semantics as BORDER=1
+            return 1;
+        }
+
+        try {
+            return Integer.parseInt(borderValue);
+        } catch (NumberFormatException e) {
+            return 0;
+        }
+    }
+
     private static final Hashtable<String, Attribute> attributeMap = new Hashtable<String, Attribute>();
     private static final Hashtable<String, Value> valueMap = new Hashtable<String, Value>();
 
@@ -1400,17 +1411,20 @@
                         }
                     }
                 } else {
-
-                    /*
-                     * The html size attribute has a mapping in the CSS world only
-                     * if it is par of a font or base font tag.
-                     */
-
                     if (key == HTML.Attribute.SIZE && !isHTMLFontTag(tag)) {
-                        continue;
+                        /*
+                         * The html size attribute has a mapping in the CSS world only
+                         * if it is par of a font or base font tag.
+                         */
+                    } else if (tag == HTML.Tag.TABLE && key == HTML.Attribute.BORDER) {
+                        int borderWidth = getTableBorder(htmlAttrSet);
+
+                        if (borderWidth > 0) {
+                            translateAttribute(HTML.Attribute.BORDER, Integer.toString(borderWidth), cssAttrSet);
+                        }
+                    } else {
+                        translateAttribute(key, (String) htmlAttrSet.getAttribute(key), cssAttrSet);
                     }
-
-                    translateAttribute(key, htmlAttrSet, cssAttrSet);
                 }
             } else if (name instanceof CSS.Attribute) {
                 cssAttrSet.addAttribute(name, htmlAttrSet.getAttribute(name));
@@ -1419,7 +1433,7 @@
     }
 
     private void translateAttribute(HTML.Attribute key,
-                                           AttributeSet htmlAttrSet,
+                                           String htmlAttrValue,
                                            MutableAttributeSet cssAttrSet) {
         /*
          * In the case of all remaining HTML.Attribute's they
@@ -1427,8 +1441,6 @@
          */
         CSS.Attribute[] cssAttrList = getCssAttribute(key);
 
-        String htmlAttrValue = (String)htmlAttrSet.getAttribute(key);
-
         if (cssAttrList == null || htmlAttrValue == null) {
             return;
         }
--- a/jdk/src/share/classes/javax/swing/text/html/TableView.java	Tue Aug 23 17:56:14 2011 +0400
+++ b/jdk/src/share/classes/javax/swing/text/html/TableView.java	Mon Aug 29 16:25:04 2011 +0400
@@ -242,7 +242,8 @@
             if (lv != null) {
                 cellSpacing = (int) lv.getValue();
             } else {
-                cellSpacing = 0;
+                // Default cell spacing equals 2
+                cellSpacing = 2;
             }
             lv = (CSS.LengthValue)
                     attr.getAttribute(CSS.Attribute.BORDER_TOP_WIDTH);
@@ -251,8 +252,7 @@
             } else {
                     borderWidth = 0;
             }
-
-                }
+        }
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/text/html/TableView/7030332/bug7030332.html	Mon Aug 29 16:25:04 2011 +0400
@@ -0,0 +1,12 @@
+<html>
+<body>
+<applet  code="bug7030332.class" width=600 height=400></applet>
+
+Compare Golden Images with rendered JEditorPane.
+They should looks simalar in each line. Pay attention to:
+
+1. Border width around tables
+2. Border width around cells
+
+</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/text/html/TableView/7030332/bug7030332.java	Mon Aug 29 16:25:04 2011 +0400
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+   @bug 7030332
+   @summary Default borders in tables looks incorrect JEditorPane
+   @author Pavel Porvatov
+ * @run applet/manual=yesno bug7030332.html
+*/
+
+import javax.swing.*;
+import java.awt.*;
+import java.net.URL;
+
+public class bug7030332 extends JApplet {
+    public static final String[] HTML_SAMPLES = new String[]{
+            "<table border><tr><th>Column1</th><th>Column2</th></tr></table>",
+            "<table border=\"\"><tr><th>Column1</th><th>Column2</th></tr></table>",
+            "<table border=\"1\"><tr><th>Column1</th><th>Column2</th></tr></table>",
+            "<table border=\"2\"><tr><th>Column1</th><th>Column2</th></tr></table>",
+    };
+
+    public static void main(String[] args) throws Exception {
+        SwingUtilities.invokeLater(new Runnable() {
+            public void run() {
+                JFrame frame = new JFrame();
+
+                frame.setContentPane(createContentPane());
+                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+                frame.setSize(600, 400);
+                frame.setLocationRelativeTo(null);
+
+                frame.setVisible(true);
+
+            }
+        });
+    }
+
+    public void init() {
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    setContentPane(createContentPane());
+                }
+            });
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private static Container createContentPane() {
+        JPanel result = new JPanel(new GridLayout(HTML_SAMPLES.length + 1, 3, 10, 10));
+
+        result.add(new JLabel("Html code"));
+        result.add(new JLabel("Golden image"));
+        result.add(new JLabel("JEditorPane"));
+
+        for (int i = 0; i < HTML_SAMPLES.length; i++) {
+            String htmlSample = HTML_SAMPLES[i];
+
+            JTextArea textArea = new JTextArea(htmlSample);
+
+            textArea.setLineWrap(true);
+
+            result.add(textArea);
+
+            String imageName = "sample" + i + ".png";
+            URL resource = bug7030332.class.getResource(imageName);
+
+            result.add(resource == null ? new JLabel(imageName + " not found") :
+                    new JLabel(new ImageIcon(resource), SwingConstants.LEFT));
+
+            result.add(new JEditorPane("text/html", htmlSample));
+        }
+
+        return result;
+    }
+}
Binary file jdk/test/javax/swing/text/html/TableView/7030332/sample0.png has changed
Binary file jdk/test/javax/swing/text/html/TableView/7030332/sample1.png has changed
Binary file jdk/test/javax/swing/text/html/TableView/7030332/sample2.png has changed
Binary file jdk/test/javax/swing/text/html/TableView/7030332/sample3.png has changed
Binary file jdk/test/javax/swing/text/html/TableView/7030332/sample4.png has changed