{"version":3,"file":"PreserveStyles.cjs","sources":["../../../../../../../src/components/ui/editor/ihub-editor/extensions/PreserveStyles.ts"],"sourcesContent":["import { Extension, Node, mergeAttributes } from \"@tiptap/core\";\nimport { sanitizeStyle, splitDeclarations } from \"./colorUtils\";\n\n/**\n * CSS properties already owned by another attribute on the same node/mark.\n * They're dropped from the preserved style so editing (align, colour, cell\n * background) isn't overridden by a stale — often !important — pasted value.\n */\nconst OWNED_PROPERTIES: Record<string, string[]> = {\n  // The table view writes its own min-width from column sizes.\n  table: [\"min-width\"],\n  paragraph: [\"text-align\"],\n  heading: [\"text-align\"],\n  textStyle: [\"color\"],\n  highlight: [\"background-color\"],\n  tableCell: [\"background-color\", \"background\"],\n  tableHeader: [\"background-color\", \"background\"],\n};\n\nexport function stripProperties(style: string | null, properties: string[] = []): string | null {\n  if (!style) return null;\n  if (!properties.length) return style;\n  const kept = splitDeclarations(style).filter((decl) => !properties.includes(decl.split(\":\")[0].trim().toLowerCase()));\n  return kept.length ? kept.join(\";\") : null;\n}\n\nconst NODE_TYPES = [\n  \"paragraph\",\n  \"heading\",\n  \"blockquote\",\n  \"bulletList\",\n  \"orderedList\",\n  \"listItem\",\n  \"horizontalRule\",\n  \"table\",\n  \"tableRow\",\n  \"tableCell\",\n  \"tableHeader\",\n];\nconst MARK_TYPES = [\"textStyle\", \"link\"];\n\n/** Keeps inline `style` from pasted or loaded HTML (emails, CMS exports). */\nexport const PreserveStyles = Extension.create({\n  name: \"preserveStyles\",\n\n  addGlobalAttributes() {\n    return [...NODE_TYPES, ...MARK_TYPES].map((type) => ({\n      types: [type],\n      attributes: {\n        style: {\n          default: null,\n          parseHTML: (el: HTMLElement) => sanitizeStyle(el.getAttribute(\"style\")),\n          renderHTML: (attrs: Record<string, unknown>) => {\n            const style = stripProperties(sanitizeStyle(attrs.style as string), OWNED_PROPERTIES[type]);\n            return style ? { style } : {};\n          },\n        },\n      },\n    }));\n  },\n});\n\n/** Generic styled <div> wrapper so layout boxes in pasted HTML survive. */\nexport const StyledBlock = Node.create({\n  name: \"styledBlock\",\n  group: \"block\",\n  content: \"block+\",\n  defining: true,\n\n  addAttributes() {\n    return {\n      style: {\n        default: null,\n        parseHTML: (el: HTMLElement) => sanitizeStyle(el.getAttribute(\"style\")),\n        // Sanitise on output too: JSON content can set style without parsing HTML.\n        renderHTML: (attrs: Record<string, unknown>) => {\n          const style = sanitizeStyle(attrs.style as string);\n          return style ? { style } : {};\n        },\n      },\n    };\n  },\n\n  parseHTML() {\n    return [\n      {\n        tag: \"div[style]\",\n        priority: 40,\n        getAttrs: (el: HTMLElement) => {\n          if (el.hasAttribute(\"data-type\") || el.hasAttribute(\"data-youtube-video\")) return false;\n          if (/\\bihub-te-/.test(el.className || \"\")) return false;\n          return sanitizeStyle(el.getAttribute(\"style\")) ? {} : false;\n        },\n      },\n    ];\n  },\n\n  renderHTML({ HTMLAttributes }) {\n    return [\"div\", mergeAttributes(HTMLAttributes, { \"data-styled\": \"true\" }), 0];\n  },\n});\n"],"names":["OWNED_PROPERTIES","table","paragraph","heading","textStyle","highlight","tableCell","tableHeader","stripProperties","style","properties","length","kept","splitDeclarations","filter","decl","includes","split","trim","toLowerCase","join","NODE_TYPES","MARK_TYPES","PreserveStyles","Extension","create","name","addGlobalAttributes","concat","map","type","types","attributes","default","parseHTML","el","sanitizeStyle","getAttribute","renderHTML","attrs","StyledBlock","Node","group","content","defining","addAttributes","tag","priority","getAttrs","hasAttribute","test","className","_ref","HTMLAttributes","mergeAttributes"],"mappings":"uOAQMA,EAA6C,CAEjDC,MAAO,CAAC,aACRC,UAAW,CAAC,cACZC,QAAS,CAAC,cACVC,UAAW,CAAC,SACZC,UAAW,CAAC,oBACZC,UAAW,CAAC,mBAAoB,cAChCC,YAAa,CAAC,mBAAoB,wBAGpBC,EAAgBC,GAA+C,IAAzBC,yDAAuB,GAC3E,IAAKD,EAAO,OAAO,KACnB,IAAKC,EAAWC,OAAQ,OAAOF,EAC/B,IAAMG,EAAOC,EAAAA,kB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