diff options
7 files changed, 229 insertions, 7 deletions
diff --git a/org.eclipse.help.base/src/org/eclipse/help/internal/xhtml/XHTMLCharsetHandler.java b/org.eclipse.help.base/src/org/eclipse/help/internal/xhtml/XHTMLCharsetHandler.java index 5d7e81e26..5f054e2c2 100644 --- a/org.eclipse.help.base/src/org/eclipse/help/internal/xhtml/XHTMLCharsetHandler.java +++ b/org.eclipse.help.base/src/org/eclipse/help/internal/xhtml/XHTMLCharsetHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2008 IBM Corporation and others. + * Copyright (c) 2006, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -12,9 +12,9 @@ package org.eclipse.help.internal.xhtml; import org.eclipse.help.internal.UAElement; import org.eclipse.help.internal.dynamic.ProcessorHandler; +import org.w3c.dom.Comment; import org.w3c.dom.Document; import org.w3c.dom.Element; -import org.w3c.dom.Text; /* * Converts the charset in XHTML meta tag to UTF-8. This is the encoding @@ -37,14 +37,29 @@ public class XHTMLCharsetHandler extends ProcessorHandler { return HANDLED_CONTINUE; } } - if ("script".equalsIgnoreCase(element.getElementName())) { //$NON-NLS-1$ + if (endTagRequired(element)) { Element domElement = element.getElement(); if (domElement.getFirstChild() == null) { Document document = domElement.getOwnerDocument(); - Text child = document.createTextNode(" "); //$NON-NLS-1$ + Comment child = document.createComment(" "); //$NON-NLS-1$ domElement.appendChild(child); } } return UNHANDLED; } + + /* + * Returns true if this element requires the end tag to be separate from the + * start tag to render correctly in browsers. + * i.e. generate <a></a> rather than <a/> + */ + private boolean endTagRequired(UAElement element) { + String elementName = element.getElementName(); + if ("a".equalsIgnoreCase(elementName)) return true; //$NON-NLS-1$ + if ("p".equalsIgnoreCase(elementName)) return true; //$NON-NLS-1$ + if ("div".equalsIgnoreCase(elementName)) return true; //$NON-NLS-1$ + if ("script".equalsIgnoreCase(elementName)) return true; //$NON-NLS-1$ + if ("textarea".equalsIgnoreCase(elementName)) return true; //$NON-NLS-1$ + return false; + } } diff --git a/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/XMLUtil.java b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/XMLUtil.java index 04f4b6d4d..b81b1d203 100644 --- a/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/XMLUtil.java +++ b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/XMLUtil.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2008 IBM Corporation and others. + * Copyright (c) 2006, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -41,13 +41,22 @@ public class XMLUtil extends Assert { InputStream in2 = new ByteArrayInputStream(s2.getBytes("UTF-8")); assertXMLEquals(msg, in1, in2); } - + public static void assertXMLEquals(String msg, InputStream in1, InputStream in2) throws Exception { String s1 = process(in1); String s2 = process(in2); assertEquals(msg, s1, s2); } + public static void assertParseableXML(String s) { + try { + InputStream in1 = new ByteArrayInputStream(s.getBytes("UTF-8")); + process(in1); + } catch (Exception e) { + fail("Unable to parse source: " + s); + } + } + private static String process(InputStream in) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); diff --git a/org.eclipse.ua.tests/data/help/dynamic/xhtml/emptyAnchor.xhtml b/org.eclipse.ua.tests/data/help/dynamic/xhtml/emptyAnchor.xhtml new file mode 100644 index 000000000..c9b70baee --- /dev/null +++ b/org.eclipse.ua.tests/data/help/dynamic/xhtml/emptyAnchor.xhtml @@ -0,0 +1,32 @@ +<!-- + Copyright (c) 2010 IBM Corporation and others. + All rights reserved. This program and the accompanying materials + are made available under the terms of the Eclipse Public License v1.0 + which accompanies this distribution, and is available at + http://www.eclipse.org/legal/epl-v10.html + Contributors: + IBM Corporation - initial API and implementation + --> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + +<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> +<meta http-equiv="Content-Style-Type" content="text/css" /> + +<link rel="STYLESHEET" href="../book.css" charset="ISO-8859-1" type="text/css" /> +<title>Title</title> +<script language="JavaScript" src="livehelp.js" type="text/javascript"></script> +</head> + +<body bgcolor="#ffffff"> +<div></div> +<div> +Check that paragraph is not collapsed. +<p></p> +Check that anchor is not collapsed. +<a></a> +</div> + +</body> +</html> diff --git a/org.eclipse.ua.tests/data/help/dynamic/xhtml/emptyAnchorWithComment.xhtml b/org.eclipse.ua.tests/data/help/dynamic/xhtml/emptyAnchorWithComment.xhtml new file mode 100644 index 000000000..afbe9bc94 --- /dev/null +++ b/org.eclipse.ua.tests/data/help/dynamic/xhtml/emptyAnchorWithComment.xhtml @@ -0,0 +1,32 @@ +<!-- + Copyright (c) 2010 IBM Corporation and others. + All rights reserved. This program and the accompanying materials + are made available under the terms of the Eclipse Public License v1.0 + which accompanies this distribution, and is available at + http://www.eclipse.org/legal/epl-v10.html + Contributors: + IBM Corporation - initial API and implementation + --> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + +<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> +<meta http-equiv="Content-Style-Type" content="text/css" /> + +<link rel="STYLESHEET" href="../book.css" charset="ISO-8859-1" type="text/css" /> +<title>Title</title> +<script language="JavaScript" src="livehelp.js" type="text/javascript"></script> +</head> + +<body bgcolor="#ffffff"> +<div></div> +<div> +Check that paragraph is not collapsed. +<p><!-- --></p> +Check that anchor is not collapsed. +<a><!-- --></a> +</div> + +</body> +</html> diff --git a/org.eclipse.ua.tests/data/help/dynamic/xhtml/emptyDiv.xhtml b/org.eclipse.ua.tests/data/help/dynamic/xhtml/emptyDiv.xhtml new file mode 100644 index 000000000..5c5c7b026 --- /dev/null +++ b/org.eclipse.ua.tests/data/help/dynamic/xhtml/emptyDiv.xhtml @@ -0,0 +1,24 @@ +<!-- + Copyright (c) 2010 IBM Corporation and others. + All rights reserved. This program and the accompanying materials + are made available under the terms of the Eclipse Public License v1.0 + which accompanies this distribution, and is available at + http://www.eclipse.org/legal/epl-v10.html + Contributors: + IBM Corporation - initial API and implementation + --> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + +<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> +<meta http-equiv="Content-Style-Type" content="text/css" /> + +<link rel="STYLESHEET" href="../book.css" charset="ISO-8859-1" type="text/css" /> +<title>Title</title> +<script language="JavaScript" src="livehelp.js" type="text/javascript"></script> +</head> + +<body bgcolor="#ffffff"> +<div></div></body> +</html> diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/AllDynamicTests.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/AllDynamicTests.java index 7d7a739f5..e259a9795 100644 --- a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/AllDynamicTests.java +++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/AllDynamicTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006 IBM Corporation and others. + * Copyright (c) 2006, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -30,5 +30,6 @@ public class AllDynamicTests extends TestSuite { */ public AllDynamicTests() { addTest(XMLProcessorTest.suite()); + addTest(DynamicXHTMLProcessorTest.suite()); } } diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/DynamicXHTMLProcessorTest.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/DynamicXHTMLProcessorTest.java new file mode 100644 index 000000000..fcf0a42ea --- /dev/null +++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/DynamicXHTMLProcessorTest.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * Copyright (c) 2010 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.ua.tests.help.dynamic; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.help.internal.xhtml.DynamicXHTMLProcessor; +import org.eclipse.help.ui.internal.HelpUIPlugin; +import org.eclipse.ua.tests.plugin.UserAssistanceTestPlugin; +import org.eclipse.ua.tests.util.XMLUtil; +import org.osgi.framework.Bundle; +import org.xml.sax.SAXException; + +public class DynamicXHTMLProcessorTest extends TestCase { + + /* + * Returns an instance of this Test. + */ + public static Test suite() { + return new TestSuite(DynamicXHTMLProcessorTest.class); + } + + protected void setUp() throws Exception { + // activate the UI plug-in for UI filtering ability + HelpUIPlugin.getDefault(); + } + + private String process(String path) throws Exception { + Bundle bundle = UserAssistanceTestPlugin.getDefault().getBundle(); + InputStream is = getProcessedInput(path, bundle); + return readStream(is); + } + + protected InputStream getProcessedInput(String path, Bundle bundle) + throws IOException, SAXException, ParserConfigurationException, + TransformerException, TransformerConfigurationException { + InputStream in = bundle.getEntry(path).openStream(); + String href = '/' + bundle.getBundleId() +path; + return DynamicXHTMLProcessor.process(href, in, "en", true); + } + + private String readStream(InputStream is) throws Exception { + InputStreamReader inputStreamReader = new InputStreamReader(is, "UTF-8"); + StringBuffer buffer = new StringBuffer(); + char[] cbuf = new char[256]; + int len; + do { + len = inputStreamReader.read(cbuf); + if (len > 0) { + buffer.append(cbuf, 0, len); + } + } while (len >= 0); + return buffer.toString(); + } + + public void testXhtmlNoCollapseAnchor() throws Exception { + String processed = process("data/help/dynamic/xhtml/emptyAnchor.xhtml"); + assertTrue("Anchor collapsed in " + processed, processed.indexOf("</a>") > 0); + } + + public void testXhtmlNoCollapseParagraph() throws Exception { + String processed = process("data/help/dynamic/xhtml/emptyAnchor.xhtml"); + assertTrue("Paragraph collapsed in " + processed, processed.indexOf("</p>") > 0); + } + + public void testXhtmlNoCollapseAnchorIC() throws Exception { + String processed = process("data/help/dynamic/xhtml/emptyAnchorWithComment.xhtml"); + assertTrue("Anchor collapsed in " + processed, processed.indexOf("</a>") > 0); + } + + public void testXhtmlNoCollapseParagraphIC() throws Exception { + String processed = process("data/help/dynamic/xhtml/emptyAnchorWithComment.xhtml"); + assertTrue("Paragraph collapsed in " + processed, processed.indexOf("</p>") > 0); + } + + public void testXhtmlNoCollapseDiv() throws Exception { + String processed = process("data/help/dynamic/xhtml/emptyDiv.xhtml"); + assertTrue("Div collapsed in " + processed, processed.indexOf("</div>") > 0); + } + + public void testXhtmlNoCollapseScript() throws Exception { + String processed = process("data/help/dynamic/xhtml/emptyAnchor.xhtml"); + assertTrue("Div collapsed in " + processed, processed.indexOf("</script>") > 0); + } + + public void testValidXML() throws Exception { + String processed = process("data/help/dynamic/xhtml/emptyAnchor.xhtml"); + XMLUtil.assertParseableXML(processed); + } + +} |