Skip to content

Commit

Permalink
Drop dependency on jQuery (#264)
Browse files Browse the repository at this point in the history
This update re-writes the calls to jQuery using regular DOM functions and drops
the dependency on jQuery.
  • Loading branch information
tidoust authored Dec 4, 2020
1 parent 0a39613 commit 171f66d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 38 deletions.
1 change: 0 additions & 1 deletion media-source-respec.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Media Source Extensions™</title>
<script src="https://www.w3.org/Tools/respec/respec-w3c" class="remove"></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="media-source.js" class="remove"></script>
<script class="remove">
var respecConfig = {
Expand Down
117 changes: 80 additions & 37 deletions media-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,43 @@
}

function eventdfn_helper(doc, df, id, text) {
df.appendChild($("<dfn/>").attr({id: 'dom-evt-' + text.toLowerCase()}).wrapInner($("<code/>").text(text))[0]);
const code = doc.createElement('code');
code.appendChild(doc.createTextNode(text));
const dfn = doc.createElement('dfn');
dfn.setAttribute('id', 'dom-evt-' + text.toLowerCase());
dfn.appendChild(code);
df.appendChild(dfn);
}

function idlref_helper(doc, df, id, text) {
df.appendChild($("<code/>").wrapInner($("<a/>").attr({href: url_helper(doc, "#" + id)}).text(text))[0]);
const anchor = doc.createElement('a');
anchor.setAttribute('href', url_helper(doc, '#' + id));
anchor.appendChild(doc.createTextNode(text));
const code = doc.createElement('code');
code.appendChild(anchor);
df.appendChild(code);
}

function eventref_helper(doc, df, id, text) {
df.appendChild($("<code/>").wrapInner($("<a/>").attr({href: url_helper(doc, "#dom-evt-" + id)}).text(text))[0]);
const anchor = doc.createElement('a');
anchor.setAttribute('href', url_helper(doc, '#dom-evt-' + id));
anchor.appendChild(doc.createTextNode(text));
const code = doc.createElement('code');
code.appendChild(anchor);
df.appendChild(code);
}

function videoref_helper(doc, df, id, text) {
link_helper(doc, df, HTML5_spec_url + '#' + id, text);
}

function code_videoref_helper(doc, df, id, text) {
df.appendChild($("<code/>").wrapInner($("<a/>").attr({href: HTML5_spec_url + "#" + id}).text(text))[0]);
const anchor = doc.createElement('a');
anchor.setAttribute('href', url_helper(doc, HTML5_spec_url + '#' + id));
anchor.appendChild(doc.createTextNode(text));
const code = doc.createElement('code');
code.appendChild(anchor);
df.appendChild(code);
}

function fileapi_helper(doc, df, id, text) {
Expand Down Expand Up @@ -63,19 +83,34 @@
}

function var_helper(doc, df, id, text) {
df.appendChild($("<var/>").wrapInner($("<a/>").attr({href: url_helper(doc, id)}).text(text))[0]);
const anchor = doc.createElement('a');
anchor.setAttribute('href', url_helper(doc, url_helper(doc, id)));
anchor.appendChild(doc.createTextNode(text));
const varEl = doc.createElement('var');
varEl.appendChild(anchor);
df.appendChild(varEl);
}

function link_helper(doc, df, id, text) {
df.appendChild($("<a/>").attr({href: url_helper(doc, id)}).text(text)[0]);
const anchor = doc.createElement('a');
anchor.setAttribute('href', url_helper(doc, url_helper(doc, id)));
anchor.appendChild(doc.createTextNode(text));
df.appendChild(anchor);
}

function exception_helper(doc, df, id, text) {
df.appendChild($("<code/>").wrapInner($("<a/>").attr({href: WEBIDL_spec_url + '#' + id}).text(text))[0]);
const anchor = doc.createElement('a');
anchor.setAttribute('href', url_helper(doc, WEBIDL_spec_url + '#' + id));
anchor.appendChild(doc.createTextNode(text));
const code = doc.createElement('code');
code.appendChild(anchor);
df.appendChild(code);
}

function typeerror_helper(doc, df, id, text) {
df.appendChild($("<code/>").text(text)[0]);
const code = doc.createElement('code');
code.appendChild(doc.createTextNode(text));
df.appendChild(code);
}

function webmref_helper(doc, df, id, text) {
Expand Down Expand Up @@ -459,26 +494,30 @@
}
}

