elm-pages 3.0.0-beta.14 → 3.0.0-beta.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/README.md +1 -1
  2. package/codegen/elm-pages-codegen.js +66 -118
  3. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmo +0 -0
  4. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateDataTest.elmo +0 -0
  5. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  6. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Runner.elm.js +20 -20
  7. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  8. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
  9. package/generator/dead-code-review/src/Pages/Review/DeadCodeEliminateData.elm +5 -5
  10. package/generator/dead-code-review/tests/Pages/Review/DeadCodeEliminateDataTest.elm +21 -21
  11. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  12. package/generator/review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  13. package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
  14. package/generator/src/RouteBuilder.elm +23 -23
  15. package/generator/src/SharedTemplate.elm +2 -2
  16. package/generator/src/SiteConfig.elm +2 -2
  17. package/generator/src/cli.js +2 -2
  18. package/generator/src/compatibility-key.js +1 -1
  19. package/generator/src/error-formatter.js +7 -3
  20. package/generator/src/render.js +6 -15
  21. package/generator/src/request-cache.js +34 -4
  22. package/generator/static-code/hmr.js +16 -2
  23. package/package.json +1 -1
  24. package/src/ApiRoute.elm +13 -16
  25. package/src/BackendTask/Env.elm +11 -8
  26. package/src/BackendTask/File.elm +49 -10
  27. package/src/BackendTask/Glob.elm +6 -6
  28. package/src/BackendTask/Http.elm +49 -13
  29. package/src/BackendTask/Port.elm +59 -47
  30. package/src/BackendTask.elm +8 -22
  31. package/src/FatalError.elm +101 -0
  32. package/src/Form.elm +3 -2
  33. package/src/Internal/ApiRoute.elm +5 -5
  34. package/src/Pages/Generate.elm +300 -103
  35. package/src/Pages/Internal/FatalError.elm +5 -0
  36. package/src/Pages/Internal/Platform/Cli.elm +21 -41
  37. package/src/Pages/Internal/Platform/CompatibilityKey.elm +1 -1
  38. package/src/Pages/Internal/Platform/GeneratorApplication.elm +24 -48
  39. package/src/Pages/Internal/Platform/StaticResponses.elm +18 -31
  40. package/src/Pages/Internal/Script.elm +2 -2
  41. package/src/Pages/Manifest.elm +2 -2
  42. package/src/Pages/ProgramConfig.elm +7 -7
  43. package/src/Pages/Script.elm +4 -4
  44. package/src/Pages/SiteConfig.elm +2 -2
  45. package/src/Pages/StaticHttpRequest.elm +1 -23
  46. package/src/Server/Request.elm +3 -2
  47. package/src/Exception.elm +0 -37
  48. package/src/MultiDict.elm +0 -49
  49. package/src/PairingHeap.elm +0 -137
  50. package/src/Parser/Extra/String.elm +0 -33
  51. package/src/Parser/Extra.elm +0 -69
  52. package/src/ProgramTest/ComplexQuery.elm +0 -360
  53. package/src/ProgramTest/EffectSimulation.elm +0 -122
  54. package/src/ProgramTest/Failure.elm +0 -367
  55. package/src/ProgramTest/HtmlHighlighter.elm +0 -116
  56. package/src/ProgramTest/HtmlParserHacks.elm +0 -58
  57. package/src/ProgramTest/HtmlRenderer.elm +0 -73
  58. package/src/ProgramTest/Program.elm +0 -30
  59. package/src/ProgramTest/StringLines.elm +0 -26
  60. package/src/ProgramTest/TestHtmlHacks.elm +0 -132
  61. package/src/ProgramTest/TestHtmlParser.elm +0 -201
  62. package/src/ProgramTest.elm +0 -2339
  63. package/src/Query/Extra.elm +0 -55
  64. package/src/SimulatedEffect/Cmd.elm +0 -69
  65. package/src/SimulatedEffect/Http.elm +0 -330
  66. package/src/SimulatedEffect/Navigation.elm +0 -69
  67. package/src/SimulatedEffect/Ports.elm +0 -62
  68. package/src/SimulatedEffect/Process.elm +0 -24
  69. package/src/SimulatedEffect/Sub.elm +0 -48
  70. package/src/SimulatedEffect/Task.elm +0 -252
  71. package/src/SimulatedEffect/Time.elm +0 -25
  72. package/src/SimulatedEffect.elm +0 -42
  73. package/src/String/Extra.elm +0 -6
  74. package/src/Test/Http.elm +0 -145
  75. package/src/TestResult.elm +0 -35
  76. package/src/TestState.elm +0 -305
  77. package/src/Url/Extra.elm +0 -100
  78. package/src/Vendored/Diff.elm +0 -321
  79. package/src/Vendored/Failure.elm +0 -217
  80. package/src/Vendored/FormatMonochrome.elm +0 -44
  81. package/src/Vendored/Highlightable.elm +0 -53
