Skip to content

Commit 0daae3a

Browse files
authored
Merge pull request #1510 from njlr/bugfix/issue-1509-pre-whitespace-handling
bugfix/issue-1509-pre-whitespace-handling
2 parents c5b026b + 1e9e6ba commit 0daae3a

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

docs/library/HtmlProvider.fsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ The `Load` method allows reading the data from a file or web resource. We could
6666
The following sample calls the `Load` method with an URL that points to a live version of the same page on wikipedia.
6767
*)
6868
// Download the table for the 2017 F1 calendar from Wikipedia
69-
let f1Calendar = F1_2017.Load(F1_2017_URL).Tables.``Season calendaredit``
69+
let f1Calendar = F1_2017.Load(F1_2017_URL).Tables.``Season calendar``
7070

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

147147
// Get the average number of viewers for each doctor's series run
148148
let viewersByDoctor =
149-
doctorWho.Tables.``Season 1 (1963-1964) edit``.Rows
149+
doctorWho.Tables.``Season 1 (1963-1964)``.Rows
150150
|> Seq.groupBy (fun season -> season.``Directed by``)
151151
|> Seq.map (fun (doctor, seasons) ->
152152
let averaged =

src/FSharp.Data.Html.Core/HtmlNode.fs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ type HtmlNode =
132132
| HtmlText _ -> true
133133
| _ -> false)
134134

135-
if canAddNewLine && not onlyText then newLine 0
135+
let isPreTag = name = "pre"
136+
137+
if canAddNewLine && not (onlyText || isPreTag) then
138+
newLine 0
139+
136140
append "<"
137141
append name
138142

@@ -150,14 +154,14 @@ type HtmlNode =
150154
appendEndTag name
151155
else
152156
append ">"
153-
if not onlyText then newLine 2
157+
if not (onlyText || isPreTag) then newLine 2
154158
let mutable canAddNewLine = false
155159

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

160-
if not onlyText then newLine 0
164+
if not (onlyText || isPreTag) then newLine 0
161165
appendEndTag name
162166
| HtmlText str -> append str
163167
| HtmlComment str ->

tests/FSharp.Data.Core.Tests/HtmlParser.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,18 @@ let ``Drops whitespace outside pre``() =
857857
let expected = $"<div>%s{nl} foo <pre> bar </pre> baz%s{nl}</div>"
858858
result |> should equal expected
859859

860+
[<Test>]
861+
let ``Maintain whitespace inside pre tag through round-trip``() =
862+
let html = """<pre>
863+
<span>Line 1</span>
864+
<span>Line 2</span>
865+
<span>Line 3</span></pre>"""
866+
867+
let result = HtmlDocument.Parse(html).ToString()
868+
869+
let expected = html
870+
result |> should equal expected
871+
860872
[<Test>]
861873
let ``Can parse national rail mobile site correctly``() =
862874
HtmlDocument.Load "UKDepartures.html"

0 commit comments

Comments
 (0)