Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
A format for representing rich text documents and changes. It aimes to be intuitive and human readable with the ability to express any change necessary to deal with rich text. A document can also be expressed with this format--as the change from an empty document.
var delta = new Delta([
{ insert: 'Gandalf', attributes: { bold: true } },
{ insert: ' the ' },
{ insert: 'Grey', attributes: { color: '#ccc' } }
]);
// Keep the first 12 characters, delete the next 4, and insert a white 'White'
var death = new Delta().retain(12)
.delete(4)
.insert('White', { color: '#fff' });
// this produces:
// {
// ops: [
// { retain: 12 },
// { delete: '4 ' },
// { insert: 'White', attributes: { color: '#fff' } }
// ]
// }
delta.compose(death);
// delta is now:
// {
// ops: [
// { insert: 'Gandalf ', attributes: { bold: true } },
// { insert: 'the ' },
// { insert: 'White', attributes: { color: '#fff' } }
// ]
// }
This format is suitable for Operational Transform and defines several functions (compose
, transform
, diff
) to support this use case.
Operations describe a singular change to a document. They can be an insert
, delete
or retain
. Note operations do not take an index. They always describe the change at the current index. Use retains to "keep" or "skip" certain parts of the document.
Insert operations have an insert
key defined. A String value represents inserting text. Any other type represents inserting an embed (however only one level of object comparison will be performed for equality).
In both cases of text and embeds, an optional attributes
key can be defined with an Object to describe additonal formatting information. Formats can be changed by the retain operation.
// Insert a bolded "Text"
{ insert: "Text", attributes: { bold: true } }
// Insert a link
{ insert: "Google", attributes: { href: 'https://www.google.com' } }
// Insert an embed
{
insert: { image: 'https://octodex.github.com/images/labtocat.png' },
attributes: { alt: "Lab Octocat" }
}
// Insert another embed
{
insert: { video: 'https://www.youtube.com/watch?v=dMH0bHeiRNg' },
attributes: {
width: 420,
height: 315
}
}
Delete operations have a Number delete
key defined representing the number of characters to delete. All embeds have a length of 1.
// Delete the next 10 characters
{ delete: 10 }
Retain operations have a Number retain
key defined representing the number of characters to keep (other libraries might use the name keep or skip). An optional attributes
key can be defined with an Object to describe formatting changes to the character range. A value of null
in the attributes
Object represents removal of that key.
Note: It is not necessary to retain the last characters of a document as this is implied.
// Keep the next 5 characters
{ retain: 5 }
// Keep and bold the next 5 characters
{ retain: 5, attributes: { bold: true } }
// Keep and unbold the next 5 characters
// More specifically, remove the bold key in the attributes Object
// in the next 5 characters
{ retain: 5, attributes: { bold: null } }
A Delta is made up of an array of operations. All methods maintain the property that Deltas are represented in the most compact form. For example two consecutive insert operations of plain text will be merged into one. Thus a vanilla deep Object/Array comparison can be used to determine Delta equality.
Creates a new Delta object.
new Delta()
new Delta(ops)
new Delta(delta)
ops
- Array of operationsdelta
- Object with an ops
key set to an array of operationsNote: No validity/sanity check is performed when constructed with ops or delta. The new delta's internal ops array will also be assigned to ops or delta.ops without deep copying.
var delta = new Delta([
{ insert: 'Hello World' },
{ insert: '!', attributes: { bold: true }}
]);
var packet = JSON.stringify(delta);
var other = new Delta(JSON.parse(packet));
var chained = new Delta().insert('Hello World').insert('!', { bold: true });
Appends an insert operation. Returns this
for chainability.
insert(text, attributes)
insert(embed, attributes)
text
- String representing text to insertembed
- Number representing embed type to insertattributes
- Optional attributes to applydelta.insert('Text', { bold: true, color: '#ccc' });
delta.insert(1, { src: 'https://octodex.github.com/images/labtocat.png' });
Appends a delete operation. Returns this
for chainability.
delete(length)
length
- Number of characters to deletedelta.delete(5);
Appends a retain operation. Returns this
for chainability.
retain(length, attributes)
length
- Number of characters to retainattributes
- Optional attributes to applydelta.retain(4).retain(5, { color: '#0c6' });
Returns length of a Delta, which is the sum of the lengths of its operations.
length()
new Delta().insert('Hello').length(); // Returns 5
new Delta().insert('A').retain(2).delete(1) // Returns 4
Returns copy of delta with subset of operations.
slice()
slice(start)
slice(start, end)
start
- Start index of subset, defaults to 0end
- End index of subset, defaults to rest of operationsvar delta = new Delta().insert('Hello', { bold: true }).insert(' World');
// {
// ops: [
// { insert: 'Hello', attributes: { bold: true } },
// { insert: ' World' }
// ]
// }
var copy = delta.slice();
// { ops: [{ insert: 'World' }] }
var world = delta.slice(6);
// { ops: [{ insert: ' ' }] }
var space = delta.slice(5, 6);
Returns a Delta that is equivalent to applying the operations of own Delta, followed by another Delta.
compose(other)
other
- Delta to composevar a = new Delta().insert('abc');
var b = new Delta().retain(1).delete(1);
var composed = a.compose(b); // composed == new Delta().insert('ac');
Transform given Delta against own operations.
transform(other, priority)
transform(index)
- Alias for transformPosition
other
- Delta to transformpriority
- Boolean used to break tiesDelta
- transformed Deltavar a = new Delta().insert('a');
var b = new Delta().insert('b');
b = a.transform(b, true); // new Delta().retain(1).insert('b');
Transform an index against the delta. Useful for representing cursor/selection positions.
transformPosition(index)
index
- index to transformNumber
- transformed indexvar index = 12;
var transformedIndex = delta.transformPosition(index);
A Delta with only insert operations can be used to represent a rich text document. This can be thought of as a Delta applied to an empty document.
The following methods are supported only for Deltas that represent a document (i.e. they only contain inserts). There are no guarantees on the behavior on non-document Deltas.
Returns a new Delta representing the concatenation of this and another document Delta's operations.
concat(other)
other
- Document Delta to concatenateDelta
- Concatenated document Deltavar a = new Delta().insert('Hello');
var b = new Delta().insert('!', { bold: true });
// {
// ops: [
// { insert: 'Hello' },
// { insert: '!', attributes: { bold: true } }
// ]
// }
var concat = a.concat(b);
Returns a Delta representing the difference between two documents.
diff(other)
other
- Document Delta to diff againstDelta
- difference between the two documentsvar a = new Delta().insert('Hello');
var b = new Delta().insert('Hello!');
var diff = a.diff(b); // { ops: [{ retain: 5 }, { insert: '!' }] }
// a.compose(diff) == b
FAQs
OT type for rich text
The npm package rich-text receives a total of 5,058 weekly downloads. As such, rich-text popularity was classified as popular.
We found that rich-text demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.