File: js_write.pl

package info (click to toggle)
swi-prolog 8.0.2%2Bdfsg-3%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 72,036 kB
  • sloc: ansic: 349,612; perl: 306,654; java: 5,208; cpp: 4,436; sh: 3,042; ruby: 1,594; yacc: 845; makefile: 136; xml: 82; sed: 12; sql: 6
file content (452 lines) | stat: -rw-r--r-- 12,488 bytes parent folder | download | duplicates (6)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker, Michiel Hildebrand
    E-mail:        J.Wielemaker@uva.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2010-2014, University of Amsterdam
                              VU University Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(javascript,
          [ js_script//1,               % +Content

            js_call//1,                 % +Function(Arg..)
            js_new//2,                  % +Id, +Function(+Args)
            js_expression//1,           % +Expression
            js_arg_list//1,             % +ListOfExpressions
            js_arg//1,                  % +Arg
            js_args//1,                 % +Args

            javascript/4                % Quasi Quotation handler
          ]).

:- use_module(library(http/html_write)).
:- use_module(library(http/json)).
:- use_module(library(apply)).
:- use_module(library(error)).
:- use_module(library(lists)).
:- use_module(library(debug)).
:- use_module(library(quasi_quotations)).
:- use_module(library(dcg/basics)).
:- use_module(js_grammar).

:- html_meta
    js_script(html, ?, ?).

:- quasi_quotation_syntax(javascript).

/** <module> Utilities for including JavaScript

This library is a supplement   to library(http/html_write) for producing
JavaScript fragments. Its main role is  to   be  able to call JavaScript
functions  with  valid  arguments  constructed  from  Prolog  data.  For
example, suppose you want to call a   JavaScript  functions to process a
list of names represented as Prolog atoms.   This  can be done using the
call below, while without this library you   would have to be careful to
properly escape special characters.

    ==
    numbers_script(Names) -->
        html(script(type('text/javascript'),
             [ \js_call('ProcessNumbers'(Names)
             ]),
    ==

The accepted arguments are described with js_expression//1.
*/

%!  js_script(+Content)// is det.
%
%   Generate a JavaScript =script= element with the given content.

js_script(Content) -->
    html(script(type('text/javascript'),
                Content)).


                 /*******************************
                 *        QUASI QUOTATION       *
                 *******************************/

%!  javascript(+Content, +Vars, +VarDict, -DOM) is det.
%
%   Quasi quotation parser for JavaScript  that allows for embedding
%   Prolog variables to substitude _identifiers_   in the JavaScript
%   snippet. Parameterizing a JavaScript string   is  achieved using
%   the JavaScript `+` operator, which   results in concatenation at
%   the client side.
%
%     ==
%         ...,
%         js_script({|javascript(Id, Config)||
%                     $(document).ready(function() {
%                        $("#"+Id).tagit(Config);
%                      });
%                    |}),
%         ...
%     ==
%
%   The current implementation tokenizes the   JavaScript  input and
%   yields syntax errors on unterminated  comments, strings, etc. No
%   further parsing is  implemented,  which   makes  it  possible to
%   produce syntactically incorrect and   partial JavaScript. Future
%   versions are likely to include a  full parser, generating syntax
%   errors.
%
%   The parser produces a  term  `\List`,   which  is  suitable  for
%   js_script//1 and html//1.  Embedded  variables   are  mapped  to
%   `\js_expression(Var)`, while the remaining  text   is  mapped to
%   atoms.
%
%   @tbd    Implement a full JavaScript parser. Users should _not_
%           rely on the ability to generate partial JavaScript
%           snippets.

javascript(Content, Vars, Dict, \Parts) :-
    include(qq_var(Vars), Dict, QQDict),
    phrase_from_quasi_quotation(
        js(QQDict, Parts),
        Content).

qq_var(Vars, _=Var) :-
    member(V, Vars),
    V == Var,
    !.

js(Dict, [Pre, Subst|More]) -->
    here(Here0),
    js_tokens(_),
    here(Here1),
    js_token(identifier(Name)),
    { memberchk(Name=Var, Dict),
      !,
      Subst = \js_expression(Var),
      diff_to_atom(Here0, Here1, Pre)
    },
    js(Dict, More).
