Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Title processing changes #42

Merged
merged 3 commits into from
May 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions Java/nz/net/ultraq/thymeleaf/decorator/HtmlHeadDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@ public class HtmlHeadDecorator extends XmlElementDecorator {
@Override
public void decorate(Element decoratorhtml, Element contenthead) {

// If the content has no HEAD, then we don't need to do anything
if (contenthead == null) {
return;
}

// If the decorator has no HEAD, then we can just copy the content HEAD
Element decoratorhead = findElement(decoratorhtml, HTML_ELEMENT_HEAD);
if (decoratorhead == null) {
decoratorhtml.insertChild(0, new Text(LINE_SEPARATOR));
decoratorhtml.insertChild(1, contenthead);
if (contenthead != null) {
decoratorhtml.insertChild(0, new Text(LINE_SEPARATOR));
decoratorhtml.insertChild(1, contenthead);
}
return;
}

// Merge the content and decorator titles into a single title element
Element decoratortitle = findElement(decoratorhead, HTML_ELEMENT_TITLE);
Element contenttitle = findElement(contenthead, HTML_ELEMENT_TITLE);
Element contenttitle = null;
if (contenthead != null) {
contenttitle = findElement(contenthead, HTML_ELEMENT_TITLE);
}
Element resultingtitle = null;
if (decoratortitle != null || contenttitle != null) {
resultingtitle = new Element(HTML_ELEMENT_TITLE);
Expand All @@ -73,8 +73,10 @@ public void decorate(Element decoratorhtml, Element contenthead) {

// Append the content's HEAD elements to the end of the decorator's HEAD
// section, placing the resulting title at the beginning of it
for (Node contentheadnode: contenthead.getChildren()) {
decoratorhead.addChild(contentheadnode);
if (contenthead != null) {
for (Node contentheadnode: contenthead.getChildren()) {
decoratorhead.addChild(contentheadnode);
}
}
if (resultingtitle != null) {
decoratorhead.insertChild(0, new Text(LINE_SEPARATOR));
Expand Down
27 changes: 21 additions & 6 deletions Java/nz/net/ultraq/thymeleaf/decorator/TitlePatternProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package nz.net.ultraq.thymeleaf.decorator;

import static nz.net.ultraq.thymeleaf.LayoutUtilities.*;
import static nz.net.ultraq.thymeleaf.LayoutUtilities.HTML_ELEMENT_TITLE;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -29,6 +29,7 @@
import org.thymeleaf.processor.attr.AbstractAttrProcessor;
import org.thymeleaf.standard.expression.IStandardExpressionParser;
import org.thymeleaf.standard.expression.StandardExpressions;
import org.thymeleaf.util.StringUtils;

/**
* Allows for greater control of the resulting <title> element by
Expand Down Expand Up @@ -86,7 +87,7 @@ protected ProcessorResult processAttribute(Arguments arguments, Element element,
String decoratortitle = (String)element.getNodeProperty(DECORATOR_TITLE);
String decoratortitlevalue;
try {
decoratortitlevalue = decoratortitle == null ? "" :
decoratortitlevalue = decoratortitle == null ? null :
parser.parseExpression(configuration, arguments, decoratortitle)
.execute(configuration, arguments)
.toString();
Expand All @@ -98,7 +99,7 @@ protected ProcessorResult processAttribute(Arguments arguments, Element element,
String contenttitle = (String)element.getNodeProperty(CONTENT_TITLE);
String contenttitlevalue;
try {
contenttitlevalue = contenttitle == null ? "" :
contenttitlevalue = contenttitle == null ? null :
parser.parseExpression(configuration, arguments, contenttitle)
.execute(configuration, arguments)
.toString();
Expand All @@ -109,10 +110,24 @@ protected ProcessorResult processAttribute(Arguments arguments, Element element,

// Replace the <title> text with an expanded title pattern expression
String titlepattern = element.getAttributeValue(attributeName);

//trim empty titles to null
decoratortitlevalue = StringUtils.isEmptyOrWhitespace(decoratortitlevalue) ? null : StringUtils.trim(decoratortitlevalue);
contenttitlevalue = StringUtils.isEmptyOrWhitespace(contenttitlevalue) ? null : StringUtils.trim(contenttitlevalue);

// only use the title pattern if both the decorator and content have a title
String title = "";
if (decoratortitlevalue != null && contenttitlevalue == null) {
title = decoratortitlevalue;
} else if (decoratortitlevalue == null && contenttitlevalue != null) {
title = contenttitlevalue;
} else if (decoratortitlevalue != null && contenttitlevalue != null) {
title = titlepattern
.replace(PARAM_TITLE_DECORATOR, decoratortitlevalue)
.replace(PARAM_TITLE_CONTENT, contenttitlevalue);
}
element.clearChildren();
element.addChild(new Text(titlepattern
.replace(PARAM_TITLE_DECORATOR, decoratortitlevalue)
.replace(PARAM_TITLE_CONTENT, contenttitlevalue)));
element.addChild(new Text(title));
element.removeAttribute(attributeName);

return ProcessorResult.OK;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
%TEMPLATE_MODE HTML5

%INPUT
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="Layout">
<body>
<section layout:fragment="content">
<p>This is a paragraph from the content page</p>
</section>
</body>
</html>

%INPUT[Layout]
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">Layout title</title>
</head>
<body>
<section layout:fragment="content">
<p>Page content goes here</p>
</section>
</body>
</html>

%OUTPUT
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Layout title</title>
</head>
<body>
<section>
<p>This is a paragraph from the content page</p>
</section>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
%TEMPLATE_MODE HTML5

%INPUT
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="Layout">
<head>
<title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">Content title</title>
</head>
<body>
<section layout:fragment="content">
<p>This is a paragraph from the content page</p>
</section>
</body>
</html>

%INPUT[Layout]
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<body>
<section layout:fragment="content">
<p>Page content goes here</p>
</section>
</body>
</html>

%OUTPUT
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Content title</title>
</head>
<body>
<section>
<p>This is a paragraph from the content page</p>
</section>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Layout title - </title>
<title>Layout title</title>
</head>
<body>
<section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> - Content title</title>
<title>Content title</title>
</head>
<body>
<section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,23 @@ public void tokenReplacementText() {
testOK("decorator/TokenReplacementText");
}

/**
* Test the behaviour when there's no content title.
*/
@Test
public void noTitleInContent() {
/**
* Test the behaviour when there's no content title.
*/
@Test
public void noTitleInContent() {

testOK("decorator/NoTitleInContent");
}
testOK("decorator/NoTitleInContent");
}

/**
* Test the behaviour when there's no content head.
*/
@Test
public void noHeadInContent() {

testOK("decorator/NoHeadInContent");
}

/**
* Test the behaviour when there's no content title.
Expand Down