-
Notifications
You must be signed in to change notification settings - Fork 879
/
Mark.js
269 lines (239 loc) · 7.31 KB
/
Mark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { defaults, isUndefined } from "lodash-es";
const defaultProperties = { original: "", marked: "", fieldsToMark: [] };
/**
* Represents a place where highlighting should be applied.
*/
class Mark {
/**
* Represents a place where highlighting should be applied.
* We allow both replacement-based highlighting (through providing `original`, `marked`, and potentially `fieldsToMark`) and
* position-based highlighting (through providing a `position`).
*
* @param {Object} [properties] The properties of this Mark.
*
* @param {string?} properties.original The original text that should be marked.
* @param {string?} properties.marked The new text including marks.
* @param {array?} properties.fieldsToMark The array that specifies which text section(s) to mark.
*
* @param {SourceCodeRange?} properties.position The position object: a range in the source code.
*/
constructor( properties ) {
properties = properties || {};
defaults( properties, defaultProperties );
this._properties = properties;
this.isValid();
}
/**
* Returns the original text.
*
* @returns {string} The original text.
*/
getOriginal() {
return this._properties.original;
}
/**
* Returns the marked text.
*
* @returns {string} The replaced text.
*/
getMarked() {
return this._properties.marked;
}
/**
* Returns the fields to mark.
*
* @returns {array} The fields to mark.
*/
getFieldsToMark() {
return this._properties.fieldsToMark;
}
/**
* Returns the position information.
*
* @returns {number} The position information.
*/
getPosition() {
return this._properties.position;
}
/**
* Returns the start position.
*
* @returns {number} The start position.
*/
getPositionStart() {
return this._properties.position && this._properties.position.startOffset;
}
/**
* Returns the end position.
*
* @returns {number} The end position.
*/
getPositionEnd() {
return this._properties.position && this._properties.position.endOffset;
}
/**
* Sets the start position.
*
* @param {number} positionStart The new start position.
*
* @returns {void}
*/
setPositionStart( positionStart ) {
this._properties.position.startOffset = positionStart;
}
/**
* Sets the end position.
*
* @param {number} positionEnd The new end position.
*
* @returns {void}
*/
setPositionEnd( positionEnd ) {
this._properties.position.endOffset = positionEnd;
}
/**
* Returns the start position of a block.
*
* @param {number} startOffsetBlock The block start offset.
*
* @returns {number} The start position of a block.
*/
setBlockPositionStart( startOffsetBlock ) {
this._properties.position.startOffsetBlock = startOffsetBlock;
}
/**
* Returns the end position of a block.
*
* @param {number} endOffsetBlock The block end offset.
*
* @returns {number} The end position of a block.
*/
setBlockPositionEnd( endOffsetBlock ) {
this._properties.position.endOffsetBlock = endOffsetBlock;
}
/**
* Gets the block client id.
*
* @returns {string} The block client id.
*/
getBlockClientId() {
return this._properties.position && this._properties.position.clientId;
}
/**
* Gets the block attribute id.
*
* @returns {string} The block attribute id.
*/
getBlockAttributeId() {
return this._properties.position && this._properties.position.attributeId;
}
/**
* Checks if the mark object is intended for the first section of a Yoast sub-block.
* This method will be used only for Yoast blocks where each block consists of sub-blocks
* with two sections.
*
* @returns {boolean} Whether the mark object is intended for the first section of a Yoast sub-block.
*/
isMarkForFirstBlockSection() {
return this._properties.position && this._properties.position.isFirstSection;
}
/**
* Returns the start position inside block.
*
* @returns {number} The start position inside block if the mark position information, undefined otherwise.
*/
getBlockPositionStart() {
return this._properties.position && this._properties.position.startOffsetBlock;
}
/**
* Returns the end position inside block if the mark has position information, undefined otherwise.
*
* @returns {number} The end position inside block.
*/
getBlockPositionEnd() {
return this._properties.position && this._properties.position.endOffsetBlock;
}
/**
* Applies this mark to the given text with replacement-based highlighting.
*
* @param {string} text The original text without the mark applied.
* @returns {string} A new text with the mark applied to it.
*/
applyWithReplace( text ) {
// (=^ ◡ ^=) Cute method to replace everything in a string without using regex.
return text.split( this._properties.original ).join( this._properties.marked );
}
/**
* Applies this mark to the given text with position-based highlighting.
*
* @param {string} text The original text without the mark applied.
* @returns {string} A new text with the mark applied to it.
*/
applyWithPosition( text ) {
const markStart = "<yoastmark class='yoast-text-mark'>";
const markEnd = "</yoastmark>";
const newPositionEnd = this.getPositionEnd() + markStart.length;
text = text.substring( 0, this.getPositionStart() ) + markStart + text.substring( this.getPositionStart() );
text = text.substring( 0, newPositionEnd ) + markEnd + text.substring( newPositionEnd );
return text;
}
/**
* Serializes the Mark instance to an object.
*
* @returns {Object} The serialized Mark.
*/
serialize() {
return {
_parseClass: "Mark",
...this._properties,
};
}
/**
* Checks if the mark object is valid for position-based highlighting.
* @returns {void}
*/
isValid() {
if ( ! isUndefined( this.getPositionStart() ) && this.getPositionStart() < 0 ) {
throw new RangeError( "positionStart should be larger or equal than 0." );
}
if ( ! isUndefined( this.getPositionEnd() ) && this.getPositionEnd() <= 0 ) {
throw new RangeError( "positionEnd should be larger than 0." );
}
if ( ! isUndefined( this.getPositionStart() ) && ! isUndefined( this.getPositionEnd() ) &&
this.getPositionStart() >= this.getPositionEnd() ) {
throw new RangeError( "The positionStart should be smaller than the positionEnd." );
}
if ( isUndefined( this.getPositionStart() ) && ! isUndefined( this.getPositionEnd() ) ||
isUndefined( this.getPositionEnd() ) && ! isUndefined( this.getPositionStart() ) ) {
throw new Error( "A mark object should either have start and end defined or start and end undefined." );
}
}
/**
* Checks if a mark has position information available.
* @returns {boolean} Returns true if the Mark object has position information, false otherwise.
*/
hasPosition() {
return ! isUndefined( this.getPositionStart() );
}
/**
* Checks if a mark has block position information available.
* A block has position information if the block start offset is available.
*
* @returns {boolean} Returns true if the Mark object has block position information, false otherwise.
*/
hasBlockPosition() {
return ! isUndefined( this.getBlockPositionStart() );
}
/**
* Parses the object to a Mark.
*
* @param {Object} serialized The serialized object.
*
* @returns {Mark} The parsed Mark.
*/
static parse( serialized ) {
delete serialized._parseClass;
return new Mark( serialized );
}
}
export default Mark;