-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmscgen-inpage.js
274 lines (236 loc) · 7.89 KB
/
mscgen-inpage.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
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
const mscparser = require("mscgenjs/dist/cjs/parse/xuparser");
const msgennyparser = require("mscgenjs/dist/cjs/parse/msgennyparser");
const renderast = require("mscgenjs/dist/cjs/render/graphics/renderast");
const config = require("./embedding/config");
const errorRendering = require("./embedding/error-rendering");
const exporter = require("./utl/exporter");
const ERR_FILE_LOADING_DISABLED =
"ERROR: Won't load the chart specified,\n" +
"because loading from separate files is switched off in the mscgen_js " +
"configuration. \n\nSee " +
"https://sverweij.github.io/mscgen_js/embed.html#loading-from-separate-files " +
"how to enable it.";
const MIME2LANG = Object.freeze({
"text/x-mscgen": "mscgen",
"text/x-msgenny": "msgenny",
"text/x-xu": "xu",
});
function getLanguage(pElement) {
let lLanguage =
pElement.dataset.language || MIME2LANG[pElement.getAttribute("type")];
if (!lLanguage) {
lLanguage = config.getConfig().defaultLanguage;
}
return lLanguage;
}
function getAST(pText, pLanguage) {
let lAST = {};
try {
if (pLanguage === "msgenny") {
lAST = msgennyparser.parse(pText);
} else if (pLanguage === "json") {
lAST = JSON.parse(pText);
} else {
lAST = mscparser.parse(pText);
}
} catch (pError) {
return pError;
}
return lAST;
}
function renderElementError(pElement, pString) {
const lElement = document.createElement("pre");
lElement.setAttribute("style", "color: #d00");
lElement.textContent = pString;
pElement.replaceChildren(lElement);
}
function setElementId(pElement, pIndex) {
if (!pElement.id) {
pElement.id = config.getConfig().parentElementPrefix + pIndex.toString();
}
}
function renderLink(pSource, pLanguage, pId) {
let lLocation = {
pathname: "index.html",
};
let lLink = document.createElement("a");
lLink.setAttribute(
"href",
config.getConfig().clickURL +
exporter.toLocationString(lLocation, pSource, pLanguage),
);
lLink.setAttribute("id", `${pId}link`);
lLink.setAttribute("style", "text-decoration: none;");
lLink.setAttribute("title", "click to edit in the mscgen_js interpreter");
return lLink;
}
function render(pAST, pElementId, pOptions) {
// eslint-disable-next-line unicorn/prefer-query-selector
let lElement = document.getElementById(pElementId);
lElement.innerHTML = "";
if (config.getConfig().clickable === true) {
lElement.append(renderLink(pOptions.source, pOptions.language, pElementId));
pElementId += "link";
}
renderast.clean(pElementId, window);
renderast.render(pAST, window, pElementId, {
source: pOptions.source,
additionalTemplate: pOptions.namedStyle,
mirrorEntitiesOnBottom: pOptions.mirrorEntities,
regularArcTextVerticalAlignment: pOptions.regularArcTextVerticalAlignment,
});
}
function getMirrorEntities(pElement) {
let lMirrorEntities = pElement.dataset.mirrorEntities;
return lMirrorEntities && lMirrorEntities === "true";
}
function getNamedStyle(pElement) {
return pElement.dataset.namedStyle || "basic";
}
function getVerticalAlignment(pElement) {
return pElement.dataset.regularArcTextVerticalAlignment || "middle";
}
function parseAndRender(
pRenderElement,
pSource,
pSourceElement = pRenderElement,
) {
let lLanguage = getLanguage(pSourceElement);
let lAST = getAST(pSource, lLanguage);
if (lAST.entities) {
render(lAST, pRenderElement.id, {
source: pSource,
language: lLanguage,
mirrorEntities: getMirrorEntities(pSourceElement),
namedStyle: getNamedStyle(pSourceElement),
regularArcTextVerticalAlignment: getVerticalAlignment(pSourceElement),
});
} else {
pRenderElement.innerHTML = errorRendering.renderError(
pSource,
lAST.location,
lAST.message,
);
}
}
function getResponseStatus(pResponse) {
if (pResponse.ok) {
return Promise.resolve(pResponse);
} else {
return Promise.reject(new Error(`ERROR: ${pResponse.statusText}`));
}
}
function getResponseText(pResponse) {
return pResponse.text();
}
function getSourceAttribute(pElement) {
return pElement.dataset.src || pElement.getAttribute("src");
}
function getElementSource(pScript) {
const lSourceURL = getSourceAttribute(pScript);
if (lSourceURL) {
// deepcode ignore Ssrf: false positive. This is not server side. It's als not 'flowing in here from an exception on line 41'
return fetch(lSourceURL).then(getResponseStatus).then(getResponseText);
} else {
return new Promise((pResolve, pReject) => {
if (pScript.textContent) {
pResolve(pScript.textContent);
} else {
pReject(new Error("ERROR: this element doesn't contain any text"));
}
});
}
}
function renderElement(
pSourceElement,
pIndex,
pRenderElement = pSourceElement,
) {
pSourceElement.dataset.renderedby = "mscgen_js";
if (
!config.getConfig().loadFromSrcAttribute &&
Boolean(getSourceAttribute(pSourceElement))
) {
renderElementError(pRenderElement, ERR_FILE_LOADING_DISABLED);
} else {
setElementId(pRenderElement, pIndex);
getElementSource(pSourceElement)
.then((pSource) => {
parseAndRender(pRenderElement, pSource, pSourceElement);
})
.catch((pError) => {
renderElementError(pRenderElement, pError.message);
});
}
}
function processElement(pElement, pIndex) {
if (!pElement.dataset.renderedby) {
// you can render inside a script element, but it won't become
// visible any time soon. Workaround: insert a span and render
// in there.
if (pElement.tagName === "SCRIPT") {
let lRenderElement = document.createElement("span");
pElement.after(lRenderElement);
renderElement(pElement, pIndex, lRenderElement);
} else {
renderElement(pElement, pIndex);
}
}
}
function getId() {
const lSomeBigNumber = 1000000000;
return Math.round(lSomeBigNumber * Math.random());
}
function renderElementArray(pMscGenElements) {
pMscGenElements.forEach((pMscGenElement) => {
processElement(pMscGenElement, getId());
});
}
function observerCallback(pEntries) {
pEntries.forEach((pEntry) => {
if (pEntry.isIntersecting) {
const lElement = pEntry.target.nextElementSibling;
processElement(lElement, getId());
}
});
}
// historically we used any element with a mscgen_js class,
// later on we added the wikimedia style mscgen tag and
// even later the script one, because they looked snazzy:
const ELEMENTS_TO_RENDER = [
...document.querySelectorAll(".mscgen_js"),
...[...document.scripts].filter((pScript) =>
Boolean(MIME2LANG[pScript.type]),
),
...document.querySelectorAll("mscgen"),
];
const OBSERVER = new IntersectionObserver(observerCallback, {
rootMargin: "100% 0% 100% 0%",
});
ELEMENTS_TO_RENDER.forEach((pElement) => {
// scripts are not visible, hence observing them for visibility
// is doing nothing. Workaround: insert a marker element right
// before it that _is_ visible, and observe that.
const lMarker = document.createElement("mscgenjs-marker");
pElement.before(lMarker);
OBSERVER.observe(lMarker);
});
// Observer trickery, of course, is nice, but when you print a page
// you want all of the graphs to show up anyway. This ensures that
// that indeed happens at the right time:
window.addEventListener("beforeprint", () => {
renderElementArray(ELEMENTS_TO_RENDER);
});
/*
This file is part of mscgen_js.
mscgen_js is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
mscgen_js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with mscgen_js. If not, see <http://www.gnu.org/licenses/>.
*/