Skip to content

Commit

Permalink
Merge pull request #1510 from njlr/bugfix/issue-1509-pre-whitespace-h…
Browse files Browse the repository at this point in the history
…andling

bugfix/issue-1509-pre-whitespace-handling
  • Loading branch information
cartermp authored Jul 4, 2024
2 parents c5b026b + 1e9e6ba commit 0daae3a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/library/HtmlProvider.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ The `Load` method allows reading the data from a file or web resource. We could
The following sample calls the `Load` method with an URL that points to a live version of the same page on wikipedia.
*)
// Download the table for the 2017 F1 calendar from Wikipedia
let f1Calendar = F1_2017.Load(F1_2017_URL).Tables.``Season calendaredit``
let f1Calendar = F1_2017.Load(F1_2017_URL).Tables.``Season calendar``

// Look at the top row, being the first race of the calendar
let firstRow = f1Calendar.Rows |> Seq.head
Expand Down Expand Up @@ -146,7 +146,7 @@ let doctorWho = new HtmlProvider<DrWho>()

// Get the average number of viewers for each doctor's series run
let viewersByDoctor =
doctorWho.Tables.``Season 1 (1963-1964) edit``.Rows
doctorWho.Tables.``Season 1 (1963-1964)``.Rows
|> Seq.groupBy (fun season -> season.``Directed by``)
|> Seq.map (fun (doctor, seasons) ->
let averaged =
Expand Down
10 changes: 7 additions & 3 deletions src/FSharp.Data.Html.Core/HtmlNode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ type HtmlNode =
| HtmlText _ -> true
| _ -> false)

if canAddNewLine && not onlyText then newLine 0
let isPreTag = name = "pre"

if canAddNewLine && not (onlyText || isPreTag) then
newLine 0

append "<"
append name

Expand All @@ -150,14 +154,14 @@ type HtmlNode =
appendEndTag name
else
append ">"
if not onlyText then newLine 2
if not (onlyText || isPreTag) then newLine 2
let mutable canAddNewLine = false

for element in elements do
serialize sb (indentation + 2) canAddNewLine element
canAddNewLine <- true

if not onlyText then newLine 0
if not (onlyText || isPreTag) then newLine 0
appendEndTag name
| HtmlText str -> append str
| HtmlComment str ->
Expand Down
12 changes: 12 additions & 0 deletions tests/FSharp.Data.Core.Tests/HtmlParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,18 @@ let ``Drops whitespace outside pre``() =
let expected = $"<div>%s{nl} foo <pre> bar </pre> baz%s{nl}</div>"
result |> should equal expected

[<Test>]
let ``Maintain whitespace inside pre tag through round-trip``() =
let html = """<pre>
<span>Line 1</span>
<span>Line 2</span>
<span>Line 3</span></pre>"""

let result = HtmlDocument.Parse(html).ToString()

let expected = html
result |> should equal expected

[<Test>]
let ``Can parse national rail mobile site correctly``() =
HtmlDocument.Load "UKDepartures.html"
Expand Down

0 comments on commit 0daae3a

Please sign in to comment.