{"version":3,"file":"index.cjs","names":["isValidCSSStyleValue","Node","mergeAttributes"],"sources":["../../src/utils/parseAlign.ts","../../src/utils/parseColwidth.ts","../../src/utils/fillEmptyCellContent.ts","../../src/utils/omitDefaultSpanAttribute.ts","../../src/cell/table-cell.ts"],"sourcesContent":["import { type Attribute, isValidCSSStyleValue } from '@tiptap/core'\n\n/**\n * Supported table cell alignment values\n */\nexport enum TableCellAlign {\n  Left = 'left',\n  Right = 'right',\n  Center = 'center',\n}\n\n/**\n * Normalize unknown input into a supported table alignment\n *\n * @param value - A potential alignment value\n * @returns A valid TableCellAlign value or null\n */\nexport function normalizeTableCellAlign(value: unknown): TableCellAlign | null {\n  if (\n    value === TableCellAlign.Left ||\n    value === TableCellAlign.Right ||\n    value === TableCellAlign.Center\n  ) {\n    return value\n  }\n\n  return null\n}\n\n/**\n * Parse table cell alignment from an HTML element\n *\n * Prefers inline style (${\"`\"}text-align${\"`\"}) and falls back to the legacy\n * ${\"`\"}align${\"`\"} attribute.\n *\n * @param element - The table cell/header DOM element\n * @returns A valid TableCellAlign value or null\n */\nfunction parseAlign(element: HTMLElement): TableCellAlign | null {\n  const styleAlign = (element.style.textAlign || '').trim().toLowerCase()\n  const attrAlign = (element.getAttribute('align') || '').trim().toLowerCase()\n  const align = styleAlign || attrAlign\n\n  return normalizeTableCellAlign(align)\n}\n\n/**\n * Normalize alignment from a generic attrs object that may include an align field\n *\n * @param attributes - A node attrs-like object with an optional align field\n * @returns A valid TableCellAlign value or null.\n */\nexport function normalizeTableCellAlignFromAttributes(\n  attributes: { align?: TableCellAlign } | null | undefined,\n): TableCellAlign | null {\n  return normalizeTableCellAlign(attributes?.align)\n}\n\n/**\n * Create a reusable Tiptap attribute config for table alignment\n *\n * @returns A Tiptap Attribute definition that parses and renders table alignment\n */\nexport function createAlignAttribute(): Attribute {\n  return {\n    default: null,\n    parseHTML: (element: HTMLElement) => parseAlign(element),\n    renderHTML: (attributes: { align?: unknown }) => {\n      const align = normalizeTableCellAlign(attributes.align)\n\n      if (!isValidCSSStyleValue(align)) {\n        return {}\n      }\n\n      return {\n        style: `text-align: ${align}`,\n      }\n    },\n  }\n}\n","/**\n * reads the width of the `<col>` element matching a cell's column from the table's `<colgroup>`\n *\n * @param element - The table cell/header DOM element\n * @returns - An array with the column width in pixels or null\n */\nfunction parseColgroupWidth(element: HTMLElement): number[] | null {\n  const row = element.parentElement\n  const table = element.closest('table')\n\n  if (!row || !table) {\n    return null\n  }\n\n  const cellIndex = Array.from(row.children).indexOf(element)\n  const width = table.querySelectorAll('colgroup > col')[cellIndex]?.getAttribute('width')\n\n  return width ? [parseInt(width, 10)] : null\n}\n\n/**\n * Parse the column width/s of a table cell/header from an HTML element.\n * it prefers the `colwidth` attribute and if not-provided falls back to the `width` attribute\n * of the matching `<col>` element in the table's `<colgroup>`.\n *\n * @param element - The table cell/header DOM element\n * @returns - An array of column widths in pixels or null\n */\nexport function parseColwidth(element: HTMLElement): number[] | null {\n  const colwidth = element.getAttribute('colwidth')\n\n  if (colwidth) {\n    return colwidth.split(',').map(width => parseInt(width, 10))\n  }\n\n  return parseColgroupWidth(element)\n}\n","import type { NodeType } from '@tiptap/pm/model'\nimport { Fragment } from '@tiptap/pm/model'\n\n// ASCII whitespace only, so a non-breaking-space cell is not treated as empty.\nconst COLLAPSIBLE_WHITESPACE = /[ \\t\\r\\n\\f]+/g\n\n/** Whether a cell/header element has no child elements and only collapsible whitespace. */\nexport function isEmptyCellElement(element: HTMLElement): boolean {\n  if (element.children.length > 0) {\n    return false\n  }\n\n  return (element.textContent ?? '').replace(COLLAPSIBLE_WHITESPACE, '') === ''\n}\n\n/** Builds a cell/header's minimal `block+` content, derived from the node type. */\nexport function fillEmptyCellContent(cellType: NodeType): Fragment {\n  const filled = cellType.createAndFill()\n\n  if (!filled) {\n    throw new Error(`[tiptap error]: \"${cellType.name}\" has no default content to backfill.`)\n  }\n\n  return filled.content\n}\n","export function omitDefaultSpanAttribute(\n  attributes: Record<string, unknown>,\n  renderedAttributes: Record<string, unknown>,\n  name: 'colspan' | 'rowspan',\n): void {\n  if (!(name in renderedAttributes)) {\n    return\n  }\n\n  const value = renderedAttributes[name]\n\n  if (value === 1 || value === '1' || value === null || value === undefined) {\n    delete attributes[name]\n  }\n}\n","import '../types.js'\n\nimport { mergeAttributes, Node } from '@tiptap/core'\n\nimport { createAlignAttribute } from '../utils/parseAlign.js'\nimport { parseColwidth } from '../utils/parseColwidth.js'\nimport { fillEmptyCellContent, isEmptyCellElement } from '../utils/fillEmptyCellContent.js'\nimport { omitDefaultSpanAttribute } from '../utils/omitDefaultSpanAttribute.js'\n\nexport interface TableCellOptions {\n  /**\n   * The HTML attributes for a table cell node.\n   * @default {}\n   * @example { class: 'foo' }\n   */\n  HTMLAttributes: Record<string, any>\n}\n\n/**\n * This extension allows you to create table cells.\n * @see https://www.tiptap.dev/api/nodes/table-cell\n */\nexport const TableCell = Node.create<TableCellOptions>({\n  name: 'tableCell',\n\n  addOptions() {\n    return {\n      HTMLAttributes: {},\n    }\n  },\n\n  content: 'block+',\n\n  addAttributes() {\n    return {\n      colspan: {\n        default: 1,\n      },\n      rowspan: {\n        default: 1,\n      },\n      colwidth: {\n        default: null,\n        parseHTML: parseColwidth,\n      },\n      align: createAlignAttribute(),\n    }\n  },\n\n  tableRole: 'cell',\n\n  isolating: true,\n\n  parseHTML() {\n    return [\n      {\n        // Backfill empty cells; non-empty cells fall through to the rule below.\n        tag: 'td',\n        getAttrs: node => (isEmptyCellElement(node) ? {} : false),\n        getContent: (_node, schema) => fillEmptyCellContent(schema.nodes[this.name]),\n      },\n      { tag: 'td' },\n    ]\n  },\n\n  renderHTML({ HTMLAttributes }) {\n    const attributes = mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)\n\n    omitDefaultSpanAttribute(attributes, HTMLAttributes, 'colspan')\n    omitDefaultSpanAttribute(attributes, HTMLAttributes, 'rowspan')\n\n    return ['td', attributes, 0]\n  },\n})\n"],"mappings":";;;;;;;;;AAiBA,SAAgB,w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