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
/
link-to-dfn.js
120 lines (118 loc) · 5.25 KB
/
link-to-dfn.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
// Module core/link-to-dfn
// Gives definitions in conf.definitionMap IDs and links <a> tags to the matching definitions.
define(
["core/pubsubhub", "core/data-cite"],
function(pubsubhub, dataCite) {
return {
run: function(conf, doc, cb) {
doc.normalize();
var titles = {};
Object.keys(conf.definitionMap).forEach(function(title) {
titles[title] = {};
conf.definitionMap[title].forEach(function(dfn) {
if (dfn.attr('data-idl') === undefined) {
// Non-IDL definitions aren't "for" an interface.
dfn.removeAttr('data-dfn-for');
}
var dfn_for = dfn.attr('data-dfn-for') || "";
if (dfn_for in titles[title]) {
// We want <dfn> definitions to take precedence over
// definitions from WebIDL. WebIDL definitions wind
// up as <span>s instead of <dfn>.
var oldIsDfn = titles[title][dfn_for].filter('dfn').length !== 0;
var newIsDfn = dfn.filter('dfn').length !== 0;
if (oldIsDfn && newIsDfn) {
// Only complain if the user provides 2 <dfn>s
// for the same term.
pubsubhub.pub("error", "Duplicate definition of '" + (dfn_for ? dfn_for + "/" : "") + title + "'");
}
if (oldIsDfn) {
// Don't overwrite <dfn> definitions.
return;
}
}
titles[title][dfn_for] = dfn;
if (dfn.attr('id') === undefined) {
if (dfn.attr('data-idl')) {
dfn.makeID("dom", (dfn_for ? dfn_for + "-" : "") + title);
} else {
dfn.makeID("dfn", title);
}
}
});
});
$("a:not([href]):not([data-cite])").each(function() {
var $ant = $(this);
if ($ant.hasClass("externalDFN")) return;
var linkTargets = $ant.linkTargets();
var foundDfn = linkTargets.some(function(target) {
if (titles[target.title] && titles[target.title][target.for_]) {
var dfn = titles[target.title][target.for_];
if (dfn[0].dataset.cite) {
$ant[0].dataset.cite = dfn[0].dataset.cite;
} else {
$ant.attr("href", "#" + dfn.prop("id")).addClass("internalDFN");
}
// add a bikeshed style indication of the type of link
if (!$ant.attr("data-link-type")) {
$ant.attr("data-link-type", "dfn");
}
// If a definition is <code>, links to it should
// also be <code>.
//
// Note that contents().length===1 excludes
// definitions that have either other text, or other
// whitespace, inside the <dfn>.
if (dfn.closest('code,pre').length ||
(dfn.contents().length === 1 && dfn.children('code').length === 1)) {
// only add code to IDL when the definition matches
const term = $ant[0].textContent.trim();
const isIDL = dfn[0].dataset.hasOwnProperty("idl");
const isSameText = (isIDL) ? dfn[0].dataset.title === term : dfn[0].textContent.trim() === term;
if (isIDL && !isSameText) {
return true;
}
$ant.wrapInner('<code></code>');
}
return true;
}
return false;
});
if (!foundDfn) {
// ignore WebIDL
if (!$ant.parents(".idl, dl.methods, dl.attributes, dl.constants, dl.constructors, dl.fields, dl.dictionary-members, span.idlMemberType, span.idlTypedefType, div.idlImplementsDesc").length) {
var link_for = linkTargets[0].for_;
var title = linkTargets[0].title;
this.classList.add("respec-offending-element");
this.title = "Linking error: not matching <dfn>";
pubsubhub.pub("warn", "Found linkless <a> element " + (link_for ? "for '" + link_for + "' " : "") + "with text '" + title + "' but no matching <dfn>.");
console.warn("Linkless element:", $ant[0]);
return;
}
$ant.replaceWith($ant.contents());
}
});
dataCite.linkInlineCitations(doc).then(function() {
// done linking, so clean up
function attrToDataAttr(name) {
return function(elem) {
var value = elem.getAttribute(name);
elem.removeAttribute(name);
elem.setAttribute("data-" + name, value);
}
}
var forList = doc.querySelectorAll("*[for]");
Array.prototype.forEach.call(forList, attrToDataAttr("for"));
var dfnForList = doc.querySelectorAll("*[dfn-for]");
Array.prototype.forEach.call(dfnForList, attrToDataAttr("dfn-for"));
var linkForList = doc.querySelectorAll("*[link-for]");
Array.prototype.forEach.call(linkForList, attrToDataAttr("link-for"));
// Added message for legacy compat with Aria specs
// See https://github.com/w3c/respec/issues/793
pubsubhub.pub("end", "core/link-to-dfn");
cb();
});
}
};
}
);