Skip to content

Commit

Permalink
Fix copy/paste bug, wrap some unit tests around it, #185
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Mar 31, 2019
1 parent 333ee74 commit 8cc00eb
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -32,7 +32,7 @@ project.sourceCompatibility = '1.7'

project.group = 'nz.net.ultraq.thymeleaf'
project.artifact = 'thymeleaf-layout-dialect'
project.version = '2.4.0'
project.version = '2.4.1-SNAPSHOT'
project.year = '2012'
project.contributors = [
[
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "thymeleaf-layout-dialect",
"version": "2.4.0",
"version": "2.4.1-SNAPSHOT",
"description": "A dialect for Thymeleaf that lets you build layouts and reusable templates in order to improve code reuse",
"author": "Emanuel Rabina <emanuelrabina@gmail.com> (http://www.ultraq.net.nz/)",
"scripts": {
Expand Down
Expand Up @@ -68,7 +68,7 @@ class GroupingStrategy implements SortingStrategy {
if (firstElementIndex != -1) {
return firstElementIndex
}
return positions > 2 ? 2 : 1
return headModel.size() > 2 ? 2 : 1
}

def type = HeadEventTypes.findMatchingType(childModel)
Expand Down
Expand Up @@ -234,21 +234,27 @@ class IModelExtensions {

if (0 <= pos && pos <= delegate.size()) {

// Use existing whitespace at the insertion point
// Use existing whitespace found at or before the insertion point
def whitespace = delegate.getModel(pos)
if (whitespace?.whitespace) {
delegate.insertModel(pos, model)
delegate.insertModel(pos, whitespace)
return
}

// Generate whitespace, usually inserting into a tag that is immediately
// closed so whitespace should be added to either side
else {
whitespace = modelFactory.createModel(modelFactory.createText('\n\t'))
delegate.insertModel(pos, whitespace)
delegate.insertModel(pos, model)
delegate.insertModel(pos, whitespace)
if (pos > 0) {
whitespace = delegate.getModel(pos - 1)
if (whitespace?.whitespace) {
delegate.insertModel(pos, whitespace)
delegate.insertModel(pos, model)
return
}
}

// Generate whitespace on either side of the model to insert
whitespace = modelFactory.createModel(modelFactory.createText('\n\t'))
delegate.insertModel(pos, whitespace)
delegate.insertModel(pos, model)
delegate.insertModel(pos, whitespace)
}
}

Expand Down
@@ -0,0 +1,112 @@
/*
* Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package nz.net.ultraq.thymeleaf.tests.decorators.strategies

import nz.net.ultraq.thymeleaf.LayoutDialect
import nz.net.ultraq.thymeleaf.decorators.SortingStrategy
import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingStrategy
import nz.net.ultraq.thymeleaf.models.ModelBuilder

import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.thymeleaf.TemplateEngine
import org.thymeleaf.templatemode.TemplateMode

/**
* Test the results of the grouping strategy.
*
* @author Emanuel Rabina
*/
class GroupingStrategyTest {

private static ModelBuilder modelBuilder

private SortingStrategy groupingStrategy

/**
* Set up, create a template engine for the model builder.
*/
@BeforeClass
static void setupThymeleafEngine() {

def templateEngine = new TemplateEngine(
additionalDialects: [
new LayoutDialect()
]
)
def modelFactory = templateEngine.configuration.getModelFactory(TemplateMode.HTML)

modelBuilder = new ModelBuilder(modelFactory, templateEngine.configuration.elementDefinitions, TemplateMode.HTML)
}

/**
* Set up, create a new grouping strategy.
*/
@Before
void setupGroupingStrategy() {

groupingStrategy = new GroupingStrategy()
}

/**
* Whitespace nodes return a -1 index value to mean to discard them.
*/
@Test
void DiscardWhitespace() {

def headModel = modelBuilder.build {
head()
}
def whitespace = modelBuilder.build {
p(' ')
}.getModel(1)
def result = groupingStrategy.findPositionForModel(headModel, whitespace)
assert result == -1
}

/**
* Historic behaviour, have <title> elements always be first.
*/
@Test
void TitleFirst() {

def titleModel = modelBuilder.build {
title('Page title')
}

def headModel = modelBuilder.build {
head()
}
def result = groupingStrategy.findPositionForModel(headModel, titleModel)
assert result == 1

headModel = modelBuilder.build {
head(' ')
}
result = groupingStrategy.findPositionForModel(headModel, titleModel)
assert result == 2

headModel = modelBuilder.build {
head {
meta(charset: 'UTF-8')
}
}
result = groupingStrategy.findPositionForModel(headModel, titleModel)
assert result == 1
}
}

0 comments on commit 8cc00eb

Please sign in to comment.