-
Notifications
You must be signed in to change notification settings - Fork 13
/
Spec.elm
327 lines (271 loc) · 11.1 KB
/
Spec.elm
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
module RichText.Internal.Spec exposing (elementDefinitionWithDefault, htmlToElementArray, markDefinitionWithDefault)
import Array exposing (Array)
import Html.Parser as Html exposing (Node(..))
import Result exposing (Result)
import RichText.Config.ElementDefinition as ElementDefinition exposing (ElementDefinition, blockNode, defaultElementDefinition)
import RichText.Config.MarkDefinition as MarkDefinition exposing (MarkDefinition, defaultMarkDefinition)
import RichText.Config.Spec exposing (Spec, elementDefinition, elementDefinitions, markDefinition, markDefinitions)
import RichText.Internal.Constants exposing (zeroWidthSpace)
import RichText.Internal.Definitions exposing (ContentType(..), nameFromElement, nameFromMark)
import RichText.Model.Element exposing (Element)
import RichText.Model.HtmlNode exposing (HtmlNode(..))
import RichText.Model.InlineElement exposing (inlineElement)
import RichText.Model.Mark
exposing
( Mark
, MarkOrder(..)
, ToggleAction(..)
, mark
, markOrderFromSpec
, toggle
)
import RichText.Model.Node as Node
exposing
( Block
, Children(..)
, Inline(..)
, blockChildren
, childNodes
, inlineChildren
)
import RichText.Model.Text as Text
import RichText.Node exposing (Fragment(..))
resultFilterMap : (a -> Result String b) -> Array a -> ( Array b, List String )
resultFilterMap f xs =
let
maybePush : (a -> Result String b) -> a -> ( Array b, List String ) -> ( Array b, List String )
maybePush f_ mx xs_ =
case f_ mx of
Ok x ->
Tuple.mapFirst (Array.push x) xs_
Err err ->
Tuple.mapSecond ((::) err) xs_
in
Array.foldl (maybePush f) ( Array.empty, [] ) xs
htmlToElementArray : Spec -> String -> Result String (Array Fragment)
htmlToElementArray spec html =
case stringToHtmlNodeArray html of
Err s ->
Err s
Ok htmlNodeArray ->
let
( newArray, errList ) =
resultFilterMap (htmlNodeToEditorFragment spec []) htmlNodeArray
in
if Array.length newArray /= Array.length htmlNodeArray then
Err <|
"Could not create a valid editor node array from html node array:\n"
++ List.foldr
(++)
""
(errList |> List.map ((++) "\n"))
else
Ok <| reduceEditorFragmentArray newArray
htmlNodeToEditorFragment : Spec -> List Mark -> HtmlNode -> Result String Fragment
htmlNodeToEditorFragment spec marks node =
case node of
TextNode s ->
Ok <|
InlineFragment <|
Array.fromList
[ Node.Text <|
(Text.empty
|> Text.withText (String.replace zeroWidthSpace "" s)
|> Text.withMarks marks
)
]
_ ->
let
definitions =
elementDefinitions spec
maybeElementAndChildren =
List.foldl
(\definition result ->
case result of
Nothing ->
case ElementDefinition.fromHtmlNode definition definition node of
Nothing ->
Nothing
Just v ->
Just ( definition, v )
Just _ ->
result
)
Nothing
definitions
in
case maybeElementAndChildren of
Just ( definition, ( element, children ) ) ->
let
contentType =
ElementDefinition.contentType definition
in
if contentType == InlineLeafNodeType then
Ok <|
InlineFragment <|
Array.fromList
[ Node.InlineElement <|
inlineElement element marks
]
else
let
childArr =
Array.map (htmlNodeToEditorFragment spec []) children
in
case arrayToChildNodes contentType childArr of
Err s ->
Err s
Ok childNodes ->
Ok <| BlockFragment <| Array.fromList [ Node.block element childNodes ]
Nothing ->
case htmlNodeToMark spec node of
Nothing ->
Err "No mark or node matches the spec"
Just ( mark, children ) ->
let
newMarks =
toggle Add (markOrderFromSpec spec) mark marks
newChildren =
Array.map (htmlNodeToEditorFragment spec newMarks) children
in
arrayToFragment newChildren
htmlNodeToMark : Spec -> HtmlNode -> Maybe ( Mark, Array HtmlNode )
htmlNodeToMark spec node =
List.foldl
(\definition result ->
case result of
Nothing ->
case MarkDefinition.fromHtmlNode definition definition node of
Nothing ->
Nothing
Just m ->
Just m
Just _ ->
result
)
Nothing
(markDefinitions spec)
reduceEditorFragmentArray : Array Fragment -> Array Fragment
reduceEditorFragmentArray fragmentArray =
Array.foldl
(\fragment arr ->
case Array.get (Array.length arr - 1) arr of
Nothing ->
Array.push fragment arr
Just prevFragment ->
case prevFragment of
InlineFragment pilf ->
case fragment of
InlineFragment ilf ->
Array.set (Array.length arr - 1) (InlineFragment (Array.append pilf ilf)) arr
BlockFragment _ ->
Array.push fragment arr
BlockFragment pbnf ->
case fragment of
InlineFragment _ ->
Array.push fragment arr
BlockFragment bnf ->
Array.set (Array.length arr - 1) (BlockFragment (Array.append pbnf bnf)) arr
)
Array.empty
fragmentArray
arrayToChildNodes : ContentType -> Array (Result String Fragment) -> Result String Children
arrayToChildNodes contentType results =
if Array.isEmpty results then
case contentType of
BlockLeafNodeType ->
Ok Leaf
_ ->
Err "Invalid node type for empty fragment result array"
else
case arrayToFragment results of
Err e ->
Err e
Ok fragment ->
case fragment of
InlineFragment ilf ->
case contentType of
TextBlockNodeType _ ->
Ok <| inlineChildren ilf
_ ->
Err "I received an inline leaf fragment, but the node I parsed doesn't accept this child type"
BlockFragment bnf ->
case contentType of
BlockNodeType _ ->
Ok <| blockChildren bnf
_ ->
Err "I received a block node fragment, but the node I parsed doesn't accept this child type"
arrayToFragment : Array (Result String Fragment) -> Result String Fragment
arrayToFragment results =
let
aResult =
Array.foldl
(\fragmentResult arrayResult ->
case arrayResult of
Err e ->
Err e
Ok arr ->
case fragmentResult of
Err e ->
Err e
Ok fragment ->
Ok <| Array.push fragment arr
)
(Ok Array.empty)
results
in
case aResult of
Err e ->
Err e
Ok result ->
let
reducedArray =
reduceEditorFragmentArray result
in
case Array.get 0 reducedArray of
Nothing ->
Err "Unable to parse an editor fragment from the results"
Just fragment ->
if Array.length reducedArray /= 1 then
Err "I received both inline and block fragments, which is invalid."
else
Ok fragment
stringToHtmlNodeArray : String -> Result String (Array HtmlNode)
stringToHtmlNodeArray html =
case Html.run html of
Err _ ->
Err "Could not parse html string"
Ok nodeList ->
Ok <| nodeListToHtmlNodeArray nodeList
nodeListToHtmlNodeArray : List Node -> Array HtmlNode
nodeListToHtmlNodeArray nodeList =
Array.fromList <|
List.concatMap
(\n ->
case n of
Html.Element name attributes children ->
-- We filter meta tags because chrome adds it to the pasted text/html
if String.toLower name /= "meta" then
[ ElementNode name attributes <| nodeListToHtmlNodeArray children ]
else
[]
Html.Text s ->
[ TextNode s ]
Html.Comment _ ->
[]
)
nodeList
markDefinitionWithDefault : Mark -> Spec -> MarkDefinition
markDefinitionWithDefault mark spec =
let
name =
nameFromMark mark
in
Maybe.withDefault (defaultMarkDefinition name) (markDefinition name spec)
elementDefinitionWithDefault : Element -> Spec -> ElementDefinition
elementDefinitionWithDefault ele spec =
let
name =
nameFromElement ele
in
Maybe.withDefault (defaultElementDefinition name "block" (blockNode [])) (elementDefinition name spec)