Element.text() doesn't make string with new lines
llsc12 opened this issue · 6 comments
let str = """
<!DOCTYPE html>
<html>
<body>
<p>
<span>
gm
this is some test
this is a new paragraph
more text
yoo its another paragraph!
gm is the best
</span>
</p>
</body>
</html>
"""
let doc = try parse(str)
let elmnt = try doc.body()?.select("span")
let gm = try elmnt?.text()
print(gm)
This returns none of the new lines and only a single line, am I doing something wrong or is this a bug? Thanks!
Maybe I misunderstood the question, but your code correctly prints the span.
this is the output:
Optional("gm this is some test this is a new paragraph more text yoo its another paragraph! gm is the best")
yeah, however I was hoping it'd output it with the new lines, like
Optional("gm\nthis is some test\n\nthis is a new paragraph\nmore text\n\nyoo its another paragraph!\ngm is the best")
try
.text(trimAndNormaliseWhitespace: false)
Optional("gm this is some test yoo its another paragraph! more text this is a new paragraph gm is the best")
same thing unfortunately, what should I do now? I've also tried using the other methods like html() and they also only return a single line without preserving line breaks.
for this code:
<!DOCTYPE html>
<html>
<body>
<p>
<span>
gm
this is some test
this is a new paragraph
more text
yoo its another paragraph!
gm is the best
</span>
</p>
</body>
</html>
"""
let doc = try parse(str)
let elmnt = try doc.body()?.select("span")
let gm = try elmnt?.text(trimAndNormaliseWhitespace: false)
print(gm)
this is the output
"\n gm\n this is some test\n\n this is a new paragraph\n more text\n\n yoo its another paragraph!\n gm is the best\n "
Oh thanks, that fixed it! Thanks for your help!
Upon looking back, I think I made an issue whilst trying to use .text(trimAndNormaliseWhitespace: false)
earlier, maybe I used it on dead code. My apologies.