MCPcopy Create free account
hub / github.com/ether/etherpad / spliceText

Method spliceText

src/node/db/Pad.ts:475–501  ·  view source on GitHub ↗

* Splices text into the pad. If the result of the splice does not end with a newline, one will be * automatically appended. * * @param {number} start - Location in pad text to start removing and inserting characters. Must * be a non-negative integer less than or equal to `this.text()

(start:number, ndel:number, ins: string, authorId: string = '')

Source from the content-addressed store, hash-verified

473 * @param {string} [authorId] - Author ID of the user making the change (if applicable).
474 */
475 async spliceText(start:number, ndel:number, ins: string, authorId: string = '') {
476 if (start < 0) throw new RangeError(`start index must be non-negative (is ${start})`);
477 if (ndel < 0) throw new RangeError(`characters to delete must be non-negative (is ${ndel})`);
478 const orig = this.text();
479 assert(orig.endsWith('\n'));
480 if (start + ndel > orig.length) throw new RangeError('start/delete past the end of the text');
481 ins = exports.cleanText(ins);
482 const willEndWithNewline =
483 start + ndel < orig.length || // Keeping last char (which is guaranteed to be a newline).
484 ins.endsWith('\n') ||
485 (!ins && start > 0 && orig[start - 1] === '\n');
486 if (!willEndWithNewline) ins += '\n';
487 if (ndel === 0 && ins.length === 0) return;
488 // An unattributed insert (empty authorId + non-empty ins) would produce
489 // an AText where `text` and `attribs` disagree on length — clients fail
490 // setDocAText reconciliation on load. Backward-compat fix: if the caller
491 // didn't provide an authorId, attribute the insert to a stable system
492 // author. ep_post_data and other plugins that want named attribution
493 // should still pass an explicit authorId.
494 const effectiveAuthorId =
495 (ins.length > 0 && !authorId) ? Pad.SYSTEM_AUTHOR_ID : authorId;
496 const attribs = effectiveAuthorId
497 ? [['author', effectiveAuthorId] as [string, string]]
498 : undefined;
499 const changeset = makeSplice(orig, start, ndel, ins, attribs, this.pool);
500 await this.appendRevision(changeset, effectiveAuthorId);
501 }
502
503 /**
504 * Replaces the pad's text with new text.

Callers 4

setTextMethod · 0.95
appendTextMethod · 0.95
Pad.tsFile · 0.80

Calls 4

textMethod · 0.95
appendRevisionMethod · 0.95
makeSpliceFunction · 0.90
assertFunction · 0.50

Tested by

no test coverage detected