runner.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. function createWindowFromHtml(e, t) {
  2. let r = templateWindows.get(t);
  3. return null == r && (r = new MockWindow(e), templateWindows.set(t, r)), cloneWindow(r);
  4. }
  5. function normalizeHydrateOptions(e) {
  6. const t = Object.assign({
  7. serializeToHtml: !1,
  8. destroyWindow: !1,
  9. destroyDocument: !1
  10. }, e || {});
  11. return "boolean" != typeof t.clientHydrateAnnotations && (t.clientHydrateAnnotations = !0),
  12. "boolean" != typeof t.constrainTimeouts && (t.constrainTimeouts = !0), "number" != typeof t.maxHydrateCount && (t.maxHydrateCount = 300),
  13. "boolean" != typeof t.runtimeLogging && (t.runtimeLogging = !1), "number" != typeof t.timeout && (t.timeout = 15e3),
  14. Array.isArray(t.excludeComponents) ? t.excludeComponents = t.excludeComponents.filter(filterValidTags).map(mapValidTags) : t.excludeComponents = [],
  15. Array.isArray(t.staticComponents) ? t.staticComponents = t.staticComponents.filter(filterValidTags).map(mapValidTags) : t.staticComponents = [],
  16. t;
  17. }
  18. function filterValidTags(e) {
  19. return "string" == typeof e && e.includes("-");
  20. }
  21. function mapValidTags(e) {
  22. return e.trim().toLowerCase();
  23. }
  24. function generateHydrateResults(e) {
  25. "string" != typeof e.url && (e.url = "https://hydrate.stenciljs.com/"), "string" != typeof e.buildId && (e.buildId = createHydrateBuildId());
  26. const t = {
  27. buildId: e.buildId,
  28. diagnostics: [],
  29. url: e.url,
  30. host: null,
  31. hostname: null,
  32. href: null,
  33. pathname: null,
  34. port: null,
  35. search: null,
  36. hash: null,
  37. html: null,
  38. httpStatus: null,
  39. hydratedCount: 0,
  40. anchors: [],
  41. components: [],
  42. imgs: [],
  43. scripts: [],
  44. staticData: [],
  45. styles: [],
  46. title: null
  47. };
  48. try {
  49. const r = new URL(e.url, "https://hydrate.stenciljs.com/");
  50. t.url = r.href, t.host = r.host, t.hostname = r.hostname, t.href = r.href, t.port = r.port,
  51. t.pathname = r.pathname, t.search = r.search, t.hash = r.hash;
  52. } catch (e) {
  53. renderCatchError(t, e);
  54. }
  55. return t;
  56. }
  57. function renderBuildDiagnostic(e, t, r, s) {
  58. const n = {
  59. level: t,
  60. type: "build",
  61. header: r,
  62. messageText: s,
  63. relFilePath: null,
  64. absFilePath: null,
  65. lines: []
  66. };
  67. return e.pathname ? "/" !== e.pathname && (n.header += ": " + e.pathname) : e.url && (n.header += ": " + e.url),
  68. e.diagnostics.push(n), n;
  69. }
  70. function renderBuildError(e, t) {
  71. return renderBuildDiagnostic(e, "error", "Hydrate Error", t);
  72. }
  73. function renderCatchError(e, t) {
  74. const r = renderBuildError(e, null);
  75. return null != t && (null != t.stack ? r.messageText = t.stack.toString() : null != t.message ? r.messageText = t.message.toString() : r.messageText = t.toString()),
  76. r;
  77. }
  78. function runtimeLog(e, t, r) {
  79. global.console[t].apply(global.console, [ `[ ${e} ${t} ] `, ...r ]);
  80. }
  81. function inspectElement(e, t, r) {
  82. const s = t.children;
  83. for (let t = 0, n = s.length; t < n; t++) {
  84. const n = s[t], o = n.nodeName.toLowerCase();
  85. if (o.includes("-")) {
  86. const t = e.components.find((e => e.tag === o));
  87. null != t && (t.count++, r > t.depth && (t.depth = r));
  88. } else switch (o) {
  89. case "a":
  90. const t = collectAttributes(n);
  91. t.href = n.href, "string" == typeof t.href && (e.anchors.some((e => e.href === t.href)) || e.anchors.push(t));
  92. break;
  93. case "img":
  94. const r = collectAttributes(n);
  95. r.src = n.src, "string" == typeof r.src && (e.imgs.some((e => e.src === r.src)) || e.imgs.push(r));
  96. break;
  97. case "link":
  98. const s = collectAttributes(n);
  99. s.href = n.href, "string" == typeof s.rel && "stylesheet" === s.rel.toLowerCase() && "string" == typeof s.href && (e.styles.some((e => e.link === s.href)) || (delete s.rel,
  100. delete s.type, e.styles.push(s)));
  101. break;
  102. case "script":
  103. const o = collectAttributes(n);
  104. if (n.hasAttribute("src")) o.src = n.src, "string" == typeof o.src && (e.scripts.some((e => e.src === o.src)) || e.scripts.push(o)); else {
  105. const t = n.getAttribute("data-stencil-static");
  106. t && e.staticData.push({
  107. id: t,
  108. type: n.getAttribute("type"),
  109. content: n.textContent
  110. });
  111. }
  112. }
  113. inspectElement(e, n, ++r);
  114. }
  115. }
  116. function collectAttributes(e) {
  117. const t = {}, r = e.attributes;
  118. for (let e = 0, s = r.length; e < s; e++) {
  119. const s = r.item(e), n = s.nodeName.toLowerCase();
  120. if (SKIP_ATTRS.has(n)) continue;
  121. const o = s.nodeValue;
  122. "class" === n && "" === o || (t[n] = o);
  123. }
  124. return t;
  125. }
  126. function patchDomImplementation(e, t) {
  127. let r;
  128. if (null != e.defaultView ? (t.destroyWindow = !0, patchWindow(e.defaultView), r = e.defaultView) : (t.destroyWindow = !0,
  129. t.destroyDocument = !1, r = new MockWindow(!1)), r.document !== e && (r.document = e),
  130. e.defaultView !== r && (e.defaultView = r), "function" != typeof e.documentElement.constructor.prototype.getRootNode && (e.createElement("unknown-element").constructor.prototype.getRootNode = getRootNode),
  131. "function" == typeof e.createEvent) {
  132. const t = e.createEvent("CustomEvent").constructor;
  133. r.CustomEvent !== t && (r.CustomEvent = t);
  134. }
  135. try {
  136. e.baseURI;
  137. } catch (t) {
  138. Object.defineProperty(e, "baseURI", {
  139. get() {
  140. const t = e.querySelector("base[href]");
  141. return t ? new URL(t.getAttribute("href"), r.location.href).href : r.location.href;
  142. }
  143. });
  144. }
  145. return r;
  146. }
  147. function getRootNode(e) {
  148. const t = null != e && !0 === e.composed;
  149. let r = this;
  150. for (;null != r.parentNode; ) r = r.parentNode, !0 === t && null == r.parentNode && null != r.host && (r = r.host);
  151. return r;
  152. }
  153. function renderToString(e, t) {
  154. const r = normalizeHydrateOptions(t);
  155. return r.serializeToHtml = !0, new Promise((t => {
  156. let s;
  157. const n = generateHydrateResults(r);
  158. if (hasError(n.diagnostics)) t(n); else if ("string" == typeof e) try {
  159. r.destroyWindow = !0, r.destroyDocument = !0, s = new MockWindow(e), render(s, r, n, t);
  160. } catch (e) {
  161. s && s.close && s.close(), s = null, renderCatchError(n, e), t(n);
  162. } else if (isValidDocument(e)) try {
  163. r.destroyDocument = !1, s = patchDomImplementation(e, r), render(s, r, n, t);
  164. } catch (e) {
  165. s && s.close && s.close(), s = null, renderCatchError(n, e), t(n);
  166. } else renderBuildError(n, 'Invalid html or document. Must be either a valid "html" string, or DOM "document".'),
  167. t(n);
  168. }));
  169. }
  170. function hydrateDocument(e, t) {
  171. const r = normalizeHydrateOptions(t);
  172. return r.serializeToHtml = !1, new Promise((t => {
  173. let s;
  174. const n = generateHydrateResults(r);
  175. if (hasError(n.diagnostics)) t(n); else if ("string" == typeof e) try {
  176. r.destroyWindow = !0, r.destroyDocument = !0, s = new MockWindow(e), render(s, r, n, t);
  177. } catch (e) {
  178. s && s.close && s.close(), s = null, renderCatchError(n, e), t(n);
  179. } else if (isValidDocument(e)) try {
  180. r.destroyDocument = !1, s = patchDomImplementation(e, r), render(s, r, n, t);
  181. } catch (e) {
  182. s && s.close && s.close(), s = null, renderCatchError(n, e), t(n);
  183. } else renderBuildError(n, 'Invalid html or document. Must be either a valid "html" string, or DOM "document".'),
  184. t(n);
  185. }));
  186. }
  187. function render(e, t, r, s) {
  188. if (process.__stencilErrors || (process.__stencilErrors = !0, process.on("unhandledRejection", (e => {
  189. console.log("unhandledRejection", e);
  190. }))), function n(e, t, r, s) {
  191. try {
  192. e.location.href = r.url;
  193. } catch (e) {
  194. renderCatchError(s, e);
  195. }
  196. if ("string" == typeof r.userAgent) try {
  197. e.navigator.userAgent = r.userAgent;
  198. } catch (e) {}
  199. if ("string" == typeof r.cookie) try {
  200. t.cookie = r.cookie;
  201. } catch (e) {}
  202. if ("string" == typeof r.referrer) try {
  203. t.referrer = r.referrer;
  204. } catch (e) {}
  205. if ("string" == typeof r.direction) try {
  206. t.documentElement.setAttribute("dir", r.direction);
  207. } catch (e) {}
  208. if ("string" == typeof r.language) try {
  209. t.documentElement.setAttribute("lang", r.language);
  210. } catch (e) {}
  211. if ("string" == typeof r.buildId) try {
  212. t.documentElement.setAttribute("data-stencil-build", r.buildId);
  213. } catch (e) {}
  214. try {
  215. e.customElements = null;
  216. } catch (e) {}
  217. return r.constrainTimeouts && constrainTimeouts(e), function n(e, t, r) {
  218. try {
  219. const s = e.location.pathname;
  220. e.console.error = (...e) => {
  221. const n = e.reduce(((e, t) => {
  222. if (t) {
  223. if (null != t.stack) return e + " " + String(t.stack);
  224. if (null != t.message) return e + " " + String(t.message);
  225. }
  226. return String(t);
  227. }), "").trim();
  228. "" !== n && (renderCatchError(r, n), t.runtimeLogging && runtimeLog(s, "error", [ n ]));
  229. }, e.console.debug = (...e) => {
  230. renderBuildDiagnostic(r, "debug", "Hydrate Debug", [ ...e ].join(", ")), t.runtimeLogging && runtimeLog(s, "debug", e);
  231. }, t.runtimeLogging && [ "log", "warn", "assert", "info", "trace" ].forEach((t => {
  232. e.console[t] = (...e) => {
  233. runtimeLog(s, t, e);
  234. };
  235. }));
  236. } catch (e) {
  237. renderCatchError(r, e);
  238. }
  239. }(e, r, s), e;
  240. }(e, e.document, t, r), "function" == typeof t.beforeHydrate) try {
  241. const n = t.beforeHydrate(e.document);
  242. isPromise(n) ? n.then((() => {
  243. hydrateFactory(e, t, r, afterHydrate, s);
  244. })) : hydrateFactory(e, t, r, afterHydrate, s);
  245. } catch (n) {
  246. renderCatchError(r, n), finalizeHydrate(e, e.document, t, r, s);
  247. } else hydrateFactory(e, t, r, afterHydrate, s);
  248. }
  249. function afterHydrate(e, t, r, s) {
  250. if ("function" == typeof t.afterHydrate) try {
  251. const n = t.afterHydrate(e.document);
  252. isPromise(n) ? n.then((() => {
  253. finalizeHydrate(e, e.document, t, r, s);
  254. })) : finalizeHydrate(e, e.document, t, r, s);
  255. } catch (n) {
  256. renderCatchError(r, n), finalizeHydrate(e, e.document, t, r, s);
  257. } else finalizeHydrate(e, e.document, t, r, s);
  258. }
  259. function finalizeHydrate(e, t, r, s, n) {
  260. try {
  261. if (inspectElement(s, t.documentElement, 0), !1 !== r.removeUnusedStyles) try {
  262. ((e, t) => {
  263. try {
  264. const r = e.head.querySelectorAll("style[data-styles]"), s = r.length;
  265. if (s > 0) {
  266. const n = (e => {
  267. const t = {
  268. attrs: new Set,
  269. classNames: new Set,
  270. ids: new Set,
  271. tags: new Set
  272. };
  273. return collectUsedSelectors(t, e), t;
  274. })(e.documentElement);
  275. for (let e = 0; e < s; e++) removeUnusedStyleText(n, t, r[e]);
  276. }
  277. } catch (e) {
  278. ((e, t, r) => {
  279. const s = {
  280. level: "error",
  281. type: "build",
  282. header: "Build Error",
  283. messageText: "build error",
  284. relFilePath: null,
  285. absFilePath: null,
  286. lines: []
  287. };
  288. null != t && (null != t.stack ? s.messageText = t.stack.toString() : null != t.message ? s.messageText = t.message.length ? t.message : "UNKNOWN ERROR" : s.messageText = t.toString()),
  289. null == e || shouldIgnoreError(s.messageText) || e.push(s);
  290. })(t, e);
  291. }
  292. })(t, s.diagnostics);
  293. } catch (e) {
  294. renderCatchError(s, e);
  295. }
  296. if ("string" == typeof r.title) try {
  297. t.title = r.title;
  298. } catch (e) {
  299. renderCatchError(s, e);
  300. }
  301. s.title = t.title, r.removeScripts && removeScripts(t.documentElement);
  302. try {
  303. ((e, t) => {
  304. let r = e.head.querySelector('link[rel="canonical"]');
  305. "string" == typeof t ? (null == r && (r = e.createElement("link"), r.setAttribute("rel", "canonical"),
  306. e.head.appendChild(r)), r.setAttribute("href", t)) : null != r && (r.getAttribute("href") || r.parentNode.removeChild(r));
  307. })(t, r.canonicalUrl);
  308. } catch (e) {
  309. renderCatchError(s, e);
  310. }
  311. try {
  312. (e => {
  313. const t = e.head;
  314. let r = t.querySelector("meta[charset]");
  315. null == r ? (r = e.createElement("meta"), r.setAttribute("charset", "utf-8")) : r.remove(),
  316. t.insertBefore(r, t.firstChild);
  317. })(t);
  318. } catch (e) {}
  319. hasError(s.diagnostics) || (s.httpStatus = 200);
  320. try {
  321. const e = t.head.querySelector('meta[http-equiv="status"]');
  322. if (null != e) {
  323. const t = e.getAttribute("content");
  324. t && t.length > 0 && (s.httpStatus = parseInt(t, 10));
  325. }
  326. } catch (e) {}
  327. r.clientHydrateAnnotations && t.documentElement.classList.add("hydrated"), r.serializeToHtml && (s.html = serializeDocumentToString(t, r));
  328. } catch (e) {
  329. renderCatchError(s, e);
  330. }
  331. if (r.destroyWindow) try {
  332. r.destroyDocument || (e.document = null, t.defaultView = null), e.close && e.close();
  333. } catch (e) {
  334. renderCatchError(s, e);
  335. }
  336. n(s);
  337. }
  338. function serializeDocumentToString(e, t) {
  339. return serializeNodeToHtml(e, {
  340. approximateLineWidth: t.approximateLineWidth,
  341. outerHtml: !1,
  342. prettyHtml: t.prettyHtml,
  343. removeAttributeQuotes: t.removeAttributeQuotes,
  344. removeBooleanAttributeQuotes: t.removeBooleanAttributeQuotes,
  345. removeEmptyAttributes: t.removeEmptyAttributes,
  346. removeHtmlComments: t.removeHtmlComments,
  347. serializeShadowRoot: !1
  348. });
  349. }
  350. function isValidDocument(e) {
  351. return null != e && 9 === e.nodeType && null != e.documentElement && 1 === e.documentElement.nodeType && null != e.body && 1 === e.body.nodeType;
  352. }
  353. function removeScripts(e) {
  354. const t = e.children;
  355. for (let e = t.length - 1; e >= 0; e--) {
  356. const r = t[e];
  357. removeScripts(r), ("SCRIPT" === r.nodeName || "LINK" === r.nodeName && "modulepreload" === r.getAttribute("rel")) && r.remove();
  358. }
  359. }
  360. import { MockWindow, cloneWindow, constrainTimeouts, patchWindow, serializeNodeToHtml } from "@stencil/core/mock-doc";
  361. import { hydrateFactory } from "@stencil/core/hydrate-factory";
  362. const templateWindows = new Map, createHydrateBuildId = () => {
  363. let e = "abcdefghijklmnopqrstuvwxyz", t = "";
  364. for (;t.length < 8; ) t += e[Math.floor(Math.random() * e.length)], 1 === t.length && (e += "0123456789");
  365. return t;
  366. }, isPromise = e => !!e && ("object" == typeof e || "function" == typeof e) && "function" == typeof e.then, hasError = e => null != e && 0 !== e.length && e.some((e => "error" === e.level && "runtime" !== e.type)), shouldIgnoreError = e => e === TASK_CANCELED_MSG, TASK_CANCELED_MSG = "task canceled", SKIP_ATTRS = new Set([ "s-id", "c-id" ]), collectUsedSelectors = (e, t) => {
  367. if (null != t && 1 === t.nodeType) {
  368. const r = t.children, s = t.nodeName.toLowerCase();
  369. e.tags.add(s);
  370. const n = t.attributes;
  371. for (let r = 0, s = n.length; r < s; r++) {
  372. const s = n.item(r), o = s.name.toLowerCase();
  373. if (e.attrs.add(o), "class" === o) {
  374. const r = t.classList;
  375. for (let t = 0, s = r.length; t < s; t++) e.classNames.add(r.item(t));
  376. } else "id" === o && e.ids.add(s.value);
  377. }
  378. if (r) for (let t = 0, s = r.length; t < s; t++) collectUsedSelectors(e, r[t]);
  379. }
  380. }, parseCss = (e, t) => {
  381. let r = 1, s = 1;
  382. const n = [], o = e => {
  383. const t = e.match(/\n/g);
  384. t && (r += t.length);
  385. const n = e.lastIndexOf("\n");
  386. s = ~n ? e.length - n : s + e.length;
  387. }, i = () => {
  388. const e = {
  389. line: r,
  390. column: s
  391. };
  392. return t => (t.position = new z(e), m(), t);
  393. }, a = o => {
  394. const i = e.split("\n"), a = {
  395. level: "error",
  396. type: "css",
  397. language: "css",
  398. header: "CSS Parse",
  399. messageText: o,
  400. absFilePath: t,
  401. lines: [ {
  402. lineIndex: r - 1,
  403. lineNumber: r,
  404. errorCharStart: s,
  405. text: e[r - 1]
  406. } ]
  407. };
  408. if (r > 1) {
  409. const t = {
  410. lineIndex: r - 1,
  411. lineNumber: r - 1,
  412. text: e[r - 2],
  413. errorCharStart: -1,
  414. errorLength: -1
  415. };
  416. a.lines.unshift(t);
  417. }
  418. if (r + 2 < i.length) {
  419. const e = {
  420. lineIndex: r,
  421. lineNumber: r + 1,
  422. text: i[r],
  423. errorCharStart: -1,
  424. errorLength: -1
  425. };
  426. a.lines.push(e);
  427. }
  428. return n.push(a), null;
  429. }, l = () => u(/^{\s*/), c = () => u(/^}/), u = t => {
  430. const r = t.exec(e);
  431. if (!r) return;
  432. const s = r[0];
  433. return o(s), e = e.slice(s.length), r;
  434. }, d = () => {
  435. let t;
  436. const r = [];
  437. for (m(), h(r); e.length && "}" !== e.charAt(0) && (t = w() || A()); ) !1 !== t && (r.push(t),
  438. h(r));
  439. return r;
  440. }, m = () => u(/^\s*/), h = e => {
  441. let t;
  442. for (e = e || []; t = p(); ) !1 !== t && e.push(t);
  443. return e;
  444. }, p = () => {
  445. const t = i();
  446. if ("/" !== e.charAt(0) || "*" !== e.charAt(1)) return null;
  447. let r = 2;
  448. for (;"" !== e.charAt(r) && ("*" !== e.charAt(r) || "/" !== e.charAt(r + 1)); ) ++r;
  449. if (r += 2, "" === e.charAt(r - 1)) return a("End of comment missing");
  450. const n = e.slice(2, r - 2);
  451. return s += 2, o(n), e = e.slice(r), s += 2, t({
  452. type: 1,
  453. comment: n
  454. });
  455. }, f = () => {
  456. const e = u(/^([^{]+)/);
  457. return e ? trim(e[0]).replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g, "").replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g, (function(e) {
  458. return e.replace(/,/g, "‌");
  459. })).split(/\s*(?![^(]*\)),\s*/).map((function(e) {
  460. return e.replace(/\u200C/g, ",");
  461. })) : null;
  462. }, g = () => {
  463. const e = i();
  464. let t = u(/^(\*?[-#\/\*\\\w]+(\[[0-9a-z_-]+\])?)\s*/);
  465. if (!t) return null;
  466. if (t = trim(t[0]), !u(/^:\s*/)) return a("property missing ':'");
  467. const r = u(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/), s = e({
  468. type: 4,
  469. property: t.replace(commentre, ""),
  470. value: r ? trim(r[0]).replace(commentre, "") : ""
  471. });
  472. return u(/^[;\s]*/), s;
  473. }, y = () => {
  474. const e = [];
  475. if (!l()) return a("missing '{'");
  476. let t;
  477. for (h(e); t = g(); ) !1 !== t && (e.push(t), h(e));
  478. return c() ? e : a("missing '}'");
  479. }, C = () => {
  480. let e;
  481. const t = [], r = i();
  482. for (;e = u(/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/); ) t.push(e[1]), u(/^,\s*/);
  483. return t.length ? r({
  484. type: 9,
  485. values: t,
  486. declarations: y()
  487. }) : null;
  488. }, S = (e, t) => {
  489. const r = new RegExp("^@" + e + "\\s*([^;]+);");
  490. return () => {
  491. const s = i(), n = u(r);
  492. if (!n) return null;
  493. const o = {
  494. type: t
  495. };
  496. return o[e] = n[1].trim(), s(o);
  497. };
  498. }, E = S("import", 7), b = S("charset", 0), T = S("namespace", 11), w = () => "@" !== e[0] ? null : (() => {
  499. const e = i();
  500. let t = u(/^@([-\w]+)?keyframes\s*/);
  501. if (!t) return null;
  502. const r = t[1];
  503. if (t = u(/^([-\w]+)\s*/), !t) return a("@keyframes missing name");
  504. const s = t[1];
  505. if (!l()) return a("@keyframes missing '{'");
  506. let n, o = h();
  507. for (;n = C(); ) o.push(n), o = o.concat(h());
  508. return c() ? e({
  509. type: 8,
  510. name: s,
  511. vendor: r,
  512. keyframes: o
  513. }) : a("@keyframes missing '}'");
  514. })() || (() => {
  515. const e = i(), t = u(/^@media *([^{]+)/);
  516. if (!t) return null;
  517. const r = trim(t[1]);
  518. if (!l()) return a("@media missing '{'");
  519. const s = h().concat(d());
  520. return c() ? e({
  521. type: 10,
  522. media: r,
  523. rules: s
  524. }) : a("@media missing '}'");
  525. })() || (() => {
  526. const e = i(), t = u(/^@custom-media\s+(--[^\s]+)\s*([^{;]+);/);
  527. return t ? e({
  528. type: 2,
  529. name: trim(t[1]),
  530. media: trim(t[2])
  531. }) : null;
  532. })() || (() => {
  533. const e = i(), t = u(/^@supports *([^{]+)/);
  534. if (!t) return null;
  535. const r = trim(t[1]);
  536. if (!l()) return a("@supports missing '{'");
  537. const s = h().concat(d());
  538. return c() ? e({
  539. type: 15,
  540. supports: r,
  541. rules: s
  542. }) : a("@supports missing '}'");
  543. })() || E() || b() || T() || (() => {
  544. const e = i(), t = u(/^@([-\w]+)?document *([^{]+)/);
  545. if (!t) return null;
  546. const r = trim(t[1]), s = trim(t[2]);
  547. if (!l()) return a("@document missing '{'");
  548. const n = h().concat(d());
  549. return c() ? e({
  550. type: 3,
  551. document: s,
  552. vendor: r,
  553. rules: n
  554. }) : a("@document missing '}'");
  555. })() || (() => {
  556. const e = i();
  557. if (!u(/^@page */)) return null;
  558. const t = f() || [];
  559. if (!l()) return a("@page missing '{'");
  560. let r, s = h();
  561. for (;r = g(); ) s.push(r), s = s.concat(h());
  562. return c() ? e({
  563. type: 12,
  564. selectors: t,
  565. declarations: s
  566. }) : a("@page missing '}'");
  567. })() || (() => {
  568. const e = i();
  569. if (!u(/^@host\s*/)) return null;
  570. if (!l()) return a("@host missing '{'");
  571. const t = h().concat(d());
  572. return c() ? e({
  573. type: 6,
  574. rules: t
  575. }) : a("@host missing '}'");
  576. })() || (() => {
  577. const e = i();
  578. if (!u(/^@font-face\s*/)) return null;
  579. if (!l()) return a("@font-face missing '{'");
  580. let t, r = h();
  581. for (;t = g(); ) r.push(t), r = r.concat(h());
  582. return c() ? e({
  583. type: 5,
  584. declarations: r
  585. }) : a("@font-face missing '}'");
  586. })(), A = () => {
  587. const e = i(), t = f();
  588. return t ? (h(), e({
  589. type: 13,
  590. selectors: t,
  591. declarations: y()
  592. })) : a("selector missing");
  593. };
  594. class z {
  595. constructor(e) {
  596. this.start = e, this.end = {
  597. line: r,
  598. column: s
  599. }, this.source = t;
  600. }
  601. }
  602. return z.prototype.content = e, {
  603. diagnostics: n,
  604. ...addParent((() => {
  605. const e = d();
  606. return {
  607. type: 14,
  608. stylesheet: {
  609. source: t,
  610. rules: e
  611. }
  612. };
  613. })())
  614. };
  615. }, trim = e => e ? e.trim() : "", addParent = (e, t) => {
  616. const r = e && "string" == typeof e.type, s = r ? e : t;
  617. for (const t in e) {
  618. const r = e[t];
  619. Array.isArray(r) ? r.forEach((function(e) {
  620. addParent(e, s);
  621. })) : r && "object" == typeof r && addParent(r, s);
  622. }
  623. return r && Object.defineProperty(e, "parent", {
  624. configurable: !0,
  625. writable: !0,
  626. enumerable: !1,
  627. value: t || null
  628. }), e;
  629. }, commentre = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//g, getCssSelectors = e => {
  630. SELECTORS.all.length = SELECTORS.tags.length = SELECTORS.classNames.length = SELECTORS.ids.length = SELECTORS.attrs.length = 0;
  631. const t = (e = e.replace(/\./g, " .").replace(/\#/g, " #").replace(/\[/g, " [").replace(/\>/g, " > ").replace(/\+/g, " + ").replace(/\~/g, " ~ ").replace(/\*/g, " * ").replace(/\:not\((.*?)\)/g, " ")).split(" ");
  632. for (let e = 0, r = t.length; e < r; e++) t[e] = t[e].split(":")[0], 0 !== t[e].length && ("." === t[e].charAt(0) ? SELECTORS.classNames.push(t[e].slice(1)) : "#" === t[e].charAt(0) ? SELECTORS.ids.push(t[e].slice(1)) : "[" === t[e].charAt(0) ? (t[e] = t[e].slice(1).split("=")[0].split("]")[0].trim(),
  633. SELECTORS.attrs.push(t[e].toLowerCase())) : /[a-z]/g.test(t[e].charAt(0)) && SELECTORS.tags.push(t[e].toLowerCase()));
  634. return SELECTORS.classNames = SELECTORS.classNames.sort(((e, t) => e.length < t.length ? -1 : e.length > t.length ? 1 : 0)),
  635. SELECTORS;
  636. }, SELECTORS = {
  637. all: [],
  638. tags: [],
  639. classNames: [],
  640. ids: [],
  641. attrs: []
  642. }, serializeCssVisitNode = (e, t, r, s) => {
  643. const n = t.type;
  644. return 4 === n ? serializeCssDeclaration(t, r, s) : 13 === n ? serializeCssRule(e, t) : 1 === n ? "!" === t.comment[0] ? `/*${t.comment}*/` : "" : 10 === n ? serializeCssMedia(e, t) : 8 === n ? serializeCssKeyframes(e, t) : 9 === n ? serializeCssKeyframe(e, t) : 5 === n ? serializeCssFontFace(e, t) : 15 === n ? serializeCssSupports(e, t) : 7 === n ? "@import " + t.import + ";" : 0 === n ? "@charset " + t.charset + ";" : 12 === n ? serializeCssPage(e, t) : 6 === n ? "@host{" + serializeCssMapVisit(e, t.rules) + "}" : 2 === n ? "@custom-media " + t.name + " " + t.media + ";" : 3 === n ? serializeCssDocument(e, t) : 11 === n ? "@namespace " + t.namespace + ";" : "";
  645. }, serializeCssRule = (e, t) => {
  646. const r = t.declarations, s = e.usedSelectors, n = t.selectors.slice();
  647. if (null == r || 0 === r.length) return "";
  648. if (s) {
  649. let t, r, o = !0;
  650. for (t = n.length - 1; t >= 0; t--) {
  651. const i = getCssSelectors(n[t]);
  652. o = !0;
  653. let a = i.classNames.length;
  654. if (a > 0 && e.hasUsedClassNames) for (r = 0; r < a; r++) if (!s.classNames.has(i.classNames[r])) {
  655. o = !1;
  656. break;
  657. }
  658. if (o && e.hasUsedTags && (a = i.tags.length, a > 0)) for (r = 0; r < a; r++) if (!s.tags.has(i.tags[r])) {
  659. o = !1;
  660. break;
  661. }
  662. if (o && e.hasUsedAttrs && (a = i.attrs.length, a > 0)) for (r = 0; r < a; r++) if (!s.attrs.has(i.attrs[r])) {
  663. o = !1;
  664. break;
  665. }
  666. if (o && e.hasUsedIds && (a = i.ids.length, a > 0)) for (r = 0; r < a; r++) if (!s.ids.has(i.ids[r])) {
  667. o = !1;
  668. break;
  669. }
  670. o || n.splice(t, 1);
  671. }
  672. }
  673. if (0 === n.length) return "";
  674. const o = [];
  675. let i = "";
  676. for (const e of t.selectors) i = removeSelectorWhitespace(e), o.includes(i) || o.push(i);
  677. return `${o}{${serializeCssMapVisit(e, r)}}`;
  678. }, serializeCssDeclaration = (e, t, r) => "" === e.value ? "" : r - 1 === t ? e.property + ":" + e.value : e.property + ":" + e.value + ";", serializeCssMedia = (e, t) => {
  679. const r = serializeCssMapVisit(e, t.rules);
  680. return "" === r ? "" : "@media " + removeMediaWhitespace(t.media) + "{" + r + "}";
  681. }, serializeCssKeyframes = (e, t) => {
  682. const r = serializeCssMapVisit(e, t.keyframes);
  683. return "" === r ? "" : "@" + (t.vendor || "") + "keyframes " + t.name + "{" + r + "}";
  684. }, serializeCssKeyframe = (e, t) => t.values.join(",") + "{" + serializeCssMapVisit(e, t.declarations) + "}", serializeCssFontFace = (e, t) => {
  685. const r = serializeCssMapVisit(e, t.declarations);
  686. return "" === r ? "" : "@font-face{" + r + "}";
  687. }, serializeCssSupports = (e, t) => {
  688. const r = serializeCssMapVisit(e, t.rules);
  689. return "" === r ? "" : "@supports " + t.supports + "{" + r + "}";
  690. }, serializeCssPage = (e, t) => "@page " + t.selectors.join(", ") + "{" + serializeCssMapVisit(e, t.declarations) + "}", serializeCssDocument = (e, t) => {
  691. const r = serializeCssMapVisit(e, t.rules), s = "@" + (t.vendor || "") + "document " + t.document;
  692. return "" === r ? "" : s + "{" + r + "}";
  693. }, serializeCssMapVisit = (e, t) => {
  694. let r = "";
  695. if (t) for (let s = 0, n = t.length; s < n; s++) r += serializeCssVisitNode(e, t[s], s, n);
  696. return r;
  697. }, removeSelectorWhitespace = e => {
  698. let t = "", r = "", s = !1;
  699. for (let n = 0, o = (e = e.trim()).length; n < o; n++) if (r = e[n], "[" === r && "\\" !== t[t.length - 1] ? s = !0 : "]" === r && "\\" !== t[t.length - 1] && (s = !1),
  700. !s && CSS_WS_REG.test(r)) {
  701. if (CSS_NEXT_CHAR_REG.test(e[n + 1])) continue;
  702. if (CSS_PREV_CHAR_REG.test(t[t.length - 1])) continue;
  703. t += " ";
  704. } else t += r;
  705. return t;
  706. }, removeMediaWhitespace = e => {
  707. let t = "", r = "";
  708. for (let s = 0, n = (e = e.trim()).length; s < n; s++) if (r = e[s], CSS_WS_REG.test(r)) {
  709. if (CSS_WS_REG.test(t[t.length - 1])) continue;
  710. t += " ";
  711. } else t += r;
  712. return t;
  713. }, CSS_WS_REG = /\s/, CSS_NEXT_CHAR_REG = /[>\(\)\~\,\+\s]/, CSS_PREV_CHAR_REG = /[>\(\~\,\+]/, removeUnusedStyleText = (e, t, r) => {
  714. try {
  715. const s = parseCss(r.innerHTML);
  716. if (t.push(...s.diagnostics), hasError(t)) return;
  717. try {
  718. r.innerHTML = ((e, t) => {
  719. const r = t.usedSelectors || null, s = {
  720. usedSelectors: r || null,
  721. hasUsedAttrs: !!r && r.attrs.size > 0,
  722. hasUsedClassNames: !!r && r.classNames.size > 0,
  723. hasUsedIds: !!r && r.ids.size > 0,
  724. hasUsedTags: !!r && r.tags.size > 0
  725. }, n = e.rules;
  726. if (!n) return "";
  727. const o = n.length, i = [];
  728. for (let e = 0; e < o; e++) i.push(serializeCssVisitNode(s, n[e], e, o));
  729. return i.join("");
  730. })(s.stylesheet, {
  731. usedSelectors: e
  732. });
  733. } catch (e) {
  734. t.push({
  735. level: "warn",
  736. type: "css",
  737. header: "CSS Stringify",
  738. messageText: e
  739. });
  740. }
  741. } catch (e) {
  742. t.push({
  743. level: "warn",
  744. type: "css",
  745. header: "CSS Parse",
  746. messageText: e
  747. });
  748. }
  749. };
  750. export { createWindowFromHtml, hydrateDocument, renderToString, serializeDocumentToString };