index.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // @ts-nocheck
  2. // #ifndef UNI-APP-X
  3. type UTSJSONObject = Record<string, any>
  4. // #endif
  5. /**
  6. * 将 CSS 字符串转换为样式对象
  7. * @param css CSS 字符串,例如 "color: red; font-size: 16px;"
  8. * @returns CSSProperties 对象
  9. */
  10. export function cssToObj(css : string | UTSJSONObject | null) : UTSJSONObject {
  11. // #ifdef APP-ANDROID
  12. if(css == null) return {}
  13. // #endif
  14. // #ifndef APP-ANDROID
  15. if(!css) return {}
  16. // #endif
  17. if(typeof css == 'object') return css as UTSJSONObject
  18. const style : UTSJSONObject = {};
  19. (css as string).split(';').forEach(decl => {
  20. // #ifdef APP-ANDROID
  21. const res = decl.split(':').map(s => s.trim());
  22. if(res.length > 1) {
  23. const [prop, val] = res;
  24. if (prop != '' && val != '') {
  25. const camelProp = prop!.replace(/-([a-z])/g, (_: string, _offset: number,c: string):string => c.toUpperCase());
  26. style[camelProp] = val!;
  27. }
  28. }
  29. // #endif
  30. // #ifndef APP-ANDROID
  31. const [prop, val] = decl.split(':').map(s => s.trim());
  32. if (prop && val) {
  33. const camelProp = prop.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
  34. style[camelProp] = val;
  35. }
  36. // #endif
  37. });
  38. return style;
  39. }