@tiptap/core 2.5.0-beta.4 → 2.5.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1782,6 +1782,7 @@ function createNodeFromContent(content, schema, options) {
1782
1782
  if (isTextContent) {
1783
1783
  let schemaToUse = schema;
1784
1784
  let hasInvalidContent = false;
1785
+ let invalidContent = '';
1785
1786
  // Only ever check for invalid content if we're supposed to throw an error
1786
1787
  if (options.errorOnInvalidContent) {
1787
1788
  schemaToUse = new model.Schema({
@@ -1796,9 +1797,11 @@ function createNodeFromContent(content, schema, options) {
1796
1797
  parseDOM: [
1797
1798
  {
1798
1799
  tag: '*',
1799
- getAttrs: () => {
1800
+ getAttrs: e => {
1800
1801
  // If this is ever called, we know that the content has something that we don't know how to handle in the schema
1801
1802
  hasInvalidContent = true;
1803
+ // Try to stringify the element for a more helpful error message
1804
+ invalidContent = typeof e === 'string' ? e : e.outerHTML;
1802
1805
  return null;
1803
1806
  },
1804
1807
  },
@@ -1812,7 +1815,7 @@ function createNodeFromContent(content, schema, options) {
1812
1815
  ? parser.parseSlice(elementFromString(content), options.parseOptions).content
1813
1816
  : parser.parse(elementFromString(content), options.parseOptions);
1814
1817
  if (options.errorOnInvalidContent && hasInvalidContent) {
1815
- throw new Error('[tiptap error]: Invalid HTML content');
1818
+ throw new Error('[tiptap error]: Invalid HTML content', { cause: new Error(`Invalid element found: ${invalidContent}`) });
1816
1819
  }
1817
1820
  return response;
1818
1821
  }
@@ -1865,10 +1868,6 @@ const insertContentAt = (position, value, options) => ({ tr, dispatch, editor })
1865
1868
  catch (e) {
1866
1869
  return false;
1867
1870
  }
1868
- // don’t dispatch an empty fragment because this can lead to strange errors
1869
- if (content.toString() === '<>') {
1870
- return true;
1871
- }
1872
1871
  let { from, to } = typeof position === 'number' ? { from: position, to: position } : { from: position.from, to: position.to };
1873
1872
  let isOnlyTextContent = true;
1874
1873
  let isOnlyBlockContent = true;
@@ -2238,22 +2237,27 @@ function createDocument(content, schema, parseOptions = {}, options = {}) {
2238
2237
  });
2239
2238
  }
2240
2239
 
2241
- const setContent = (content, emitUpdate = false, parseOptions = {}, options = {}) => ({ tr, editor, dispatch }) => {
2242
- var _a;
2240
+ const setContent = (content, emitUpdate = false, parseOptions = {}, options = {}) => ({ editor, tr, dispatch, commands, }) => {
2241
+ var _a, _b;
2243
2242
  const { doc } = tr;
2244
- let document;
2245
- try {
2246
- document = createDocument(content, editor.schema, parseOptions, {
2243
+ // This is to keep backward compatibility with the previous behavior
2244
+ // TODO remove this in the next major version
2245
+ if (parseOptions.preserveWhitespace !== 'full') {
2246
+ const document = createDocument(content, editor.schema, parseOptions, {
2247
2247
  errorOnInvalidContent: (_a = options.errorOnInvalidContent) !== null && _a !== void 0 ? _a : editor.options.enableContentCheck,
2248
2248
  });
2249
- }
2250
- catch (e) {
2251
- return false;
2249
+ if (dispatch) {
2250
+ tr.replaceWith(0, doc.content.size, document).setMeta('preventUpdate', !emitUpdate);
2251
+ }
2252
+ return true;
2252
2253
  }
2253
2254
  if (dispatch) {
2254
- tr.replaceWith(0, doc.content.size, document).setMeta('preventUpdate', !emitUpdate);
2255
+ tr.setMeta('preventUpdate', !emitUpdate);
2255
2256
  }
2256
- return true;
2257
+ return commands.insertContentAt({ from: 0, to: doc.content.size }, content, {
2258
+ parseOptions,
2259
+ errorOnInvalidContent: (_b = options.errorOnInvalidContent) !== null && _b !== void 0 ? _b : editor.options.enableContentCheck,
2260
+ });
2257
2261
  };
2258
2262
 
2259
2263
  function getMarkAttributes(state, typeOrName) {
@@ -3357,40 +3361,30 @@ const updateAttributes = (typeOrName, attributes = {}) => ({ tr, state, dispatch
3357
3361
  markType = getMarkType(typeOrName, state.schema);
3358
3362
  }
3359
3363
  if (dispatch) {
3360
- let lastPos;
3361
- let lastNode;
3362
- let trimmedFrom;
3363
- let trimmedTo;
3364
- tr.selection.ranges.forEach((range) => {
3364
+ tr.selection.ranges.forEach(range => {
3365
3365
  const from = range.$from.pos;
3366
3366
  const to = range.$to.pos;
3367
3367
  state.doc.nodesBetween(from, to, (node, pos) => {
3368
3368
  if (nodeType && nodeType === node.type) {
3369
- trimmedFrom = Math.max(pos, from);
3370
- trimmedTo = Math.min(pos + node.nodeSize, to);
3371
- lastPos = pos;
3372
- lastNode = node;
3369
+ tr.setNodeMarkup(pos, undefined, {
3370
+ ...node.attrs,
3371
+ ...attributes,
3372
+ });
3373
+ }
3374
+ if (markType && node.marks.length) {
3375
+ node.marks.forEach(mark => {
3376
+ if (markType === mark.type) {
3377
+ const trimmedFrom = Math.max(pos, from);
3378
+ const trimmedTo = Math.min(pos + node.nodeSize, to);
3379
+ tr.addMark(trimmedFrom, trimmedTo, markType.create({
3380
+ ...mark.attrs,
3381
+ ...attributes,
3382
+ }));
3383
+ }
3384
+ });
3373
3385
  }
3374
3386
  });
3375
3387
  });
3376
- if (lastNode) {
3377
- if (lastPos !== undefined) {
3378
- tr.setNodeMarkup(lastPos, undefined, {
3379
- ...lastNode.attrs,
3380
- ...attributes,
3381
- });
3382
- }
3383
- if (markType && lastNode.marks.length) {
3384
- lastNode.marks.forEach((mark) => {
3385
- if (markType === mark.type) {
3386
- tr.addMark(trimmedFrom, trimmedTo, markType.create({
3387
- ...mark.attrs,
3388
- ...attributes,
3389
- }));
3390
- }
3391
- });
3392
- }
3393
- }
3394
3388
  }
3395
3389
  return true;
3396
3390
  };