js(_, [Last]) -->
    string(Codes),
    \+ [_],
    !,
    { atom_codes(Last, Codes) }.

js_tokens([]) --> [].
js_tokens([H|T]) -->
    js_token(H),
    js_tokens(T).


%       diff_to_atom(+Start, +End, -Atom)
%
%       True when Atom is an atom that represents the characters between
%       Start and End, where End must be in the tail of the list Start.

diff_to_atom(Start, End, Atom) :-
    diff_list(Start, End, List),
    atom_codes(Atom, List).

diff_list(Start, End, List) :-
    Start == End,
    !,
    List = [].
diff_list([H|Start], End, [H|List]) :-
    diff_list(Start, End, List).

here(Here, Here, Here).


                 /*******************************
                 *     PROLOG --> JAVASCRIPT    *
                 *******************************/

%!  js_call(+Term)// is det.
%
%   Emit a call to a Javascript function.  The Prolog functor is the
%   name of the function. The arguments are converted from Prolog to
%   JavaScript using js_arg_list//1. Please not that Prolog functors can
%   be quoted atom and thus the following is legal:
%
%       ==
%           ...
%           html(script(type('text/javascript'),
%                [ \js_call('x.y.z'(hello, 42))
%                ]),
%       ==

js_call(Term) -->
    { Term =.. [Function|Args] },
    html(Function), js_arg_list(Args), [';\n'].


%!  js_new(+Id, +Term)// is det.
%
%   Emit a call to a Javascript object declaration. This is the same
%   as:
%
%       ==
%       ['var ', Id, ' = new ', \js_call(Term)]
%       ==


js_new(Id, Term) -->
    { Term =.. [Function|Args] },
    html(['var ', Id, ' = new ', Function]), js_arg_list(Args), [';\n'].

%!  js_arg_list(+Expressions:list)// is det.
%
%   Write javascript (function) arguments.  This   writes  "(", Arg,
%   ..., ")".  See js_expression//1 for valid argument values.


js_arg_list(Args) -->
    ['('], js_args(Args), [')'].

js_args([]) -->
    [].
js_args([H|T]) -->
    js_expression(H),
    (   { T == [] }
    ->  []
    ;   html(', '),
        js_args(T)
    ).

%!  js_expression(+Expression)// is det.
%
%   Emit a single JSON argument.  Expression is one of:
%
%       $ Variable :
%       Emitted as Javascript =null=
%       $ List :
%       Produces a Javascript list, where each element is processed
%       by this library.
%       $ object(Attributes) :
%       Where Attributes is a Key-Value list where each pair can be
%       written as Key-Value, Key=Value or Key(Value), accomodating
%       all common constructs for this used in Prolog.
%       $ { K:V, ... }
%       Same as object(Attributes), providing a more JavaScript-like
%       syntax.  This may be useful if the object appears literally
%       in the source-code, but is generally less friendlyto produce
%       as a result from a computation.
%       $ Dict :
%       Emit a dict as a JSON object using json_write_dict/3.
%       $ json(Term) :
%       Emits a term using json_write/3.
%       $ @(Atom) :
%       Emits these constants without quotes.  Normally used for the
%       symbols =true=, =false= and =null=, but can also be use for
%       emitting JavaScript symbols (i.e. function- or variable
%       names).
%       $ Number :
%       Emited literally
%       $ symbol(Atom) :
%       Synonym for @(Atom).  Deprecated.
%       $ Atom or String :
%       Emitted as quoted JavaScript string.

js_expression(Expr) -->
    js_arg(Expr),
    !.
js_expression(Expr) -->
    { type_error(js(expression), Expr) }.

%!  js_arg(+Expression)// is semidet.
%
%   Same as js_expression//1, but fails if Expression is invalid,
%   where js_expression//1 raises an error.
%
%   @deprecated     New code should use js_expression//1.

js_arg(H) -->
    { var(H) },
    !,
    [null].
js_arg(object(H)) -->
    { is_list(H) },
    !,
    html([ '{', \js_kv_list(H), '}' ]).
js_arg({}(Attrs)) -->
    !,
    html([ '{', \js_kv_cslist(Attrs), '}' ]).
js_arg(@(Id)) --> js_identifier(Id).
js_arg(symbol(Id)) --> js_identifier(Id).
js_arg(json(Term)) -->
    { json_to_string(json(Term), String),
      debug(json_arg, '~w~n', String)
    },
    [ String ].
js_arg(Dict) -->
    { is_dict(Dict),
      !,
      with_output_to(string(String),
                     json_write_dict(current_output, Dict, [width(0)]))
    },
    [ String ].
js_arg(H) -->
    { is_list(H) },
    !,
    html([ '[', \js_args(H), ']' ]).
js_arg(H) -->
    { number(H) },
    !,
    [H].
js_arg(H) -->
    { atomic(H),
      !,
      js_quoted_string(H, Q)
    },
    [ '"', Q, '"'
    ].

js_kv_list([]) --> [].
js_kv_list([H|T]) -->
    (   js_kv(H)
    ->  (   { T == [] }
        ->  []
        ;   html(', '),
            js_kv_list(T)
        )
    ;   { type_error(javascript_key_value, H) }
    ).

js_kv(Key:Value) -->
    !,
    js_key(Key), [:], js_expression(Value).
js_kv(Key-Value) -->
    !,
    js_key(Key), [:], js_expression(Value).
js_kv(Key=Value) -->
    !,
    js_key(Key), [:], js_expression(Value).
js_kv(Term) -->
    { compound(Term),
      Term =.. [Key,Value]
    },
    !,
    js_key(Key), [:], js_expression(Value).

js_key(Key) -->
    (   { must_be(atom, Key),
          js_identifier(Key)
        }
    ->  [Key]
    ;   { js_quoted_string(Key, QKey) },
        html(['\'', QKey, '\''])
    ).

js_kv_cslist((A,B)) -->
    !,
    js_kv(A),
    html(', '),
    js_kv_cslist(B).
js_kv_cslist(A) -->
    js_kv(A).

%!  js_quoted_string(+Raw, -Quoted)
%
%   Quote text for use in JavaScript.  Quoted does _not_ include the
%   leading and trailing quotes.
%
%   @tbd    Join with json stuff.

js_quoted_string(Raw, Quoted) :-
    atom_codes(Raw, Codes),
    phrase(js_quote_codes(Codes), QuotedCodes),
    atom_codes(Quoted, QuotedCodes).

js_quote_codes([]) -->
    [].
js_quote_codes([0'\r,0'\n|T]) -->
    !,
    "\\n",
    js_quote_codes(T).
js_quote_codes([0'<,0'/|T]) -->        % Avoid XSS scripting hacks
    !,
    "<\\/",
    js_quote_codes(T).
js_quote_codes([H|T]) -->
    js_quote_code(H),
    js_quote_codes(T).

js_quote_code(0'') -->
    !,
    "\\'".
js_quote_code(0'") -->
    !,
    "\\\"".
js_quote_code(0'\\) -->
    !,
    "\\\\".
js_quote_code(0'\n) -->
    !,
    "\\n".
js_quote_code(0'\r) -->
    !,
    "\\r".
js_quote_code(0'\t) -->
    !,
    "\\t".
js_quote_code(C) -->
    [C].

%!  js_identifier(+Id:atom)// is det.
%
%   Emit an identifier if it is a valid one

js_identifier(Id) -->
    { must_be(atom, Id),
      js_identifier(Id)
    },
    !,
    [ Id ].
js_identifier(Id) -->
    { domain_error(js(identifier), Id)
    }.

%!  js_identifier(+Id:atom) is semidet.
%
%   True if Id is a  valid   identifier.  In traditional JavaScript,
%   this means it starts  with  [$_:letter:]   and  is  followed  by
%   [$_:letter:digit:]

js_identifier(Id) :-
    sub_atom(Id, 0, 1, _, First),
    char_type(First, csymf),
    forall(sub_atom(Id, _, 1, _, Char), char_type(Char, csym)).


%!  json_to_string(+JSONTerm, -String)
%
%   Write JSONTerm to String.

json_to_string(JSON, String) :-
    with_output_to(string(String),
                   json_write(current_output,JSON,[width(0)])).