Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HtmlState: restore FormattedMode if still in pre after char ref #1414

Merged
merged 4 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/Html/HtmlParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,11 @@ module internal HtmlParser =
x.Tokens := result :: !x.Tokens

member x.IsFormattedTag
with get() =
match x.CurrentTagName() with
| "pre" | "code" -> true
| _ -> false
with get() = x.CurrentTagName().ToLower() = "pre"

member x.IsScriptTag
with get() =
match x.CurrentTagName().Trim().ToLower() with
match x.CurrentTagName().ToLower() with
| "script" | "style" -> true
| _ -> false

Expand Down Expand Up @@ -386,7 +383,9 @@ module internal HtmlParser =
| DocTypeMode -> DocType content
| CDATAMode -> CData (content.Replace("<![CDATA[", "").Replace("]]>", ""))
x.Content := CharList.Empty
x.InsertionMode := DefaultMode
x.InsertionMode :=
if x.IsFormattedTag then FormattedMode
else DefaultMode
match result with
| Text t when String.IsNullOrEmpty(t) -> ()
| _ -> x.Tokens := result :: !x.Tokens
Expand Down
17 changes: 9 additions & 8 deletions tests/FSharp.Data.Tests/HtmlParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -805,15 +805,16 @@ let ``Can parse pre blocks``() =
result |> should equal [ "\r\n This code should be indented and\r\n have line feeds in it" ]

[<Test>]
let ``Can parse code blocks``() =
let html = "<code>\r\n let f a b = a * b\r\n f 5 6 |> should equal 30</code>"
let ``Can parse pre blocks with char refs``() =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes the code blocks test - could you add that test back as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, it would test for invalid behavior though since code is an inline element. Should I restore the behavior for code anyways and open another issue for elements nested in pre?

let html = "<pre>let hello who =\r\n &quot;hello&quot; + who</pre>"

let result =
(HtmlDocument.Parse html)
|> HtmlDocument.descendantsNamed true [ "code" ]
|> Seq.map (HtmlNode.innerText)
|> Seq.toList
result |> should equal [ "\r\n let f a b = a * b\r\n f 5 6 |> should equal 30" ]
|> HtmlDocument.descendantsNamed true [ "pre" ]
|> Seq.head
|> HtmlNode.innerText
let expected = "let hello who =\r\n \"hello\" + who"
result |> should equal expected

[<Test>]
let ``Can parse national rail mobile site correctly``() =
Expand Down Expand Up @@ -911,9 +912,9 @@ let ``Parsing non-html content doesn't cause an infinite loop - Github-1264``()
[<Test; Timeout(2000)>]
let ``Can handle incomplete tags at end of file without creating an infinite loop``() =
let result = HtmlDocument.Parse """<html><head></head></html"""
let expected =
let expected =
HtmlDocument.New
[ HtmlNode.NewElement
("html",
[ HtmlNode.NewElement("head")])]
result |> should equal expected
result |> should equal expected