@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.umd.js CHANGED
@@ -1778,6 +1778,7 @@
1778
1778
  if (isTextContent) {
1779
1779
  let schemaToUse = schema;
1780
1780
  let hasInvalidContent = false;
1781
+ let invalidContent = '';
1781
1782
  // Only ever check for invalid content if we're supposed to throw an error
1782
1783
  if (options.errorOnInvalidContent) {
1783
1784
  schemaToUse = new model.Schema({
@@ -1792,9 +1793,11 @@
1792
1793
  parseDOM: [
1793
1794
  {
1794
1795
  tag: '*',
1795
- getAttrs: () => {
1796
+ getAttrs: e => {
1796
1797
  // If this is ever called, we know that the content has something that we don't know how to handle in the schema
1797
1798
  hasInvalidContent = true;
1799
+ // Try to stringify the element for a more helpful error message
1800
+ invalidContent = typeof e === 'string' ? e : e.outerHTML;
1798
1801
  return null;
1799
1802
  },
1800
1803
  },
@@ -1808,7 +1811,7 @@
1808
1811
  ? parser.parseSlice(elementFromString(content), options.parseOptions).content
1809
1812
  : parser.parse(elementFromString(content), options.parseOptions);
1810
1813
  if (options.errorOnInvalidContent && hasInvalidContent) {
1811
- throw new Error('[tiptap error]: Invalid HTML content');
1814
+ throw new Error('[tiptap error]: Invalid HTML content', { cause: new Error(`Invalid element found: ${invalidContent}`) });
1812
1815
  }
1813
1816
  return response;
1814
1817
  }
@@ -1861,10 +1864,6 @@
1861
1864
  catch (e) {
1862
1865
  return false;
1863
1866
  }
1864
- // don’t dispatch an empty fragment because this can lead to strange errors
1865
- if (content.toString() === '<>') {
1866
- return true;
1867
- }
1868
1867
  let { from, to } = typeof position === 'number' ? { from: position, to: position } : { from: position.from, to: position.to };
1869
1868
  let isOnlyTextContent = true;
1870
1869
  let isOnlyBlockContent = true;
@@ -2234,22 +2233,27 @@
2234
2233
  });
2235
2234
  }
2236
2235
 
2237
- const setContent = (content, emitUpdate = false, parseOptions = {}, options = {}) => ({ tr, editor, dispatch }) => {
2238
- var _a;
2236
+ const setContent = (content, emitUpdate = false, parseOptions = {}, options = {}) => ({ editor, tr, dispatch, commands, }) => {
2237
+ var _a, _b;
2239
2238
  const { doc } = tr;
2240
- let document;
2241
- try {
2242
- document = createDocument(content, editor.schema, parseOptions, {
2239
+ // This is to keep backward compatibility with the previous behavior
2240
+ // TODO remove this in the next major version
2241
+ if (parseOptions.preserveWhitespace !== 'full') {
2242
+ const document = createDocument(content, editor.schema, parseOptions, {
2243
2243
  errorOnInvalidContent: (_a = options.errorOnInvalidContent) !== null && _a !== void 0 ? _a : editor.options.enableContentCheck,
2244
2244
  });
2245
- }
2246
- catch (e) {
2247
- return false;
2245
+ if (dispatch) {
2246
+ tr.replaceWith(0, doc.content.size, document).setMeta('preventUpdate', !emitUpdate);
2247
+ }
2248
+ return true;
2248
2249
  }
2249
2250
  if (dispatch) {
2250
- tr.replaceWith(0, doc.content.size, document).setMeta('preventUpdate', !emitUpdate);
2251
+ tr.setMeta('preventUpdate', !emitUpdate);
2251
2252
  }
2252
- return true;
2253
+ return commands.insertContentAt({ from: 0, to: doc.content.size }, content, {
2254
+ parseOptions,
2255
+ errorOnInvalidContent: (_b = options.errorOnInvalidContent) !== null && _b !== void 0 ? _b : editor.options.enableContentCheck,
2256
+ });
2253
2257
  };
2254
2258
 
2255
2259
  function getMarkAttributes(state, typeOrName) {
@@ -3353,40 +3357,30 @@
3353
3357
  markType = getMarkType(typeOrName, state.schema);
3354
3358
  }
3355
3359
  if (dispatch) {
3356
- let lastPos;
3357
- let lastNode;
3358
- let trimmedFrom;
3359
- let trimmedTo;
3360
- tr.selection.ranges.forEach((range) => {
3360
+ tr.selection.ranges.forEach(range => {
3361
3361
  const from = range.$from.pos;
3362
3362
  const to = range.$to.pos;
3363
3363
  state.doc.nodesBetween(from, to, (node, pos) => {
3364
3364
  if (nodeType && nodeType === node.type) {
3365
- trimmedFrom = Math.max(pos, from);
3366
- trimmedTo = Math.min(pos + node.nodeSize, to);
3367
- lastPos = pos;
3368
- lastNode = node;
3365
+ tr.setNodeMarkup(pos, undefined, {
3366
+ ...node.attrs,
3367
+ ...attributes,
3368
+ });
3369
+ }
3370
+ if (markType && node.marks.length) {
3371
+ node.marks.forEach(mark => {
3372
+ if (markType === mark.type) {
3373
+ const trimmedFrom = Math.max(pos, from);
3374
+ const trimmedTo = Math.min(pos + node.nodeSize, to);
3375
+ tr.addMark(trimmedFrom, trimmedTo, markType.create({
3376
+ ...mark.attrs,
3377
+ ...attributes,
3378
+ }));
3379
+ }
3380
+ });
3369
3381
  }
3370
3382
  });
3371
3383
  });
3372
- if (lastNode) {
3373
- if (lastPos !== undefined) {
3374
- tr.setNodeMarkup(lastPos, undefined, {
3375
- ...lastNode.attrs,
3376
- ...attributes,
3377
- });
3378
- }
3379
- if (markType && lastNode.marks.length) {
3380
- lastNode.marks.forEach((mark) => {
3381
- if (markType === mark.type) {
3382
- tr.addMark(trimmedFrom, trimmedTo, markType.create({
3383
- ...mark.attrs,
3384
- ...attributes,
3385
- }));
3386
- }
3387
- });
3388
- }
3389
- }
3390
3384
  }
3391
3385
  return true;
3392
3386
  };