-
Notifications
You must be signed in to change notification settings - Fork 513
/
CSSInfo.ejs
308 lines (258 loc) · 9.67 KB
/
CSSInfo.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<%
/*
Displays general information about a CSS property or descriptor
$0 - property/descriptor name - defaults to the slug name
$1 - @-rule - defaults to the @-rule within the URL
*/
const data = require('mdn-data/css');
const localStrings = require('mdn-data/l10n/css');
let slug = env.slug;
let name = $0 || (slug ? slug.split("/").pop().toLowerCase() :
"preview-wiki-content");
let atRule = $1;
let formattedError = "<span style=\"color:red;\">$1$</span>";
let locale = env.locale;
let localize = mdn.getLocalString;
let localizeString = mdn.localString;
let replacePlaceholders = mdn.replacePlaceholders;
let localWiki = wiki;
let currentPage = page;
let cssLocalStrings = web.getJSONData("L10n-CSS");
/*
Creates a link to another API, e.g. a CSS property or SVG element
This function is used as a workaround for not being able to call other
templates like 'cssxref' from within functions (see https://bugzil.la/939214)
apiName - Name of the API to link to
area - Area of the API (i.e. CSS, SVG, etc.)
linkName - String to use as link label
*/
function createLink(apiName, area, linkName) {
let formattedLinkName = linkName || apiName;
let url = "/" + locale + "/docs/Web/" + (area || "CSS") + "/" + apiName;
let thisPage = localWiki.getPage(url);
if (thisPage.pageType === "css-function") {
formattedLinkName += "()";
} else if (thisPage.pageType === "css-type" || thisPage.pageType === "svg-element") {
formattedLinkName = "<" + formattedLinkName + ">";
}
return "<a href=\"" + url + "\"><code>" +
formattedLinkName + "</code></a>";
}
/*
Parses all macros within a string
s - String to parse
*/
async function parseMacros(s) {
if (s === undefined) {
return undefined;
}
var area = "";
let parsedString = s.replace(/\{\{(.+?)(?:\((.+?)\))?\}\}/g, (match, macroName, paramsString) => {
const params = paramsString ? paramsString.split(/, ?/) : [];
params.forEach((param, index, params) => {
params[index] = params[index].toLowerCase().slice(1, -1);
});
let apiName = (params.length !== 0 ? params[0] : "");
switch (macroName.toLowerCase()) {
case "xref_csslength":
apiName = "length";
area = "CSS";
break;
case "xref_cssangle":
apiName = "angle";
area = "CSS";
break;
case "svgelement":
area = "SVG/Element";
break;
default:
area = "CSS";
}
return createLink(apiName, area, params[1]);
});
return parsedString;
}
/*
Appends information about to which pseudo-elements a CSS property applies to
the output to which normal elements it applies
output - Current value output
property - CSS info item name
cssInfo - Structure containing information about a CSS property
*/
function addAdditionalAppliesToOutput(output, property, cssInfo) {
if (property !== "appliesto" || !Object.prototype.hasOwnProperty.call(cssInfo, "alsoAppliesTo")) {
return output;
}
var additionalApplies = "";
// Remove '::placeholder' from array to avoid displaying it,
// because it's not standardized yet
var placeholderIndex = cssInfo.alsoAppliesTo.indexOf("::placeholder");
if (placeholderIndex !== -1) {
cssInfo.alsoAppliesTo.splice(placeholderIndex, 1);
}
// In case there are no items left, the output is returned unchanged
if (cssInfo.alsoAppliesTo.length === 0) {
return output;
}
cssInfo.alsoAppliesTo.forEach(function(element, index, elements) {
additionalApplies += "{{cssxref(\"" + element + "\")}}";
if (index < elements.length - 2) {
additionalApplies += localize(localStrings, "listSeparator");
} else if (index < elements.length - 1) {
additionalApplies += localize(localStrings, "andInEnumeration");
}
});
return replacePlaceholders(localize(localStrings, "applyingToMultiple"),
[output, additionalApplies]);
}
/*
Parses all macros within a string
string - String to parse
*/
async function getValueOutput(cssInfo, property, atRule) {
if (property === "relatedAtRule") {
return "<a href=\"/" + locale + "/docs/Web/CSS/" + atRule +
"\"><code>" + atRule + "</code></a>";
}
var value = cssInfo[property];
if (typeof value === "string") {
if (property === "animationType") {
var animationTypeValues = value.split(" ");
var parsedAnimationTypeValues =
animationTypeValues.map(function(animationTypeValue) {
var localizedString = localize(localStrings, animationTypeValue);
if (animationTypeValue === "lpc") {
localizedString = replacePlaceholders(localizedString,
[localize(localStrings, "length")]);
}
return localizedString;
});
return await parseMacros(addAdditionalAppliesToOutput(parsedAnimationTypeValues.join(
localize(localStrings, "listSeparator")), property, cssInfo));
} else {
var propMatches = value.match(/^'(.+?)'$/);
if (propMatches) {
return await getValueOutput(data.properties[propMatches[1]], property);
} else {
if (property === "initial" && !Object.prototype.hasOwnProperty.call(localStrings, value)) {
return '<code>' + value + '</code>';
} else {
var keywords = value.split(", ");
var replacedKeywords = keywords.map(function(keyword) {
return localize(localStrings, keyword);
});
var output = addAdditionalAppliesToOutput(
replacedKeywords.join(", "), property, cssInfo);
return await parseMacros(output);
}
}
}
} else if (Array.isArray(value)) {
var output = localize(localStrings, "asLonghands") + "<br/>" +
"<ul>";
for(let longhand of value) {
output += "<li>{{cssxref(\"" + longhand + "\")}}: " +
(Object.prototype.hasOwnProperty.call(data.properties, longhand) ?
await getValueOutput(data.properties[longhand], property) :
replacePlaceholders(formattedError,
[localize(cssLocalStrings, "missing")])) +
"</li>";
}
output += "</ul>";
return await parseMacros(addAdditionalAppliesToOutput(output, property,
cssInfo));
} else if (typeof value === "object") {
var output = localizeString(value);
return await parseMacros(addAdditionalAppliesToOutput(output, property,
cssInfo));
} else if (typeof value === "boolean") {
return localize(localStrings, value ? "yes" : "no");
} else if (typeof value === "undefined") {
return replacePlaceholders(formattedError, [localize(cssLocalStrings,
"missing")]);
}
return await parseMacros(value);
}
var cssInfo = null;
if (!atRule) {
var matches = null;
if (slug) {
matches = slug.match(/\/CSS\/(@.+?)(?=\/)/);
}
if (matches) {
atRule = matches[1];
}
}
if (name !== "preview-wiki-content") {
if (atRule) {
if (data.atRules[atRule] && data.atRules[atRule].descriptors) {
cssInfo = data.atRules[atRule].descriptors[name];
}
} else if (data.properties[name]) {
cssInfo = data.properties[name];
}
}
var result = "";
if (name === "preview-wiki-content") {
result = "<span style=\"color:red;\">" +
localize(cssLocalStrings, "info_in_preview_not_available") + "</span>";
} else if (cssInfo) {
result = "<table class=\"properties\">" +
"<tbody>";
var properties = [];
if (atRule) {
properties = properties.concat({
name: "relatedAtRule",
label: localize(localStrings, "relatedAtRule")
});
}
properties = properties.concat({
name: "initial",
label: await template("Xref_cssinitial")
});
if (!atRule) {
properties = properties.concat({
name: "appliesto",
label: localize(localStrings, "appliesTo")
});
}
if (Object.prototype.hasOwnProperty.call(cssInfo, "inherited")) {
properties = properties.concat({
name: "inherited",
label: await template("Xref_cssinherited")
});
}
if (cssInfo.percentages !== "no") {
properties = properties.concat({
name: "percentages",
label: localize(localStrings, "percentages")
});
}
properties = properties.concat({
name: "computed",
label: await template("Xref_csscomputed")
});
if (!atRule) {
properties = properties.concat({
name: "animationType",
label: web.smartLink(`/${env.locale}/docs/Web/CSS/CSS_animated_properties`, null, localize(localStrings, "animationType"))
});
}
if (cssInfo.stacking) {
properties = properties.concat({
name: "stacking",
label: localize(localStrings, "createsStackingContext")
});
}
for(let property of properties) {
result += "<tr>" +
"<th scope=\"row\">" + property.label + "</th><td>" +
await getValueOutput(cssInfo, property.name, atRule) + "</td>";
}
result += "</tbody>" +
"</table>";
} else {
result = replacePlaceholders(formattedError,
[localize(cssLocalStrings, "missing")]);
}
%><%- result %>