Repository navigation
Expand file tree
/
Copy pathHtmlNode.fs
More file actions
275 lines (230 loc) · 9.32 KB
/
Copy pathHtmlNode.fs
File metadata and controls
275 lines (230 loc) · 9.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#nowarn "10001"
namespace FSharp.Data
open System
open System.ComponentModel
open System.Text
// --------------------------------------------------------------------------------------
[<AutoOpen>]
module private HtmlNodeHelpers =
// Void elements per the HTML spec — stored as a module-level constant so the Set is not
// re-created on every HtmlNode.ToString() call.
let private htmlVoidElementSet =
Set.ofArray
[| "area"
"base"
"br"
"col"
"command"
"embed"
"hr"
"img"
"input"
"keygen"
"link"
"meta"
"param"
"source"
"track"
"wbr" |]
let isVoidElement name = Set.contains name htmlVoidElementSet
// --------------------------------------------------------------------------------------
/// <summary>Represents an HTML attribute. The name is always normalized to lowercase</summary>
/// <namespacedoc>
/// <summary>Contains the primary types for the FSharp.Data package.</summary>
/// </namespacedoc>
///
type HtmlAttribute =
#if HIDE_REPRESENTATION
internal
#endif
| HtmlAttribute of name: string * value: string
/// <summary>
/// Creates an html attribute
/// </summary>
/// <param name="name">The name of the attribute</param>
/// <param name="value">The value of the attribute</param>
static member New(name: string, value: string) =
HtmlAttribute(name.ToLowerInvariant(), value)
[<StructuredFormatDisplay("{_Print}")>]
[<RequireQualifiedAccess>]
/// Represents an HTML node. The names of elements are always normalized to lowercase
type HtmlNode =
#if HIDE_REPRESENTATION
internal
#endif
| HtmlElement of name: string * attributes: HtmlAttribute list * elements: HtmlNode list
| HtmlText of content: string
| HtmlComment of content: string
| HtmlCData of content: string
/// <summary>
/// Creates an html element
/// </summary>
/// <param name="name">The name of the element</param>
static member NewElement(name: string) =
HtmlElement(name.ToLowerInvariant(), [], [])
/// <summary>
/// Creates an html element
/// </summary>
/// <param name="name">The name of the element</param>
/// <param name="attrs">The HtmlAttribute(s) of the element</param>
static member NewElement(name: string, attrs: seq<_>) =
let attrs = attrs |> Seq.map HtmlAttribute.New |> Seq.toList
HtmlElement(name.ToLowerInvariant(), attrs, [])
/// <summary>
/// Creates an html element
/// </summary>
/// <param name="name">The name of the element</param>
/// <param name="children">The children elements of this element</param>
static member NewElement(name: string, children: seq<_>) =
HtmlElement(name.ToLowerInvariant(), [], List.ofSeq children)
/// <summary>
/// Creates an html element
/// </summary>
/// <param name="name">The name of the element</param>
/// <param name="attrs">The HtmlAttribute(s) of the element</param>
/// <param name="children">The children elements of this element</param>
static member NewElement(name: string, attrs: seq<_>, children: seq<_>) =
let attrs = attrs |> Seq.map HtmlAttribute.New |> Seq.toList
HtmlElement(name.ToLowerInvariant(), attrs, List.ofSeq children)
/// <summary>
/// Creates a text content element
/// </summary>
/// <param name="content">The actual content</param>
static member NewText content = HtmlText(content)
/// <summary>
/// Creates a comment element
/// </summary>
/// <param name="content">The actual content</param>
static member NewComment content = HtmlComment(content)
/// <summary>
/// Creates a CData element
/// </summary>
/// <param name="content">The actual content</param>
static member NewCData content = HtmlCData(content)
/// Serialize this node into an existing StringBuilder.
/// Used by HtmlDocument.ToString() to avoid creating an intermediate string per element.
member internal x.SerializeTo(sb: StringBuilder, indentation: int, canAddNewLine: bool, insidePre: bool) =
let append (str: string) = sb.Append str |> ignore
let appendEndTag name =
append "</"
append name
append ">"
let newLine ind plus =
sb.AppendLine() |> ignore
sb.Append(' ', ind + plus) |> ignore
// serialization uses an explicit work stack instead of call-stack recursion:
// the parser accepts arbitrarily deep documents (it uses its own stack), so
// ToString must not StackOverflow on them either
let work = System.Collections.Generic.Stack<unit -> unit>()
let rec serialize ind canAdd inPre html =
match html with
| HtmlElement(name, attributes, elements) ->
let onlyText =
elements
|> List.forall (function
| HtmlText _ -> true
| _ -> false)
let isPreTag = name = "pre"
let nowInsidePre = inPre || isPreTag
if canAdd && not inPre && not (onlyText || isPreTag) then
newLine ind 0
append "<"
append name
for HtmlAttribute(name, value) in attributes do
append " "
append name
append "=\""
append value
append "\""
if isVoidElement name then
append " />"
elif elements.IsEmpty then
append ">"
appendEndTag name
else
append ">"
if not inPre && not (onlyText || isPreTag) then
newLine ind 2
// pushed first so it runs after all children are processed
work.Push(fun () ->
if not inPre && not (onlyText || isPreTag) then
newLine ind 0
appendEndTag name)
// push children in reverse so they pop in document order;
// only the first child is serialized with canAddNewLine = false
let elements = List.toArray elements
for i in elements.Length - 1 .. -1 .. 0 do
let element = elements.[i]
let canAddNewLine = i > 0
work.Push(fun () -> serialize (ind + 2) canAddNewLine nowInsidePre element)
| HtmlText str -> append str
| HtmlComment str ->
append "<!--"
append str
append "-->"
| HtmlCData str ->
append "<![CDATA["
append str
append "]]>"
serialize indentation canAddNewLine insidePre x
while work.Count > 0 do
work.Pop () ()
override x.ToString() =
let sb = StringBuilder()
x.SerializeTo(sb, 0, false, false)
sb.ToString()
/// <exclude />
[<EditorBrowsableAttribute(EditorBrowsableState.Never)>]
[<CompilerMessageAttribute("This method is intended for use in generated code only.",
10001,
IsHidden = true,
IsError = false)>]
member x._Print =
let str = x.ToString()
if str.Length > 512 then
str.Substring(0, 509) + "..."
else
str
[<StructuredFormatDisplay("{_Print}")>]
/// Represents an HTML document
type HtmlDocument =
#if HIDE_REPRESENTATION
internal
#endif
| HtmlDocument of docType: string * elements: HtmlNode list
/// <summary>
/// Creates an html document
/// </summary>
/// <param name="docType">The document type specifier string</param>
/// <param name="children">The child elements of this document</param>
static member New(docType, children: seq<_>) =
HtmlDocument(docType, List.ofSeq children)
/// <summary>
/// Creates an html document
/// </summary>
/// <param name="children">The child elements of this document</param>
static member New(children: seq<_>) = HtmlDocument("", List.ofSeq children)
override x.ToString() =
match x with
| HtmlDocument(docType, elements) ->
let sb = StringBuilder()
if not (String.IsNullOrEmpty docType) then
sb.Append("<!DOCTYPE ") |> ignore
sb.Append(docType) |> ignore
sb.AppendLine(">") |> ignore
// Reuse the same StringBuilder for all elements to avoid intermediate strings.
for element in elements do
element.SerializeTo(sb, 0, false, false)
sb.ToString()
/// <exclude />
[<EditorBrowsableAttribute(EditorBrowsableState.Never)>]
[<CompilerMessageAttribute("This method is intended for use in generated code only.",
10001,
IsHidden = true,
IsError = false)>]
member x._Print =
let str = x.ToString()
if str.Length > 512 then
str.Substring(0, 509) + "..."
else
str