-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathTag.js
460 lines (407 loc) · 7.78 KB
/
Tag.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
* @constructor
*
* @param {number} type Tag's type
* @param {string} name Name of the tag
* @param {number} pos Position of the tag in the text
* @param {number} len Length of text consumed by the tag
* @param {number=} priority This tag's sorting tiebreaker
*/
function Tag(type, name, pos, len, priority)
{
this.type = +type;
this.name = name;
this.pos = +pos;
this.len = +len;
this.sortPriority = +priority || 0;
this.attributes = {};
this.cascade = [];
// Invalidate this tag now if any value is not a number, they could wreck
// havoc in other parts of the program
if (isNaN(type + pos + len))
{
this.invalidate();
}
}
/** @const */
Tag.START_TAG = 1;
/** @const */
Tag.END_TAG = 2;
/** @const */
Tag.SELF_CLOSING_TAG = 3;
/**
* @type {!Object} Dictionary of attributes
*/
Tag.prototype.attributes;
/**
* @type {!Array.<!Tag>} List of tags that are invalidated when this tag is invalidated
*/
Tag.prototype.cascade;
/**
* @type {?Tag} End tag that unconditionally ends this start tag
*/
Tag.prototype.endTag;
/**
* @type {boolean} Whether this tag is be invalid
*/
Tag.prototype.invalid = false;
/**
* @type {number} Length of text consumed by this tag
*/
Tag.prototype.len;
/**
* @type {string} Name of this tag
*/
Tag.prototype.name;
/**
* @type {number} Position of this tag in the text
*/
Tag.prototype.pos;
/**
* @type {number} Tiebreaker used when sorting identical tags
*/
Tag.prototype.sortPriority;
/**
* @type {?Tag} Start tag that is unconditionally closed this end tag
*/
Tag.prototype.startTag;
/**
* @type {number} Tag type
*/
Tag.prototype.type;
/**
* Add a set of flags to this tag's
*
* @param {number} flags
*/
Tag.prototype.addFlags = function(flags)
{
this.flags |= flags;
};
/**
* Set given tag to be invalidated if this tag is invalidated
*
* @param {!Tag} tag
*/
Tag.prototype.cascadeInvalidationTo = function(tag)
{
this.cascade.push(tag);
// If this tag is already invalid, cascade it now
if (this.invalid)
{
tag.invalidate();
}
};
/**
* Invalidate this tag, as well as tags bound to this tag
*/
Tag.prototype.invalidate = function()
{
// Only invalidate if this tag is valid to prevent infinite loops
if (!this.invalid)
{
this.invalid = true;
this.cascade.forEach(
/**
* @param {!Tag} tag
*/
function(tag)
{
tag.invalidate();
}
);
}
};
/**
* Pair this tag with given tag
*
* @param {!Tag} tag
*/
Tag.prototype.pairWith = function(tag)
{
if (this.canBePaired(this, tag))
{
this.endTag = tag;
tag.startTag = this;
this.cascadeInvalidationTo(tag);
}
else if (this.canBePaired(tag, this))
{
this.startTag = tag;
tag.endTag = this;
}
};
/**
* Test whether two tags can be paired
*
* @param {!Tag} startTag
* @param {!Tag} endTag
* @return {boolean}
*/
Tag.prototype.canBePaired = function(startTag, endTag)
{
return startTag.name === endTag.name && startTag.type === Tag.START_TAG && endTag.type === Tag.END_TAG && startTag.pos <= startTag.pos;
};
/**
* Remove a set of flags from this tag's
*
* @param {number} flags
*/
Tag.prototype.removeFlags = function(flags)
{
this.flags &= ~flags;
};
/**
* Set the bitfield of boolean rules that apply to this tag
*
* @param {number} flags Bitfield of boolean rules that apply to this tag
*/
Tag.prototype.setFlags = function(flags)
{
this.flags = flags;
};
//==========================================================================
// Getters
//==========================================================================
/**
* Return this tag's attributes
*
* @return {!Object}
*/
Tag.prototype.getAttributes = function()
{
let attributes = {};
for (let attrName in this.attributes)
{
attributes[attrName] = this.attributes[attrName];
}
return attributes;
};
/**
* Return this tag's end tag
*
* @return {?Tag} This tag's end tag
*/
Tag.prototype.getEndTag = function()
{
return this.endTag;
};
/**
* Return the bitfield of boolean rules that apply to this tag
*
* @return {number}
*/
Tag.prototype.getFlags = function()
{
return this.flags;
};
/**
* Return the length of text consumed by this tag
*
* @return {number}
*/
Tag.prototype.getLen = function()
{
return this.len;
};
/**
* Return this tag's name
*
* @return {string}
*/
Tag.prototype.getName = function()
{
return this.name;
};
/**
* Return this tag's position
*
* @return {number}
*/
Tag.prototype.getPos = function()
{
return this.pos;
};
/**
* Return this tag's tiebreaker
*
* @return {number}
*/
Tag.prototype.getSortPriority = function()
{
return this.sortPriority;
};
/**
* Return this tag's start tag
*
* @return {?Tag} This tag's start tag
*/
Tag.prototype.getStartTag = function()
{
return this.startTag;
};
/**
* Return this tag's type
*
* @return {number}
*/
Tag.prototype.getType = function()
{
return this.type;
};
//==========================================================================
// Tag's status
//==========================================================================
/**
* Test whether this tag can close given start tag
*
* @param {!Tag} startTag
* @return {boolean}
*/
Tag.prototype.canClose = function(startTag)
{
if (this.invalid
|| !this.canBePaired(startTag, this)
|| (this.startTag && this.startTag !== startTag)
|| (startTag.endTag && startTag.endTag !== this))
{
return false;
}
return true;
};
/**
* Test whether this tag is a br tag
*
* @return {boolean}
*/
Tag.prototype.isBrTag = function()
{
return (this.name === 'br');
};
/**
* Test whether this tag is an end tag (self-closing tags inclusive)
*
* @return {boolean}
*/
Tag.prototype.isEndTag = function()
{
return !!(this.type & Tag.END_TAG);
};
/**
* Test whether this tag is an ignore tag
*
* @return {boolean}
*/
Tag.prototype.isIgnoreTag = function()
{
return (this.name === 'i');
};
/**
* Test whether this tag is invalid
*
* @return {boolean}
*/
Tag.prototype.isInvalid = function()
{
return this.invalid;
};
/**
* Test whether this tag represents a paragraph break
*
* @return {boolean}
*/
Tag.prototype.isParagraphBreak = function()
{
return (this.name === 'pb');
};
/**
* Test whether this tag is a self-closing tag
*
* @return {boolean}
*/
Tag.prototype.isSelfClosingTag = function()
{
return (this.type === Tag.SELF_CLOSING_TAG);
};
/**
* Test whether this tag is a special tag: "br", "i", "pb" or "v"
*
* @return {boolean}
*/
Tag.prototype.isSystemTag = function()
{
return ('br i pb v'.indexOf(this.name) > -1);
};
/**
* Test whether this tag is a start tag (self-closing tags inclusive)
*
* @return {boolean}
*/
Tag.prototype.isStartTag = function()
{
return !!(this.type & Tag.START_TAG);
};
/**
* Test whether this tag represents verbatim text
*
* @return {boolean}
*/
Tag.prototype.isVerbatim = function()
{
return (this.name === 'v');
};
//==========================================================================
// Attributes handling
//==========================================================================
/**
* Return the value of given attribute
*
* @param {string} attrName
* @return {string}
*/
Tag.prototype.getAttribute = function(attrName)
{
return this.attributes[attrName];
};
/**
* Return whether given attribute is set
*
* @param {string} attrName
* @return {boolean}
*/
Tag.prototype.hasAttribute = function(attrName)
{
return (attrName in this.attributes);
};
/**
* Remove given attribute
*
* @param {string} attrName
*/
Tag.prototype.removeAttribute = function(attrName)
{
delete this.attributes[attrName];
};
/**
* Set the value of an attribute
*
* @param {string} attrName Attribute's name
* @param {*} attrValue Attribute's value
*/
Tag.prototype.setAttribute = function(attrName, attrValue)
{
this.attributes[attrName] = attrValue;
};
/**
* Set all of this tag's attributes at once
*
* @param {!Object} attributes
*/
Tag.prototype.setAttributes = function(attributes)
{
this.attributes = {};
for (let attrName in attributes)
{
this.attributes[attrName] = attributes[attrName];
}
};