Skip to content

Commit

Permalink
Fixes #446 - Look for lang attribute on ancestor elements.
Browse files Browse the repository at this point in the history
Also with test and removal of some dead code.
  • Loading branch information
danfickle committed Feb 20, 2020
1 parent f4d8eec commit 179494b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 457 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class XhtmlCssOnlyNamespaceHandler extends NoNamespaceHandler {
private static StylesheetInfo _defaultStylesheet;
private static boolean _defaultStylesheetError = false;

private final Map _metadata = null;
private String _contentLanguageMetaValue;

/**
* Gets the namespace attribute of the XhtmlNamespaceHandler object
Expand Down Expand Up @@ -156,6 +156,7 @@ public String getLinkUri(org.w3c.dom.Element e) {
}
return href;
}

public String getAnchorName(Element e) {
if (e != null && e.getNodeName().equalsIgnoreCase("a") &&
e.hasAttribute("name")) {
Expand Down Expand Up @@ -250,6 +251,7 @@ private Element findFirstChild(Element parent, String targetName) {

return null;
}

protected StylesheetInfo readStyleElement(Element style) {
String media = style.getAttribute("media");
if ("".equals(media)) {
Expand Down Expand Up @@ -323,7 +325,7 @@ protected StylesheetInfo readLinkElement(Element link) {
* @return The stylesheetLinks value
*/
public StylesheetInfo[] getStylesheets(org.w3c.dom.Document doc) {
List result = new ArrayList();
List<StylesheetInfo> result = new ArrayList<>();
//get the processing-instructions (actually for XmlDocuments)
result.addAll(Arrays.asList(super.getStylesheets(doc)));

Expand Down Expand Up @@ -354,7 +356,7 @@ public StylesheetInfo[] getStylesheets(org.w3c.dom.Document doc) {
}
}

return (StylesheetInfo[])result.toArray(new StylesheetInfo[result.size()]);
return result.toArray(new StylesheetInfo[result.size()]);
}

public StylesheetInfo getDefaultStylesheet(StylesheetFactory factory) {
Expand Down Expand Up @@ -418,15 +420,12 @@ private InputStream getDefaultStylesheetStream() {
return stream;
}

private Map getMetaInfo(org.w3c.dom.Document doc) {
if(this._metadata != null) {
return this._metadata;
}

Map metadata = new HashMap();
private Map<String, String> getMetaInfo(org.w3c.dom.Document doc) {
Map<String, String> metadata = new HashMap<>();

Element html = doc.getDocumentElement();
Element head = findFirstChild(html, "head");

if (head != null) {
Node current = head.getFirstChild();
while (current != null) {
Expand All @@ -453,14 +452,37 @@ private Map getMetaInfo(org.w3c.dom.Document doc) {
return metadata;
}

/**
* Get the Content-Language meta tag value from the head section of the doc
* or the empty string. Caches value so can be called multiple times without performance
* issues.
*/
private String getContentLanguageMetaTag(org.w3c.dom.Document doc) {
if (this._contentLanguageMetaValue == null) {
String possible = this.getMetaInfo(doc).get("Content-Language");
this._contentLanguageMetaValue = possible != null ? possible : "";
}

return this._contentLanguageMetaValue;
}

/**
* Gets the language of an element as specified (in order of precedence) by the lang attribute on the element itself,
* the first ancestor with a lang attribute, the Content-Language meta tag or the empty string.
*/
public String getLang(org.w3c.dom.Element e) {
String lang = e.getAttribute("lang");
if(lang.equals("")) {
lang = (String) this.getMetaInfo(e.getOwnerDocument()).get("Content-Language");
if(lang == null) {
lang = "";

if (lang.isEmpty()) {
org.w3c.dom.Node parent = e.getParentNode();

if (parent instanceof org.w3c.dom.Element) {
return getLang((org.w3c.dom.Element) parent);
} else {
return getContentLanguageMetaTag(e.getOwnerDocument());
}
}

return lang;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package com.openhtmltopdf.simple.extend;

import java.awt.Point;
import java.util.logging.Level;

import org.w3c.dom.Element;
Expand Down Expand Up @@ -52,11 +51,7 @@ public boolean isFormElement(Element e) {
}

public String getImageSourceURI(Element e) {
String uri = null;
if (e != null) {
uri = e.getAttribute("src");
}
return uri;
return e != null ? e.getAttribute("src") : null;
}

public String getNonCssStyling(Element e) {
Expand Down
Loading

0 comments on commit 179494b

Please sign in to comment.