Skip to content
Merged
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
5 changes: 0 additions & 5 deletions flowifier/src/test/java/htmlflow/flowifier/FlowifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) {
}
}

@Test
public void testFlowifierTuerSourceforgeHomepage() throws Exception {
testFlowifier("https://tuer.sourceforge.io/en/");
}

@Test
public void testFlowifierWithGamboa() throws Exception {
testFlowifier("https://gamboa.pt/");
Expand Down
7 changes: 6 additions & 1 deletion htmlflow-core/src/main/java/htmlflow/HtmlPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.net.URL;
import org.xmlet.htmlapifaster.Div;
import org.xmlet.htmlapifaster.Element;
import org.xmlet.htmlapifaster.Html;
import org.xmlet.htmlapifaster.Tr;
import org.xmlet.htmlapifaster.Div;
import org.xmlet.htmlapifaster.Span;

/**
* The root container for HTML elements. It is responsible for managing the {@code
Expand Down Expand Up @@ -85,6 +86,10 @@ public final Tr<HtmlPage> tr() {
return new Tr<>(this);
}

public final Span<HtmlPage> span() {
return new Span<>(this);
}

/**
* Returns a new instance of HtmlFlow with the same properties of this object but with indented
* set to the value of isIndented parameter.
Expand Down
14 changes: 10 additions & 4 deletions htmlflow-kotlin/src/main/kotlin/htmlflow/HtmlFlowExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ package htmlflow
import htmlflow.continuations.HtmlContinuationSuspendableTerminationNode
import htmlflow.visitor.HtmlVisitor
import htmlflow.visitor.PreprocessingVisitor
import org.xmlet.htmlapifaster.Div
import org.xmlet.htmlapifaster.Element
import org.xmlet.htmlapifaster.Html
import org.xmlet.htmlapifaster.Span
import org.xmlet.htmlapifaster.Text
import org.xmlet.htmlapifaster.Tr

/** Alternative close tag function for `__()`. */
inline val <T : Element<*, Z>, Z : Element<*, *>> T.l: Z
Expand All @@ -20,10 +23,13 @@ inline val HtmlPage.html: Html<HtmlPage>
}

/** Root builder of HTML element with lambda with receiver. */
inline fun HtmlPage.html(block: Html<HtmlPage>.() -> Unit): HtmlPage {
(this.visitor as HtmlVisitor).write(HtmlPage.HEADER)
return Html(self()).also { it.block() }.l
}
inline fun HtmlPage.html(block: Html<HtmlPage>.() -> Unit): HtmlPage = this.html().apply(block).l

inline fun HtmlPage.div(block: Div<HtmlPage>.() -> Unit): HtmlPage = this.div().apply(block).l

inline fun HtmlPage.tr(block: Tr<HtmlPage>.() -> Unit): HtmlPage = this.tr().apply(block).l

inline fun HtmlPage.span(block: Span<HtmlPage>.() -> Unit): HtmlPage = this.span().apply(block).l

/** Text node property. */
inline var <T : Element<T, Z>, Z : Element<*, *>> T.text: T
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package htmlflow.test

import htmlflow.*
import htmlflow.HtmlFlow.doc
import htmlflow.test.model.Track
import kotlinx.coroutines.reactive.asFlow
import kotlinx.coroutines.runBlocking
Expand All @@ -13,7 +14,6 @@ import java.time.Duration
import java.time.LocalDate
import java.util.*


/**
* These tests do not contain any assertion because they are only a sample for README.md
* and HtmlFlow site examples.
Expand Down Expand Up @@ -82,7 +82,7 @@ class TestKotlinExtensionsOnPartials {
actual.trackDoc(spaceOddity)
assertEquals(actual.toString(), trackView.render(spaceOddity))
// trackView.setOut(System.out).write(spaceOddity);
}
}

/**
* Sample showcase of loop with HtmlDoc
Expand Down Expand Up @@ -275,5 +275,192 @@ class TestKotlinExtensionsOnPartials {
// view.setOut(System.out).write(Owner("Ze Manel", "Rua da Alfandega"))
}

class Owner(val name: String, val address: String)
}
class Owner(val name: String, val address: String)

@Test
fun testThatDivElementCanBeUsedOnHtmlDocAndHtmlView() {
val mainMenuDoc =
StringBuilder()
.apply {
doc {
div {
attrId("main-menu")
a {
attrHref("/series")
text("My Series")
}
a {
attrHref("/movies")
text("My Movies")
}
a {
attrHref("/musics")
text("My Musics")
}
}
}
}.toString()

assertEquals(expectedMainMenuDoc, mainMenuDoc)

val favoriteMovies =
listOf(
Movie("Inception", "Christopher Nolan", 2010, "Sci-Fi"),
Movie("The Godfather", "Francis Ford Coppola", 1972, "Crime"),
Movie("Pulp Fiction", "Quentin Tarantino", 1994, "Crime"),
)

val favoriteMoviesView =
view<List<Movie>> {
div {
attrId("favorite-movies")
dyn { movies: List<Movie> ->
movies.forEach { movie ->
div {
p { text("Movie: ${movie.title}") }
p { text("Director: ${movie.director}") }
p { text("Release Year: ${movie.releaseYear}") }
p { text("Genre: ${movie.genre}") }
}
}
}
}
}.render(favoriteMovies)

assertEquals(expectedFavoriteMoviesView, favoriteMoviesView)
}

data class Movie(
val title: String,
val director: String,
val releaseYear: Int,
val genre: String,
)

@Test
fun testThatTrElementCanBeUsedOnHtmlDoc() {
val newAgentRow =
view<Agent>{
tr {
dyn { agent: Agent ->
td { text(agent.name) }
td { text(agent.email) }
td { text(agent.id) }
}
}
}.render(Agent("Agent Smith 0", "void0@null.org", "0"))

assertEquals(expectedAgentRow, newAgentRow)
}

data class Agent(
val name: String,
val email: String,
val id: String,
)
@Test
fun testThatSpanElementCanBeUsedOnHtmlView() {

val counter1 = Counter(0)
val counter2 = Counter(1)
val counterSpans =
StringBuilder()
.apply {
doc {
span {
text("The counter 1 has the value ${counter1.count}")
}
span {
text("The counter 2 has the value ${counter2.count}")
}
}
}.toString()

assertEquals(expectedCounterSpans, counterSpans)
}
data class Counter(val count: Int)

}

private const val expectedFavoriteMoviesView =
"""<div id="favorite-movies">
<div>
<p>
Movie: Inception
</p>
<p>
Director: Christopher Nolan
</p>
<p>
Release Year: 2010
</p>
<p>
Genre: Sci-Fi
</p>
</div>
<div>
<p>
Movie: The Godfather
</p>
<p>
Director: Francis Ford Coppola
</p>
<p>
Release Year: 1972
</p>
<p>
Genre: Crime
</p>
</div>
<div>
<p>
Movie: Pulp Fiction
</p>
<p>
Director: Quentin Tarantino
</p>
<p>
Release Year: 1994
</p>
<p>
Genre: Crime
</p>
</div>
</div>"""

private const val expectedMainMenuDoc =
"""
<div id="main-menu">
<a href="/series">
My Series
</a>
<a href="/movies">
My Movies
</a>
<a href="/musics">
My Musics
</a>
</div>"""

private const val expectedAgentRow =
"""<tr>
<td>
Agent Smith 0
</td>
<td>
void0@null.org
</td>
<td>
0
</td>
</tr>"""


private val expectedCounterSpans =
"""
<span>
The counter 1 has the value 0
</span>
<span>
The counter 2 has the value 1
</span>"""