Skip to content
Open
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
11 changes: 11 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Apache Roller — Changes

## 6.1.8 (in development)

### Bug fixes

- **Entry titles show as text in the next/previous links and the big
calendar.** The links on a permalink page and the titles in
`#showWeblogEntryCalendarBig` now drop markup, decode entities and encode the
result, the same way the rest of the page shows titles. A title saved with
`&` no longer turns into `&`, and the calendar cuts long titles
at 20 visible characters.

## 6.1.7

### Improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.roller.weblogger.business.WebloggerFactory;
import org.apache.roller.weblogger.business.WeblogEntryManager;
import org.apache.roller.weblogger.pojos.WeblogEntry;
import org.apache.roller.weblogger.util.Utilities;
import org.apache.roller.weblogger.pojos.WeblogEntrySearchCriteria;
import org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest;
import org.apache.roller.util.DateUtil;
Expand Down Expand Up @@ -106,15 +107,12 @@ public String getContent(Date day) {
sb.append(((WeblogEntry)entries.get(i)).getPermalink());
sb.append("\">");

String title = ((WeblogEntry)entries.get(i)).getTitle().trim();
if ( title.length()==0 ) {
String title = ((WeblogEntry)entries.get(i)).getTitle();
if (title == null || title.trim().isEmpty()) {
title = ((WeblogEntry)entries.get(i)).getAnchor();
}
if ( title.length() > 20 ) {
title = title.substring(0,20)+"...";
}

sb.append( title );
// the title is printed as text, cut to fit the day's box
sb.append(Utilities.plainTextExcerpt(title, 20, 20, "..."));
sb.append("</a></div>");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public String getNextLink() {
@Override
public String getNextName() {
if (getNextEntry() != null) {
String title = Utilities.truncateNicely(getNextEntry().getTitle(), 15, 20, "...");
String title = Utilities.plainTextExcerpt(getNextEntry().getTitle(), 15, 20, "...");
return messageUtils.getString("weblogEntriesPager.single.next", new Object[] {title});
}
return null;
Expand All @@ -129,7 +129,7 @@ public String getPrevLink() {
@Override
public String getPrevName() {
if (getPrevEntry() != null) {
String title = Utilities.truncateNicely(getPrevEntry().getTitle(), 15, 20, "...");
String title = Utilities.plainTextExcerpt(getPrevEntry().getTitle(), 15, 20, "...");
return messageUtils.getString("weblogEntriesPager.single.prev", new Object[] {title});
}
return null;
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/org/apache/roller/weblogger/util/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,31 @@ public static String truncate(String str, int lower, int upper,
return str2;
}

/**
* Turns stored entry text, such as a title, into a short excerpt that is
* safe to print as HTML text: markup is removed, entities are decoded, the
* text is cut at a word boundary between <code>lower</code> and
* <code>upper</code> characters (with <code>appendToEnd</code> added when
* it is cut), and the result is HTML-encoded.
*/
public static String plainTextExcerpt(String str, int lower, int upper,
String appendToEnd) {
String text = unescapeHTML(removeHTML(str, false));
if (upper < lower) {
upper = lower;
}
if (text.length() > upper) {
int loc = text.lastIndexOf(' ', upper);
if (loc >= lower) {
text = text.substring(0, loc);
} else {
text = text.substring(0, upper);
}
text = text + appendToEnd;
}
return escapeHTML(text);
}

/**
* This method based on code from the String taglib at Apache Jakarta:
* http:/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. The ASF licenses this file to You
* 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. For additional
* information regarding copyright in this work, please see the NOTICE
* file in the top level directory of this distribution.
*/

package org.apache.roller.weblogger.ui.rendering.pagers;

import java.sql.Timestamp;

import org.apache.roller.weblogger.TestUtils;
import org.apache.roller.weblogger.business.WeblogEntryManager;
import org.apache.roller.weblogger.business.WebloggerFactory;
import org.apache.roller.weblogger.pojos.User;
import org.apache.roller.weblogger.pojos.Weblog;
import org.apache.roller.weblogger.pojos.WeblogEntry;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* The next and previous links on a permalink page show the neighbouring
* entries' titles as text.
*/
public class WeblogEntriesPermalinkPagerTitleTest {

private User user;
private Weblog weblog;

@BeforeEach
public void setUp() throws Exception {
TestUtils.setupWeblogger();
user = TestUtils.setupUser("pagerTitleUser");
weblog = TestUtils.setupWeblog("pagerTitleWeblog", user);
TestUtils.endSession(true);
}

@AfterEach
public void tearDown() throws Exception {
TestUtils.teardownWeblog(weblog.getId());
TestUtils.teardownUser(user.getUserName());
TestUtils.endSession(true);
}

private void entry(String anchor, String title, long pubTime) throws Exception {
WeblogEntry entry = TestUtils.setupWeblogEntry(anchor, weblog, user);
entry.setTitle(title);
entry.setPubTime(new Timestamp(pubTime));
WeblogEntryManager mgr = WebloggerFactory.getWeblogger().getWeblogEntryManager();
mgr.saveWeblogEntry(entry);
TestUtils.endSession(true);
}

private WeblogEntriesPermalinkPager pagerFor(String anchor) throws Exception {
Weblog managed = TestUtils.getManagedWebsite(weblog);
return new WeblogEntriesPermalinkPager(
WebloggerFactory.getWeblogger().getUrlStrategy(),
managed, null, null, anchor, null, null, null, 0);
}

@Test
public void neighbouringTitlesAreShownAsText() throws Exception {
long now = System.currentTimeMillis();
entry("older", "<em>Older</em>", now - 3 * 60_000L);
entry("middle", "Middle", now - 2 * 60_000L);
entry("newer", "A &amp; B <b>bold</b>", now - 60_000L);

WeblogEntriesPermalinkPager pager = pagerFor("middle");

// markup is dropped, entities stay encoded exactly once
assertEquals("Older", pager.getPrevName());
assertEquals("A &amp; B bold", pager.getNextName());
}

@Test
public void encodedMarkupStaysEncoded() throws Exception {
long now = System.currentTimeMillis();
entry("older", "&lt;em&gt; tags", now - 2 * 60_000L);
entry("middle", "Middle", now - 60_000L);

assertEquals("&lt;em&gt; tags", pagerFor("middle").getPrevName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,20 @@ public void testAddNoFollow() {
}
*/

@Test
public void testPlainTextExcerpt() {
// markup is removed and the text is HTML-encoded once
assertEquals("Older", Utilities.plainTextExcerpt("<em>Older</em>", 15, 20, "..."));
assertEquals("A &amp; B bold", Utilities.plainTextExcerpt("A &amp; B <b>bold</b>", 15, 20, "..."));
assertEquals("&lt;i&gt; is italic",
Utilities.plainTextExcerpt("&lt;i&gt; is italic", 15, 20, "..."));
assertEquals("&quot;quoted&quot;", Utilities.plainTextExcerpt("\"quoted\"", 15, 20, "..."));

// long text is cut at a word boundary, then encoded
assertEquals("The quick brown fox...",
Utilities.plainTextExcerpt("The quick brown fox jumps over", 15, 20, "..."));
assertEquals("&lt;em&gt;Emphasis&lt;/em&gt;",
Utilities.plainTextExcerpt("&lt;em&gt;Emphasis&lt;/em&gt;", 20, 20, "..."));
assertEquals("", Utilities.plainTextExcerpt(null, 15, 20, "..."));
}
}
Loading