Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 950ec745bd622b03a8a51078e9c01d850343ecb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*******************************************************************************
 * 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
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
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;

/*
 * Converts the charset in XHTML meta tag to UTF-8. This is the encoding
 * output by the XMLProcessor, and we need the charset in the meta tags
 * to match, otherwise browsers will be confused.
 * Also ensure that all <script> and <div> elements have a child, 
 */
public class XHTMLCharsetHandler extends ProcessorHandler {

	private static final String ELEMENT_META = "meta"; //$NON-NLS-1$
	private static final String ATTRIBUTE_CONTENT = "content"; //$NON-NLS-1$
	private static final String PREFIX_CHARSET = "text/html; charset="; //$NON-NLS-1$
	private static final String ENCODING_UTF8 = "UTF-8"; //$NON-NLS-1$

	public short handle(UAElement element, String id) {
		if (ELEMENT_META.equals(element.getElementName())) {
			String content = element.getAttribute(ATTRIBUTE_CONTENT);
			if (content != null && content.startsWith(PREFIX_CHARSET)) {
				element.setAttribute(ATTRIBUTE_CONTENT, PREFIX_CHARSET + ENCODING_UTF8);
				return HANDLED_CONTINUE;
			}
		}
		if (endTagRequired(element)) { 
			Element domElement = element.getElement();
			if (domElement.getFirstChild() == null) {
				Document document = domElement.getOwnerDocument();
				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$
		if ("iframe".equalsIgnoreCase(elementName)) return true; //$NON-NLS-1$
		if ("title".equalsIgnoreCase(elementName)) return true; //$NON-NLS-1$
		return false;
	}
}

Back to the top