@@ -1,122 +0,0 @@
1
- module ProgramTest.EffectSimulation exposing
2
- ( EffectSimulation
3
- , SimulationState
4
- , clearOutgoingPortValues
5
- , emptySimulationState
6
- , init
7
- , outgoingPortValues
8
- , queueTask
9
- , stepWorkQueue
10
- )
11
-
12
- import Dict exposing (Dict)
13
- import Fifo exposing (Fifo)
14
- import Json.Encode
15
- import MultiDict exposing (MultiDict)
16
- import PairingHeap exposing (PairingHeap)
17
- import SimulatedEffect exposing (SimulatedEffect, SimulatedTask)
18
- import Time
19
-
20
-
21
- type alias EffectSimulation msg effect =
22
- { deconstructEffect :
23
- effect
24
- -> SimulatedEffect msg -- TODO: this should not be in here
25
- , workQueue : Fifo (SimulatedTask msg msg)
26
- , state : SimulationState msg
27
- , outgoingPortValues : Dict String (List Json.Encode.Value)
28
- }
29
-
30
-
31
- init : (effect -> SimulatedEffect msg) -> EffectSimulation msg effect
32
- init f =
33
- { deconstructEffect = f
34
- , workQueue = Fifo.empty
35
- , state = emptySimulationState
36
- , outgoingPortValues = Dict.empty
37
- }
38
-
39
-
40
- type alias SimulationState msg =
41
- { http : MultiDict ( String, String ) (SimulatedEffect.HttpRequest msg msg)
42
- , futureTasks : PairingHeap Int (() -> SimulatedTask msg msg)
43
- , nowMs : Int
44
- }
45
-
46
-
47
- emptySimulationState : SimulationState msg
48
- emptySimulationState =
49
- { http = MultiDict.empty
50
- , futureTasks = PairingHeap.empty
51
- , nowMs = 0
52
- }
53
-
54
-
55
- queueTask : SimulatedTask msg msg -> EffectSimulation msg effect -> EffectSimulation msg effect
56
- queueTask task simulation =
57
- { simulation
58
- | workQueue = Fifo.insert task simulation.workQueue
59
- }
60
-
61
-
62
- stepWorkQueue : EffectSimulation msg effect -> Maybe ( EffectSimulation msg effect, Maybe msg )
63
- stepWorkQueue simulation =
64
- case Fifo.remove simulation.workQueue of
65
- ( Nothing, _ ) ->
66
- Nothing
67
-
68
- ( Just task, rest ) ->
69
- let
70
- ( newState, msg ) =
71
- simulateTask task simulation.state
72
- in
73
- Just
74
- ( { simulation
75
- | workQueue = rest
76
- , state = newState
77
- }
78
- , msg
79
- )
80
-
81
-
82
- simulateTask : SimulatedTask msg msg -> SimulationState msg -> ( SimulationState msg, Maybe msg )
83
- simulateTask task simulationState =
84
- case task of
85
- SimulatedEffect.Succeed msg ->
86
- ( simulationState, Just msg )
87
-
88
- SimulatedEffect.Fail msg ->
89
- ( simulationState, Just msg )
90
-
91
- SimulatedEffect.HttpTask request ->
92
- ( { simulationState
93
- | http =
94
- MultiDict.insert ( request.method, request.url )
95
- request
96
- simulationState.http
97
- }
98
- , Nothing
99
- )
100
-
101
- SimulatedEffect.SleepTask delay onResult ->
102
- ( { simulationState
103
- | futureTasks =
104
- PairingHeap.insert (simulationState.nowMs + round delay) onResult simulationState.futureTasks
105
- }
106
- , Nothing
107
- )
108
-
109
- SimulatedEffect.NowTask onResult ->
110
- simulateTask (onResult (Time.millisToPosix simulationState.nowMs)) simulationState
111
-
112
-
113
- outgoingPortValues : String -> EffectSimulation msg effect -> List Json.Encode.Value
114
- outgoingPortValues portName simulation =
115
- Dict.get portName simulation.outgoingPortValues
116
- |> Maybe.withDefault []
117
- |> List.reverse
118
-
119
-
120
- clearOutgoingPortValues : String -> EffectSimulation msg effect -> EffectSimulation msg effect
121
- clearOutgoingPortValues portName simulation =
122
- { simulation | outgoingPortValues = Dict.remove portName simulation.outgoingPortValues }
@@ -1,367 +0,0 @@
1
- module ProgramTest.Failure exposing (Failure(..), toString)
2
-
3
- import Html exposing (Html)
4
- import ProgramTest.ComplexQuery as ComplexQuery exposing (Failure(..), FailureContext1(..))
5
- import ProgramTest.TestHtmlHacks as TestHtmlHacks
6
- import Set
7
- import String.Extra
8
- import Test.Html.Query as Query
9
- import Test.Runner.Failure
10
- import Url exposing (Url)
11
- import Vendored.Failure
12
- import Vendored.FormatMonochrome
13
-
14
-
15
- type Failure
16
- = ChangedPage String Url
17
- -- Errors
18
- | ExpectFailed String String Test.Runner.Failure.Reason
19
- | SimulateFailed String String
20
- | SimulateFailedToFindTarget String String
21
- | SimulateLastEffectFailed String
22
- | InvalidLocationUrl String String
23
- | InvalidFlags String String
24
- | ProgramDoesNotSupportNavigation String
25
- | NoBaseUrl String String
26
- | NoMatchingHttpRequest Int Int String { method : String, url : String } (List ( String, String ))
27
- | MultipleMatchingHttpRequest Int Int String { method : String, url : String } (List ( String, String ))
28
- | EffectSimulationNotConfigured String
29
- | ViewAssertionFailed String (Html ()) ComplexQuery.Highlight ( ComplexQuery.FailureContext, ComplexQuery.Failure )
30
- | CustomFailure String String
31
-
32
-
33
- toString : Failure -> String
34
- toString failure =
35
- case failure of
36
- ChangedPage cause finalLocation ->
37
- cause ++ " caused the program to end by navigating to " ++ String.Extra.escape (Url.toString finalLocation) ++ ". NOTE: If this is what you intended, use ProgramTest.expectPageChange to end your test."
38
-
39
- ExpectFailed expectationName description reason ->
40
- expectationName
41
- ++ ":\n"
42
- ++ Vendored.Failure.format
43
- Vendored.FormatMonochrome.formatEquality
44
- description
45
- reason
46
-
47
- SimulateFailed functionName message ->
48
- functionName ++ ":\n" ++ message
49
-
50
- SimulateFailedToFindTarget functionName message ->
51
- functionName ++ ":\n" ++ message
52
-
53
- SimulateLastEffectFailed message ->
54
- "simulateLastEffect failed: " ++ message
55
-
56
- InvalidLocationUrl functionName invalidUrl ->
57
- functionName ++ ": " ++ "Not a valid absolute URL:\n" ++ String.Extra.escape invalidUrl
58
-
59
- InvalidFlags functionName message ->
60
- functionName ++ ":\n" ++ message
61
-
62
- ProgramDoesNotSupportNavigation functionName ->
63
- functionName ++ ": Program does not support navigation. Use ProgramTest.createApplication to create a ProgramTest that supports navigation."
64
-
65
- NoBaseUrl functionName relativeUrl ->
66
- functionName ++ ": The ProgramTest does not have a base URL and cannot resolve the relative URL " ++ String.Extra.escape relativeUrl ++ ". Use ProgramTest.withBaseUrl before calling ProgramTest.start to create a ProgramTest that can resolve relative URLs."
67
-
68
- NoMatchingHttpRequest expected actual functionName request pendingRequests ->
69
- String.concat
70
- [ functionName
71
- , ": "
72
- , "Expected "
73
- , case expected of
74
- 1 ->
75
- "HTTP request"
76
-
77
- _ ->
78
- "at least " ++ String.fromInt expected ++ " HTTP requests"
79
- , " ("
80
- , request.method
81
- , " "
82
- , request.url
83
- , ") to have been made and still be pending, "
84
- , case actual of
85
- 0 ->
86
- "but no such requests were made."
87
-
88
- _ ->
89
- "but only " ++ String.fromInt actual ++ " such requests were made."
90
- , "\n"
91
- , case pendingRequests of
92
- [] ->
93
- " No requests were made."
94
-
95
- _ ->
96
- String.concat
97
- [ " The following requests were made:\n"
98
- , String.join "\n" <|
99
- List.map (\( method, url ) -> " - " ++ method ++ " " ++ url) pendingRequests
100
- ]
101
- ]
102
-
103
- MultipleMatchingHttpRequest expected actual functionName request pendingRequests ->
104
- String.concat
105
- [ functionName
106
- , ": "
107
- , "Expected "
108
- , case expected of
109
- 1 ->
110
- "a single HTTP request"
111
-
112
- _ ->
113
- String.fromInt expected ++ " HTTP requests"
114
- , " ("
115
- , request.method
116
- , " "
117
- , request.url
118
- , ") to have been made, but "
119
- , String.fromInt actual
120
- , " such requests were made.\n"
121
- , case pendingRequests of
122
- [] ->
123
- " No requests were made."
124
-
125
- _ ->
126
- String.concat
127
- [ " The following requests were made:\n"
128
- , String.join "\n" <|
129
- List.map (\( method, url ) -> " - " ++ method ++ " " ++ url) pendingRequests
130
- ]
131
- , if expected == 1 && actual > 1 then
132
- let
133
- useInstead =
134
- if String.startsWith "simulate" functionName then
135
- "simulateHttpResponseAdvanced"
136
-
137
- else if String.startsWith "expect" functionName then
138
- "expectHttpRequests"
139
-
140
- else
141
- "ensureHttpRequests"
142
- in
143
- "\n\nNOTE: If you want to allow multiple requests to the same endpoint, use ProgramTest." ++ useInstead ++ "."
144
-
145
- else
146
- ""
147
- ]
148
-
149
- EffectSimulationNotConfigured functionName ->
150
- "TEST SETUP ERROR: In order to use " ++ functionName ++ ", you MUST use ProgramTest.withSimulatedEffects before calling ProgramTest.start"
151
-
152
- ViewAssertionFailed functionName html highlight reason ->
153
- let
154
- highlighter =
155
- if Set.isEmpty highlight then
156
- \_ _ _ -> True
157
-
158
- else
159
- \tag attrs children ->
160
- Set.member tag highlight
161
- in
162
- String.join "\n"
163
- [ TestHtmlHacks.renderHtml showColors.dim highlighter (Query.fromHtml html)
164
- , ""
165
- , "▼ " ++ functionName
166
- , ""
167
- , renderQueryFailureWithContext renderQueryFailure 0 True reason
168
- ]
169
-
170
- CustomFailure assertionName message ->
171
- assertionName ++ ": " ++ message
172
-
173
-
174
- renderQueryFailureWithContext : (Int -> Bool -> a -> String) -> Int -> Bool -> ( ComplexQuery.FailureContext, a ) -> String
175
- renderQueryFailureWithContext renderInner indent color failure =
176
- let
177
- indentS =
178
- String.repeat indent " "
179
- in
180
- case failure of
181
- ( [], inner ) ->
182
- renderInner indent color inner
183
-
184
- ( (Description description) :: baseFailure, inner ) ->
185
- String.join "\n" <|
186
- List.filter ((/=) "")
187
- [ indentS ++ renderDescriptionResult (colorsFor color) description ++ ":"
188
- , renderQueryFailureWithContext renderInner (indent + 2) color ( baseFailure, inner )
189
- ]
190
-
191
- ( (CheckSucceeded description checkContext) :: baseFailure, inner ) ->
192
- String.join "\n" <|
193
- List.filter ((/=) "")
194
- [ indentS ++ renderDescriptionResult (colorsFor color) (Ok description) ++ ":"
195
- , renderQueryFailureWithContext_ (\_ _ () -> "") (indent + 2) color ( checkContext, () )
196
- , renderQueryFailureWithContext renderInner indent color ( baseFailure, inner )
197
- ]
198
-
199
- ( (FindSucceeded (Just description) successfulChecks) :: baseFailure, inner ) ->
200
- String.join "\n" <|
201
- List.filter ((/=) "")
202
- [ indentS ++ renderDescriptionResult (colorsFor color) (Ok description) ++ ":"
203
- , renderSelectorResults (indent + 2) (colorsFor color) (List.map Ok (successfulChecks ()))
204
- , renderQueryFailureWithContext renderInner indent color ( baseFailure, inner )
205
- ]
206
-
207
- ( (FindSucceeded Nothing successfulChecks) :: baseFailure, inner ) ->
208
- String.join "\n" <|
209
- List.filter ((/=) "")
210
- [ renderSelectorResults indent (colorsFor color) (List.map Ok (successfulChecks ()))
211
- , renderQueryFailureWithContext renderInner indent color ( baseFailure, inner )
212
- ]
213
-
214
-
215
- renderQueryFailureWithContext_ : (Int -> Bool -> a -> String) -> Int -> Bool -> ( ComplexQuery.FailureContext, a ) -> String
216
- renderQueryFailureWithContext_ =
217
- renderQueryFailureWithContext
218
-
219
-
220
- renderQueryFailure : Int -> Bool -> ComplexQuery.Failure -> String
221
- renderQueryFailure indent color failure =
222
- let
223
- indentS =
224
- String.repeat indent " "
225
- in
226
- case failure of
227
- QueryFailed failureReason ->
228
- renderSelectorResults indent (colorsFor color) failureReason
229
-
230
- ComplexQuery.SimulateFailed string ->
231
- let
232
- colors =
233
- colorsFor color
234
- in
235
- indentS ++ renderSelectorResult colors (Err string)
236
-
237
- NoMatches description options ->
238
- let
239
- sortedByPriority =
240
- options
241
- |> List.sortBy (\( _, prio, _ ) -> -prio)
242
-
243
- maxPriority =
244
- List.head sortedByPriority
245
- |> Maybe.map (\( _, prio, _ ) -> prio)
246
- |> Maybe.withDefault 0
247
- in
248
- String.join "\n" <|
249
- List.concat
250
- [ [ indentS ++ description ++ ":" ]
251
- , sortedByPriority
252
- |> List.filter (\( _, prio, _ ) -> prio > maxPriority - 2)
253
- |> List.map (\( desc, prio, reason ) -> indentS ++ "- " ++ desc ++ "\n" ++ renderQueryFailureWithContext renderQueryFailure (indent + 4) (color && prio >= maxPriority - 1) reason)
254
- ]
255
-
256
- TooManyMatches description matches ->
257
- String.join "\n" <|
258
- List.concat
259
- [ [ indentS ++ description ++ ", but there were multiple successful matches:" ]
260
- , matches
261
- |> List.sortBy (\( _, prio, _ ) -> -prio)
262
- |> List.map (\( desc, _, todo ) -> indentS ++ "- " ++ desc)
263
- , [ ""
264
- , "If that's what you intended, use `ProgramTest.within` to focus in on a portion of"
265
- , "the view that contains only one of the matches."
266
- ]
267
- ]
268
-
269
-
270
- renderSelectorResults : Int -> Colors -> List (Result String String) -> String
271
- renderSelectorResults indent colors results =
272
- let
273
- indentS =
274
- String.repeat indent " "
275
- in
276
- List.map ((++) indentS << renderSelectorResult colors) (upToFirstErr results)
277
- |> String.join "\n"
278
-
279
-
280
- renderSelectorResult : Colors -> Result String String -> String
281
- renderSelectorResult colors result =
282
- case result of
283
- Ok selector ->
284
- String.concat
285
- [ colors.green "✓"
286
- , " "
287
- , colors.bold selector
288
- ]
289
-
290
- Err selector ->
291
- colors.red <|
292
- String.concat
293
- [ "✗"
294
- , " "
295
- , selector
296
- ]
297
-
298
-
299
- renderDescriptionResult : Colors -> Result String String -> String
300
- renderDescriptionResult colors result =
301
- case result of
302
- Ok selector ->
303
- String.concat
304
- [ colors.green "✓"
305
- , " "
306
- , selector
307
- ]
308
-
309
- Err selector ->
310
- String.concat
311
- [ colors.red "✗"
312
- , " "
313
- , selector
314
- ]
315
-
316
-
317
- upToFirstErr : List (Result x a) -> List (Result x a)
318
- upToFirstErr results =
319
- let
320
- step acc results_ =
321
- case results_ of
322
- [] ->
323
- acc
324
-
325
- (Err x) :: _ ->
326
- Err x :: acc
327
-
328
- (Ok a) :: rest ->
329
- step (Ok a :: acc) rest
330
- in
331
- step [] results
332
- |> List.reverse
333
-
334
-
335
- type alias Colors =
336
- { bold : String -> String
337
- , red : String -> String
338
- , green : String -> String
339
- , dim : String -> String
340
- }
341
-
342
-
343
- colorsFor : Bool -> Colors
344
- colorsFor show =
345
- if show then
346
- showColors
347
-
348
- else
349
- noColors
350
-
351
-
352
- showColors : Colors
353
- showColors =
354
- { bold = \s -> String.concat [ "\u{001B}[1m", s, "\u{001B}[0m" ]
355
- , red = \s -> String.concat [ "\u{001B}[31m", s, "\u{001B}[0m" ]
356
- , green = \s -> String.concat [ "\u{001B}[32m", s, "\u{001B}[0m" ]
357
- , dim = \s -> String.concat [ "\u{001B}[2m", s, "\u{001B}[0m" ]
358
- }
359
-
360
-
361
- noColors : Colors
362
- noColors =
363
- { bold = identity
364
- , red = identity
365
- , green = identity
366
- , dim = identity
367
- }
@@ -1,116 +0,0 @@
1
- module ProgramTest.HtmlHighlighter exposing (Attribute, Node(..), NodeF(..), fold, foldWithOriginal, highlight, isNonHiddenElement)
2
-
3
- import Html.Parser
4
-
5
-
6
- type NodeF a
7
- = TextF String
8
- | ElementF String (List Attribute) (List a)
9
- | CommentF String
10
-
11
-
12
- type alias Attribute =
13
- ( String, String )
14
-
15
-
16
- fold : (NodeF a -> a) -> Html.Parser.Node -> a
17
- fold f node =
18
- case node of
19
- Html.Parser.Text text ->
20
- f (TextF text)
21
-
22
- Html.Parser.Element tag attrs children ->
23
- f (ElementF tag attrs (List.map (fold f) children))
24
-
25
- Html.Parser.Comment string ->
26
- f (CommentF string)
27
-
28
-
29
- foldWithOriginal : (NodeF ( Html.Parser.Node, a ) -> a) -> Html.Parser.Node -> a
30
- foldWithOriginal f node =
31
- case node of
32
- Html.Parser.Text text ->
33
- f (TextF text)
34
-
35
- Html.Parser.Element tag attrs children ->
36
- f (ElementF tag attrs (List.map (\child -> ( child, foldWithOriginal f child )) children))
37
-
38
- Html.Parser.Comment string ->
39
- f (CommentF string)
40
-
41
-
42
- type Node
43
- = Text String
44
- | Element String (List Attribute) (List Node)
45
- | Comment String
46
- | Hidden String
47
-
48
-
49
- highlight : (String -> List Attribute -> List Html.Parser.Node -> Bool) -> Html.Parser.Node -> Node
50
- highlight predicate =
51
- foldWithOriginal <|
52
- \node ->
53
- case node of
54
- TextF text ->
55
- Text text
56
-
57
- ElementF tag attrs children ->
58
- let
59
- foldedChildren =
60
- List.map Tuple.second children
61
- in
62
- if predicate tag attrs (List.map Tuple.first children) || List.any isNonHiddenElement foldedChildren then
63
- Element tag attrs foldedChildren
64
-
65
- else
66
- let
67
- bestId =
68
- List.concatMap identity
69
- [ List.filter (Tuple.first >> (==) "id") attrs
70
- , List.filter (Tuple.first >> (==) "name") attrs
71
- , List.filter (Tuple.first >> (==) "class") attrs
72
- ]
73
- |> List.head
74
- |> Maybe.map (\( name, value ) -> " " ++ name ++ "=\"" ++ value ++ "\"")
75
- |> Maybe.withDefault ""
76
-
77
- bestContent =
78
- case foldedChildren of
79
- [] ->
80
- ""
81
-
82
- [ Text single ] ->
83
- truncate 15 (String.trim single)
84
-
85
- _ ->
86
- "..."
87
- in
88
- Hidden ("<" ++ tag ++ bestId ++ ">" ++ bestContent ++ "</" ++ tag ++ ">")
89
-
90
- CommentF string ->
91
- Comment string
92
-
93
-
94
- isNonHiddenElement : Node -> Bool
95
- isNonHiddenElement node =
96
- case node of
97
- Text _ ->
98
- False
99
-
100
- Element _ _ _ ->
101
- True
102
-
103
- Comment _ ->
104
- False
105
-
106
- Hidden _ ->
107
- False
108
-
109
-
110
- truncate : Int -> String -> String
111
- truncate max input =
112
- if String.length input < max - 3 then
113
- input
114
-
115
- else
116
- String.left (max - 3) input ++ "..."
@@ -1,58 +0,0 @@
1
- module ProgramTest.HtmlParserHacks exposing (parse, trimText)
2
-
3
- import Html.Parser
4
- import Parser
5
- import ProgramTest.StringLines as StringLines
6
-
7
-
8
- parse : String -> Result (List Parser.DeadEnd) (List Html.Parser.Node)
9
- parse input =
10
- case Html.Parser.run input of
11
- Ok nodes ->
12
- Ok nodes
13
-
14
- Err errs ->
15
- case fixError errs input of
16
- Nothing ->
17
- Err errs
18
-
19
- Just nodes ->
20
- Ok nodes
21
-
22
-
23
- fixError : List Parser.DeadEnd -> String -> Maybe (List Html.Parser.Node)
24
- fixError errs input =
25
- case errs of
26
- [] ->
27
- Nothing
28
-
29
- { row, col, problem } :: rest ->
30
- case problem of
31
- Parser.UnexpectedChar ->
32
- case StringLines.charAt row (col - 1) input of
33
- Just "<" ->
34
- parse (StringLines.replaceAt row (col - 1) "&lt;" input)
35
- |> Result.toMaybe
36
-
37
- _ ->
38
- fixError rest input
39
-
40
- _ ->
41
- fixError rest input
42
-
43
-
44
- trimText : Html.Parser.Node -> Html.Parser.Node
45
- trimText node =
46
- case node of
47
- Html.Parser.Text string ->
48
- Html.Parser.Text (String.trim string)
49
-
50
- Html.Parser.Element string list nodes ->
51
- Html.Parser.Element string
52
- list
53
- (List.map trimText nodes
54
- |> List.filter ((/=) (Html.Parser.Text ""))
55
- )
56
-
57
- Html.Parser.Comment string ->
58
- Html.Parser.Comment string