index.d.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { ComponentValue } from '@csstools/css-parser-algorithms';
  2. import type { TokenDimension } from '@csstools/css-tokenizer';
  3. import type { TokenNumber } from '@csstools/css-tokenizer';
  4. import type { TokenPercentage } from '@csstools/css-tokenizer';
  5. export declare function calc(css: string, options?: conversionOptions): string;
  6. export declare function calcFromComponentValues(componentValuesList: Array<Array<ComponentValue>>, options?: conversionOptions): Array<Array<ComponentValue>>;
  7. export declare type conversionOptions = {
  8. /**
  9. * If a calc expression can not be solved the parse error might be reported through this callback.
  10. * Not all cases are covered. Open an issue if you need specific errors reported.
  11. *
  12. * Values are recursively visited and at each nesting level an attempt is made to solve the expression.
  13. * Errors can be reported multiple times as a result of this.
  14. */
  15. onParseError?: (error: ParseError) => void;
  16. /**
  17. * Pass global values as a map of key value pairs.
  18. */
  19. globals?: GlobalsWithStrings;
  20. /**
  21. * The default precision is fairly high.
  22. * It aims to be high enough to make rounding unnoticeable in the browser.
  23. * You can set it to a lower number to suite your needs.
  24. */
  25. precision?: number;
  26. /**
  27. * By default this package will try to preserve units.
  28. * The heuristic to do this is very simplistic.
  29. * We take the first unit we encounter and try to convert other dimensions to that unit.
  30. *
  31. * This better matches what users expect from a CSS dev tool.
  32. *
  33. * If you want to have outputs that are closes to CSS serialized values you can set `true`.
  34. */
  35. toCanonicalUnits?: boolean;
  36. /**
  37. * Convert NaN, Infinity, ... into standard representable values.
  38. */
  39. censorIntoStandardRepresentableValues?: boolean;
  40. /**
  41. * Some percentages resolve against other values and might be negative or positive depending on context.
  42. * Raw percentages are more likely to be safe to simplify outside of a browser context
  43. *
  44. * @see https://drafts.csswg.org/css-values-4/#calc-simplification
  45. */
  46. rawPercentages?: boolean;
  47. /**
  48. * The values used to generate random value cache keys.
  49. */
  50. randomCaching?: {
  51. /**
  52. * The name of the property the random function is used in.
  53. */
  54. propertyName: string;
  55. /**
  56. * N is the index of the random function among other random functions in the same property value.
  57. */
  58. propertyN: number;
  59. /**
  60. * An element ID identifying the element the style is being applied to.
  61. * When omitted any `random()` call will not be computed.
  62. */
  63. elementID: string;
  64. /**
  65. * A document ID identifying the Document the styles are from.
  66. * When omitted any `random()` call will not be computed.
  67. */
  68. documentID: string;
  69. };
  70. };
  71. export declare type GlobalsWithStrings = Map<string, TokenDimension | TokenNumber | TokenPercentage | string>;
  72. export declare const mathFunctionNames: Set<string>;
  73. /**
  74. * Any errors are reported through the `onParseError` callback.
  75. */
  76. export declare class ParseError extends Error {
  77. /** The index of the start character of the current token. */
  78. sourceStart: number;
  79. /** The index of the end character of the current token. */
  80. sourceEnd: number;
  81. constructor(message: string, sourceStart: number, sourceEnd: number);
  82. }
  83. export declare const ParseErrorMessage: {
  84. UnexpectedAdditionOfDimensionOrPercentageWithNumber: string;
  85. UnexpectedSubtractionOfDimensionOrPercentageWithNumber: string;
  86. };
  87. export declare class ParseErrorWithComponentValues extends ParseError {
  88. /** The associated component values. */
  89. componentValues: Array<ComponentValue>;
  90. constructor(message: string, componentValues: Array<ComponentValue>);
  91. }
  92. export { }