scinfu / SwiftSoup

SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS)
https://scinfu.github.io/SwiftSoup/
MIT License
4.52k stars 345 forks source link

Element.text() doesn't make string with new lines #238

Closed llsc12 closed 1 year ago

llsc12 commented 1 year ago
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!

scinfu commented 1 year ago

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")

llsc12 commented 1 year ago

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")
scinfu commented 1 year ago

try .text(trimAndNormaliseWhitespace: false)

llsc12 commented 1 year ago
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.

scinfu commented 1 year ago

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      "
llsc12 commented 1 year ago

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.