|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.awt.Dimension; |
| 25 | +import java.awt.Graphics; |
| 26 | +import java.awt.image.BufferedImage; |
| 27 | +import java.io.File; |
| 28 | +import java.io.IOException; |
| 29 | + |
| 30 | +import javax.imageio.ImageIO; |
| 31 | +import javax.swing.JEditorPane; |
| 32 | +import javax.swing.text.View; |
| 33 | +import javax.swing.text.html.CSS; |
| 34 | + |
| 35 | +/* |
| 36 | + * @test |
| 37 | + * @bug 8335967 |
| 38 | + * @summary Tests 'text-decoration: none' is respected |
| 39 | + * @run main HTMLTextDecorationNone |
| 40 | + */ |
| 41 | +public final class HTMLTextDecorationNone { |
| 42 | + private static final String HTML = """ |
| 43 | + <!DOCTYPE html> |
| 44 | + <html lang="en"> |
| 45 | + <head> |
| 46 | + <meta charset="UTF-8"> |
| 47 | + <title>text-decoration: none (<a>)</title> |
| 48 | + <style> |
| 49 | + a.none { text-decoration: none } |
| 50 | + </style> |
| 51 | + </head> |
| 52 | + <body> |
| 53 | + <p><a href="https://openjdk.org/">underlined</a></p> |
| 54 | + <p><a href="https://openjdk.org/" style="text-decoration: none">not underlined</a></p> |
| 55 | + <p><a href="https://openjdk.org/" class="none">not underlined</a></p> |
| 56 | + <p style="text-decoration: underline"><a |
| 57 | + href="https://openjdk.org/" style="text-decoration: none">underlined?</a></p> |
| 58 | + <p style="text-decoration: underline"><a |
| 59 | + href="https://openjdk.org/" class="none">underlined?</a></p> |
| 60 | + </body> |
| 61 | + </html> |
| 62 | + """; |
| 63 | + |
| 64 | + private static final boolean[] underlined = {true, false, false, true, true}; |
| 65 | + |
| 66 | + public static void main(String[] args) { |
| 67 | + final JEditorPane html = new JEditorPane("text/html", HTML); |
| 68 | + html.setEditable(false); |
| 69 | + |
| 70 | + final Dimension size = html.getPreferredSize(); |
| 71 | + html.setSize(size); |
| 72 | + |
| 73 | + BufferedImage image = new BufferedImage(size.width, size.height, |
| 74 | + BufferedImage.TYPE_INT_RGB); |
| 75 | + Graphics g = image.createGraphics(); |
| 76 | + // Paint the editor pane to ensure all views are created |
| 77 | + html.paint(g); |
| 78 | + g.dispose(); |
| 79 | + |
| 80 | + int errorCount = 0; |
| 81 | + String firstError = null; |
| 82 | + |
| 83 | + System.out.println("----- Views -----"); |
| 84 | + final View bodyView = html.getUI() |
| 85 | + .getRootView(html) |
| 86 | + .getView(1) |
| 87 | + .getView(1); |
| 88 | + for (int i = 0; i < bodyView.getViewCount(); i++) { |
| 89 | + View pView = bodyView.getView(i); |
| 90 | + View contentView = getContentView(pView); |
| 91 | + |
| 92 | + Object decorationAttr = |
| 93 | + contentView.getAttributes() |
| 94 | + .getAttribute(CSS.Attribute.TEXT_DECORATION); |
| 95 | + String decoration = decorationAttr == null |
| 96 | + ? "none" : decorationAttr.toString(); |
| 97 | + |
| 98 | + System.out.println(i + ": " + decoration); |
| 99 | + if (decoration.contains("underline") != underlined[i]) { |
| 100 | + errorCount++; |
| 101 | + if (firstError == null) { |
| 102 | + firstError = "Line " + i + ": " + decoration + " vs " |
| 103 | + + (underlined[i] ? "underline" : "none"); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (errorCount > 0) { |
| 109 | + saveImage(image); |
| 110 | + throw new RuntimeException(errorCount + " error(s) found, " |
| 111 | + + "the first one: " + firstError); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static View getContentView(View parent) { |
| 116 | + View view = parent.getView(0); |
| 117 | + return view.getViewCount() > 0 |
| 118 | + ? getContentView(view) |
| 119 | + : view; |
| 120 | + } |
| 121 | + |
| 122 | + private static void saveImage(BufferedImage image) { |
| 123 | + try { |
| 124 | + ImageIO.write(image, "png", |
| 125 | + new File("html.png")); |
| 126 | + } catch (IOException ignored) { } |
| 127 | + } |
| 128 | +} |
0 commit comments