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 inspectElement(e, t, r) {
  6. const s = t.children;
  7. for (let t = 0, n = s.length; t < n; t++) {
  8. const n = s[t], o = n.nodeName.toLowerCase();
  9. if (o.includes("-")) {
  10. const t = e.components.find((e => e.tag === o));
  11. null != t && (t.count++, r > t.depth && (t.depth = r));
  12. } else switch (o) {
  13. case "a":
  14. const t = collectAttributes(n);
  15. t.href = n.href, "string" == typeof t.href && (e.anchors.some((e => e.href === t.href)) || e.anchors.push(t));
  16. break;
  17. case "img":
  18. const r = collectAttributes(n);
  19. r.src = n.src, "string" == typeof r.src && (e.imgs.some((e => e.src === r.src)) || e.imgs.push(r));
  20. break;
  21. case "link":
  22. const s = collectAttributes(n);
  23. 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,
  24. delete s.type, e.styles.push(s)));
  25. break;
  26. case "script":
  27. const o = collectAttributes(n);
  28. 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 {
  29. const t = n.getAttribute("data-stencil-static");
  30. t && e.staticData.push({
  31. id: t,
  32. type: n.getAttribute("type"),
  33. content: n.textContent
  34. });
  35. }
  36. }
  37. inspectElement(e, n, ++r);
  38. }
  39. }
  40. function collectAttributes(e) {
  41. const t = {}, r = e.attributes;
  42. for (let e = 0, s = r.length; e < s; e++) {
  43. const s = r.item(e), n = s.nodeName.toLowerCase();
  44. if (SKIP_ATTRS.has(n)) continue;
  45. const o = s.nodeValue;
  46. "class" === n && "" === o || (t[n] = o);
  47. }
  48. return t;
  49. }
  50. function patchDomImplementation(e, t) {
  51. let r;
  52. if (null != e.defaultView ? (t.destroyWindow = !0, patchWindow(e.defaultView), r = e.defaultView) : (t.destroyWindow = !0,
  53. t.destroyDocument = !1, r = new MockWindow(!1)), r.document !== e && (r.document = e),
  54. e.defaultView !== r && (e.defaultView = r), "function" != typeof e.documentElement.constructor.prototype.getRootNode && (e.createElement("unknown-element").constructor.prototype.getRootNode = getRootNode),
  55. "function" == typeof e.createEvent) {
  56. const t = e.createEvent("CustomEvent").constructor;
  57. r.CustomEvent !== t && (r.CustomEvent = t);
  58. }
  59. try {
  60. e.baseURI;
  61. } catch (t) {
  62. Object.defineProperty(e, "baseURI", {
  63. get() {
  64. const t = e.querySelector("base[href]");
  65. return t ? new URL(t.getAttribute("href"), r.location.href).href : r.location.href;
  66. }
  67. });
  68. }
  69. return r;
  70. }
  71. function getRootNode(e) {
  72. const t = null != e && !0 === e.composed;
  73. let r = this;
  74. for (;null != r.parentNode; ) r = r.parentNode, !0 === t && null == r.parentNode && null != r.host && (r = r.host);
  75. return r;
  76. }
  77. function normalizeHydrateOptions(e) {
  78. const t = Object.assign({
  79. serializeToHtml: !1,
  80. destroyWindow: !1,
  81. destroyDocument: !1
  82. }, e || {});
  83. return "boolean" != typeof t.clientHydrateAnnotations && (t.clientHydrateAnnotations = !0),
  84. "boolean" != typeof t.constrainTimeouts && (t.constrainTimeouts = !0), "number" != typeof t.maxHydrateCount && (t.maxHydrateCount = 300),
  85. "boolean" != typeof t.runtimeLogging && (t.runtimeLogging = !1), "number" != typeof t.timeout && (t.timeout = 15e3),
  86. Array.isArray(t.excludeComponents) ? t.excludeComponents = t.excludeComponents.filter(filterValidTags).map(mapValidTags) : t.excludeComponents = [],
  87. Array.isArray(t.staticComponents) ? t.staticComponents = t.staticComponents.filter(filterValidTags).map(mapValidTags) : t.staticComponents = [],
  88. t;
  89. }
  90. function filterValidTags(e) {
  91. return "string" == typeof e && e.includes("-");
  92. }
  93. function mapValidTags(e) {
  94. return e.trim().toLowerCase();
  95. }
  96. function generateHydrateResults(e) {
  97. "string" != typeof e.url && (e.url = "https://hydrate.stenciljs.com/"), "string" != typeof e.buildId && (e.buildId = createHydrateBuildId());
  98. const t = {
  99. buildId: e.buildId,
  100. diagnostics: [],
  101. url: e.url,
  102. host: null,
  103. hostname: null,
  104. href: null,
  105. pathname: null,
  106. port: null,
  107. search: null,
  108. hash: null,
  109. html: null,
  110. httpStatus: null,
  111. hydratedCount: 0,
  112. anchors: [],
  113. components: [],
  114. imgs: [],
  115. scripts: [],
  116. staticData: [],
  117. styles: [],
  118. title: null
  119. };
  120. try {
  121. const r = new URL(e.url, "https://hydrate.stenciljs.com/");
  122. t.url = r.href, t.host = r.host, t.hostname = r.hostname, t.href = r.href, t.port = r.port,
  123. t.pathname = r.pathname, t.search = r.search, t.hash = r.hash;
  124. } catch (e) {
  125. renderCatchError(t, e);
  126. }
  127. return t;
  128. }
  129. function renderBuildDiagnostic(e, t, r, s) {
  130. const n = {
  131. level: t,
  132. type: "build",
  133. header: r,
  134. messageText: s,
  135. relFilePath: null,
  136. absFilePath: null,
  137. lines: []
  138. };
  139. return e.pathname ? "/" !== e.pathname && (n.header += ": " + e.pathname) : e.url && (n.header += ": " + e.url),
  140. e.diagnostics.push(n), n;
  141. }
  142. function renderBuildError(e, t) {
  143. return renderBuildDiagnostic(e, "error", "Hydrate Error", t);
  144. }
  145. function renderCatchError(e, t) {
  146. const r = renderBuildError(e, null);
  147. return null != t && (null != t.stack ? r.messageText = t.stack.toString() : null != t.message ? r.messageText = t.message.toString() : r.messageText = t.toString()),
  148. r;
  149. }
  150. function runtimeLog(e, t, r) {
  151. global.console[t].apply(global.console, [ `[ ${e} ${t} ] `, ...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, patchWindow, constrainTimeouts, serializeNodeToHtml } from "@stencil/core/mock-doc";
  361. import { hydrateFactory } from "@stencil/core/hydrate-factory";
  362. const templateWindows = new Map, 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", parseCss = (e, t) => {
  363. let r = 1, s = 1;
  364. const n = [], o = e => {
  365. const t = e.match(/\n/g);
  366. t && (r += t.length);
  367. const n = e.lastIndexOf("\n");
  368. s = ~n ? e.length - n : s + e.length;
  369. }, i = () => {
  370. const e = {
  371. line: r,
  372. column: s
  373. };
  374. return t => (t.position = new z(e), m(), t);
  375. }, a = o => {
  376. const i = e.split("\n"), a = {
  377. level: "error",
  378. type: "css",
  379. language: "css",
  380. header: "CSS Parse",
  381. messageText: o,
  382. absFilePath: t,
  383. lines: [ {
  384. lineIndex: r - 1,
  385. lineNumber: r,
  386. errorCharStart: s,
  387. text: e[r - 1]
  388. } ]
  389. };
  390. if (r > 1) {
  391. const t = {
  392. lineIndex: r - 1,
  393. lineNumber: r - 1,
  394. text: e[r - 2],
  395. errorCharStart: -1,
  396. errorLength: -1
  397. };
  398. a.lines.unshift(t);
  399. }
  400. if (r + 2 < i.length) {
  401. const e = {
  402. lineIndex: r,
  403. lineNumber: r + 1,
  404. text: i[r],
  405. errorCharStart: -1,
  406. errorLength: -1
  407. };
  408. a.lines.push(e);
  409. }
  410. return n.push(a), null;
  411. }, l = () => u(/^{\s*/), c = () => u(/^}/), u = t => {
  412. const r = t.exec(e);
  413. if (!r) return;
  414. const s = r[0];
  415. return o(s), e = e.slice(s.length), r;
  416. }, d = () => {
  417. let t;
  418. const r = [];
  419. for (m(), h(r); e.length && "}" !== e.charAt(0) && (t = w() || A()); ) !1 !== t && (r.push(t),
  420. h(r));
  421. return r;
  422. }, m = () => u(/^\s*/), h = e => {
  423. let t;
  424. for (e = e || []; t = p(); ) !1 !== t && e.push(t);
  425. return e;
  426. }, p = () => {
  427. const t = i();
  428. if ("/" !== e.charAt(0) || "*" !== e.charAt(1)) return null;
  429. let r = 2;
  430. for (;"" !== e.charAt(r) && ("*" !== e.charAt(r) || "/" !== e.charAt(r + 1)); ) ++r;
  431. if (r += 2, "" === e.charAt(r - 1)) return a("End of comment missing");
  432. const n = e.slice(2, r - 2);
  433. return s += 2, o(n), e = e.slice(r), s += 2, t({
  434. type: 1,
  435. comment: n
  436. });
  437. }, f = () => {
  438. const e = u(/^([^{]+)/);
  439. return e ? trim(e[0]).replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g, "").replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g, (function(e) {
  440. return e.replace(/,/g, "‌");
  441. })).split(/\s*(?![^(]*\)),\s*/).map((function(e) {
  442. return e.replace(/\u200C/g, ",");
  443. })) : null;
  444. }, g = () => {
  445. const e = i();
  446. let t = u(/^(\*?[-#\/\*\\\w]+(\[[0-9a-z_-]+\])?)\s*/);
  447. if (!t) return null;
  448. if (t = trim(t[0]), !u(/^:\s*/)) return a("property missing ':'");
  449. const r = u(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/), s = e({
  450. type: 4,
  451. property: t.replace(commentre, ""),
  452. value: r ? trim(r[0]).replace(commentre, "") : ""
  453. });
  454. return u(/^[;\s]*/), s;
  455. }, y = () => {
  456. const e = [];
  457. if (!l()) return a("missing '{'");
  458. let t;
  459. for (h(e); t = g(); ) !1 !== t && (e.push(t), h(e));
  460. return c() ? e : a("missing '}'");
  461. }, C = () => {
  462. let e;
  463. const t = [], r = i();
  464. for (;e = u(/^((\d+\.\d+|\.\d+|\d+)%?|[a-z]+)\s*/); ) t.push(e[1]), u(/^,\s*/);
  465. return t.length ? r({
  466. type: 9,
  467. values: t,
  468. declarations: y()
  469. }) : null;
  470. }, S = (e, t) => {
  471. const r = new RegExp("^@" + e + "\\s*([^;]+);");
  472. return () => {
  473. const s = i(), n = u(r);
  474. if (!n) return null;
  475. const o = {
  476. type: t
  477. };
  478. return o[e] = n[1].trim(), s(o);
  479. };
  480. }, E = S("import", 7), b = S("charset", 0), T = S("namespace", 11), w = () => "@" !== e[0] ? null : (() => {
  481. const e = i();
  482. let t = u(/^@([-\w]+)?keyframes\s*/);
  483. if (!t) return null;
  484. const r = t[1];
  485. if (t = u(/^([-\w]+)\s*/), !t) return a("@keyframes missing name");
  486. const s = t[1];
  487. if (!l()) return a("@keyframes missing '{'");
  488. let n, o = h();
  489. for (;n = C(); ) o.push(n), o = o.concat(h());
  490. return c() ? e({
  491. type: 8,
  492. name: s,
  493. vendor: r,
  494. keyframes: o
  495. }) : a("@keyframes missing '}'");
  496. })() || (() => {
  497. const e = i(), t = u(/^@media *([^{]+)/);
  498. if (!t) return null;
  499. const r = trim(t[1]);
  500. if (!l()) return a("@media missing '{'");
  501. const s = h().concat(d());
  502. return c() ? e({
  503. type: 10,
  504. media: r,
  505. rules: s
  506. }) : a("@media missing '}'");
  507. })() || (() => {
  508. const e = i(), t = u(/^@custom-media\s+(--[^\s]+)\s*([^{;]+);/);
  509. return t ? e({
  510. type: 2,
  511. name: trim(t[1]),
  512. media: trim(t[2])
  513. }) : null;
  514. })() || (() => {
  515. const e = i(), t = u(/^@supports *([^{]+)/);
  516. if (!t) return null;
  517. const r = trim(t[1]);
  518. if (!l()) return a("@supports missing '{'");
  519. const s = h().concat(d());
  520. return c() ? e({
  521. type: 15,
  522. supports: r,
  523. rules: s
  524. }) : a("@supports missing '}'");
  525. })() || E() || b() || T() || (() => {
  526. const e = i(), t = u(/^@([-\w]+)?document *([^{]+)/);
  527. if (!t) return null;
  528. const r = trim(t[1]), s = trim(t[2]);
  529. if (!l()) return a("@document missing '{'");
  530. const n = h().concat(d());
  531. return c() ? e({
  532. type: 3,
  533. document: s,
  534. vendor: r,
  535. rules: n
  536. }) : a("@document missing '}'");
  537. })() || (() => {
  538. const e = i();
  539. if (!u(/^@page */)) return null;
  540. const t = f() || [];
  541. if (!l()) return a("@page missing '{'");
  542. let r, s = h();
  543. for (;r = g(); ) s.push(r), s = s.concat(h());
  544. return c() ? e({
  545. type: 12,
  546. selectors: t,
  547. declarations: s
  548. }) : a("@page missing '}'");
  549. })() || (() => {
  550. const e = i();
  551. if (!u(/^@host\s*/)) return null;
  552. if (!l()) return a("@host missing '{'");
  553. const t = h().concat(d());
  554. return c() ? e({
  555. type: 6,
  556. rules: t
  557. }) : a("@host missing '}'");
  558. })() || (() => {
  559. const e = i();
  560. if (!u(/^@font-face\s*/)) return null;
  561. if (!l()) return a("@font-face missing '{'");
  562. let t, r = h();
  563. for (;t = g(); ) r.push(t), r = r.concat(h());
  564. return c() ? e({
  565. type: 5,
  566. declarations: r
  567. }) : a("@font-face missing '}'");
  568. })(), A = () => {
  569. const e = i(), t = f();
  570. return t ? (h(), e({
  571. type: 13,
  572. selectors: t,
  573. declarations: y()
  574. })) : a("selector missing");
  575. };
  576. class z {
  577. constructor(e) {
  578. this.start = e, this.end = {
  579. line: r,
  580. column: s
  581. }, this.source = t;
  582. }
  583. }
  584. return z.prototype.content = e, {
  585. diagnostics: n,
  586. ...addParent((() => {
  587. const e = d();
  588. return {
  589. type: 14,
  590. stylesheet: {
  591. source: t,
  592. rules: e
  593. }
  594. };
  595. })())
  596. };
  597. }, trim = e => e ? e.trim() : "", addParent = (e, t) => {
  598. const r = e && "string" == typeof e.type, s = r ? e : t;
  599. for (const t in e) {
  600. const r = e[t];
  601. Array.isArray(r) ? r.forEach((function(e) {
  602. addParent(e, s);
  603. })) : r && "object" == typeof r && addParent(r, s);
  604. }
  605. return r && Object.defineProperty(e, "parent", {
  606. configurable: !0,
  607. writable: !0,
  608. enumerable: !1,
  609. value: t || null
  610. }), e;
  611. }, commentre = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//g, getCssSelectors = e => {
  612. SELECTORS.all.length = SELECTORS.tags.length = SELECTORS.classNames.length = SELECTORS.ids.length = SELECTORS.attrs.length = 0;
  613. const t = (e = e.replace(/\./g, " .").replace(/\#/g, " #").replace(/\[/g, " [").replace(/\>/g, " > ").replace(/\+/g, " + ").replace(/\~/g, " ~ ").replace(/\*/g, " * ").replace(/\:not\((.*?)\)/g, " ")).split(" ");
  614. 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(),
  615. SELECTORS.attrs.push(t[e].toLowerCase())) : /[a-z]/g.test(t[e].charAt(0)) && SELECTORS.tags.push(t[e].toLowerCase()));
  616. return SELECTORS.classNames = SELECTORS.classNames.sort(((e, t) => e.length < t.length ? -1 : e.length > t.length ? 1 : 0)),
  617. SELECTORS;
  618. }, SELECTORS = {
  619. all: [],
  620. tags: [],
  621. classNames: [],
  622. ids: [],
  623. attrs: []
  624. }, serializeCssVisitNode = (e, t, r, s) => {
  625. const n = t.type;
  626. 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 + ";" : "";
  627. }, serializeCssRule = (e, t) => {
  628. const r = t.declarations, s = e.usedSelectors, n = t.selectors.slice();
  629. if (null == r || 0 === r.length) return "";
  630. if (s) {
  631. let t, r, o = !0;
  632. for (t = n.length - 1; t >= 0; t--) {
  633. const i = getCssSelectors(n[t]);
  634. o = !0;
  635. let a = i.classNames.length;
  636. if (a > 0 && e.hasUsedClassNames) for (r = 0; r < a; r++) if (!s.classNames.has(i.classNames[r])) {
  637. o = !1;
  638. break;
  639. }
  640. if (o && e.hasUsedTags && (a = i.tags.length, a > 0)) for (r = 0; r < a; r++) if (!s.tags.has(i.tags[r])) {
  641. o = !1;
  642. break;
  643. }
  644. if (o && e.hasUsedAttrs && (a = i.attrs.length, a > 0)) for (r = 0; r < a; r++) if (!s.attrs.has(i.attrs[r])) {
  645. o = !1;
  646. break;
  647. }
  648. if (o && e.hasUsedIds && (a = i.ids.length, a > 0)) for (r = 0; r < a; r++) if (!s.ids.has(i.ids[r])) {
  649. o = !1;
  650. break;
  651. }
  652. o || n.splice(t, 1);
  653. }
  654. }
  655. if (0 === n.length) return "";
  656. const o = [];
  657. let i = "";
  658. for (const e of t.selectors) i = removeSelectorWhitespace(e), o.includes(i) || o.push(i);
  659. return `${o}{${serializeCssMapVisit(e, r)}}`;
  660. }, serializeCssDeclaration = (e, t, r) => "" === e.value ? "" : r - 1 === t ? e.property + ":" + e.value : e.property + ":" + e.value + ";", serializeCssMedia = (e, t) => {
  661. const r = serializeCssMapVisit(e, t.rules);
  662. return "" === r ? "" : "@media " + removeMediaWhitespace(t.media) + "{" + r + "}";
  663. }, serializeCssKeyframes = (e, t) => {
  664. const r = serializeCssMapVisit(e, t.keyframes);
  665. return "" === r ? "" : "@" + (t.vendor || "") + "keyframes " + t.name + "{" + r + "}";
  666. }, serializeCssKeyframe = (e, t) => t.values.join(",") + "{" + serializeCssMapVisit(e, t.declarations) + "}", serializeCssFontFace = (e, t) => {
  667. const r = serializeCssMapVisit(e, t.declarations);
  668. return "" === r ? "" : "@font-face{" + r + "}";
  669. }, serializeCssSupports = (e, t) => {
  670. const r = serializeCssMapVisit(e, t.rules);
  671. return "" === r ? "" : "@supports " + t.supports + "{" + r + "}";
  672. }, serializeCssPage = (e, t) => "@page " + t.selectors.join(", ") + "{" + serializeCssMapVisit(e, t.declarations) + "}", serializeCssDocument = (e, t) => {
  673. const r = serializeCssMapVisit(e, t.rules), s = "@" + (t.vendor || "") + "document " + t.document;
  674. return "" === r ? "" : s + "{" + r + "}";
  675. }, serializeCssMapVisit = (e, t) => {
  676. let r = "";
  677. if (t) for (let s = 0, n = t.length; s < n; s++) r += serializeCssVisitNode(e, t[s], s, n);
  678. return r;
  679. }, removeSelectorWhitespace = e => {
  680. let t = "", r = "", s = !1;
  681. 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),
  682. !s && CSS_WS_REG.test(r)) {
  683. if (CSS_NEXT_CHAR_REG.test(e[n + 1])) continue;
  684. if (CSS_PREV_CHAR_REG.test(t[t.length - 1])) continue;
  685. t += " ";
  686. } else t += r;
  687. return t;
  688. }, removeMediaWhitespace = e => {
  689. let t = "", r = "";
  690. for (let s = 0, n = (e = e.trim()).length; s < n; s++) if (r = e[s], CSS_WS_REG.test(r)) {
  691. if (CSS_WS_REG.test(t[t.length - 1])) continue;
  692. t += " ";
  693. } else t += r;
  694. return t;
  695. }, CSS_WS_REG = /\s/, CSS_NEXT_CHAR_REG = /[>\(\)\~\,\+\s]/, CSS_PREV_CHAR_REG = /[>\(\~\,\+]/, collectUsedSelectors = (e, t) => {
  696. if (null != t && 1 === t.nodeType) {
  697. const r = t.children, s = t.nodeName.toLowerCase();
  698. e.tags.add(s);
  699. const n = t.attributes;
  700. for (let r = 0, s = n.length; r < s; r++) {
  701. const s = n.item(r), o = s.name.toLowerCase();
  702. if (e.attrs.add(o), "class" === o) {
  703. const r = t.classList;
  704. for (let t = 0, s = r.length; t < s; t++) e.classNames.add(r.item(t));
  705. } else "id" === o && e.ids.add(s.value);
  706. }
  707. if (r) for (let t = 0, s = r.length; t < s; t++) collectUsedSelectors(e, r[t]);
  708. }
  709. }, removeUnusedStyleText = (e, t, r) => {
  710. try {
  711. const s = parseCss(r.innerHTML);
  712. if (t.push(...s.diagnostics), hasError(t)) return;
  713. try {
  714. r.innerHTML = ((e, t) => {
  715. const r = t.usedSelectors || null, s = {
  716. usedSelectors: r || null,
  717. hasUsedAttrs: !!r && r.attrs.size > 0,
  718. hasUsedClassNames: !!r && r.classNames.size > 0,
  719. hasUsedIds: !!r && r.ids.size > 0,
  720. hasUsedTags: !!r && r.tags.size > 0
  721. }, n = e.rules;
  722. if (!n) return "";
  723. const o = n.length, i = [];
  724. for (let e = 0; e < o; e++) i.push(serializeCssVisitNode(s, n[e], e, o));
  725. return i.join("");
  726. })(s.stylesheet, {
  727. usedSelectors: e
  728. });
  729. } catch (e) {
  730. t.push({
  731. level: "warn",
  732. type: "css",
  733. header: "CSS Stringify",
  734. messageText: e
  735. });
  736. }
  737. } catch (e) {
  738. t.push({
  739. level: "warn",
  740. type: "css",
  741. header: "CSS Parse",
  742. messageText: e
  743. });
  744. }
  745. }, SKIP_ATTRS = new Set([ "s-id", "c-id" ]), createHydrateBuildId = () => {
  746. let e = "abcdefghijklmnopqrstuvwxyz", t = "";
  747. for (;t.length < 8; ) t += e[Math.floor(Math.random() * e.length)], 1 === t.length && (e += "0123456789");
  748. return t;
  749. };
  750. export { createWindowFromHtml, hydrateDocument, renderToString, serializeDocumentToString };