feat: parseHTML for attributes should return the value instead of an object now, fix #1863

This commit is contained in:
Philipp Kühn
2021-09-08 23:53:44 +02:00
parent d3285e9308
commit 8a3b47a529
17 changed files with 38 additions and 92 deletions

View File

@@ -1,6 +1,7 @@
import { ParseRule } from 'prosemirror-model'
import { ExtensionAttribute } from '../types'
import fromString from '../utilities/fromString'
import isObject from '../utilities/isObject'
/**
* This function merges extension attributes into parserule attributes (`attrs` or `getAttrs`).
@@ -27,18 +28,21 @@ export default function injectExtensionAttributesToParseRule(parseRule: ParseRul
const newAttributes = extensionAttributes
.filter(item => item.attribute.rendered)
.reduce((items, item) => {
const attributes = item.attribute.parseHTML
? item.attribute.parseHTML(node as HTMLElement) || {}
: {
[item.name]: fromString((node as HTMLElement).getAttribute(item.name)),
}
const value = item.attribute.parseHTML
? item.attribute.parseHTML(node as HTMLElement)
: fromString((node as HTMLElement).getAttribute(item.name))
const filteredAttributes = Object.fromEntries(Object.entries(attributes)
.filter(([, value]) => value !== undefined && value !== null))
if (isObject(value)) {
console.warn(`[tiptap warn]: BREAKING CHANGE: "parseHTML" for your attribute "${item.name}" returns an object but should return the value itself. If this is expected you can ignore this message. This warning will be removed in one of the next releases. Further information: https://github.com/ueberdosis/tiptap/issues/1863`)
}
if (value === null || value === undefined) {
return items
}
return {
...items,
...filteredAttributes,
[item.name]: value,
}
}, {})

View File

@@ -98,7 +98,7 @@ export type Attribute = {
default: any,
rendered?: boolean,
renderHTML?: ((attributes: Record<string, any>) => Record<string, any> | null) | null,
parseHTML?: ((element: HTMLElement) => Record<string, any> | null) | null,
parseHTML?: ((element: HTMLElement) => any | null) | null,
keepOnSplit: boolean,
}