1
|
using Core.Entities;
|
2
|
using Core.GraphUtils;
|
3
|
using Ganss.XSS;
|
4
|
using HtmlAgilityPack;
|
5
|
using Models.Tags;
|
6
|
using System;
|
7
|
using System.Collections.Generic;
|
8
|
using System.Linq;
|
9
|
using System.Text;
|
10
|
using System.Threading.Tasks;
|
11
|
|
12
|
namespace Core.Services
|
13
|
{
|
14
|
public class HTMLService : IHTMLService
|
15
|
{
|
16
|
|
17
|
private const string TAG_ID_ATTRIBUTE_NAME = "aswi-tag-id";
|
18
|
private const string TAG_INSTANCE_ATTRIBUTE_NAME = "aswi-tag-instance";
|
19
|
private const string TAG_EF_ID_ATTRIBUTE_NAME = "aswi-tag-ef-id";
|
20
|
|
21
|
public class CachedInfo
|
22
|
{
|
23
|
public List<int> TagStartPositions = new();
|
24
|
public List<int> TagStartLengths = new();
|
25
|
public List<int> TagClosingPositions = new();
|
26
|
public List<int> TagClosingLengths = new();
|
27
|
public Dictionary<HtmlNode, HtmlNode> NodeDict = new();
|
28
|
public List<TagInstanceCSSInfo> TagInstanceCSS = new();
|
29
|
}
|
30
|
|
31
|
private List<int> TagStartPositions = new();
|
32
|
private List<int> TagStartLengths = new();
|
33
|
private List<int> TagClosingPositions = new();
|
34
|
private List<int> TagClosingLengths = new();
|
35
|
private Dictionary<HtmlNode, HtmlNode> NodeDict = new();
|
36
|
private List<TagInstanceCSSInfo> TagInstanceCSS = new();
|
37
|
|
38
|
private void UnpackCachedInfo(CachedInfo cachedInfo)
|
39
|
{
|
40
|
TagStartPositions = cachedInfo.TagStartPositions;
|
41
|
TagStartLengths = cachedInfo.TagStartLengths;
|
42
|
TagClosingPositions = cachedInfo.TagClosingPositions;
|
43
|
TagClosingLengths = cachedInfo.TagClosingLengths;
|
44
|
NodeDict = cachedInfo.NodeDict;
|
45
|
TagInstanceCSS = cachedInfo.TagInstanceCSS;
|
46
|
}
|
47
|
|
48
|
|
49
|
/*
|
50
|
* Full HTML Preprocessing -------------------------------------------------------------------------------
|
51
|
*/
|
52
|
|
53
|
public (string, CachedInfo) FullPreprocessHTML(string htmlSource, List<AnnotationTagGeneric> tags)
|
54
|
{
|
55
|
var docOriginal = new HtmlDocument();
|
56
|
docOriginal.LoadHtml(htmlSource);
|
57
|
var docToEdit = new HtmlDocument();
|
58
|
docToEdit.LoadHtml(htmlSource);
|
59
|
|
60
|
var descendantsOriginal = docOriginal.DocumentNode.DescendantsAndSelf();
|
61
|
var descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
|
62
|
|
63
|
int currentId = 0;
|
64
|
|
65
|
FillNodeDict(descendantsOriginal, descendantsToEdit);
|
66
|
AssignIdsToOriginalDocument(descendantsOriginal, ref currentId);
|
67
|
|
68
|
WrapTextInSpan(descendantsOriginal, docToEdit);
|
69
|
int descCount = descendantsToEdit.Count;
|
70
|
|
71
|
foreach (var tag in tags)
|
72
|
{
|
73
|
int i = 0;
|
74
|
List<HtmlNode> addedForSelection = new();
|
75
|
while (i < descCount)
|
76
|
{
|
77
|
for (; i < descCount; i++)
|
78
|
{
|
79
|
var node = descendantsToEdit.ElementAt(i);
|
80
|
if (!node.Name.Contains("#text") || addedForSelection.Contains(node) || addedForSelection.Contains(node.ParentNode) || node.ParentNode.Name == "style")
|
81
|
{
|
82
|
continue;
|
83
|
}
|
84
|
|
85
|
int nodeId = node.ParentNode.GetAttributeValue(TAG_ID_ATTRIBUTE_NAME, -1);
|
86
|
|
87
|
var start = TagStartPositions[nodeId] + TagStartLengths[nodeId];
|
88
|
var end = TagClosingPositions[nodeId];
|
89
|
|
90
|
int selectionStart = tag.Position;
|
91
|
int selectionEnd = tag.Position + tag.Length;
|
92
|
|
93
|
if (selectionStart < end && selectionEnd > start)
|
94
|
{
|
95
|
if (selectionStart <= start && selectionEnd >= end)
|
96
|
{
|
97
|
addedForSelection.Add(SolveFullFill(node, selectionStart, selectionEnd, start, end, docToEdit, tag));
|
98
|
}
|
99
|
else if (selectionStart <= start)
|
100
|
{
|
101
|
addedForSelection.AddRange(SolveRightGap(node, selectionStart, selectionEnd, start, end, docToEdit, tag));
|
102
|
}
|
103
|
else if (selectionEnd >= end)
|
104
|
{
|
105
|
addedForSelection.AddRange(SolveLeftGap(node, selectionStart, selectionEnd, start, end, docToEdit, tag));
|
106
|
}
|
107
|
else
|
108
|
{
|
109
|
addedForSelection.AddRange(SolveLeftRightGap(node, selectionStart, selectionEnd, start, end, docToEdit, tag));
|
110
|
}
|
111
|
descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
|
112
|
descCount = descendantsToEdit.Count;
|
113
|
break;
|
114
|
}
|
115
|
}
|
116
|
}
|
117
|
|
118
|
}
|
119
|
|
120
|
ModifyLinks(descendantsToEdit);
|
121
|
string docToRender = docToEdit.DocumentNode.OuterHtml;
|
122
|
HtmlSanitizer sanitizer = new HtmlSanitizer();
|
123
|
sanitizer.AllowedAttributes.Clear();
|
124
|
sanitizer.AllowedAttributes.Add(TAG_ID_ATTRIBUTE_NAME);
|
125
|
sanitizer.AllowedAttributes.Add(TAG_INSTANCE_ATTRIBUTE_NAME);
|
126
|
sanitizer.AllowedAttributes.Add("href");
|
127
|
sanitizer.AllowedAttributes.Add("end");
|
128
|
sanitizer.AllowedAttributes.Add("start");
|
129
|
sanitizer.AllowedAttributes.Add("class");
|
130
|
sanitizer.AllowedAttributes.Add("target");
|
131
|
if (sanitizer.AllowedTags.Contains("script"))
|
132
|
{
|
133
|
sanitizer.AllowedTags.Remove("script");
|
134
|
}
|
135
|
if (!sanitizer.AllowedTags.Contains("style"))
|
136
|
{
|
137
|
sanitizer.AllowedTags.Add("style");
|
138
|
}
|
139
|
sanitizer.AllowedTags.Add("a");
|
140
|
docToRender = sanitizer.Sanitize(docToRender);
|
141
|
GenerateCSS(tags);
|
142
|
|
143
|
return (docToRender, new CachedInfo()
|
144
|
{
|
145
|
TagStartPositions = TagStartPositions,
|
146
|
TagClosingPositions = TagClosingPositions,
|
147
|
TagStartLengths = TagStartLengths,
|
148
|
TagClosingLengths = TagClosingLengths,
|
149
|
TagInstanceCSS = TagInstanceCSS,
|
150
|
NodeDict = NodeDict
|
151
|
});
|
152
|
}
|
153
|
|
154
|
private HtmlNode SolveFullFill(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit, AnnotationTagGeneric tag)
|
155
|
{
|
156
|
// full fill
|
157
|
string textSelected = node.InnerText;
|
158
|
|
159
|
var parentNode = node.ParentNode;
|
160
|
int nodeIndex = parentNode.ChildNodes.IndexOf(node);
|
161
|
parentNode.ChildNodes.RemoveAt(nodeIndex);
|
162
|
|
163
|
EPosition markerPosition = EPosition.MARK_NONE;
|
164
|
if (selectionEnd == end && selectionStart == start)
|
165
|
{
|
166
|
markerPosition = EPosition.MARK_LEFT_RIGHT;
|
167
|
}
|
168
|
|
169
|
HtmlNode spanSelected = CreateSpan(docToEdit, textSelected, TagStartPositions.Count, tag.Instance, tag.Id, start, markerPosition);
|
170
|
parentNode.ChildNodes.Insert(nodeIndex, spanSelected);
|
171
|
|
172
|
return spanSelected;
|
173
|
}
|
174
|
|
175
|
private List<HtmlNode> SolveRightGap(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit,
|
176
|
AnnotationTagGeneric tag)
|
177
|
{
|
178
|
// partial fill, end gap
|
179
|
string text = node.InnerText;
|
180
|
string textAfter = text.Substring(Math.Min(selectionStart - start + tag.Length, text.Length));
|
181
|
string textSelected = text.Substring(0, selectionEnd - start);
|
182
|
|
183
|
var parentNode = node.ParentNode;
|
184
|
int nodeIndex = parentNode.ChildNodes.IndexOf(node);
|
185
|
parentNode.ChildNodes.RemoveAt(nodeIndex);
|
186
|
|
187
|
int spanSelectedStart = start;
|
188
|
int spanAfterStart = start + textSelected.Length;
|
189
|
|
190
|
HtmlNode spanSelected = CreateSpan(docToEdit, textSelected, TagStartPositions.Count, tag.Instance, tag.Id, spanSelectedStart, EPosition.MARK_RIGHT);
|
191
|
parentNode.ChildNodes.Insert(nodeIndex, spanSelected);
|
192
|
|
193
|
HtmlNode spanAfter = CreateSpan(docToEdit, textAfter, TagStartPositions.Count, null, null, spanAfterStart);
|
194
|
parentNode.ChildNodes.Insert(nodeIndex + 1, spanAfter);
|
195
|
|
196
|
return new() { spanSelected, spanAfter };
|
197
|
}
|
198
|
|
199
|
private List<HtmlNode> SolveLeftGap(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit,
|
200
|
AnnotationTagGeneric tag)
|
201
|
{
|
202
|
// partial fill, start gap
|
203
|
string text = node.InnerText;
|
204
|
string textBefore = text.Substring(0, selectionStart - start);
|
205
|
string textSelected = text.Substring(selectionStart - start, Math.Min(tag.Length, text.Length - textBefore.Length));
|
206
|
|
207
|
var parentNode = node.ParentNode;
|
208
|
int nodeIndex = parentNode.ChildNodes.IndexOf(node);
|
209
|
parentNode.ChildNodes.RemoveAt(nodeIndex);
|
210
|
|
211
|
int spanBeforeStart = start;
|
212
|
int spanSelectedStart = start + textBefore.Length;
|
213
|
|
214
|
HtmlNode spanBefore = CreateSpan(docToEdit, textBefore, TagStartPositions.Count, null, null, spanBeforeStart);
|
215
|
parentNode.ChildNodes.Insert(nodeIndex, spanBefore);
|
216
|
|
217
|
HtmlNode spanSelected = CreateSpan(docToEdit, textSelected, TagStartPositions.Count, tag.Instance, tag.Id, spanSelectedStart, EPosition.MARK_LEFT);
|
218
|
parentNode.ChildNodes.Insert(nodeIndex + 1, spanSelected);
|
219
|
|
220
|
return new() { spanSelected, spanBefore };
|
221
|
}
|
222
|
|
223
|
private List<HtmlNode> SolveLeftRightGap(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit,
|
224
|
AnnotationTagGeneric tag)
|
225
|
{
|
226
|
// partial fill, start gap end gap
|
227
|
string text = node.InnerText;
|
228
|
string textBefore = text.Substring(0, selectionStart - start);
|
229
|
string textAfter = text.Substring(selectionStart - start + tag.Length);
|
230
|
string textSelected = text.Substring(selectionStart - start, tag.Length);
|
231
|
|
232
|
var parentNode = node.ParentNode;
|
233
|
int nodeIndex = parentNode.ChildNodes.IndexOf(node);
|
234
|
parentNode.ChildNodes.RemoveAt(nodeIndex);
|
235
|
|
236
|
int spanBeforeStart = start;
|
237
|
int spanSelectedStart = start + textBefore.Length;
|
238
|
int spanAfterStart = start + textBefore.Length + textSelected.Length;
|
239
|
|
240
|
HtmlNode spanBefore = CreateSpan(docToEdit, textBefore, TagStartPositions.Count, null, null, spanBeforeStart);
|
241
|
parentNode.ChildNodes.Insert(nodeIndex, spanBefore);
|
242
|
|
243
|
HtmlNode spanSelected = CreateSpan(docToEdit, textSelected, TagStartPositions.Count, tag.Instance, tag.Id, spanSelectedStart, EPosition.MARK_LEFT_RIGHT);
|
244
|
parentNode.ChildNodes.Insert(nodeIndex + 1, spanSelected);
|
245
|
|
246
|
HtmlNode spanAfter = CreateSpan(docToEdit, textAfter, TagStartPositions.Count, null, null, spanAfterStart);
|
247
|
parentNode.ChildNodes.Insert(nodeIndex + 2, spanAfter);
|
248
|
|
249
|
return new() { spanSelected, spanBefore, spanAfter };
|
250
|
}
|
251
|
|
252
|
private HtmlNode CreateSpan(HtmlDocument doc, string text, int tagId, Guid? instanceId, Guid? entityId, int startPosition, EPosition position = EPosition.MARK_NONE)
|
253
|
{
|
254
|
HtmlNode span = doc.CreateElement("span");
|
255
|
span.InnerHtml = text;
|
256
|
TagStartPositions.Add(startPosition);
|
257
|
TagStartLengths.Add(0);
|
258
|
TagClosingPositions.Add(startPosition + text.Length);
|
259
|
TagClosingLengths.Add(0);
|
260
|
span.Attributes.Add(TAG_ID_ATTRIBUTE_NAME, tagId.ToString());
|
261
|
|
262
|
if (instanceId != null)
|
263
|
{
|
264
|
span.AddClass("annotation");
|
265
|
span.Attributes.Add(TAG_INSTANCE_ATTRIBUTE_NAME, instanceId.Value.ToString());
|
266
|
span.Attributes.Add(TAG_EF_ID_ATTRIBUTE_NAME, entityId.ToString());
|
267
|
|
268
|
if (position == EPosition.MARK_LEFT || position == EPosition.MARK_LEFT_RIGHT)
|
269
|
{
|
270
|
span.Attributes.Add("start", "1");
|
271
|
}
|
272
|
if (position == EPosition.MARK_RIGHT || position == EPosition.MARK_LEFT_RIGHT)
|
273
|
{
|
274
|
span.Attributes.Add("end", "1");
|
275
|
}
|
276
|
}
|
277
|
|
278
|
return span;
|
279
|
}
|
280
|
|
281
|
private enum EPosition
|
282
|
{
|
283
|
MARK_LEFT = 5,
|
284
|
MARK_RIGHT = 3,
|
285
|
MARK_LEFT_RIGHT = 2,
|
286
|
MARK_NONE = 0
|
287
|
}
|
288
|
|
289
|
private void ModifyLinks(IEnumerable<HtmlNode> descendantsOriginal)
|
290
|
{
|
291
|
foreach (var descendant in descendantsOriginal)
|
292
|
{
|
293
|
if (descendant.Name == "a")
|
294
|
{
|
295
|
if (descendant.Attributes.Contains("href"))
|
296
|
{
|
297
|
descendant.SetAttributeValue("href", "/link?url=" + descendant.Attributes["href"].Value);
|
298
|
descendant.SetAttributeValue("target", "_blank");
|
299
|
}
|
300
|
}
|
301
|
}
|
302
|
}
|
303
|
|
304
|
private void WrapTextInSpan(IEnumerable<HtmlNode> descendantsOriginal, HtmlDocument docToEdit)
|
305
|
{
|
306
|
// Special case for non-html documents
|
307
|
if (descendantsOriginal.Count() == 2)
|
308
|
{
|
309
|
var documentNode = descendantsOriginal.ElementAt(0);
|
310
|
var childNode = descendantsOriginal.ElementAt(1);
|
311
|
if (documentNode.Name == "#document" && childNode.Name == "#text")
|
312
|
{
|
313
|
HtmlNode coveringSpan = docToEdit.CreateElement("span");
|
314
|
coveringSpan.InnerHtml = childNode.InnerHtml;
|
315
|
TagStartPositions.Add(childNode.InnerStartIndex);
|
316
|
TagStartLengths.Add(0);
|
317
|
TagClosingPositions.Add(childNode.InnerStartIndex + childNode.InnerLength);
|
318
|
TagClosingLengths.Add(0);
|
319
|
coveringSpan.Attributes.Add(TAG_ID_ATTRIBUTE_NAME, (TagStartPositions.Count - 1).ToString());
|
320
|
|
321
|
var parent = NodeDict[documentNode];
|
322
|
|
323
|
parent.ChildNodes.RemoveAt(0);
|
324
|
parent.ChildNodes.Add(coveringSpan);
|
325
|
|
326
|
return;
|
327
|
}
|
328
|
}
|
329
|
|
330
|
foreach (var node in descendantsOriginal)
|
331
|
{
|
332
|
var originalNode = node;
|
333
|
var toEditNode = NodeDict[node];
|
334
|
|
335
|
if (originalNode.Name.Contains("#"))
|
336
|
{
|
337
|
continue;
|
338
|
}
|
339
|
else
|
340
|
{
|
341
|
bool onlyText = true;
|
342
|
bool onlySubtags = true;
|
343
|
|
344
|
foreach (var child in node.ChildNodes)
|
345
|
{
|
346
|
if (child.Name.Contains("#"))
|
347
|
{
|
348
|
onlySubtags = false;
|
349
|
}
|
350
|
else
|
351
|
{
|
352
|
onlyText = false;
|
353
|
}
|
354
|
}
|
355
|
|
356
|
if (onlyText || onlySubtags)
|
357
|
{
|
358
|
continue;
|
359
|
}
|
360
|
else
|
361
|
{
|
362
|
|
363
|
foreach (var child in node.ChildNodes)
|
364
|
{
|
365
|
if (child.Name.Contains("#text"))
|
366
|
{
|
367
|
HtmlNode coveringSpan = docToEdit.CreateElement("span");
|
368
|
coveringSpan.InnerHtml = child.InnerHtml;
|
369
|
TagStartPositions.Add(child.InnerStartIndex);
|
370
|
TagStartLengths.Add(0);
|
371
|
TagClosingPositions.Add(child.InnerStartIndex + child.InnerLength);
|
372
|
TagClosingLengths.Add(0);
|
373
|
coveringSpan.Attributes.Add(TAG_ID_ATTRIBUTE_NAME, (TagStartPositions.Count - 1).ToString());
|
374
|
|
375
|
var parent = NodeDict[node];
|
376
|
var index = parent.ChildNodes.IndexOf(NodeDict[child]);
|
377
|
|
378
|
parent.ChildNodes.RemoveAt(index);
|
379
|
parent.ChildNodes.Insert(index, coveringSpan);
|
380
|
}
|
381
|
}
|
382
|
}
|
383
|
}
|
384
|
}
|
385
|
}
|
386
|
|
387
|
private void GenerateCSS(List<AnnotationTagGeneric> tags)
|
388
|
{
|
389
|
/*string inner = "span.annotation {border-bottom: 2px solid;}";
|
390
|
inner += "span {line-height: 30px}\n";*/
|
391
|
|
392
|
var tagPaddingDict = Intersections.ColorGraph(Intersections.FindIntersections(tags));
|
393
|
foreach (var tag in tags.DistinctBy(t => t.Instance))
|
394
|
{
|
395
|
var padding = (tagPaddingDict[tag] + 1) * 2;
|
396
|
TagInstanceCSS.Add(new()
|
397
|
{
|
398
|
InstanceId = tag.Instance,
|
399
|
Color = tag.Tag.Color,
|
400
|
Padding = padding
|
401
|
});
|
402
|
}
|
403
|
}
|
404
|
|
405
|
private void AssignIdsToOriginalDocument(IEnumerable<HtmlNode> descendantsOriginal, ref int currentId)
|
406
|
{
|
407
|
foreach (var node in descendantsOriginal)
|
408
|
{
|
409
|
var originalNode = node;
|
410
|
var toEditNode = NodeDict[node];
|
411
|
|
412
|
if (originalNode.Name.Contains("#"))
|
413
|
{
|
414
|
continue;
|
415
|
}
|
416
|
else
|
417
|
{
|
418
|
TagStartPositions.Add(originalNode.OuterStartIndex);
|
419
|
TagStartLengths.Add(originalNode.InnerStartIndex - originalNode.OuterStartIndex);
|
420
|
currentId = TagStartPositions.Count - 1;
|
421
|
toEditNode.Attributes.Add(TAG_ID_ATTRIBUTE_NAME, currentId.ToString());
|
422
|
|
423
|
TagClosingPositions.Add(originalNode.InnerStartIndex + originalNode.InnerLength);
|
424
|
TagClosingLengths.Add((originalNode.OuterStartIndex + originalNode.OuterLength) - (originalNode.InnerStartIndex + originalNode.InnerLength));
|
425
|
}
|
426
|
}
|
427
|
}
|
428
|
|
429
|
private void FillNodeDict(IEnumerable<HtmlNode> descendantsOriginal, IEnumerable<HtmlNode> descendantsToEdit)
|
430
|
{
|
431
|
var zipped = descendantsOriginal.Zip(descendantsToEdit, (orig, toEdit) => new
|
432
|
{
|
433
|
Original = orig,
|
434
|
ToEdit = toEdit
|
435
|
});
|
436
|
foreach (var node in zipped)
|
437
|
{
|
438
|
var originalNode = node.Original;
|
439
|
var toEditNode = node.ToEdit;
|
440
|
NodeDict.Add(originalNode, toEditNode);
|
441
|
}
|
442
|
}
|
443
|
|
444
|
|
445
|
/*
|
446
|
* Full HTML Preprocessing -------------------------------------------------------------------------------
|
447
|
*/
|
448
|
|
449
|
/*
|
450
|
* Partial HTML Preprocessing ----------------------------------------------------------------------------
|
451
|
*/
|
452
|
|
453
|
public (string, CachedInfo) PartialPreprocessHTMLAddTag(string htmlToEdit, string htmlOriginal, AnnotationTag tagToAdd, List<AnnotationTagGeneric> tags, CachedInfo cachedInfo)
|
454
|
{
|
455
|
UnpackCachedInfo(cachedInfo);
|
456
|
|
457
|
var docOriginal = new HtmlDocument();
|
458
|
docOriginal.LoadHtml(htmlOriginal);
|
459
|
var docToEdit = new HtmlDocument();
|
460
|
docToEdit.LoadHtml(htmlToEdit);
|
461
|
|
462
|
var descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
|
463
|
|
464
|
int i = 0;
|
465
|
List<HtmlNode> addedForSelection = new();
|
466
|
|
467
|
int descendantsCount = descendantsToEdit.Count();
|
468
|
while (i < descendantsCount)
|
469
|
{
|
470
|
for (; i < descendantsCount; i++)
|
471
|
{
|
472
|
var node = descendantsToEdit.ElementAt(i);
|
473
|
if (!node.Name.Contains("#text") || addedForSelection.Contains(node) || addedForSelection.Contains(node.ParentNode) ||
|
474
|
node.ParentNode.Name == "style" || node.ParentNode.Name.StartsWith("#"))
|
475
|
{
|
476
|
continue;
|
477
|
}
|
478
|
|
479
|
int nodeId = node.ParentNode.GetAttributeValue(TAG_ID_ATTRIBUTE_NAME, -1);
|
480
|
|
481
|
var start = TagStartPositions[nodeId] + TagStartLengths[nodeId];
|
482
|
var end = TagClosingPositions[nodeId];
|
483
|
|
484
|
int selectionStart = tagToAdd.Position;
|
485
|
int selectionEnd = tagToAdd.Position + tagToAdd.Length;
|
486
|
|
487
|
if (selectionStart < end && selectionEnd > start)
|
488
|
{
|
489
|
if (selectionStart <= start && selectionEnd >= end)
|
490
|
{
|
491
|
addedForSelection.Add(SolveFullFill(node, selectionStart, selectionEnd, start, end, docToEdit, tagToAdd));
|
492
|
}
|
493
|
else if (selectionStart <= start)
|
494
|
{
|
495
|
addedForSelection.AddRange(SolveRightGap(node, selectionStart, selectionEnd, start, end, docToEdit, tagToAdd));
|
496
|
}
|
497
|
else if (selectionEnd >= end)
|
498
|
{
|
499
|
addedForSelection.AddRange(SolveLeftGap(node, selectionStart, selectionEnd, start, end, docToEdit, tagToAdd));
|
500
|
}
|
501
|
else
|
502
|
{
|
503
|
addedForSelection.AddRange(SolveLeftRightGap(node, selectionStart, selectionEnd, start, end, docToEdit, tagToAdd));
|
504
|
}
|
505
|
descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
|
506
|
descendantsCount = descendantsToEdit.Count();
|
507
|
break;
|
508
|
}
|
509
|
}
|
510
|
}
|
511
|
|
512
|
GenerateCSS(tags);
|
513
|
return (docToEdit.DocumentNode.OuterHtml, new()
|
514
|
{
|
515
|
TagStartPositions = TagStartPositions,
|
516
|
TagStartLengths = TagStartLengths,
|
517
|
TagClosingPositions = TagClosingPositions,
|
518
|
TagClosingLengths = TagClosingLengths,
|
519
|
TagInstanceCSS = TagInstanceCSS,
|
520
|
NodeDict = NodeDict
|
521
|
});
|
522
|
}
|
523
|
|
524
|
|
525
|
public (string, CachedInfo) PartialPreprocessHTMLRemoveTag(string htmlToEdit, string htmlOriginal, AnnotationTag tagToRemove, List<AnnotationTagGeneric> tags, CachedInfo cachedInfo)
|
526
|
{
|
527
|
UnpackCachedInfo(cachedInfo);
|
528
|
|
529
|
var docOriginal = new HtmlDocument();
|
530
|
docOriginal.LoadHtml(htmlOriginal);
|
531
|
var docToEdit = new HtmlDocument();
|
532
|
docToEdit.LoadHtml(htmlToEdit);
|
533
|
|
534
|
var descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
|
535
|
|
536
|
int i = 0;
|
537
|
int descendantsCount = descendantsToEdit.Count();
|
538
|
while (i < descendantsCount)
|
539
|
{
|
540
|
for (; i < descendantsCount; i++)
|
541
|
{
|
542
|
var node = descendantsToEdit.ElementAt(i);
|
543
|
if (!node.Attributes.Contains(TAG_EF_ID_ATTRIBUTE_NAME))
|
544
|
{
|
545
|
continue;
|
546
|
}
|
547
|
else
|
548
|
{
|
549
|
if (node.Attributes[TAG_EF_ID_ATTRIBUTE_NAME].Value != tagToRemove.Id.ToString())
|
550
|
{
|
551
|
continue;
|
552
|
}
|
553
|
|
554
|
node.Attributes.Remove(TAG_EF_ID_ATTRIBUTE_NAME);
|
555
|
node.Attributes.Remove(TAG_INSTANCE_ATTRIBUTE_NAME);
|
556
|
node.Attributes.Remove("class");
|
557
|
node.Attributes.Remove("start");
|
558
|
node.Attributes.Remove("end");
|
559
|
|
560
|
descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
|
561
|
descendantsCount = descendantsToEdit.Count();
|
562
|
break;
|
563
|
}
|
564
|
}
|
565
|
}
|
566
|
|
567
|
GenerateCSS(tags);
|
568
|
return (docToEdit.DocumentNode.OuterHtml, new()
|
569
|
{
|
570
|
TagStartPositions = TagStartPositions,
|
571
|
TagStartLengths = TagStartLengths,
|
572
|
TagClosingPositions = TagClosingPositions,
|
573
|
TagClosingLengths = TagClosingLengths,
|
574
|
TagInstanceCSS = TagInstanceCSS,
|
575
|
NodeDict = NodeDict
|
576
|
});
|
577
|
}
|
578
|
|
579
|
/*
|
580
|
* Partial HTML Preprocessing ----------------------------------------------------------------------------
|
581
|
*/
|
582
|
}
|
583
|
}
|