Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed missing spaces inside some tags #460

Merged
merged 1 commit into from
Dec 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class HtmlLinearizer {
if (blockStyle.shouldSoftWrap) {
node.appendCorrectlyNormalizedWhiteSpace(
linearTextBuilder,
stripLeading = linearTextBuilder.endsWithWhitespace,
)
} else {
append(node.wholeText)
Expand Down Expand Up @@ -113,7 +112,6 @@ class HtmlLinearizer {
withLinearTextAnnotation(LinearTextAnnotationH1) {
element.appendCorrectlyNormalizedWhiteSpaceRecursively(
linearTextBuilder,
stripLeading = linearTextBuilder.endsWithWhitespace,
)
}
}
Expand All @@ -124,7 +122,6 @@ class HtmlLinearizer {
withLinearTextAnnotation(LinearTextAnnotationH2) {
element.appendCorrectlyNormalizedWhiteSpaceRecursively(
linearTextBuilder,
stripLeading = linearTextBuilder.endsWithWhitespace,
)
}
}
Expand All @@ -135,7 +132,6 @@ class HtmlLinearizer {
withLinearTextAnnotation(LinearTextAnnotationH3) {
element.appendCorrectlyNormalizedWhiteSpaceRecursively(
linearTextBuilder,
stripLeading = linearTextBuilder.endsWithWhitespace,
)
}
}
Expand All @@ -146,7 +142,6 @@ class HtmlLinearizer {
withLinearTextAnnotation(LinearTextAnnotationH4) {
element.appendCorrectlyNormalizedWhiteSpaceRecursively(
linearTextBuilder,
stripLeading = linearTextBuilder.endsWithWhitespace,
)
}
}
Expand All @@ -157,7 +152,6 @@ class HtmlLinearizer {
withLinearTextAnnotation(LinearTextAnnotationH5) {
element.appendCorrectlyNormalizedWhiteSpaceRecursively(
linearTextBuilder,
stripLeading = linearTextBuilder.endsWithWhitespace,
)
}
}
Expand All @@ -168,7 +162,6 @@ class HtmlLinearizer {
withLinearTextAnnotation(LinearTextAnnotationH6) {
element.appendCorrectlyNormalizedWhiteSpaceRecursively(
linearTextBuilder,
stripLeading = linearTextBuilder.endsWithWhitespace,
)
}
}
Expand Down Expand Up @@ -925,13 +918,10 @@ class HtmlLinearizer {
* Can't use JSoup's text() method because that strips invisible characters
* such as ZWNJ which are crucial for several languages.
*/
fun TextNode.appendCorrectlyNormalizedWhiteSpace(
builder: LinearTextBuilder,
stripLeading: Boolean,
) {
fun TextNode.appendCorrectlyNormalizedWhiteSpace(builder: LinearTextBuilder) {
wholeText.asUTF8Sequence()
.dropWhile {
stripLeading && isCollapsableWhiteSpace(it)
builder.endsWithWhitespace && isCollapsableWhiteSpace(it)
}
.fold(false) { lastWasWhite, char ->
if (isCollapsableWhiteSpace(char)) {
Expand All @@ -946,17 +936,13 @@ fun TextNode.appendCorrectlyNormalizedWhiteSpace(
}
}

fun Element.appendCorrectlyNormalizedWhiteSpaceRecursively(
builder: LinearTextBuilder,
stripLeading: Boolean,
) {
fun Element.appendCorrectlyNormalizedWhiteSpaceRecursively(builder: LinearTextBuilder) {
for (child in childNodes()) {
when (child) {
is TextNode -> child.appendCorrectlyNormalizedWhiteSpace(builder, stripLeading)
is TextNode -> child.appendCorrectlyNormalizedWhiteSpace(builder)
is Element ->
child.appendCorrectlyNormalizedWhiteSpaceRecursively(
builder,
stripLeading,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ class HtmlLinearizerTest {
assertEquals(LinearText("Hello, world!", LinearTextBlockStyle.TEXT), result[0])
}

@Test
fun `spaces inside headers are kept`() {
val html =
"""
<html><body><h2><a href="http://example.com">Link</a> <small>small</small></h2></body></html>
""".trimIndent()
val baseUrl = "https://example.com"

val result = linearizer.linearize(html, baseUrl).elements

assertEquals(1, result.size)
assertEquals(
LinearText("Link small", LinearTextBlockStyle.TEXT, LinearTextAnnotation(LinearTextAnnotationH2, 0, 9)),
result[0],
)
}

@Test
fun `should return annotations with bold, italic, and underline`() {
val html = "<html><body><b><i><u>Hello, world!</u></i></b></body></html>"
Expand Down
Loading