This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
/
CSSRef.ejs
291 lines (251 loc) · 9.87 KB
/
CSSRef.ejs
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
<%
// CSS Sidebar
var slug = env.slug;
var locale = env.locale;
var pageTags = env.tags;
var title = env.title;
var CSSHref = '/' + locale + '/docs/Web/CSS';
var CSSRefHref = '/' + locale + '/docs/Web/CSS/Reference';
var output = "";
// slug is not available in preview mode.
if (slug) {
var name = slug.replace('Web/CSS/','').split('/')[0];
var hasTag = page.hasTag;
var escapeQuotes = mdn.escapeQuotes;
var htmlEscape = kuma.htmlEscape;
var rtlLocales = ['ar', 'he', 'fa'];
var data = require('mdn-data/css');
var commonl10n = string.deserialize(await template('L10n:Common'));
var cssl10n = string.deserialize(await template('L10n:CSS'));
var text = {
'Properties': mdn.getLocalString(commonl10n, 'Properties'),
'translate': mdn.getLocalString(commonl10n, '[Translate]'),
'title': mdn.getLocalString(commonl10n, 'TranslationCTA'),
'css_ref_title': mdn.getLocalString(cssl10n, 'css_ref_title'),
'at_rules': mdn.getLocalString(cssl10n, 'at_rules'),
'types': mdn.getLocalString(cssl10n, 'types'),
'Guides': mdn.getLocalString(commonl10n, 'Guides'),
'tag_selectors': mdn.getLocalString(cssl10n, 'tag_selectors'),
'Basic Selectors': mdn.getLocalString(cssl10n, 'Basic Selectors'),
'Pseudo-classes': mdn.getLocalString(cssl10n, 'Pseudo-classes'),
'Pseudo-elements': mdn.getLocalString(cssl10n, 'Pseudo-elements'),
'Combinators': mdn.getLocalString(cssl10n, 'Combinators')
}
function isAtRule(foo) {
return foo.indexOf('@') !== -1;
}
function isType(foo) {
return foo.indexOf('<') !== -1 && foo.indexOf('>') !== -1;
}
function containsTag(tagList, tag) {
if (!tagList || typeof tagList == 'undefined') return 0;
tag = tag.toLowerCase();
for (var i = 0, len = tagList.length; i < len; i++) {
if (tagList[i].toLowerCase() == tag) return 1;
}
return 0;
}
var group = [];
var isSelector = containsTag(pageTags, text['tag_selectors']) || slug.indexOf(':') !== -1;
var isOverviewPage = slug.indexOf("_") !== -1 && !isType(title);
if (isOverviewPage && !isSelector) {
group.push(name.replace(/_/g, ' '));
} else if (isSelector) {
group.push('CSS Selectors');
} else {
name = name.toLowerCase();
// Find group of this page in CSSData
if (isAtRule(slug) && data.atRules[name] && data.atRules[name].groups) {
group = group.concat(data.atRules[name].groups);
} else if (data.properties[name] && data.properties[name].groups) {
group = group.concat(data.properties[name].groups);
} else if (isType(title)) {
var cleanType = title;
if (!(cleanType in data.types)) {
// https://github.com/mdn/data/pull/81 was merged
cleanType = title.replace('<', '').replace('>', '');
}
if (data.types[cleanType] && data.types[cleanType].groups) {
group = group.concat(data.types[cleanType].groups);
}
}
}
var groupData = [];
var properties = [];
var atRules = [];
var selectors = [];
var types = [];
// I am a selector or a type, we want to list all of them.
if (isSelector) {
for (var selector in data.selectors) {
groupData.push(selector);
}
} else {
// I am a property or an at-rule
// Find other properties, at-rules or types with these groups
for (var property in data.properties) {
var propertyGroup = data.properties[property].groups;
for (var g in propertyGroup) {
if (group.indexOf(propertyGroup[g]) !== -1 &&
groupData.indexOf(property) === -1) {
groupData.push(property);
}
}
}
for (var atRule in data.atRules) {
var atRuleGroup = data.atRules[atRule].groups;
for (var g in atRuleGroup) {
if (group.indexOf(atRuleGroup[g]) !== -1 &&
groupData.indexOf(atRule) === -1) {
groupData.push(atRule);
}
}
}
for (var type in data.types) {
var typeGroup = data.types[type].groups;
for (var g in typeGroup) {
if (group.indexOf(typeGroup[g]) !== -1 &&
groupData.indexOf(type) === -1) {
groupData.push(type);
}
}
}
}
// Load the CSS pages data, gather info for the found properties and atRules
var pageList = await page.subpagesExpand('/en-US/docs/Web/CSS');
for (var i in pageList) {
var aPage = pageList[i];
for (var x in groupData) {
if (aPage.slug.split("/").pop() === groupData[x].replace(/ /g, '_')) {
if (isAtRule(groupData[x])) {
atRules.push(aPage);
} else if (aPage.slug.indexOf('selectors') !== -1 ||
aPage.slug.indexOf('combinator') !== -1 ||
aPage.slug.indexOf(':') !== -1 ) {
selectors.push(aPage);
} else {
properties.push(aPage);
}
} else if (aPage.title === groupData[x] && isType(groupData[x])) {
types.push(aPage);
}
}
}
if (isSelector) {
var selectorData = data.selectors;
var selectorCollection = {};
for (var selector in selectorData) {
var g = selectorData[selector].groups[0];
for (s in selectors) {
var aPage = selectors[s];
if(aPage.slug.split("/").pop().replace(/_/g, ' ') === selector) {
if (g in selectorCollection) {
selectorCollection[g].push(aPage);
} else {
selectorCollection[g] = [];
selectorCollection[g].push(aPage);
}
}
}
}
}
// Load sub pages of the group
var groupPages = [];
for (var i = 0; i < group.length; i++) {
groupPages = groupPages.concat(await page.subpagesExpand('/en-US/docs/Web/CSS/' + group[i].replace(/ /g, '_')));
}
// output helpers
var badges = {
ExperimentalBadge : '<span class="sidebar-icon">' + await template("ExperimentalBadge", [1]) + '</span>',
NonStandardBadge : '<span class="sidebar-icon">' + await template("NonStandardBadge", [1]) + '</span>',
DeprecatedBadge : '<span class="sidebar-icon">' + await template("DeprecatedBadge", [1]) + '</span>',
ObsoleteBadge : '<span class="sidebar-icon">' + await template("ObsoleteBadge", [1]) + '</span>',
}
function buildSublist(pages, title, noCodeTags) {
var result = '<li class="toggle"><details open><summary>' + title + '</summary><ol>';
var code = !noCodeTags ? '<code>' : '';
var endcode = !noCodeTags ? '</code>' : '';
for (var i in pages) {
var aPage = pages[i];
var summary = escapeQuotes(aPage.summary) || '';
var url = aPage.url.replace('en-US', locale);
var title = htmlEscape(aPage.title);
var translated = false;
if (locale != 'en-US') {
aPage.translations.forEach(function(translation){
if(translation.locale === locale) {
summary = escapeQuotes(translation.summary) || '';
url = translation.url;
title = htmlEscape(translation.title);
translated = true;
}
});
}
var cta = '';
if (!translated && locale != 'en-US') {
cta += ' <a href="'+ url +'$translate" style="opacity:0.5" title="'+ text['title'] + '">' + text['translate'] + '</a>';
}
result += '<li>';
if (hasTag(aPage, 'Experimental')) {
result += badges.ExperimentalBadge;
}
if (hasTag(aPage, 'Non-standard') || hasTag(aPage, 'Non Standard')) {
result += badges.NonStandardBadge;
}
if (hasTag(aPage, 'Deprecated')) {
result += badges.DeprecatedBadge;
}
if (hasTag(aPage, 'Obsolete')) {
result += badges.ObsoleteBadge;
result += '<s class="obsoleteElement">';
}
if (rtlLocales.indexOf(locale) != -1) {
result += '<bdi>';
}
if (slug == aPage.slug) {
result += '<em>' + code + title + endcode + '</em>'
} else {
result += '<a href="' + url + '" title="' + summary + '">' + code + title + endcode + '</a>' + cta;
}
if (rtlLocales.indexOf(locale) != -1) {
result += '</bdi>';
}
if (hasTag(aPage, 'Obsolete')) {
result += '</s>';
}
result += '</li>';
}
result += '</ol></details></li>';
return result;
}
// output
output = '<section class="Quick_links" id="Quick_Links"><ol>';
output += '<li><strong><a href="' + CSSHref + '">CSS</a></strong></li>';
output += '<li><strong><a href="' + CSSRefHref + '">' + text['css_ref_title'] + '</a></strong></li>';
for (var i = 0; i < group.length; i++) {
output += '<li><strong><a href="' + CSSHref + '/' + group[i].replace(/ /g, '_') + '">' + group[i] + '</a></strong></li>';
}
if (groupPages.length > 0) {
output += buildSublist(groupPages, text['Guides'], true);
}
if (atRules.length > 0) {
output += buildSublist(atRules, text['at_rules']);
}
if (properties.length > 0) {
output += buildSublist(properties, text['Properties']);
}
if (types.length > 0) {
output += buildSublist(types, text['types']);
}
if (isSelector) {
for (sg in selectorCollection) {
var selectorGroup = selectorCollection[sg];
if (selectorGroup.length > 0) {
output += buildSublist(selectorGroup, text[sg]);
}
}
}
output += '</ol></section>';
}
%>
<%-output%>