Bug 253974: WPE design view's default unknown tag converter renders some tags poorly.
diff --git a/jsf/plugins/org.eclipse.jst.pagedesigner/src/org/eclipse/jst/pagedesigner/converter/ConverterUtil.java b/jsf/plugins/org.eclipse.jst.pagedesigner/src/org/eclipse/jst/pagedesigner/converter/ConverterUtil.java
index 32531b7..8476dfd 100644
--- a/jsf/plugins/org.eclipse.jst.pagedesigner/src/org/eclipse/jst/pagedesigner/converter/ConverterUtil.java
+++ b/jsf/plugins/org.eclipse.jst.pagedesigner/src/org/eclipse/jst/pagedesigner/converter/ConverterUtil.java
@@ -15,6 +15,7 @@
 
 import org.eclipse.jst.pagedesigner.IHTMLConstants;
 import org.eclipse.jst.pagedesigner.PDPlugin;
+import org.eclipse.jst.pagedesigner.dtmanager.DTManager;
 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMText;
 import org.w3c.dom.Attr;
@@ -86,7 +87,7 @@
 	/**
 	 * @param document
 	 * @param text
-	 * @return the descripton element in the document containing text
+	 * @return the description element in the document containing text
 	 */
 	public static Element createDescriptionElement(IDOMDocument document,
 			String text) {
@@ -104,4 +105,112 @@
 		}
 		return span;
 	}
+
+	/**
+	 * Method to check if the resulting converted tag of a source
+	 * element is contained within a table. Recursively walks up
+	 * the source element's ancestry to get a result element from
+	 * tag conversion that indicates that the element will be
+	 * rendered in a table. The converted element that will be
+	 * the parent tag is returned so the caller can then determine
+	 * if the parent is a table, header, body, footer, row, or cell
+	 * element. 
+	 * 
+	 * @param srcElem the source element to test.
+	 * @param childElem a child of the source element (used by a
+	 *                  recursive call to handle special case where
+	 *                  it was moved up a level to the child model
+	 *                  list of the grandparent).
+	 * @return a converted element for a table or tag within a table. 
+	 */
+	public static Node findAncestorTableElement(Element srcElem, Element childElem) {
+		Node parent = srcElem.getParentNode();
+		if ((parent == null) || !(parent instanceof Element)) {
+			return null;
+		}
+
+		String name = parent.getNodeName();
+		if (IHTMLConstants.TAG_HTML.equalsIgnoreCase(name)
+				|| IHTMLConstants.TAG_BODY.equalsIgnoreCase(name)) {
+			return null;
+		}
+
+		ITagConverter converter = createTagConverter((Element) parent);
+		if (!converter.isVisualByHTML()) {
+			return null;
+		}
+
+		converter.convertRefresh(null);
+		ConvertPosition position = null;
+		if (childElem != null) {
+			// If a child elem (grand child of current parent) was
+			// passed in, check for its position. It may have been
+			// moved up a level to child model list of the current
+			// parent. In JSF this is done with a header or
+			// footer facet tag in a column tag for a dataTable.
+			position = converter.getChildVisualPosition(childElem);
+		}
+		if (position == null) {
+			position = converter.getChildVisualPosition(srcElem);
+		}
+		if (position != null) {
+			// check the converted ancestor to see if this element
+			// is contained in a table.
+			Node node = position.getParentNode();
+			Node tableItem = findTableElemContainingNode(node);
+			if (tableItem != null) {
+				// return the node that will contain the visual
+				// child, not the actual table element found.
+				return node;
+			}
+
+			Node resultFromParent = findAncestorTableElement((Element) parent, null);
+			if (resultFromParent != null) {
+				// return the node that will contain the visual
+				// child, not the result from the parent.
+				return node;
+			}
+		}
+		if (position == null) {
+			// The current src element is not in the child model
+			// list for the converted parent so recurse to next
+			// ancestor and pass src element to see if it has been
+			// moved up a level as child model of the grandparent.
+			return findAncestorTableElement((Element) parent, srcElem);
+		}
+
+		return null;
+	}
+
+	private static ITagConverter createTagConverter(Element ele) {
+		return DTManager.getInstance().getTagConverter(ele,
+				IConverterFactory.MODE_DESIGNER, null);
+	}
+
+	private static Node findTableElemContainingNode(Node elem) {
+		if ((elem == null) || !(elem instanceof Element)) {
+			return null;
+		}
+
+		if (isTableElem(elem)) {
+			return elem;
+		}
+
+		return findTableElemContainingNode(elem.getParentNode());
+	}
+
+	private static boolean isTableElem(Node elem) {
+		if (elem instanceof Element) {
+			if (IHTMLConstants.TAG_TABLE.equalsIgnoreCase(elem.getNodeName())
+					|| IHTMLConstants.TAG_TR.equalsIgnoreCase(elem.getNodeName())
+					|| IHTMLConstants.TAG_TH.equalsIgnoreCase(elem.getNodeName())
+					|| IHTMLConstants.TAG_TD.equalsIgnoreCase(elem.getNodeName())
+					|| IHTMLConstants.TAG_TBODY.equalsIgnoreCase(elem.getNodeName())
+					|| IHTMLConstants.TAG_THEAD.equalsIgnoreCase(elem.getNodeName())
+					|| IHTMLConstants.TAG_TFOOT.equalsIgnoreCase(elem.getNodeName())) {
+				return true;
+			}
+		}
+		return false;
+	}
 }