$("a[def-id]").each(function () {
$(this).addClass('externalDFN');
});
[...document.querySelectorAll('a[def-id]')].forEach(el =>
el.classList.add('externalDFN'));

// Process external links first, so ReSpec will leave them alone
$("a:not([href])").each( function() {
var $ant = $(this);
var className = $ant.text();
[...document.querySelectorAll('a:not([href])')].forEach(el => {
var className = el.textContent;
var info = externalClassInfo[className];
if (info) {
var id = info.fragment;
var df = document.createDocumentFragment();
var baseURL = lookupBaseUrlForSpec( info );

if (baseURL) {
df.appendChild($("<code/>").wrapInner($("<a/>").attr({href: baseURL + "#" + id, 'class': 'idlType'}).text(className))[0]);
this.parentNode.replaceChild(df, this);
const anchor = document.createElement('a');
anchor.setAttribute('href', baseURL + "#" + id);
anchor.className = 'idlType';
anchor.appendChild(document.createTextNode(className));
const code = document.createElement('code');
code.appendChild(anchor);
df.appendChild(code);
el.parentNode.replaceChild(df, el);
}
}
} );
});

var registry_biblio_entries =
getMediaSourceRegistryBibioEntries(specStatus);
Expand Down Expand Up @@ -529,9 +568,8 @@

var usedMap = {};

$("a[def-id]").each(function () {
var $ant = $(this);
var def_id = $ant.attr('def-id');
[...document.querySelectorAll('a[def-id]')].forEach(el => {
var def_id = el.getAttribute('def-id');
var info = definitionInfo[def_id];
if (info) {
if (!usedMap[def_id]) {
Expand All @@ -543,11 +581,11 @@
var id = info.fragment;
var text = info.link_text;

if ($ant.attr('name')) {
id = $ant.attr('name');
if (el.getAttribute('name')) {
id = el.getAttribute('name');
}

var element_text = this.innerHTML;
var element_text = el.textContent;
if (element_text) {
text = element_text;
}
Expand All @@ -556,35 +594,41 @@
doc.mseDefGroupName = info.groupName;
info.func(doc, df, id, text);
doc.mseDefGroupName = "";
this.parentNode.replaceChild(df, this);
el.parentNode.replaceChild(df, el);

} else {
console.log("Found def-id '" + def_id + "' but it does not correspond to anything");
}
});

// Link external references from IDL and method parameter tables
$("span.idlAttrType, span.idlMethType, span.idlParamType, td.prmType").each( function() {
var $ant = $(this);
var className = $ant.text();
[...document.querySelectorAll('span.idlAttrType, span.idlMethType, span.idlParamType, td.prmType')].forEach(el => {
var className = el.textContent;
var info = externalClassInfo[className];
if (info) {
var id = info.fragment;
var baseURL = lookupBaseUrlForSpec( info );

if (baseURL) {
$ant.empty();
$ant.append($("<code/>").wrapInner($("<a/>").attr({href: baseURL + "#" + id }).text(className)));
el.innerHTML = '';
const anchor = document.createElement('a');
anchor.setAttribute('href', baseURL + "#" + id);
anchor.appendChild(document.createTextNode(className));
const code = document.createElement('code');
code.appendChild(anchor);
el.appendChild(code);
}
}
} );

// Move algorithm text after method parameter & return value information.
$("ol.method-algorithm").each(function() {
var parent = this.parentNode;
parent.removeChild(this);
parent.appendChild($("<p/>").text("When this method is invoked, the user agent must run the following steps:")[0]);
parent.appendChild(this);
[...document.querySelectorAll('ol.method-algorithm')].forEach(el => {
var parent = el.parentNode;
parent.removeChild(el);
const p = document.createElement('p');
p.appendChild(document.createTextNode('When this method is invoked, the user agent must run the following steps:'));
parent.appendChild(p);
parent.appendChild(el);
});

// Validate that all defined def-ids are actually used.
Expand All @@ -596,9 +640,8 @@
}
}

$("a[href]").each(function () {
var link = $(this);
var href = link.attr('href');
[...document.querySelectorAll('a[href]')].forEach(el => {
var href = el.getAttribute('href');
var matched = /^#(.+)$/.exec(href);
if (matched) {
var id = matched[1];
Expand Down

0 comments on commit 171f66d

Please sign in to comment.