This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webidl-contiguous.js
794 lines (771 loc) · 35.6 KB
/
webidl-contiguous.js
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/*global Handlebars */
// Module core/webidl-contiguous
// Highlights and links WebIDL marked up inside <pre class="idl">.
// TODO:
// - It could be useful to report parsed IDL items as events
// - don't use generated content in the CSS!
define(
[
"handlebars"
, "webidl2"
, "tmpl!core/css/webidl-oldschool.css"
, "tmpl!core/templates/webidl-contiguous/typedef.html"
, "tmpl!core/templates/webidl-contiguous/implements.html"
, "tmpl!core/templates/webidl-contiguous/dict-member.html"
, "tmpl!core/templates/webidl-contiguous/dictionary.html"
, "tmpl!core/templates/webidl-contiguous/enum-item.html"
, "tmpl!core/templates/webidl-contiguous/enum.html"
, "tmpl!core/templates/webidl-contiguous/const.html"
, "tmpl!core/templates/webidl-contiguous/param.html"
, "tmpl!core/templates/webidl-contiguous/callback.html"
, "tmpl!core/templates/webidl-contiguous/method.html"
, "tmpl!core/templates/webidl-contiguous/attribute.html"
, "tmpl!core/templates/webidl-contiguous/serializer.html"
, "tmpl!core/templates/webidl-contiguous/line-comment.html"
, "tmpl!core/templates/webidl-contiguous/multiline-comment.html"
, "tmpl!core/templates/webidl-contiguous/field.html"
, "tmpl!core/templates/webidl-contiguous/exception.html"
, "tmpl!core/templates/webidl-contiguous/extended-attribute.html"
, "tmpl!core/templates/webidl-contiguous/interface.html"
],
function (hb, webidl2, css, idlTypedefTmpl, idlImplementsTmpl, idlDictMemberTmpl, idlDictionaryTmpl,
idlEnumItemTmpl, idlEnumTmpl, idlConstTmpl, idlParamTmpl, idlCallbackTmpl, idlMethodTmpl,
idlAttributeTmpl, idlSerializerTmpl, idlLineCommentTmpl, idlMultiLineCommentTmpl, idlFieldTmpl, idlExceptionTmpl,
idlExtAttributeTmpl, idlInterfaceTmpl) {
"use strict";
function registerHelpers (msg) {
Handlebars.registerHelper("extAttr", function (obj, indent) {
return extAttr(obj.extAttrs, indent, /*singleLine=*/false);
});
Handlebars.registerHelper("extAttrInline", function (obj) {
return extAttr(obj.extAttrs, 0, /*singleLine=*/true);
});
Handlebars.registerHelper("typeExtAttrs", function (obj) {
return extAttr(obj.typeExtAttrs, 0, /*singleLine=*/true);
});
Handlebars.registerHelper("extAttrClassName", function() {
var extAttr = this;
if (extAttr.name === "Constructor" || extAttr.name === "NamedConstructor") {
return "idlCtor";
}
return "extAttr";
});
Handlebars.registerHelper("extAttrRhs", function(rhs, options) {
if (rhs.type === "identifier") {
return options.fn(rhs.value);
}
return "(" + rhs.value.map(function(item) { return options.fn(item); }).join(",") + ")";
});
Handlebars.registerHelper("param", function (obj) {
return new Handlebars.SafeString(
idlParamTmpl({
obj: obj
, optional: obj.optional ? "optional " : ""
, variadic: obj.variadic ? "..." : ""
}));
});
Handlebars.registerHelper("jsIf", function (condition, options) {
if (condition) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
Handlebars.registerHelper("idn", function (indent) {
return new Handlebars.SafeString(idn(indent));
});
Handlebars.registerHelper("idlType", function (obj) {
return new Handlebars.SafeString(idlType2Html(obj.idlType));
});
Handlebars.registerHelper("stringifyIdlConst", function (value) {
switch (value.type) {
case "null": return "null";
case "Infinity": return value.negative ? "-Infinity" : "Infinity";
case "NaN": return "NaN";
case "string":
case "number":
case "boolean":
case "sequence":
return JSON.stringify(value.value);
default:
msg.pub("error", "Unexpected constant value type: " + value.type);
return "<Unknown>";
}
});
Handlebars.registerHelper("escapeArgumentName", escapeArgumentName);
Handlebars.registerHelper("escapeAttributeName", escapeAttributeName);
Handlebars.registerHelper("escapeIdentifier", escapeIdentifier);
Handlebars.registerHelper("pads", function (num) {
return new Handlebars.SafeString(pads(num));
});
Handlebars.registerHelper("join", function(arr, between, options) {
return arr.map(function(elem) { return options.fn(elem); }).join(between);
});
Handlebars.registerHelper("joinNonWhitespace", function(arr, between, options) {
return arr.filter(function(elem) {
return elem.type !== "ws";
}).map(function(elem) {
return options.fn(elem);
}).join(between);
});
// A block helper that emits an <a title> around its contents
// if obj.dfn exists. If it exists, that implies that
// there's another <dfn> for the object.
Handlebars.registerHelper("tryLink", function(obj, options) {
var content = options.fn(this);
if (obj.dfn) {
var result = "<a for='" + Handlebars.Utils.escapeExpression(obj.linkFor || "") + "'";
if (obj.name) {
result += " data-lt='" + Handlebars.Utils.escapeExpression(obj.name) + "'";
}
result += ">" + content + "</a>";
return result;
} else {
return content;
}
});
}
function idn (lvl) {
var str = "";
for (var i = 0; i < lvl; i++) str += " ";
return str;
}
function idlType2Html (idlType) {
if (typeof idlType === "string") {
return "<a>" + Handlebars.Utils.escapeExpression(idlType) + "</a>";
}
var nullable = idlType.nullable ? "?" : "";
if (idlType.union) {
return '(' + idlType.idlType.map(function(type) {
return idlType2Html(type);
}).join(' or ') + ')' + nullable;
}
if (idlType.array) {
var arrayStr = '';
for (var i = 0; i < idlType.array; ++i) {
if (idlType.nullableArray[i]) {
arrayStr += '?';
}
arrayStr += '[]';
}
return idlType2Html({
generic: idlType.generic,
idlType: idlType.idlType,
}) + arrayStr + nullable;
}
if (idlType.generic) {
return Handlebars.Utils.escapeExpression(idlType.generic) + '<' + idlType2Html(idlType.idlType) + '>' + nullable;
}
return idlType2Html(idlType.idlType) + nullable;
}
function idlType2Text(idlType) {
if (typeof idlType === 'string') {
return idlType;
}
var nullable = idlType.nullable ? "?" : "";
if (idlType.union) {
return '(' + idlType.idlType.map(function(type) {
return idlType2Text(type);
}).join(' or ') + ')' + nullable;
}
if (idlType.array) {
var arrayStr = '';
for (var i = 0; i < idlType.array; ++i) {
if (idlType.nullableArray[i]) {
arrayStr += '?';
}
arrayStr += '[]';
}
return idlType2Text({
generic: idlType.generic,
idlType: idlType.idlType,
}) + arrayStr + nullable;
}
if (idlType.generic) {
return idlType.generic + '<' + idlType2Text(idlType.idlType) + '>' + nullable;
}
return idlType2Text(idlType.idlType) + nullable;
}
function pads (num) {
// XXX
// this might be more simply done as
// return Array(num + 1).join(" ")
var str = "";
for (var i = 0; i < num; i++) str += " ";
return str;
}
var whitespaceTypes = {"ws": true, "ws-pea": true, "ws-tpea": true, "line-comment": true, "multiline-comment": true};
function typeIsWhitespace(webIdlType) {
return whitespaceTypes[webIdlType];
}
function extAttr(extAttrs, indent, singleLine) {
if (extAttrs.length === 0) {
// If there are no extended attributes, omit the [] entirely.
return "";
}
var opt = {
extAttrs: extAttrs,
indent: indent,
sep: singleLine ? ", " : ",\n " + idn(indent),
end: singleLine ? " " : "\n",
};
return new Handlebars.SafeString(idlExtAttributeTmpl(opt));
}
var idlKeywords = [
"ByteString",
"DOMString",
"Date",
"Infinity",
"NaN",
"RegExp",
"USVString",
"any",
"attribute",
"boolean",
"byte",
"callback",
"const",
"creator",
"deleter",
"dictionary",
"double",
"enum",
"false",
"float",
"getter",
"implements",
"inherit",
"interface",
"iterable",
"legacycaller",
"legacyiterable",
"long",
"maplike",
"null",
"object",
"octet",
"optional",
"or",
"partial",
"readonly",
"required",
"sequence",
"serializer",
"setlike",
"setter",
"short",
"static",
"stringifier",
"true",
"typedef",
"unrestricted",
"unsigned",
"void",
]
, ArgumentNameKeyword = [
"attribute",
"callback",
"const",
"creator",
"deleter",
"dictionary",
"enum",
"getter",
"implements",
"inherit",
"interface",
"iterable",
"legacycaller",
"legacyiterable",
"maplike",
"partial",
"required",
"serializer",
"setlike",
"setter",
"static",
"stringifier",
"typedef",
"unrestricted",
]
, AttributeNameKeyword = ["required"];
function escapeArgumentName(argumentName) {
if (idlKeywords.indexOf(argumentName) !== -1 && ArgumentNameKeyword.indexOf(argumentName) === -1)
return "_" + argumentName;
return argumentName;
}
function escapeAttributeName(attributeName) {
if (idlKeywords.indexOf(attributeName) !== -1 && AttributeNameKeyword.indexOf(attributeName) === -1)
return "_" + attributeName;
return attributeName;
}
function escapeIdentifier(identifier) {
if (idlKeywords.indexOf(identifier) !== -1)
return "_" + identifier;
return identifier;
}
// Takes the result of WebIDL2.parse(), an array of definitions.
function makeMarkup (parse, msg) {
var attr = { "class": "idl" };
var $pre = $("<pre></pre>").attr(attr);
$pre.html(parse.filter(function(defn) { return !typeIsWhitespace(defn.type); })
.map(function(defn) { return writeDefinition(defn, -1, msg); })
.join('\n\n'));
return $pre;
}
function writeDefinition (obj, indent, msg) {
indent++;
var opt = { indent: indent, obj: obj };
switch (obj.type) {
case "typedef":
return idlTypedefTmpl(opt);
case "implements":
return idlImplementsTmpl(opt);
case "interface":
return writeInterfaceDefinition(opt);
case "callback interface":
return writeInterfaceDefinition(opt, "callback ");
case "exception":
var maxAttr = 0, maxConst = 0;
obj.members.forEach(function (it) {
if (typeIsWhitespace(it.type)) {
return;
}
var len = idlType2Text(it.idlType).length;
if (it.type === "field") maxAttr = (len > maxAttr) ? len : maxAttr;
else if (it.type === "const") maxConst = (len > maxConst) ? len : maxConst;
});
var children = obj.members
.map(function (ch) {
switch (ch.type) {
case "field": return writeField(ch, maxAttr, indent + 1);
case "const": return writeConst(ch, maxConst, indent + 1);
case "line-comment": return writeLineComment(ch, indent + 1);
case "multiline-comment": return writeMultiLineComment(ch, indent + 1);
case "ws": return writeBlankLines(ch);
case "ws-pea": break;
default:
throw new Error('Unexpected type in exception: ' + it.type);
}
})
.join("")
;
return idlExceptionTmpl({ obj: obj, indent: indent, children: children });
case "dictionary":
var maxQualifiers = 0, maxType = 0;
var members = obj.members.filter(function(member) { return !typeIsWhitespace(member.type); });
obj.members.forEach(function (it) {
if (typeIsWhitespace(it.type)) {
return;
}
var qualifiers = '';
if (it.required) qualifiers += 'required ';
if (maxQualifiers < qualifiers.length) maxQualifiers = qualifiers.length;
var typeLen = idlType2Text(it.idlType).length;
if (maxType < typeLen) maxType = typeLen;
});
var children = obj.members
.map(function (it) {
switch(it.type) {
case "field": return writeMember(it, maxQualifiers, maxType, indent + 1);
case "line-comment": return writeLineComment(it, indent + 1);
case "multiline-comment": return writeMultiLineComment(it, indent + 1);
case "ws": return writeBlankLines(it);
case "ws-pea": break;
default:
throw new Error('Unexpected type in dictionary: ' + it.type);
}
})
.join("")
;
return idlDictionaryTmpl({ obj: obj, indent: indent, children: children, partial: obj.partial ? "partial " : "" });
case "callback":
var params = obj.arguments
.filter(function(it) {
return !typeIsWhitespace(it.type);
})
.map(function (it) {
return idlParamTmpl({
obj: it
, optional: it.optional ? "optional " : ""
, variadic: it.variadic ? "..." : ""
});
})
.join(", ");
return idlCallbackTmpl({
obj: obj
, indent: indent
, children: params
});
case "enum":
var children = "";
for (var i = 0; i < obj.values.length; i++) {
var item = obj.values[i];
switch (item.type) {
case undefined:
var needsComma = false;
for (var j = i + 1; j < obj.values.length; j++) {
var lookahead = obj.values[j];
if (lookahead.type === undefined) break;
if (lookahead.type === ",") {
needsComma = true;
break;
}
}
children += idlEnumItemTmpl({
obj: item,
parentID: obj.fullPath,
indent: indent + 1,
needsComma: needsComma
});
break;
case "line-comment": children += writeLineComment(item, indent + 1); break;
case "multiline-comment": children += writeMultiLineComment(item, indent + 1); break;
case "ws": children += writeBlankLines(item); break;
case ",":
case "ws-pea": break;
default:
throw new Error('Unexpected type in exception: ' + item.type);
}
}
return idlEnumTmpl({obj: obj, indent: indent, children: children });
default:
msg.pub("error", "Unexpected object type " + obj.type + " in " + JSON.stringify(obj));
return "";
}
}
function writeInterfaceDefinition(opt, callback) {
var obj = opt.obj, indent = opt.indent;
var maxAttr = 0, maxMeth = 0, maxConst = 0;
obj.members.forEach(function (it) {
if (typeIsWhitespace(it.type) || it.type === "serializer") {
return;
}
var len = idlType2Text(it.idlType).length;
if (it.static) len += 7;
if (it.type === "attribute") maxAttr = (len > maxAttr) ? len : maxAttr;
else if (it.type === "operation") maxMeth = (len > maxMeth) ? len : maxMeth;
else if (it.type === "const") maxConst = (len > maxConst) ? len : maxConst;
});
var children = obj.members
.map(function (ch) {
switch (ch.type) {
case "attribute": return writeAttribute(ch, maxAttr, indent + 1);
case "operation": return writeMethod(ch, maxMeth, indent + 1);
case "const": return writeConst(ch, maxConst, indent + 1);
case "serializer": return writeSerializer(ch, indent + 1);
case "ws": return writeBlankLines(ch);
case "line-comment": return writeLineComment(ch, indent + 1);
case "multiline-comment": return writeMultiLineComment(ch, indent + 1);
default: throw new Error("Unexpected member type: " + ch.type);
}
})
.join("")
;
return idlInterfaceTmpl({
obj: obj
, indent: indent
, partial: obj.partial ? "partial " : ""
, callback: callback
, children: children
});
}
function writeField (attr, max, indent) {
var pad = max - idlType2Text(attr.idlType).length;
return idlFieldTmpl({
obj: attr
, indent: indent
, pad: pad
});
}
function writeAttribute (attr, max, indent) {
var len = idlType2Text(attr.idlType).length;
var pad = max - len;
var qualifiers = "";
if (attr.static) qualifiers += "static ";
if (attr.stringifier) qualifiers += "stringifier ";
if (attr.inherit) qualifiers += "inherit ";
if (attr.readonly) qualifiers += "readonly ";
qualifiers += " ";
qualifiers = qualifiers.slice(0, 11);
return idlAttributeTmpl({
obj: attr
, indent: indent
, qualifiers: qualifiers
, pad: pad
});
}
function writeMethod (meth, max, indent) {
var params = meth.arguments
.filter(function (it) {
return !typeIsWhitespace(it.type);
}).map(function (it) {
return idlParamTmpl({
obj: it
, optional: it.optional ? "optional " : ""
, variadic: it.variadic ? "..." : ""
});
})
.join(", ");
var len = idlType2Text(meth.idlType).length;
if (meth.static) len += 7;
var specialProps = ["getter", "setter", "deleter", "legacycaller", "serializer", "stringifier"];
var special = "";
for (var i in specialProps) {
if (meth[specialProps[i]]) {
special = specialProps[i] + " ";
len += special.length;
break;
}
}
var pad = max - len;
return idlMethodTmpl({
obj: meth
, indent: indent
, "static": meth.static ? "static " : ""
, special: special
, pad: pad
, children: params
});
}
function writeConst (cons, max, indent) {
var pad = max - idlType2Text(cons.idlType).length;
if (cons.nullable) pad--;
return idlConstTmpl({ obj: cons, indent: indent, pad: pad, nullable: cons.nullable ? "?" : ""});
}
// Writes a single blank line if whitespace includes at least one blank line.
function writeBlankLines(whitespace) {
if (/\n.*\n/.test(whitespace.value)) {
// Members end with a newline, so we only need 1 extra one to get a blank line.
return "\n";
}
return "";
}
function writeLineComment (comment, indent) {
return idlLineCommentTmpl({ indent: indent, comment: comment.value});
}
function writeMultiLineComment (comment, indent) {
// Split the multi-line comment into lines so we can indent it properly.
var lines = comment.value.split(/\r\n|\r|\n/);
if (lines.length === 0) {
return "";
} else if (lines.length === 1) {
return idlLineCommentTmpl({ indent: indent, comment: lines[0]});
}
var initialSpaces = Math.max(0, /^ */.exec(lines[1])[0].length - 3);
function trimInitialSpace(line) {
return line.slice(initialSpaces);
}
return idlMultiLineCommentTmpl({
indent: indent,
firstLine: lines[0],
lastLine: trimInitialSpace(lines[lines.length - 1]),
innerLine: lines.slice(1, -1).map(trimInitialSpace) });
}
function writeSerializer (serializer, indent) {
var values = "";
if (serializer.patternMap) {
values = "{" + serializer.names.join(", ") + "}";
}
else if (serializer.patternList) {
values = "[" + listValues.join(", ") + "]";
}
else if (serializer.name) {
values = serializer.name;
}
return idlSerializerTmpl({
obj: serializer
, indent: indent
, values: values
});
}
function writeMember (memb, maxQualifiers, maxType, indent) {
var opt = { obj: memb, indent: indent };
opt.typePad = maxType - idlType2Text(memb.idlType).length;
if (memb.required) opt.qualifiers = 'required ';
else opt.qualifiers = ' ';
opt.qualifiers = opt.qualifiers.slice(0, maxQualifiers);
return idlDictMemberTmpl(opt);
}
// Each entity defined in IDL is either a top- or second-level entity:
// Interface or Interface.member. This function finds the <dfn>
// element defining each entity and attaches it to the entity's
// 'refTitle' property, and records that it describes an IDL entity by
// adding a [data-idl] attribute.
function linkDefinitions(parse, definitionMap, parent, msg) {
parse.forEach(function(defn) {
var name;
switch (defn.type) {
// Top-level entities with linkable members.
case "callback interface":
case "dictionary":
case "exception":
case "interface":
linkDefinitions(defn.members, definitionMap, defn.name, msg);
name = defn.name;
defn.idlId = "idl-def-" + name.toLowerCase();
break;
// Top-level entities without linkable members.
case "enum": // TODO: link enum members.
case "callback":
case "typedef":
name = defn.name;
defn.idlId = "idl-def-" + name.toLowerCase();
break;
// Members of top-level entities.
case "attribute":
case "const":
case "field":
name = defn.name;
defn.idlId = "idl-def-" + parent.toLowerCase() + "-" + name.toLowerCase();
break;
case "operation":
if (defn.name) {
name = defn.name;
} else if (defn.getter || defn.setter || defn.deleter ||
defn.legacycaller || defn.stringifier ||
defn.serializer ) {
name = "";
}
defn.idlId = ("idl-def-" + parent.toLowerCase() + "-" +
name.toLowerCase() + '(' +
defn.arguments.filter(function(arg) {
return !typeIsWhitespace(arg.type);
}).map(function(arg) {
var optional = arg.optional ? "optional-" : "";
var variadic = arg.variadic ? "..." : "";
return optional + idlType2Text(arg.idlType).toLowerCase() + variadic;
}).join(',') + ')');
break;
case "iterator":
name = "iterator";
defn.idlId = "idl-def-" + parent.toLowerCase() + "-" + name.toLowerCase();
break;
case "serializer":
name = "serializer";
defn.idlId = "idl-def-" + parent.toLowerCase() + "-" + name.toLowerCase();
break;
case "implements":
case "ws":
case "ws-pea":
case "ws-tpea":
case "line-comment":
case "multiline-comment":
// Nothing to link here.
return;
default:
msg.pub("error", "Unexpected type when computing refTitles: " + defn.type);
return;
}
if (parent) {
defn.linkFor = parent;
}
defn.dfn = findDfn(parent, name, definitionMap, msg);
});
}
// This function looks for a <dfn> element whose title is 'name' and
// that is "for" 'parent', which is the empty string when 'name'
// refers to a top-level entity. For top-level entities, <dfn>
// elements that inherit a non-empty [dfn-for] attribute are also
// counted as matching.
//
// When a matching <dfn> is found, it's given <code> formatting,
// marked as an IDL definition, and returned. If no <dfn> is found,
// the function returns 'undefined'.
function findDfn(parent, name, definitionMap, msg) {
parent = parent.toLowerCase();
name = name.toLowerCase();
var dfnForArray = definitionMap[name];
var dfns = [];
if (dfnForArray) {
// Definitions that have a title and [for] that exactly match the
// IDL entity:
dfns = dfnForArray.filter(function(dfn) {
return dfn.attr('data-dfn-for') === parent;
});
// If this is a top-level entity, and we didn't find anything with
// an explicitly empty [for], try <dfn> that inherited a [for].
if (dfns.length === 0 && parent === "" && dfnForArray.length === 1) {
dfns = dfnForArray;
}
}
// If we haven't found any definitions with explicit [for]
// and [title], look for a dotted definition, "parent.name".
if (dfns.length === 0 && parent !== "") {
var dottedName = parent + '.' + name;
dfnForArray = definitionMap[dottedName];
if (dfnForArray !== undefined && dfnForArray.length === 1) {
dfns = dfnForArray;
// Found it: update the definition to specify its [for] and data-lt.
delete definitionMap[dottedName];
dfns[0].attr('data-dfn-for', parent);
dfns[0].attr('data-lt', name);
if (definitionMap[name] === undefined) {
definitionMap[name] = [];
}
definitionMap[name].push(dfns[0]);
}
}
if (dfns.length > 1) {
msg.pub("error", "Multiple <dfn>s for " + name + (parent ? " in " + parent : ""));
}
if (dfns.length === 0) {
return undefined;
}
var dfn = dfns[0];
// Mark the definition as code.
dfn.attr('id', "dom-" + (parent ? parent + "-" : "") + name)
dfn.attr('data-idl', '');
dfn.attr('data-dfn-for', parent);
if (dfn.children('code').length === 0 && dfn.parents('code').length === 0)
dfn.wrapInner('<code></code>');
return dfn;
}
return {
run: function (conf, doc, cb, msg) {
msg.pub("start", "core/webidl-contiguous");
registerHelpers(msg);
var $idl = $("pre.idl", doc)
, finish = function () {
msg.pub("end", "core/webidl-contiguous");
cb();
};
if (!$idl.length) return finish();
if (!$(".idl", doc).not("pre").length) {
$(doc).find("head link").first().before($("<style/>").text(css));
}
var idlNames = [];
$idl.each(function () {
var parse;
try {
parse = window.WebIDL2.parse($(this).text(), {ws: true});
} catch(e) {
msg.pub("error", "Failed to parse <pre>" + $idl.text() + "</pre> as IDL: " + (e.stack || e));
// Skip this <pre> and move on to the next one.
return;
}
linkDefinitions(parse, conf.definitionMap, "", msg);
var $df = makeMarkup(parse, msg);
$df.attr({id: this.id});
$df.find('.idlAttribute,.idlCallback,.idlConst,.idlDictionary,.idlEnum,.idlException,.idlField,.idlInterface,.idlMember,.idlMethod,.idlSerializer,.idlTypedef')
.each(function() {
var elem = $(this);
var title = elem.attr('data-title').toLowerCase();
// Select the nearest ancestor element that can contain members.
var parent = elem.parent().closest('.idlDictionary,.idlEnum,.idlException,.idlInterface');
if (parent.length) {
elem.attr('data-dfn-for', parent.attr('data-title').toLowerCase());
}
if (!conf.definitionMap[title]) {
conf.definitionMap[title] = [];
}
conf.definitionMap[title].push(elem);
});
$(this).replaceWith($df);
});
doc.normalize();
finish();
}
};
}
);