This fixes some minor stability issues with the HTML renderer. Most
importantly it makes the renderer handle misplaced td th and tr tags
handle more gracefully. Also, this fixes the alignment of blocks.
This should be OK to go into the release branch, though not strictly
necessary.
2006-12-06 Roman Kennke <[EMAIL PROTECTED]>
* javax/swing/text/html/BlockView.java
(getAlignment): Align blocks horizontally by the superclass.
* javax/swing/text/html/HTMLEditorKit.java
(HTMLFactory.create): Replace equals comparison by == for efficiency.
Add mapping for misplaced tr, td and th tags. Include object mapping.
* javax/swing/text/html/TableView.java
(RowView.replace): Invalidate grid early.
(gridValid): Initialize with false.
(create): Only create RowView and CellView for correctly placed
tags. Avoid unnecessary casts.
(getAlignment): Removed.
(replace): Invalidate grid early.
/Roman
Index: javax/swing/text/html/BlockView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/BlockView.java,v
retrieving revision 1.9
diff -u -1 -5 -r1.9 BlockView.java
--- javax/swing/text/html/BlockView.java 5 Dec 2006 21:52:51 -0000 1.9
+++ javax/swing/text/html/BlockView.java 6 Dec 2006 19:35:09 -0000
@@ -511,31 +511,31 @@
return 0;
if (axis == X_AXIS)
return 1;
throw new IllegalArgumentException("Invalid Axis");
}
/**
* Gets the alignment.
*
* @param axis - the axis to get the alignment for.
* @return the alignment.
*/
public float getAlignment(int axis)
{
if (axis == X_AXIS)
- return 0.0F;
+ return super.getAlignment(axis);
if (axis == Y_AXIS)
{
if (getViewCount() == 0)
return 0.0F;
float prefHeight = getPreferredSpan(Y_AXIS);
View first = getView(0);
float firstRowHeight = first.getPreferredSpan(Y_AXIS);
return prefHeight != 0 ? (firstRowHeight * first.getAlignment(Y_AXIS))
/ prefHeight
: 0;
}
throw new IllegalArgumentException("Invalid Axis");
}
/**
Index: javax/swing/text/html/HTMLEditorKit.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/HTMLEditorKit.java,v
retrieving revision 1.46
diff -u -1 -5 -r1.46 HTMLEditorKit.java
--- javax/swing/text/html/HTMLEditorKit.java 5 Dec 2006 22:18:37 -0000 1.46
+++ javax/swing/text/html/HTMLEditorKit.java 6 Dec 2006 19:35:10 -0000
@@ -780,72 +780,77 @@
* @param element the <code>Element</code> to create a <code>View</code>
* for
* @return the <code>View</code> for the specified <code>Element</code>
* or <code>null</code> if the type of <code>element</code> is
* not supported
*/
public View create(Element element)
{
View view = null;
Object attr =
element.getAttributes().getAttribute(StyleConstants.NameAttribute);
if (attr instanceof HTML.Tag)
{
HTML.Tag tag = (HTML.Tag) attr;
- if (tag.equals(HTML.Tag.IMPLIED) || tag.equals(HTML.Tag.P)
- || tag.equals(HTML.Tag.H1) || tag.equals(HTML.Tag.H2)
- || tag.equals(HTML.Tag.H3) || tag.equals(HTML.Tag.H4)
- || tag.equals(HTML.Tag.H5) || tag.equals(HTML.Tag.H6)
- || tag.equals(HTML.Tag.DT))
+ if (tag == HTML.Tag.IMPLIED || tag == HTML.Tag.P
+ || tag == HTML.Tag.H1 || tag == HTML.Tag.H2
+ || tag == HTML.Tag.H3 || tag == HTML.Tag.H4
+ || tag == HTML.Tag.H5 || tag == HTML.Tag.H6
+ || tag == HTML.Tag.DT)
view = new ParagraphView(element);
- else if (tag.equals(HTML.Tag.LI) || tag.equals(HTML.Tag.DL)
- || tag.equals(HTML.Tag.DD) || tag.equals(HTML.Tag.BODY)
- || tag.equals(HTML.Tag.HTML) || tag.equals(HTML.Tag.CENTER)
- || tag.equals(HTML.Tag.DIV)
- || tag.equals(HTML.Tag.BLOCKQUOTE)
- || tag.equals(HTML.Tag.PRE)
- || tag.equals(HTML.Tag.FORM))
+ else if (tag == HTML.Tag.LI || tag == HTML.Tag.DL
+ || tag == HTML.Tag.DD || tag == HTML.Tag.BODY
+ || tag == HTML.Tag.HTML || tag == HTML.Tag.CENTER
+ || tag == HTML.Tag.DIV
+ || tag == HTML.Tag.BLOCKQUOTE
+ || tag == HTML.Tag.PRE
+ || tag == HTML.Tag.FORM
+ // Misplaced TD and TH tags get mapped as vertical block.
+ // Note that correctly placed tags get mapped in TableView.
+ || tag == HTML.Tag.TD || tag == HTML.Tag.TH)
view = new BlockView(element, View.Y_AXIS);
- else if (tag.equals(HTML.Tag.IMG))
+ else if (tag == HTML.Tag.TR)
+ // Misplaced TR tags get mapped as horizontal blocks.
+ // Note that correctly placed tags get mapped in TableView.
+ view = new BlockView(element, View.X_AXIS);
+ else if (tag == HTML.Tag.IMG)
view = new ImageView(element);
- else if (tag.equals(HTML.Tag.CONTENT))
+ else if (tag == HTML.Tag.CONTENT)
view = new InlineView(element);
else if (tag == HTML.Tag.HEAD)
view = new NullView(element);
- else if (tag.equals(HTML.Tag.TABLE))
+ else if (tag == HTML.Tag.TABLE)
view = new javax.swing.text.html.TableView(element);
- else if (tag.equals(HTML.Tag.HR))
+ else if (tag == HTML.Tag.HR)
view = new HRuleView(element);
- else if (tag.equals(HTML.Tag.BR))
+ else if (tag == HTML.Tag.BR)
view = new BRView(element);
- else if (tag.equals(HTML.Tag.INPUT) || tag.equals(HTML.Tag.SELECT)
- || tag.equals(HTML.Tag.TEXTAREA))
+ else if (tag == HTML.Tag.INPUT || tag == HTML.Tag.SELECT
+ || tag == HTML.Tag.TEXTAREA)
view = new FormView(element);
- else if (tag.equals(HTML.Tag.MENU) || tag.equals(HTML.Tag.DIR)
- || tag.equals(HTML.Tag.UL) || tag.equals(HTML.Tag.OL))
+ else if (tag == HTML.Tag.MENU || tag == HTML.Tag.DIR
+ || tag == HTML.Tag.UL || tag == HTML.Tag.OL)
view = new ListView(element);
- else if (tag.equals(HTML.Tag.FRAMESET))
+ else if (tag == HTML.Tag.FRAMESET)
view = new FrameSetView(element);
- else if (tag.equals(HTML.Tag.FRAME))
+ else if (tag == HTML.Tag.FRAME)
view = new FrameView(element);
- // FIXME: Uncomment when the views have been implemented
- /*
- else if (tag.equals(HTML.Tag.OBJECT))
- view = new ObjectView(element); */
+ else if (tag == HTML.Tag.OBJECT)
+ view = new ObjectView(element);
}
if (view == null)
{
view = new NullView(element);
}
return view;
}
}
/**
* The abstract HTML parser declaration.
*/
public abstract static class Parser
{
/**
Index: javax/swing/text/html/TableView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/TableView.java,v
retrieving revision 1.13
diff -u -1 -5 -r1.13 TableView.java
--- javax/swing/text/html/TableView.java 5 Dec 2006 21:52:52 -0000 1.13
+++ javax/swing/text/html/TableView.java 6 Dec 2006 19:35:10 -0000
@@ -80,32 +80,32 @@
*/
int rowIndex;
/**
* Creates a new RowView.
*
* @param el the element for the row view
*/
RowView(Element el)
{
super(el, X_AXIS);
}
public void replace(int offset, int len, View[] views)
{
- super.replace(offset, len, views);
gridValid = false;
+ super.replace(offset, len, views);
}
/**
* Overridden to make rows not resizable along the Y axis.
*/
public float getMaximumSpan(int axis)
{
float span;
if (axis == Y_AXIS)
span = super.getPreferredSpan(axis);
else
span = Integer.MAX_VALUE;
return span;
}
@@ -323,31 +323,31 @@
Length[] columnWidths;
/**
* The total number of columns.
*/
int numColumns;
/**
* The table width.
*/
private Length width;
/**
* Indicates if the grid setup is ok.
*/
- boolean gridValid;
+ boolean gridValid = false;
/**
* Additional space that is added _between_ table cells.
*
* This is package private to avoid accessor methods.
*/
int cellSpacing;
/**
* A cached Rectangle object for reuse in paint().
*/
private Rectangle tmpRect;
/**
* Creates a new HTML table view for the specified element.
@@ -358,45 +358,44 @@
{
super(el, Y_AXIS);
totalColumnRequirements = new SizeRequirements();
tmpRect = new Rectangle();
}
/**
* Implementation of the ViewFactory interface for creating the
* child views correctly.
*/
public View create(Element elem)
{
View view = null;
AttributeSet atts = elem.getAttributes();
Object name = atts.getAttribute(StyleConstants.NameAttribute);
- if (name instanceof HTML.Tag)
- {
- HTML.Tag tag = (HTML.Tag) name;
- if (tag == HTML.Tag.TR)
- view = new RowView(elem);
- else if (tag == HTML.Tag.TD || tag == HTML.Tag.TH)
- view = new CellView(elem);
- else if (tag == HTML.Tag.CAPTION)
- view = new ParagraphView(elem);
- }
+ AttributeSet pAtts = elem.getParentElement().getAttributes();
+ Object pName = pAtts.getAttribute(StyleConstants.NameAttribute);
- // If we haven't mapped the element, then fall back to the standard
- // view factory.
- if (view == null)
+ if (name == HTML.Tag.TR && pName == HTML.Tag.TABLE)
+ view = new RowView(elem);
+ else if ((name == HTML.Tag.TD || name == HTML.Tag.TH)
+ && pName == HTML.Tag.TR)
+ view = new CellView(elem);
+ else if (name == HTML.Tag.CAPTION)
+ view = new ParagraphView(elem);
+ else
{
+ // If we haven't mapped the element, then fall back to the standard
+ // view factory.
View parent = getParent();
if (parent != null)
{
ViewFactory vf = parent.getViewFactory();
if (vf != null)
view = vf.create(elem);
}
}
return view;
}
/**
* Returns this object as view factory so that we get our TR, TD, TH
* and CAPTION subelements created correctly.
*/
@@ -932,49 +931,39 @@
public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f)
{
super.removeUpdate(e, a, this);
}
/**
* Overridden to replace view factory with this one.
*/
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f)
{
super.changedUpdate(e, a, this);
}
public void replace(int offset, int len, View[] views)
{
- super.replace(offset, len, views);
gridValid = false;
+ super.replace(offset, len, views);
}
/**
* We can't use the super class's paint() method because it might cut
* off multi-row children. Instead we trigger painting for all rows
* and let the rows sort out what to paint and what not.
*/
public void paint(Graphics g, Shape a)
{
Rectangle rect = a instanceof Rectangle ? (Rectangle) a : a.getBounds();
painter.paint(g, rect.x, rect.y, rect.width, rect.height, this);
int nRows = getViewCount();
Rectangle inside = getInsideAllocation(a);
for (int r = 0; r < nRows; r++)
{
tmpRect.setBounds(inside);
childAllocation(r, tmpRect);
paintChild(g, tmpRect, r);
}
}
- /**
- * Overridden to place a table centered in the horizontal direction.
- */
- public float getAlignment(int axis)
- {
- if (axis == X_AXIS)
- return 0.5F;
- else
- return super.getAlignment(axis);
- }
}