common.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var common_exports = {};
  19. __export(common_exports, {
  20. CLASS_NAME: () => CLASS_NAME,
  21. DEFAULT_STYLE_ID: () => DEFAULT_STYLE_ID,
  22. EXTERNAL_CLASS_NAMES: () => EXTERNAL_CLASS_NAMES,
  23. IS_CSS_ESCAPED: () => IS_CSS_ESCAPED,
  24. PSEUDO_GLOBAL_SELECTOR: () => PSEUDO_GLOBAL_SELECTOR,
  25. SELECTOR: () => SELECTOR,
  26. SELECTORS: () => SELECTORS,
  27. STYLE_STRING: () => STYLE_STRING,
  28. buildStyleString: () => buildStyleString,
  29. cssCommon: () => cssCommon,
  30. cxCommon: () => cxCommon,
  31. isPseudoGlobalSelectorRe: () => isPseudoGlobalSelectorRe,
  32. keyframesCommon: () => keyframesCommon,
  33. minify: () => minify,
  34. rawCssString: () => rawCssString,
  35. viewTransitionCommon: () => viewTransitionCommon
  36. });
  37. module.exports = __toCommonJS(common_exports);
  38. const PSEUDO_GLOBAL_SELECTOR = ":-hono-global";
  39. const isPseudoGlobalSelectorRe = new RegExp(`^${PSEUDO_GLOBAL_SELECTOR}{(.*)}$`);
  40. const DEFAULT_STYLE_ID = "hono-css";
  41. const SELECTOR = /* @__PURE__ */ Symbol();
  42. const CLASS_NAME = /* @__PURE__ */ Symbol();
  43. const STYLE_STRING = /* @__PURE__ */ Symbol();
  44. const SELECTORS = /* @__PURE__ */ Symbol();
  45. const EXTERNAL_CLASS_NAMES = /* @__PURE__ */ Symbol();
  46. const CSS_ESCAPED = /* @__PURE__ */ Symbol();
  47. const IS_CSS_ESCAPED = /* @__PURE__ */ Symbol();
  48. const rawCssString = (value) => {
  49. return {
  50. [CSS_ESCAPED]: value
  51. };
  52. };
  53. const toHash = (str) => {
  54. let i = 0, out = 11;
  55. while (i < str.length) {
  56. out = 101 * out + str.charCodeAt(i++) >>> 0;
  57. }
  58. return "css-" + out;
  59. };
  60. const normalizeLabel = (label) => {
  61. return label.trim().replace(/\s+/g, "-");
  62. };
  63. const isValidClassName = (name) => /^-?[_a-zA-Z][_a-zA-Z0-9-]*$/.test(name);
  64. const RESERVED_KEYFRAME_NAMES = /* @__PURE__ */ new Set([
  65. "default",
  66. "inherit",
  67. "initial",
  68. "none",
  69. "revert",
  70. "revert-layer",
  71. "unset"
  72. ]);
  73. const isValidKeyframeName = (name) => isValidClassName(name) && !RESERVED_KEYFRAME_NAMES.has(name.toLowerCase());
  74. const defaultOnInvalidSlug = (slug) => {
  75. console.warn(`Invalid slug: ${slug}`);
  76. };
  77. const cssStringReStr = [
  78. '"(?:(?:\\\\[\\s\\S]|[^"\\\\])*)"',
  79. // double quoted string
  80. "'(?:(?:\\\\[\\s\\S]|[^'\\\\])*)'"
  81. // single quoted string
  82. ].join("|");
  83. const minifyCssRe = new RegExp(
  84. [
  85. "(" + cssStringReStr + ")",
  86. // $1: quoted string
  87. "(?:" + [
  88. "^\\s+",
  89. // head whitespace
  90. "\\/\\*.*?\\*\\/\\s*",
  91. // multi-line comment
  92. "\\/\\/.*\\n\\s*",
  93. // single-line comment
  94. "\\s+$"
  95. // tail whitespace
  96. ].join("|") + ")",
  97. "\\s*;\\s*(}|$)\\s*",
  98. // $2: trailing semicolon
  99. "\\s*([{};:,])\\s*",
  100. // $3: whitespace around { } : , ;
  101. "(\\s)\\s+"
  102. // $4: 2+ spaces
  103. ].join("|"),
  104. "g"
  105. );
  106. const minify = (css) => {
  107. return css.replace(minifyCssRe, (_, $1, $2, $3, $4) => $1 || $2 || $3 || $4 || "");
  108. };
  109. const buildStyleString = (strings, values) => {
  110. const selectors = [];
  111. const externalClassNames = [];
  112. const label = strings[0].match(/^\s*\/\*(.*?)\*\//)?.[1] || "";
  113. let styleString = "";
  114. for (let i = 0, len = strings.length; i < len; i++) {
  115. styleString += strings[i];
  116. let vArray = values[i];
  117. if (typeof vArray === "boolean" || vArray === null || vArray === void 0) {
  118. continue;
  119. }
  120. if (!Array.isArray(vArray)) {
  121. vArray = [vArray];
  122. }
  123. for (let j = 0, len2 = vArray.length; j < len2; j++) {
  124. let value = vArray[j];
  125. if (typeof value === "boolean" || value === null || value === void 0) {
  126. continue;
  127. }
  128. if (typeof value === "string") {
  129. if (/([\\"'\/])/.test(value)) {
  130. styleString += value.replace(/([\\"']|(?<=<)\/)/g, "\\$1");
  131. } else {
  132. styleString += value;
  133. }
  134. } else if (typeof value === "number") {
  135. styleString += value;
  136. } else if (value[CSS_ESCAPED]) {
  137. styleString += value[CSS_ESCAPED];
  138. } else if (value[CLASS_NAME].startsWith("@keyframes ")) {
  139. selectors.push(value);
  140. styleString += ` ${value[CLASS_NAME].substring(11)} `;
  141. } else {
  142. if (strings[i + 1]?.match(/^\s*{/)) {
  143. selectors.push(value);
  144. value = `.${value[CLASS_NAME]}`;
  145. } else {
  146. selectors.push(...value[SELECTORS]);
  147. externalClassNames.push(...value[EXTERNAL_CLASS_NAMES]);
  148. value = value[STYLE_STRING];
  149. const valueLen = value.length;
  150. if (valueLen > 0) {
  151. const lastChar = value[valueLen - 1];
  152. if (lastChar !== ";" && lastChar !== "}") {
  153. value += ";";
  154. }
  155. }
  156. }
  157. styleString += `${value || ""}`;
  158. }
  159. }
  160. }
  161. return [label, minify(styleString), selectors, externalClassNames];
  162. };
  163. const cssCommon = (strings, values, classNameSlug, onInvalidSlug) => {
  164. let [label, thisStyleString, selectors, externalClassNames] = buildStyleString(strings, values);
  165. const isPseudoGlobal = isPseudoGlobalSelectorRe.exec(thisStyleString);
  166. if (isPseudoGlobal) {
  167. thisStyleString = isPseudoGlobal[1];
  168. }
  169. const hash = toHash(label + thisStyleString);
  170. let customSlug;
  171. if (classNameSlug) {
  172. const slug = classNameSlug(hash, normalizeLabel(label), thisStyleString);
  173. if (slug) {
  174. if (isValidClassName(slug)) {
  175. customSlug = slug;
  176. } else {
  177. ;
  178. (onInvalidSlug || defaultOnInvalidSlug)(slug);
  179. }
  180. }
  181. }
  182. const selector = (isPseudoGlobal ? PSEUDO_GLOBAL_SELECTOR : "") + (customSlug || hash);
  183. const className = (isPseudoGlobal ? selectors.map((s) => s[CLASS_NAME]) : [selector, ...externalClassNames]).join(" ");
  184. return {
  185. [SELECTOR]: selector,
  186. [CLASS_NAME]: className,
  187. [STYLE_STRING]: thisStyleString,
  188. [SELECTORS]: selectors,
  189. [EXTERNAL_CLASS_NAMES]: externalClassNames
  190. };
  191. };
  192. const cxCommon = (args) => {
  193. for (let i = 0, len = args.length; i < len; i++) {
  194. const arg = args[i];
  195. if (typeof arg === "string") {
  196. args[i] = {
  197. [SELECTOR]: "",
  198. [CLASS_NAME]: "",
  199. [STYLE_STRING]: "",
  200. [SELECTORS]: [],
  201. [EXTERNAL_CLASS_NAMES]: [arg]
  202. };
  203. }
  204. }
  205. return args;
  206. };
  207. const keyframesCommon = (strings, values, classNameSlug, onInvalidSlug) => {
  208. const [label, styleString] = buildStyleString(strings, values);
  209. const hash = toHash(label + styleString);
  210. let customSlug;
  211. if (classNameSlug) {
  212. const slug = classNameSlug(hash, normalizeLabel(label), styleString);
  213. if (slug) {
  214. if (isValidKeyframeName(slug)) {
  215. customSlug = slug;
  216. } else {
  217. ;
  218. (onInvalidSlug || defaultOnInvalidSlug)(slug);
  219. }
  220. }
  221. }
  222. return {
  223. [SELECTOR]: "",
  224. [CLASS_NAME]: `@keyframes ${customSlug || hash}`,
  225. [STYLE_STRING]: styleString,
  226. [SELECTORS]: [],
  227. [EXTERNAL_CLASS_NAMES]: []
  228. };
  229. };
  230. let viewTransitionNameIndex = 0;
  231. const viewTransitionCommon = ((strings, values, classNameSlug, onInvalidSlug) => {
  232. if (!strings) {
  233. strings = [`/* h-v-t ${viewTransitionNameIndex++} */`];
  234. }
  235. const content = Array.isArray(strings) ? cssCommon(strings, values, classNameSlug, onInvalidSlug) : strings;
  236. const transitionName = content[CLASS_NAME];
  237. const res = cssCommon(
  238. ["view-transition-name:", ""],
  239. // eslint-disable-line @typescript-eslint/no-explicit-any
  240. [transitionName],
  241. classNameSlug,
  242. onInvalidSlug
  243. );
  244. content[CLASS_NAME] = PSEUDO_GLOBAL_SELECTOR + content[CLASS_NAME];
  245. content[STYLE_STRING] = content[STYLE_STRING].replace(
  246. /(?<=::view-transition(?:[a-z-]*)\()(?=\))/g,
  247. transitionName
  248. );
  249. res[CLASS_NAME] = res[SELECTOR] = transitionName;
  250. res[SELECTORS] = [...content[SELECTORS], content];
  251. return res;
  252. });
  253. // Annotate the CommonJS export names for ESM import in node:
  254. 0 && (module.exports = {
  255. CLASS_NAME,
  256. DEFAULT_STYLE_ID,
  257. EXTERNAL_CLASS_NAMES,
  258. IS_CSS_ESCAPED,
  259. PSEUDO_GLOBAL_SELECTOR,
  260. SELECTOR,
  261. SELECTORS,
  262. STYLE_STRING,
  263. buildStyleString,
  264. cssCommon,
  265. cxCommon,
  266. isPseudoGlobalSelectorRe,
  267. keyframesCommon,
  268. minify,
  269. rawCssString,
  270. viewTransitionCommon
  271. });