diff --git a/jsf/plugins/org.eclipse.jst.pagedesigner/src/org/eclipse/jst/pagedesigner/converter/DefaultUnknownTagConverter.java b/jsf/plugins/org.eclipse.jst.pagedesigner/src/org/eclipse/jst/pagedesigner/converter/DefaultUnknownTagConverter.java
index c0ba0b2..2e9d191 100644
--- a/jsf/plugins/org.eclipse.jst.pagedesigner/src/org/eclipse/jst/pagedesigner/converter/DefaultUnknownTagConverter.java
+++ b/jsf/plugins/org.eclipse.jst.pagedesigner/src/org/eclipse/jst/pagedesigner/converter/DefaultUnknownTagConverter.java
@@ -11,8 +11,10 @@
  *******************************************************************************/
 package org.eclipse.jst.pagedesigner.converter;
 
+import org.eclipse.jst.pagedesigner.IHTMLConstants;
 import org.eclipse.jst.pagedesigner.utils.DOMUtil;
 import org.w3c.dom.Element;
+import org.w3c.dom.Node;
 import org.w3c.dom.Text;
 
 /**
@@ -23,6 +25,10 @@
  */
 public class DefaultUnknownTagConverter extends AbstractTagConverter {
 
+	private static final int NO_ELEMENT = 0;
+	private static final int TABLE_ELEMENT = 1;
+	private static final int TABLE_ROW_ELEMENT = 2;
+
 	/**
 	 * @param host
 	 * @param mode 
@@ -39,6 +45,43 @@
 	 */
 	protected Element doConvertRefresh() {
 		Element hostEle = this.getHostElement();
+
+		// Test to see if the src element is contained in an
+		// element that renders as a table. If so, render this
+		// element accordingly as content in the table.
+		// This is done to address the use case where tags
+		// (such as JSTL) are used in collaboration within HTML
+		// tables. The CSS layout for tables does not handle
+		// invalid HTML so this change tries to produce valid
+		// HTML. If the table layout code gets updated to handle
+		// invalid HTML tables, then this code can be removed.
+		// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=253974
+		Node containingElement = ConverterUtil.findAncestorTableElement(hostEle, null);
+		String name = null;
+		if (containingElement != null) {
+			name = containingElement.getNodeName(); 
+		}
+		if (name != null) {
+			if (IHTMLConstants.TAG_TABLE.equalsIgnoreCase(name)
+					|| IHTMLConstants.TAG_TBODY.equalsIgnoreCase(name)
+					|| IHTMLConstants.TAG_TFOOT.equalsIgnoreCase(name)) {
+				// this element is contained in a table, tbody or tfoot
+				return renderAsTableRow(hostEle, false, TABLE_ELEMENT);
+			} else if (IHTMLConstants.TAG_THEAD.equalsIgnoreCase(name)) {
+				// this element is contained in a thead
+				return renderAsTableRow(hostEle, true, TABLE_ELEMENT);
+			} else if (IHTMLConstants.TAG_TR.equalsIgnoreCase(name)) {
+				// this element is contained in a tr
+				return renderAsTableCell(hostEle, false, TABLE_ROW_ELEMENT);
+			}
+		}
+		
+		// Otherwise, use the default rendering for an unknown tag
+		return renderDefault(hostEle, NO_ELEMENT);
+	}
+
+	private Element renderDefault(Element hostEle, int tableElement) {
+		// rendering for host element not contained in a table
 		Element divEle = createElement("div"); //$NON-NLS-1$
 		String style = DOMUtil.getAttributeIgnoreCase(hostEle, "style"); //$NON-NLS-1$
 		if (style == null) {
@@ -57,15 +100,58 @@
 
 		divEle.appendChild(div2);
 
-		Element div3 = createElement("div"); //$NON-NLS-1$
-		div3.setAttribute("style", "margin: 0; padding: 0"); //$NON-NLS-1$ //$NON-NLS-2$
-		divEle.appendChild(div3);
+		Element childContainer = null;
+		switch (tableElement) {
+		case TABLE_ELEMENT:
+			childContainer = createElement(IHTMLConstants.TAG_TABLE);
+			copyChildren(getHostElement(), childContainer);
+			break;
+		case TABLE_ROW_ELEMENT:
+			childContainer = createElement(IHTMLConstants.TAG_TABLE);
+			Element trElem = createElement(IHTMLConstants.TAG_TR);
+			childContainer.appendChild(trElem);
+			copyChildren(getHostElement(), trElem);
+			break;
+		case NO_ELEMENT:
+		default:
+			childContainer = createElement("div"); //$NON-NLS-1$
+			childContainer.setAttribute("style", "margin: 0; padding: 0"); //$NON-NLS-1$ //$NON-NLS-2$
+			copyChildren(getHostElement(), childContainer);
+			break;
+		}
 
-		copyChildren(getHostElement(), div3);
+		divEle.appendChild(childContainer);
+
 		return divEle;
 	}
 
 	/*
+	 * Creates a table row, and adds either a table cell (data
+	 * or a header depending on the boolean flag).
+	 */
+	private Element renderAsTableRow(Element hostEle, boolean isHeader, int tableElement) {
+		Element trElem = createElement(IHTMLConstants.TAG_TR);
+		Element tdElem = renderAsTableCell(hostEle, isHeader, tableElement);
+		trElem.appendChild(tdElem);
+		return trElem;
+	}
+
+	/*
+	 * Creates a table cell, as either data or a header depending
+	 * on the boolean flag.
+	 */
+	private Element renderAsTableCell(Element hostEle, boolean isHeader, int tableElement) {
+		Element tdElem = null;
+		if (isHeader) {
+			tdElem = createElement(IHTMLConstants.TAG_TH);
+		} else {
+			tdElem = createElement(IHTMLConstants.TAG_TD);
+		}
+		tdElem.appendChild(renderDefault(hostEle, tableElement));
+		return tdElem;
+	}
+
+	/*
 	 * (non-Javadoc)
 	 * 
 	 * @see org.eclipse.jst.pagedesigner.converter.ITagConverter#isMultiLevel()