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

Unwanted whitespace before <svg> when inside <a> #249

Closed annomusa closed 1 year ago

annomusa commented 1 year ago

Description:

When I try to create an HTML element from a given string containing an <a> tag followed by an <svg>, the parsed result introduces an unwanted whitespace between the link text and the <svg> element.

Steps to Reproduce:

  1. Parse the following string using SwiftSoup:
    let inputHTML = """
    <a href="https://www.google.com">STRING<svg width="1em" height="1em"><circle cx="0.5em" cy="0.5em" r="0.5em" /></svg></a>
    """
    let element = try Element(Tag.valueOf("span"), "")
    try element.html(inputHTML)
  2. Print the element variable, the result will be like below
    
    <html>
    <a href="https://www.google.com">STRING
    <svg width="1em" height="1em">
    <circle cx="0.5em" cy="0.5em" r="0.5em" />
    </svg></a>



#### Expected Result
No whitespace after "STRING" just like the `inputHTML` that I inject

<details>
<summary>Image</summary>
<img width="454" alt="image" src="https://github.com/scinfu/SwiftSoup/assets/6714815/73724ec6-89d6-4bc8-b9c1-b61fcf15891e">

</details>

#### Actual Result
Theres an extra space after the "STRING"

<details>
<summary>Image</summary>
<img width="454" alt="image" src="https://github.com/scinfu/SwiftSoup/assets/6714815/56d55b70-8cc4-4caa-8e22-fb7904887de0">
</details>

### Environment/Setup:

SwiftSoup version: 2.4.3
Xcode: 14.3.1
iOS version: iOS 16.0

anyway, thank you for your hard work and dedication. your efforts make a difference for developers like me. I appreciate it!
annomusa commented 1 year ago

I can avoid this by setting the prettyPrint to be false

doc.outputSettings().prettyPrint(pretty: false)

But I think its better to avoid new line (+tab) if the parent tag is an <a>