Skip to content

Commit 4bc8c39

Browse files
committed
test
1 parent 45b128b commit 4bc8c39

File tree

8 files changed

+108
-70
lines changed

8 files changed

+108
-70
lines changed

pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<plugin>
5858
<groupId>org.jetbrains.kotlin</groupId>
5959
<artifactId>kotlin-maven-plugin</artifactId>
60-
<version>1.6.10</version>
60+
<version>1.6.20</version>
6161
<executions>
6262
<execution>
6363
<id>compile</id>
@@ -82,7 +82,12 @@
8282
<dependency>
8383
<groupId>org.jetbrains.kotlin</groupId>
8484
<artifactId>kotlin-stdlib</artifactId>
85-
<version>1.6.10</version>
85+
<version>1.6.20</version>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.jetbrains.kotlin</groupId>
89+
<artifactId>kotlin-test-junit</artifactId>
90+
<version>1.6.20</version>
8691
</dependency>
8792
</dependencies>
8893

src/main/kotlin/ink/meodinger/htmlparser/HNode.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ package ink.meodinger.htmlparser
1010
/**
1111
* HTML node, all elements are nodes
1212
*/
13-
open class HNode(val nodeType: String, attributes: Map<String, String> = emptyMap(), children: List<HNode> = emptyList()) {
13+
open class HNode(
14+
val nodeType: String,
15+
attributes: Map<String, String> = emptyMap(),
16+
children: List<HNode> = emptyList()
17+
) {
1418

1519
companion object {
1620
val EOF: HNode = HNode("EOF")

src/main/kotlin/ink/meodinger/htmlparser/HPage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package ink.meodinger.htmlparser
1212
/**
1313
* HTML Page, with html as HNode
1414
*/
15-
class HPage(val html: HNode, val type: String = "!DOCTYPE HTML") {
15+
class HPage(val html: HNode, val type: String = "DOCTYPE HTML") {
1616

1717
/**
1818
* The lang attribute of <html>, "en" as default

src/main/kotlin/ink/meodinger/htmlparser/HParser.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import ink.meodinger.htmlparser.internal.TokenStream.TokenType
1515
/**
1616
* Parser
1717
*/
18-
fun parse(htmlText: String): HPage {
18+
fun parse(htmlText: String, trimComment: Boolean = true): HPage {
1919
// Build a TokenStream
2020
val tokenStream = TokenStream(StringStream(htmlText))
2121

@@ -36,7 +36,8 @@ fun parse(htmlText: String): HPage {
3636
if (tokenStream.peek().isComment()) {
3737
val comment = HNode.HComment(tokenStream.next().value)
3838
tokenStream.next().except(TokenType.SYMBOL, ">")
39-
return comment
39+
40+
return if (trimComment) parseNode() else comment
4041
}
4142

4243
// Read tag name

src/test/kotlin/TestParser.kt

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/test/kotlin/TestToken.kt

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package ink.meodinger.htmlparser
2+
3+
import org.junit.Test
4+
import java.nio.charset.StandardCharsets
5+
import kotlin.test.BeforeTest
6+
import kotlin.test.assertEquals
7+
import kotlin.test.assertIs
8+
9+
10+
/**
11+
* Author: Meodinger
12+
* Date: 2022/1/18
13+
* Have fun with my code!
14+
*/
15+
16+
class TestParser {
17+
18+
private var text: String = ""
19+
20+
@BeforeTest
21+
fun loadHtml() {
22+
text = TestParser::class.java.getResource("test-page.html")!!
23+
.openStream().bufferedReader(StandardCharsets.UTF_8).readText()
24+
}
25+
26+
@Test
27+
fun testNoTrimComment() {
28+
val html = parse(text, false)
29+
30+
assertEquals("DOCTYPE html", html.type)
31+
assertEquals("zh-cn", html.lang)
32+
33+
// head
34+
assertEquals("title", html.head.children[1].nodeType)
35+
assertIs<HNode.HText>(html.head.children[1].children[0])
36+
assertEquals("script", html.head.children[3].nodeType)
37+
assertEquals("style", html.head.children[5].nodeType)
38+
39+
// body
40+
assertEquals("h1", html.body.children[1].nodeType)
41+
assertEquals("title", html.body.children[1].attributes["class"])
42+
assertEquals("", html.body.children[1].attributes["hidden"])
43+
assertEquals("alert('foo');", html.body.children[2].attributes["onclick"])
44+
}
45+
46+
@Test
47+
fun test() {
48+
val html = parse(text)
49+
50+
assertEquals("DOCTYPE html", html.type)
51+
assertEquals("zh-cn", html.lang)
52+
53+
// head
54+
assertEquals("title", html.head.children[0].nodeType)
55+
assertIs<HNode.HText>(html.head.children[0].children[0])
56+
assertEquals("script", html.head.children[1].nodeType)
57+
assertEquals("style", html.head.children[2].nodeType)
58+
59+
// body
60+
assertEquals("h1", html.body.children[0].nodeType)
61+
assertEquals("title", html.body.children[0].attributes["class"])
62+
assertEquals("", html.body.children[0].attributes["hidden"])
63+
assertEquals("alert('foo');", html.body.children[1].attributes["onclick"])
64+
}
65+
66+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="zh-cn">
3+
<head>
4+
<!-- Element -->
5+
<title>Title</title>
6+
7+
<!-- Script -->
8+
<script type="text/javascript">
9+
function max(a, b) {
10+
return a > b ? a : b;
11+
}
12+
</script>
13+
14+
<!-- Style -->
15+
<style>
16+
.title {
17+
color: #66ccff;
18+
}
19+
</style>
20+
</head>
21+
<body>
22+
<!-- Element with attributes -->
23+
<h1 class="title" hidden>Hidden</h1>
24+
<h1 class="title" onclick="alert('foo');">Click Me</h1>
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)