Skip to content

Commit

Permalink
Display sitemesh:write tag body if tag property is not found. Fixes #137
Browse files Browse the repository at this point in the history
  • Loading branch information
codeconsole committed Nov 11, 2023
1 parent 1872f3c commit c952612
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
public class SiteMeshWriteRule extends BasicBlockRule {

private final SiteMeshContext siteMeshContext;
private ContentProperty property;

public SiteMeshWriteRule(SiteMeshContext siteMeshContext) {
this.siteMeshContext = siteMeshContext;
Expand All @@ -45,7 +46,7 @@ protected Object processStart(Tag tag) throws IOException {
String propertyPath = tag.getAttributeValue("property", true);
Content contentToMerge = siteMeshContext.getContentToMerge();
if (contentToMerge != null) {
ContentProperty property = getProperty(contentToMerge, propertyPath);
property = getProperty(contentToMerge, propertyPath);
property.writeValueTo(tagProcessorContext.currentBuffer());
}
tagProcessorContext.pushBuffer();
Expand All @@ -64,7 +65,7 @@ protected ContentProperty getProperty(Content content, String propertyPath) {
protected void processEnd(Tag tag, Object data) throws IOException {
CharSequence defaultContents = tagProcessorContext.currentBufferContents();
tagProcessorContext.popBuffer();
if (siteMeshContext.getContentToMerge() == null) {
if (siteMeshContext.getContentToMerge() == null || !property.hasValue()) {
tagProcessorContext.currentBuffer().append(defaultContents);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public void testRemovesTagBodyIfContentSupplied() throws IOException {
SiteMeshContextStub context = new SiteMeshContextStub();
context.setContentToMerge(content);

String in = "Hello <sitemesh:write property='notfound'>X</sitemesh:write>" +
" <sitemesh:write property='found.not'>X</sitemesh:write>!";
String in = "Hello <sitemesh:write property='notfound'></sitemesh:write>" +
" <sitemesh:write property='found.not'></sitemesh:write>!";
TagProcessor tagProcessor = new TagProcessor(CharBuffer.wrap(in));
tagProcessor.addRule("sitemesh:write", new SiteMeshWriteRule(context));
tagProcessor.process();
Expand All @@ -81,20 +81,20 @@ public void testLeavesTagBodyIfContentMissing() throws IOException {
assertEquals("Hello X X!", out.toString());
}

public void testSkipsMissingProperties() throws IOException {
public void testLeavesTagBodyForMissingProperties() throws IOException {
Content content = new InMemoryContent();
SiteMeshContextStub context = new SiteMeshContextStub();
content.getExtractedProperties().getChild("found").setValue("FOUND");
context.setContentToMerge(content);

String in = "Hello <sitemesh:write property='found'>BAD</sitemesh:write>" +
" <sitemesh:write property='notfound'>BAD</sitemesh:write>!";
" <sitemesh:write property='notfound'>GOOD</sitemesh:write>!";
TagProcessor tagProcessor = new TagProcessor(CharBuffer.wrap(in));
tagProcessor.addRule("sitemesh:write", new SiteMeshWriteRule(context));
tagProcessor.process();
CharSequence out = tagProcessor.getDefaultBufferContents();

assertEquals("Hello FOUND !", out.toString());
assertEquals("Hello FOUND GOOD!", out.toString());
}

public void testWritesBodyAttributesSimple() throws IOException {
Expand Down

0 comments on commit c952612

Please sign in to comment.