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= {\n /**\n * Avoids style injection, use getRules() to read the styles.\n */\n none: 0 as 0,\n\n /**\n * Inserts rules using the insertRule api.\n */\n insertNode: 1 as 1,\n\n /**\n * Appends rules using appendChild.\n */\n appendChild: 2 as 2,\n};\n\nexport type InjectionMode = typeof InjectionMode[keyof typeof InjectionMode];\n\n/**\n * CSP settings for the stylesheet\n */\nexport interface ICSPSettings {\n /**\n * Nonce to inject into script tag\n */\n nonce?: string;\n}\n\n/**\n * Stylesheet config.\n *\n * @public\n */\nexport interface IStyleSheetConfig {\n /**\n * Injection mode for how rules are inserted.\n */\n injectionMode?: InjectionMode;\n\n /**\n * Default 'displayName' to use for a className.\n * @defaultvalue 'css'\n */\n defaultPrefix?: string;\n\n /**\n * Defines the default direction of rules for auto-rtlifying things.\n * While typically this is represented as a DIR attribute in the markup,\n * the DIR is not enough to control whether padding goes on the left or\n * right. Use this to set the default direction when rules are registered.\n */\n rtl?: boolean;\n\n /**\n * Default 'namespace' to attach before the className.\n */\n namespace?: string;\n\n /**\n * CSP settings\n */\n cspSettings?: ICSPSettings;\n\n /**\n * Callback executed when a rule is inserted.\n */\n onInsertRule?: (rule: string) => void;\n\n /**\n * Initial value for classnames cache. Key is serialized css rules associated with a classname.\n */\n classNameCache?: { [key: string]: string };\n}\n\nconst STYLESHEET_SETTING = '__stylesheet__';\n/**\n * MSIE 11 doesn't cascade styles based on DOM ordering, but rather on the order that each style node\n * is created. As such, to maintain consistent priority, IE11 should reuse a single style node.\n */\nconst REUSE_STYLE_NODE = typeof navigator !== 'undefined' && /rv:11.0/.test(navigator.userAgent);\n\nlet _global: { [key: string]: any } = {};\n\n// Grab window.\ntry {\n _global = window;\n} catch {\n /* leave as blank object */\n}\n\nlet _stylesheet: Stylesheet;\n\n/**\n * Represents the state of styles registered in the page. Abstracts\n * the surface for adding styles to the stylesheet, exposes helpers\n * for reading the styles registered in server rendered scenarios.\n *\n * @public\n */\nexport class Stylesheet {\n private _lastStyleElement?: HTMLStyleElement;\n private _styleElement?: HTMLStyleElement;\n private _rules: string[] = [];\n private _preservedRules: string[] = [];\n private _config: IStyleSheetConfig;\n private _rulesToInsert: string[] = [];\n private _counter = 0;\n private _keyToClassName: { [key: string]: string } = {};\n private _onResetCallbacks: (() => void)[] = [];\n\n private _classNameToArgs: { [key: string]: { args: any; rules: string[] } } = {};\n\n /**\n * Gets the singleton instance.\n */\n public static getInstance(): Stylesheet {\n _stylesheet = _global[STYLESHEET_SETTING] as Stylesheet;\n\n if (!_stylesheet || (_stylesheet._lastStyleElement && _stylesheet._lastStyleElement.ownerDocument !== document)) {\n const fabricConfig = _global?.FabricConfig || {};\n\n _stylesheet = _global[STYLESHEET_SETTING] = new Stylesheet(fabricConfig.mergeStyles);\n }\n\n return _stylesheet;\n }\n\n constructor(config?: IStyleSheetConfig) {\n this._config = {\n injectionMode: InjectionMode.insertNode,\n defaultPrefix: 'css',\n namespace: undefined,\n cspSettings: undefined,\n ...config,\n };\n\n this._keyToClassName = this._config.classNameCache || {};\n }\n\n /**\n * Configures the stylesheet.\n */\n public setConfig(config?: IStyleSheetConfig): void {\n this._config = {\n ...this._config,\n ...config,\n };\n }\n\n /**\n * Configures a reset callback.\n *\n * @param callback - A callback which will be called when the Stylesheet is reset.\n */\n public onReset(callback: () => void): void {\n this._onResetCallbacks.push(callback);\n }\n\n /**\n * Generates a unique classname.\n *\n * @param displayName - Optional value to use as a prefix.\n */\n public getClassName(displayName?: string): string {\n const { namespace } = this._config;\n const prefix = displayName || this._config.defaultPrefix;\n\n return `${namespace ? namespace + '-' : ''}${prefix}-${this._counter++}`;\n }\n\n /**\n * Used internally to cache information about a class which was\n * registered with the stylesheet.\n */\n public cacheClassName(className: string, key: string, args: IStyle[], rules: string[]): void {\n this._keyToClassName[key] = className;\n this._classNameToArgs[className] = {\n args,\n rules,\n };\n }\n\n /**\n * Gets the appropriate classname given a key which was previously\n * registered using cacheClassName.\n */\n public classNameFromKey(key: string): string | undefined {\n return this._keyToClassName[key];\n }\n\n /**\n * Gets all classnames cache with the stylesheet.\n */\n public getClassNameCache(): { [key: string]: string } {\n return this._keyToClassName;\n }\n\n /**\n * Gets the arguments associated with a given classname which was\n * previously registered using cacheClassName.\n */\n public argsFromClassName(className: string): IStyle[] | undefined {\n const entry = this._classNameToArgs[className];\n\n return entry && entry.args;\n }\n\n /**\n * Gets the arguments associated with a given classname which was\n * previously registered using cacheClassName.\n */\n public insertedRulesFromClassName(className: string): string[] | undefined {\n const entry = this._classNameToArgs[className];\n\n return entry && entry.rules;\n }\n\n /**\n * Inserts a css rule into the stylesheet.\n * @param preserve - Preserves the rule beyond a reset boundary.\n */\n public insertRule(rule: string, preserve?: boolean): void {\n const { injectionMode } = this._config;\n const element = injectionMode !== InjectionMode.none ? this._getStyleElement() : undefined;\n\n if (preserve) {\n this._preservedRules.push(rule);\n }\n\n if (element) {\n switch (this._config.injectionMode) {\n case InjectionMode.insertNode:\n const { sheet } = element!;\n\n try {\n (sheet as CSSStyleSheet).insertRule(rule, (sheet as CSSStyleSheet).cssRules.length);\n } catch (e) {\n // The browser will throw exceptions on unsupported rules (such as a moz prefix in webkit.)\n // We need to swallow the exceptions for this scenario, otherwise we'd need to filter\n // which could be slower and bulkier.\n }\n break;\n\n case InjectionMode.appendChild:\n element.appendChild(document.createTextNode(rule));\n break;\n }\n } else {\n this._rules.push(rule);\n }\n\n if (this._config.onInsertRule) {\n this._config.onInsertRule(rule);\n }\n }\n\n /**\n * Gets all rules registered with the stylesheet; only valid when\n * using InsertionMode.none.\n */\n public getRules(includePreservedRules?: boolean): string {\n return (\n (includePreservedRules ? this._preservedRules.join('') : '') + this._rules.join('') + this._rulesToInsert.join('')\n );\n }\n\n /**\n * Resets the internal state of the stylesheet. Only used in server\n * rendered scenarios where we're using InsertionMode.none.\n */\n public reset(): void {\n this._rules = [];\n this._rulesToInsert = [];\n this._counter = 0;\n this._classNameToArgs = {};\n this._keyToClassName = {};\n\n this._onResetCallbacks.forEach((callback: () => void) => callback());\n }\n\n // Forces the regeneration of incoming styles without totally resetting the stylesheet.\n public resetKeys(): void {\n this._keyToClassName = {};\n }\n\n private _getStyleElement(): HTMLStyleElement | undefined {\n if (!this._styleElement && typeof document !== 'undefined') {\n this._styleElement = this._createStyleElement();\n\n if (!REUSE_STYLE_NODE) {\n // Reset the style element on the next frame.\n window.requestAnimationFrame(() => {\n this._styleElement = undefined;\n });\n }\n }\n return this._styleElement;\n }\n\n private _createStyleElement(): HTMLStyleElement {\n const head: HTMLHeadElement = document.head;\n const styleElement = document.createElement('style');\n\n styleElement.setAttribute('data-merge-styles', 'true');\n\n const { cspSettings } = this._config;\n if (cspSettings) {\n if (cspSettings.nonce) {\n styleElement.setAttribute('nonce', cspSettings.nonce);\n }\n }\n if (this._lastStyleElement) {\n // If the `nextElementSibling` is null, then the insertBefore will act as a regular append.\n // https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore#Syntax\n head!.insertBefore(styleElement, this._lastStyleElement.nextElementSibling);\n } else {\n const placeholderStyleTag: Element | null = this._findPlaceholderStyleTag();\n\n if (placeholderStyleTag) {\n head!.insertBefore(styleElement, placeholderStyleTag.nextElementSibling);\n } else {\n head!.insertBefore(styleElement, head.childNodes[0]);\n }\n }\n this._lastStyleElement = styleElement;\n\n return styleElement;\n }\n\n private _findPlaceholderStyleTag(): Element | null {\n const head: HTMLHeadElement = document.head;\n if (head) {\n return head.querySelector('style[data-merge-styles]');\n }\n return null;\n }\n}\n"]}
\No newline at end of file