Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for iterable to oldschool IDL #449

Merged
merged 2 commits into from
Jun 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions js/core/css/webidl-oldschool.css
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ a.idlEnumItem {
color: gray;
}

/*.idlIterable*/
.idlIterableKeyType, .idlIterableValueType {
color: #005a9c;
}

/*.idlMaplike*/
.idlMaplikeKeyType, .idlMaplikeValueType {
color: #005a9c;
Expand Down
2 changes: 2 additions & 0 deletions js/core/templates/webidl/iterable.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<span class='idlIterable'>{{extAttr obj indent true
}}{{idn indent}}iterable&lt;<span class='idlIterableKeyType'>{{datatype obj.key}}</span>{{#if obj.value}},<span class='idlIterableValueType'>{{datatype obj.value}}</span>{{/if}}&gt;;</span>
42 changes: 39 additions & 3 deletions js/core/webidl-oldschool.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ define(
, "tmpl!core/templates/webidl/constructor.html"
, "tmpl!core/templates/webidl/attribute.html"
, "tmpl!core/templates/webidl/serializer.html"
, "tmpl!core/templates/webidl/iterable.html"
, "tmpl!core/templates/webidl/maplike.html"
, "tmpl!core/templates/webidl/comment.html"
, "tmpl!core/templates/webidl/field.html"
Expand All @@ -34,7 +35,7 @@ define(
],
function (hb, css, idlModuleTmpl, idlTypedefTmpl, idlImplementsTmpl, idlDictMemberTmpl, idlDictionaryTmpl,
idlEnumItemTmpl, idlEnumTmpl, idlConstTmpl, idlParamTmpl, idlCallbackTmpl, idlMethodTmpl,
idlConstructorTmpl, idlAttributeTmpl, idlSerializerTmpl, idlMaplikeTmpl, idlCommentTmpl, idlFieldTmpl, idlExceptionTmpl, idlInterfaceTmpl) {
idlConstructorTmpl, idlAttributeTmpl, idlSerializerTmpl, idlIterableTmpl, idlMaplikeTmpl, idlCommentTmpl, idlFieldTmpl, idlExceptionTmpl, idlInterfaceTmpl) {
var WebIDLProcessor = function (cfg) {
this.parent = { type: "module", id: "outermost", children: [] };
if (!cfg) cfg = {};
Expand Down Expand Up @@ -560,6 +561,15 @@ define(
return obj;
}

// ITERABLE
match = /^\s*iterable\s*<\s*([^,]*)\s*(,\s*(.*)\s*)?>\s*$/.exec(str);
if (match) {
obj.type = "iterable";
obj.key = match[1];
obj.value = match[3];
return obj;
}

// MAPLIKE
match = /^\s*(readonly\s+)?maplike\s*<\s*(.*)\s*,\s*(.*)\s*>\s*$/.exec(str);
if (match) {
Expand Down Expand Up @@ -741,6 +751,17 @@ define(
sn.element("p", {}, div, [it.description]);
},

writeIterableAsHTML: function (parent, iterable) {
var members = '"entries", "keys", "values" and @@iterator methods';

var p = sn.element("p", {}, parent);
sn.text("This interface has " + members + " brought by ", p);
sn.element("code", {}, p, "iterable");
sn.text(".", p);

sn.element("p", {}, parent, iterable.description);
},

writeMaplikeAsHTML: function (parent, maplike) {
var readonly = "";
var members = "";
Expand All @@ -760,6 +781,13 @@ define(
},

writeTypeFilteredThingsInInterfaceAsHTML: function (obj, curLnk, parent, type, things) {

if (type == "iterable") {
// We assume iterable is specified at most once in one interface.
this.writeIterableAsHTML(parent, things[0]);
return;
}

if (type == "maplike") {
// We assume maplike is specified at most once in one interface.
this.writeMaplikeAsHTML(parent, things[0]);
Expand Down Expand Up @@ -907,8 +935,8 @@ define(
writeInterfaceAsHTML: function (obj) {
var df = sn.documentFragment();
var curLnk = "widl-" + obj.refId + "-";
// maplike is placed first because it doesn't have its own section.
var types = ["maplike", "constructor", "attribute", "method", "constant", "serializer"];
// iterable and maplike are placed first because they don't have their own sections.
var types = ["iterable", "maplike", "constructor", "attribute", "method", "constant", "serializer"];
var filterFunc = function (it) { return it.type == type; }
, sortFunc = function (a, b) {
if (a.unescapedId < b.unescapedId) return -1;
Expand Down Expand Up @@ -1213,6 +1241,7 @@ define(
else if (ch.type == "constant") return self.writeConst(ch, maxConst, indent + 1, curLnk);
else if (ch.type == "serializer") return self.writeSerializer(ch, indent + 1, curLnk);
else if (ch.type == "constructor") ctor.push(self.writeConstructor(ch, indent, "widl-ctor-"));
else if (ch.type == "iterable") return self.writeIterable(ch, indent + 1, curLnk);
else if (ch.type == "maplike") return self.writeMaplike(ch, indent + 1, curLnk);
else if (ch.type == "comment") return self.writeComment(ch, indent + 1);
})
Expand Down Expand Up @@ -1421,6 +1450,13 @@ define(
, values: values
});
},

writeIterable: function (iterable, indent) {
return idlIterableTmpl({
obj: iterable
, indent: indent
});
},

writeMaplike: function (maplike, indent) {
var readonly = maplike.readonly ? "readonly " : "";
Expand Down
57 changes: 56 additions & 1 deletion tests/spec/core/webidl-oldschool-spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe("Core - WebIDL", function () {
var MAXOUT = 5000
, $widl = $("<iframe width='800' height='200' style='display: none' src='spec/core/webidl.html'></iframe>")
, $widl = $("<iframe width='800' height='200' src='spec/core/webidl.html'></iframe>")
, loaded = false
, $target
, text
Expand Down Expand Up @@ -209,6 +209,61 @@ describe("Core - WebIDL", function () {
expect($serializer.find(".idlSerializerValues").text()).toEqual("{foo, bar}");
});

it("should handle maplike", function() {
$target = $("#if-maplike", doc);
text = "interface SuperStar {\n" +
" void foo ();\n" +
" maplike<DOMString, SuperStar>;\n" +
" attribute DOMString bar;\n" +
"};";
expect($target.text()).toEqual(text);
expect($target.find(".idlMaplike").length).toEqual(1);
var $iterable = $target.find(".idlMaplike").first();
expect($iterable.find(".idlMaplikeKeyType").text()).toEqual("DOMString");
expect($iterable.find(".idlMaplikeValueType").text()).toEqual("SuperStar");
});

it("should handle readonly maplike", function() {
$target = $("#if-readonly-maplike", doc);
text = "interface SuperStar {\n" +
" void foo ();\n" +
" readonly maplike<DOMString, SuperStar>;\n" +
" attribute DOMString bar;\n" +
"};";
expect($target.text()).toEqual(text);
expect($target.find(".idlMaplike").length).toEqual(1);
var $iterable = $target.find(".idlMaplike").first();
expect($iterable.find(".idlMaplikeKeyType").text()).toEqual("DOMString");
expect($iterable.find(".idlMaplikeValueType").text()).toEqual("SuperStar");
});

it("should handle value iterable", function() {
$target = $("#if-iterable", doc);
text = "interface AnIterableInterface {\n" +
" iterable<DOMString>;\n" +
" attribute DOMString foo;\n" +
" attribute DOMString bar;\n" +
"};";
expect($target.text()).toEqual(text);
expect($target.find(".idlIterable").length).toEqual(1);
var $iterable = $target.find(".idlIterable").first();
expect($iterable.find(".idlIterableKeyType").text()).toEqual("DOMString");
});

it("should handle pair iterable", function() {
$target = $("#if-iterable-pairs", doc);
text = "interface AnIterablePairInterface {\n" +
" iterable<DOMString,SuperStar>;\n" +
" attribute DOMString foo;\n" +
" attribute DOMString bar;\n" +
"};";
expect($target.text()).toEqual(text);
expect($target.find(".idlIterable").length).toEqual(1);
var $iterable = $target.find(".idlIterable").first();
expect($iterable.find(".idlIterableKeyType").text()).toEqual("DOMString");
expect($iterable.find(".idlIterableValueType").text()).toEqual("SuperStar");
});

it("should handle comments", function () {
$target = $("#comments-basic", doc);
text = "interface SuperStar {\n" +
Expand Down
23 changes: 23 additions & 0 deletions tests/spec/core/webidl.html
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ <h2>Implements</h2>
</p>
<div id='impl-less-basic' class='idl' title='[Something]Window implements Breakable'></div>
</section>

<section>
<h2>maplike</h2>
<dl id="if-maplike" class="idl" title="interface SuperStar">
Expand All @@ -436,5 +437,27 @@ <h2>readonly maplike</h2>
<dd>bar description</dd>
</dl>
</section>


<section>
<h2>iterable</h2>
<dl id="if-iterable" class="idl" title="interface AnIterableInterface">
<dt>iterable&lt;DOMString&gt;</dt>
<dd>The values to iterate over are a snapshot of the names of the mome raths, in lexical order.</dd>
<dt>attribute DOMString foo</dt>
<dt>attribute DOMString bar</dt>
</dl>
</section>
<section>
<h2>iterable-pairs</h2>
<dl id="if-iterable-pairs" class="idl" title="interface AnIterablePairInterface">
<dt>iterable&lt;DOMString,SuperStar&gt;</dt>
<dd>The key value pairs to iterate over are a snapshot of the names and mimsy states of the mome raths, in lexical order of name.</dd>
<dt>attribute DOMString foo</dt>
<dt>attribute DOMString bar</dt>
</dl>
</section>


</body>
</html>