Skip to content

Commit

Permalink
Fix more title-pattern edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Jul 3, 2016
1 parent d17c1fb commit 484f8ab
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,39 +56,40 @@ class HtmlHeadDecorator implements Decorator {
@Override
IModel decorate(IModel targetHeadModel, IModel sourceHeadModel) {

// If one of the parameters is missing return a copy of the other, or
// nothing if both parameters are missing.
if (!targetHeadModel || !sourceHeadModel) {
return targetHeadModel ? targetHeadModel.cloneModel() : sourceHeadModel ? sourceHeadModel.cloneModel() : null
// If none of the parameters are present, return nothing
if (!targetHeadModel && !sourceHeadModel) {
return null
}

// Get the source and target title elements to pass to the title decorator
def titleRetriever = { headModel ->
def title = headModel.findModel { event ->
return event instanceof IOpenElementTag && event.elementCompleteName == 'title'
}
if (title) {
headModel.removeModelWithWhitespace(title.startIndex)
}
return title
def isTitle = { event -> event instanceof IOpenElementTag && event.elementCompleteName == 'title' }

// New head model based off the target being decorated
def resultHeadModel = new AttributeMerger(context.modelFactory).merge(targetHeadModel, sourceHeadModel)
def titleInResult = resultHeadModel.find(isTitle)
if (titleInResult) {
resultHeadModel.removeModelWithWhitespace(titleInResult.index)
}

// Get the source and target title elements to pass to the title decorator
def titleRetriever = { headModel -> headModel?.findModel(isTitle) }
def resultTitle = new HtmlTitleDecorator(context).decorate(
titleRetriever(targetHeadModel),
titleRetriever(sourceHeadModel))
targetHeadModel.insertModelWithWhitespace(1, resultTitle)
resultHeadModel.insertModelWithWhitespace(1, resultTitle)

// Merge the source <head> elements with the target <head> elements using
// the current merging strategy, placing the resulting title at the
// beginning of it
// Merge the rest of the source <head> elements with the target <head>
// elements using the current merging strategy
if (sourceHeadModel) {
sourceHeadModel.childModelIterator().each { childModel ->
def position = sortingStrategy.findPositionForModel(targetHeadModel, childModel)
if (position != -1) {
targetHeadModel.insertModelWithWhitespace(position, childModel)
sourceHeadModel.childModelIterator()
.findAll { model -> !isTitle(model.first()) }
.each { model ->
def position = sortingStrategy.findPositionForModel(resultHeadModel, model)
if (position != -1) {
resultHeadModel.insertModelWithWhitespace(position, model)
}
}
}
}

return new AttributeMerger(context.modelFactory).merge(targetHeadModel, sourceHeadModel)
return resultHeadModel
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,21 @@ class HtmlTitleDecorator implements Decorator {
// title result parts that we want to use on the pattern.
if (titlePatternProcessor) {
def titleValueRetriever = { titleModel ->
return titleModel.first().getAttributeValue(StandardDialect.PREFIX, StandardTextTagProcessor.ATTR_NAME) ?:
titleModel.size() > 2 ? "'${HtmlEscape.escapeHtml5Xml(titleModel.get(1).text)}'" : null
return titleModel?.first()?.getAttributeValue(StandardDialect.PREFIX, StandardTextTagProcessor.ATTR_NAME) ?:
titleModel?.size() > 2 ? "'${HtmlEscape.escapeHtml5Xml(titleModel.get(1).text)}'" : null
}
def titleValuesMap = [:]
def contentTitle = titleValueRetriever(sourceTitleModel)
def decoratorTitle = titleValueRetriever(targetTitleModel)
if (contentTitle) {
titleValuesMap << [(TitlePatternProcessor.CONTENT_TITLE_ATTRIBUTE): contentTitle]
}
def layoutTitle = titleValueRetriever(targetTitleModel)
if (layoutTitle) {
titleValuesMap << [(TitlePatternProcessor.LAYOUT_TITLE_ATTRIBUTE): layoutTitle]
}

resultTitle = new ModelBuilder(context).build {
title([
(titlePatternProcessor.attributeCompleteName): titlePatternProcessor.value,
(TitlePatternProcessor.CONTENT_TITLE_ATTRIBUTE): contentTitle,
(TitlePatternProcessor.LAYOUT_TITLE_ATTRIBUTE): decoratorTitle
])
title([(titlePatternProcessor.attributeCompleteName): titlePatternProcessor.value] << titleValuesMap)
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,10 @@ class LayoutDialectTestExecutor extends JUnitTestExecutor {
def tests = new Reflections('', new ResourcesScanner())
.getResources(~/(?!GroupingStrategy|Examples).*\.thtest/) as List
def exclusions = [
'nz/net/ultraq/thymeleaf/tests/decorators/Decorator-DeepHierarchy.thtest',
'nz/net/ultraq/thymeleaf/tests/decorators/TitlePattern-AllowOtherProcessors.thtest',
'nz/net/ultraq/thymeleaf/tests/decorators/TitlePattern-DynamicContent.thtest',
'nz/net/ultraq/thymeleaf/tests/decorators/TitlePattern-EmptyTitleInContent.thtest',
'nz/net/ultraq/thymeleaf/tests/decorators/TitlePattern-EmptyTitleInDecorator.thtest',
'nz/net/ultraq/thymeleaf/tests/decorators/TitlePattern-NoHeadInContent.thtest',
'nz/net/ultraq/thymeleaf/tests/decorators/TitlePattern-NoHeadInDecorator.thtest',
'nz/net/ultraq/thymeleaf/tests/decorators/TitlePattern-NoTitleInContent.thtest',
'nz/net/ultraq/thymeleaf/tests/decorators/TitlePattern-NoTitleInDecorator.thtest',
'nz/net/ultraq/thymeleaf/tests/decorators/TitlePattern-ResultAccessible.thtest'
'nz/net/ultraq/thymeleaf/tests/decorators/Decorate-DeepHierarchy.thtest',
'nz/net/ultraq/thymeleaf/tests/decorators/html/TitlePattern-AllowOtherProcessors.thtest',
'nz/net/ultraq/thymeleaf/tests/decorators/html/TitlePattern-DynamicContent.thtest',
'nz/net/ultraq/thymeleaf/tests/decorators/html/TitlePattern-ResultAccessible.thtest'
]
return tests - exclusions
}
Expand Down

0 comments on commit 484f8ab

Please sign in to comment.