* BAEL-3941
- Code snippets for preserving line breaks using Jsoup while parsing HTML strings

* BAEL-3941
- swapped the order of arguments in assertEquals
This commit is contained in:
vatsalgosar 2020-07-31 01:26:27 +05:30 committed by GitHub
parent f1525d652d
commit ed51104e26
1 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package com.baeldung.jsoup;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class PreservingLineBreaksUnitTest {
@Test
public void whenBackSlashNNewLineCharacter_thenPreserveLineBreak() {
String strHTML = "<html><body>Hello\nworld</body></html>";
Document.OutputSettings outputSettings = new Document.OutputSettings();
outputSettings.prettyPrint(false);
String strWithNewLines = Jsoup.clean(strHTML, "", Whitelist.none(), outputSettings);
assertEquals("Hello\nworld", strWithNewLines);
}
@Test
public void whenHTMLNewLineCharacters_thenPreserveLineBreak() {
String strHTML = "<html><body>" +
"Hello" +
"<br>" +
"World" +
"<p>Paragraph</p>" +
"</body></html>";
Document jsoupDoc = Jsoup.parse(strHTML);
Document.OutputSettings outputSettings = new Document.OutputSettings();
outputSettings.prettyPrint(false);
jsoupDoc.outputSettings(outputSettings);
jsoupDoc.select("br").before("\\n");
jsoupDoc.select("p").before("\\n");
String str = jsoupDoc.html().replaceAll("\\\\n", "\n");
String strWithNewLines =
Jsoup.clean(str, "", Whitelist.none(), outputSettings);
assertEquals("Hello\nWorld\nParagraph", strWithNewLines);
}
}