Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 2c9afc72

Přidáno uživatelem Vojtěch Bartička před asi 2 roky(ů)

Moved HTML processing to HTMLService, inheritance changes in entities

Zobrazit rozdíly:

Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
24 24
        private readonly DatabaseContext context;
25 25
        private readonly ILogger logger;
26 26
        private readonly IMapper mapper;
27
        private readonly IHTMLService htmlService;
27 28

  
28
        private const string TAG_ID_ATTRIBUTE_NAME = "aswi-tag-id";
29
        private const string TAG_INSTANCE_ATTRIBUTE_NAME = "aswi-tag-instance";
30
        private const string TAG_EF_ID_ATTRIBUTE_NAME = "aswi-tag-ef-id";
31 29

  
32
        public AnnotationServiceEF(DatabaseContext context, ILogger logger, IMapper mapper)
30
        public AnnotationServiceEF(DatabaseContext context, ILogger logger, IMapper mapper, IHTMLService htmlService)
33 31
        {
34 32
            this.context = context;
35 33
            this.logger = logger;
36 34
            this.mapper = mapper;
35
            this.htmlService = htmlService;
37 36
        }
38 37

  
39 38
        public void CreateDocumentAnnotations(AnnotationsAddRequest request, Guid clientUserId)
......
145 144
                .ThenInclude(t => t.Category)
146 145
                .Include(at => at.SubTag)
147 146
                .OrderBy(at => at.Position)
147
                .Select(at => at as AnnotationTagGeneric)
148 148
                .ToList();
149 149

  
150 150
            List<TagInstanceInfo> tagInstanceInfos = new();
......
155 155
            }
156 156

  
157 157
            var docToRender = "";
158
            HTMLService.CachedInfo cachedInfoToReturn = new(); 
158 159
            if (annotation.CachedDocumentHTML == "")
159 160
            {
160
                docToRender = FullPreprocessHTML(documentContent.Content, tags);
161
                annotation.CachedStartPositions = JsonConvert.SerializeObject(TagStartPositions);
162
                annotation.CachedLengths = JsonConvert.SerializeObject(TagStartLengths);
163
                annotation.CachedClosingPositions = JsonConvert.SerializeObject(TagClosingPositions);
164
                annotation.CachedClosingLengths = JsonConvert.SerializeObject(TagClosingLengths);
165
                annotation.CachedCSS = JsonConvert.SerializeObject(TagInstanceCSS);
161
                var result = htmlService.FullPreprocessHTML(documentContent.Content, tags);
162
                cachedInfoToReturn = result.Item2;
163
                docToRender = result.Item1;
164

  
165
                annotation.CachedStartPositions = JsonConvert.SerializeObject(cachedInfoToReturn.TagStartPositions);
166
                annotation.CachedLengths = JsonConvert.SerializeObject(cachedInfoToReturn.TagStartLengths);
167
                annotation.CachedClosingPositions = JsonConvert.SerializeObject(cachedInfoToReturn.TagClosingPositions);
168
                annotation.CachedClosingLengths = JsonConvert.SerializeObject(cachedInfoToReturn.TagClosingLengths);
169
                annotation.CachedCSS = JsonConvert.SerializeObject(cachedInfoToReturn.TagInstanceCSS);
166 170
                annotation.ModifiedType = EModified.NONE;
167 171
                annotation.CachedDocumentHTML = docToRender;
168 172
                context.SaveChanges();
......
170 174
            else
171 175
            {
172 176
                docToRender = annotation.CachedDocumentHTML;
173
                TagStartPositions = JsonConvert.DeserializeObject<List<int>>(annotation.CachedStartPositions);
174
                TagStartLengths = JsonConvert.DeserializeObject<List<int>>(annotation.CachedLengths);
175
                TagClosingPositions = JsonConvert.DeserializeObject<List<int>>(annotation.CachedClosingPositions);
176
                TagClosingLengths = JsonConvert.DeserializeObject<List<int>>(annotation.CachedClosingLengths);
177
                //TagInstanceCSS = JsonConvert.DeserializeObject<List<TagInstanceCSSInfo>>(annotation.CachedCSS);
178

  
177
                HTMLService.CachedInfo cachedInfo = new()
178
                {
179
                    TagStartPositions = JsonConvert.DeserializeObject<List<int>>(annotation.CachedStartPositions),
180
                    TagStartLengths = JsonConvert.DeserializeObject<List<int>>(annotation.CachedLengths),
181
                    TagClosingPositions = JsonConvert.DeserializeObject<List<int>>(annotation.CachedClosingPositions),
182
                    TagClosingLengths = JsonConvert.DeserializeObject<List<int>>(annotation.CachedClosingLengths)
183
                };
184
                
179 185
                // The annotation has been modified and we need to either add the new tag or remove the tag
180 186
                if (annotation.ModifiedType != EModified.NONE)
181 187
                {
182 188
                    if (annotation.ModifiedType == EModified.ADDED)
183 189
                    {
184 190
                        var lastModifiedTag = context.AnnotationTags.Where(at => at.Id == annotation.LastModifiedTagId).First();
185
                        docToRender = PartialPreprocessHTMLAddTag(docToRender, documentContent.Content, lastModifiedTag, tags);
191
                        var result = htmlService.PartialPreprocessHTMLAddTag(docToRender, documentContent.Content, lastModifiedTag, tags, cachedInfo);
192
                        docToRender = result.Item1;
193
                        cachedInfoToReturn = result.Item2;
186 194
                    }
187 195
                    else if (annotation.ModifiedType == EModified.REMOVED)
188 196
                    {
189
                        docToRender = PartialPreprocessHTMLRemoveTag(docToRender, documentContent.Content, new AnnotationTag() { Id = annotation.LastModifiedTagId.Value }, tags);
197
                        var result = htmlService.PartialPreprocessHTMLRemoveTag(docToRender, documentContent.Content, new AnnotationTag() { Id = annotation.LastModifiedTagId.Value }, tags, cachedInfo);
198
                        docToRender = result.Item1;
199
                        cachedInfoToReturn = result.Item2;
190 200
                    }
191 201

  
192 202
                    annotation.ModifiedType = EModified.NONE;
193
                    annotation.CachedStartPositions = JsonConvert.SerializeObject(TagStartPositions);
194
                    annotation.CachedLengths = JsonConvert.SerializeObject(TagStartLengths);
195
                    annotation.CachedClosingPositions = JsonConvert.SerializeObject(TagClosingPositions);
196
                    annotation.CachedClosingLengths = JsonConvert.SerializeObject(TagClosingLengths);
203
                    annotation.CachedStartPositions = JsonConvert.SerializeObject(cachedInfoToReturn.TagStartPositions);
204
                    annotation.CachedLengths = JsonConvert.SerializeObject(cachedInfoToReturn.TagStartLengths);
205
                    annotation.CachedClosingPositions = JsonConvert.SerializeObject(cachedInfoToReturn.TagClosingPositions);
206
                    annotation.CachedClosingLengths = JsonConvert.SerializeObject(cachedInfoToReturn.TagClosingLengths);
197 207
                    annotation.CachedDocumentHTML = docToRender;
198
                    annotation.CachedCSS = JsonConvert.SerializeObject(TagInstanceCSS);
208
                    annotation.CachedCSS = JsonConvert.SerializeObject(cachedInfoToReturn.TagInstanceCSS);
199 209
                    context.SaveChanges();
200 210
                }
201 211
            }
......
205 215
            {
206 216
                SourceDocumentContent = documentContent.Content,
207 217
                DocumentToRender = docToRender,
208
                TagStartPositions = TagStartPositions.ToArray(),
209
                TagLengths = TagStartLengths.ToArray(),
218
                TagStartPositions = cachedInfoToReturn.TagStartPositions.ToArray(),
219
                TagLengths = cachedInfoToReturn.TagStartLengths.ToArray(),
210 220
                Note = annotation.Note,
211 221
                State = annotation.State,
212 222
                Type = IsHtml(documentContent.Content) ? EDocumentType.HTML : EDocumentType.TEXT,
213 223
                TagInstances = tagInstanceInfos,
214
                CSSInfo = TagInstanceCSS
224
                CSSInfo = cachedInfoToReturn.TagInstanceCSS
215 225
            };
216 226

  
217
            NodeDict.Clear();
218

  
219 227
            return annotationInfo;
220 228
        }
221 229

  
222
        private List<int> TagStartPositions = new();
223
        private List<int> TagStartLengths = new();
224
        private List<int> TagClosingPositions = new();
225
        private List<int> TagClosingLengths = new();
226
        private Dictionary<HtmlNode, HtmlNode> NodeDict = new();
227
        private List<TagInstanceCSSInfo> TagInstanceCSS = new();
228

  
229
        /*
230
         *      Full HTML Preprocessing -------------------------------------------------------------------------------
231
         */
232

  
233

  
234
        private string FullPreprocessHTML(string htmlSource, List<AnnotationTag> tags)
235
        {
236
            var docOriginal = new HtmlDocument();
237
            docOriginal.LoadHtml(htmlSource);
238
            var docToEdit = new HtmlDocument();
239
            docToEdit.LoadHtml(htmlSource);
240

  
241
            var descendantsOriginal = docOriginal.DocumentNode.DescendantsAndSelf();
242
            var descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
243

  
244
            int currentId = 0;
245

  
246
            FillNodeDict(descendantsOriginal, descendantsToEdit);
247
            AssignIdsToOriginalDocument(descendantsOriginal, ref currentId);
248

  
249
            WrapTextInSpan(descendantsOriginal, docToEdit);
250
            int descCount = descendantsToEdit.Count;
251

  
252
            foreach (var tag in tags)
253
            {
254
                int i = 0;
255
                List<HtmlNode> addedForSelection = new();
256
                while (i < descCount)
257
                {
258
                    for (; i < descCount; i++)
259
                    {
260
                        var node = descendantsToEdit.ElementAt(i);
261
                        if (!node.Name.Contains("#text") || addedForSelection.Contains(node) || addedForSelection.Contains(node.ParentNode) || node.ParentNode.Name == "style")
262
                        {
263
                            continue;
264
                        }
265

  
266
                        int nodeId = node.ParentNode.GetAttributeValue(TAG_ID_ATTRIBUTE_NAME, -1);
267

  
268
                        var start = TagStartPositions[nodeId] + TagStartLengths[nodeId];
269
                        var end = TagClosingPositions[nodeId];
270

  
271
                        int selectionStart = tag.Position;
272
                        int selectionEnd = tag.Position + tag.Length;
273

  
274
                        if (selectionStart < end && selectionEnd > start)
275
                        {
276
                            if (selectionStart <= start && selectionEnd >= end)
277
                            {
278
                                addedForSelection.Add(SolveFullFill(node, selectionStart, selectionEnd, start, end, docToEdit, tag));
279
                            }
280
                            else if (selectionStart <= start)
281
                            {
282
                                addedForSelection.AddRange(SolveRightGap(node, selectionStart, selectionEnd, start, end, docToEdit, tag));
283
                            }
284
                            else if (selectionEnd >= end)
285
                            {
286
                                addedForSelection.AddRange(SolveLeftGap(node, selectionStart, selectionEnd, start, end, docToEdit, tag));
287
                            }
288
                            else
289
                            {
290
                                addedForSelection.AddRange(SolveLeftRightGap(node, selectionStart, selectionEnd, start, end, docToEdit, tag));
291
                            }
292
                            descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
293
                            descCount = descendantsToEdit.Count;
294
                            break;
295
                        }
296
                    }
297
                }
298

  
299
            }
300

  
301
            ModifyLinks(descendantsToEdit);
302
            string docToRender = docToEdit.DocumentNode.OuterHtml;
303
            HtmlSanitizer sanitizer = new HtmlSanitizer();
304
            sanitizer.AllowedAttributes.Clear();
305
            sanitizer.AllowedAttributes.Add(TAG_ID_ATTRIBUTE_NAME);
306
            sanitizer.AllowedAttributes.Add(TAG_INSTANCE_ATTRIBUTE_NAME);
307
            sanitizer.AllowedAttributes.Add("href");
308
            sanitizer.AllowedAttributes.Add("end");
309
            sanitizer.AllowedAttributes.Add("start");
310
            sanitizer.AllowedAttributes.Add("class");
311
            sanitizer.AllowedAttributes.Add("target");
312
            if (sanitizer.AllowedTags.Contains("script"))
313
            {
314
                sanitizer.AllowedTags.Remove("script");
315
            }
316
            if (!sanitizer.AllowedTags.Contains("style"))
317
            {
318
                sanitizer.AllowedTags.Add("style");
319
            }
320
            sanitizer.AllowedTags.Add("a");
321
            docToRender = sanitizer.Sanitize(docToRender);
322
            GenerateCSS(tags);
323
            return docToRender;
324
        }
325

  
326
        private HtmlNode SolveFullFill(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit, AnnotationTag tag)
327
        {
328
            // full fill
329
            string textSelected = node.InnerText;
330

  
331
            var parentNode = node.ParentNode;
332
            int nodeIndex = parentNode.ChildNodes.IndexOf(node);
333
            parentNode.ChildNodes.RemoveAt(nodeIndex);
334

  
335
            EPosition markerPosition = EPosition.MARK_NONE;
336
            if (selectionEnd == end && selectionStart == start)
337
            {
338
                markerPosition = EPosition.MARK_LEFT_RIGHT;
339
            }
340

  
341
            HtmlNode spanSelected = CreateSpan(docToEdit, textSelected, TagStartPositions.Count, tag.Instance, tag.Id, start, markerPosition);
342
            parentNode.ChildNodes.Insert(nodeIndex, spanSelected);
343

  
344
            return spanSelected;
345
        }
346

  
347
        private List<HtmlNode> SolveRightGap(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit,
348
                                             AnnotationTag tag)
349
        {
350
            // partial fill, end gap
351
            string text = node.InnerText;
352
            string textAfter = text.Substring(Math.Min(selectionStart - start + tag.Length, text.Length));
353
            string textSelected = text.Substring(0, selectionEnd - start);
354

  
355
            var parentNode = node.ParentNode;
356
            int nodeIndex = parentNode.ChildNodes.IndexOf(node);
357
            parentNode.ChildNodes.RemoveAt(nodeIndex);
358

  
359
            int spanSelectedStart = start;
360
            int spanAfterStart = start + textSelected.Length;
361

  
362
            HtmlNode spanSelected = CreateSpan(docToEdit, textSelected, TagStartPositions.Count, tag.Instance, tag.Id, spanSelectedStart, EPosition.MARK_RIGHT);
363
            parentNode.ChildNodes.Insert(nodeIndex, spanSelected);
364

  
365
            HtmlNode spanAfter = CreateSpan(docToEdit, textAfter, TagStartPositions.Count, null, null, spanAfterStart);
366
            parentNode.ChildNodes.Insert(nodeIndex + 1, spanAfter);
367

  
368
            return new() { spanSelected, spanAfter };
369
        }
370

  
371
        private List<HtmlNode> SolveLeftGap(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit,
372
                                             AnnotationTag tag)
373
        {
374
            // partial fill, start gap
375
            string text = node.InnerText;
376
            string textBefore = text.Substring(0, selectionStart - start);
377
            string textSelected = text.Substring(selectionStart - start, Math.Min(tag.Length, text.Length - textBefore.Length));
378

  
379
            var parentNode = node.ParentNode;
380
            int nodeIndex = parentNode.ChildNodes.IndexOf(node);
381
            parentNode.ChildNodes.RemoveAt(nodeIndex);
382

  
383
            int spanBeforeStart = start;
384
            int spanSelectedStart = start + textBefore.Length;
385

  
386
            HtmlNode spanBefore = CreateSpan(docToEdit, textBefore, TagStartPositions.Count, null, null, spanBeforeStart);
387
            parentNode.ChildNodes.Insert(nodeIndex, spanBefore);
388

  
389
            HtmlNode spanSelected = CreateSpan(docToEdit, textSelected, TagStartPositions.Count, tag.Instance, tag.Id, spanSelectedStart, EPosition.MARK_LEFT);
390
            parentNode.ChildNodes.Insert(nodeIndex + 1, spanSelected);
391

  
392
            return new() { spanSelected, spanBefore };
393
        }
394

  
395
        private List<HtmlNode> SolveLeftRightGap(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit,
396
                                                 AnnotationTag tag)
397
        {
398
            // partial fill, start gap end gap
399
            string text = node.InnerText;
400
            string textBefore = text.Substring(0, selectionStart - start);
401
            string textAfter = text.Substring(selectionStart - start + tag.Length);
402
            string textSelected = text.Substring(selectionStart - start, tag.Length);
403

  
404
            var parentNode = node.ParentNode;
405
            int nodeIndex = parentNode.ChildNodes.IndexOf(node);
406
            parentNode.ChildNodes.RemoveAt(nodeIndex);
407

  
408
            int spanBeforeStart = start;
409
            int spanSelectedStart = start + textBefore.Length;
410
            int spanAfterStart = start + textBefore.Length + textSelected.Length;
411

  
412
            HtmlNode spanBefore = CreateSpan(docToEdit, textBefore, TagStartPositions.Count, null, null, spanBeforeStart);
413
            parentNode.ChildNodes.Insert(nodeIndex, spanBefore);
414

  
415
            HtmlNode spanSelected = CreateSpan(docToEdit, textSelected, TagStartPositions.Count, tag.Instance, tag.Id, spanSelectedStart, EPosition.MARK_LEFT_RIGHT);
416
            parentNode.ChildNodes.Insert(nodeIndex + 1, spanSelected);
417

  
418
            HtmlNode spanAfter = CreateSpan(docToEdit, textAfter, TagStartPositions.Count, null, null, spanAfterStart);
419
            parentNode.ChildNodes.Insert(nodeIndex + 2, spanAfter);
420

  
421
            return new() { spanSelected, spanBefore, spanAfter };
422
        }
423

  
424
        private HtmlNode CreateSpan(HtmlDocument doc, string text, int tagId, Guid? instanceId, Guid? entityId, int startPosition, EPosition position = EPosition.MARK_NONE)
425
        {
426
            HtmlNode span = doc.CreateElement("span");
427
            span.InnerHtml = text;
428
            TagStartPositions.Add(startPosition);
429
            TagStartLengths.Add(0);
430
            TagClosingPositions.Add(startPosition + text.Length);
431
            TagClosingLengths.Add(0);
432
            span.Attributes.Add(TAG_ID_ATTRIBUTE_NAME, tagId.ToString());
433

  
434
            if (instanceId != null)
435
            {
436
                span.AddClass("annotation");
437
                span.Attributes.Add(TAG_INSTANCE_ATTRIBUTE_NAME, instanceId.Value.ToString());
438
                span.Attributes.Add(TAG_EF_ID_ATTRIBUTE_NAME, entityId.ToString());
439

  
440
                if (position == EPosition.MARK_LEFT || position == EPosition.MARK_LEFT_RIGHT)
441
                {
442
                    span.Attributes.Add("start", "1");
443
                }
444
                if (position == EPosition.MARK_RIGHT || position == EPosition.MARK_LEFT_RIGHT)
445
                {
446
                    span.Attributes.Add("end", "1");
447
                }
448
            }
449

  
450
            return span;
451
        }
452

  
453
        private enum EPosition
454
        {
455
            MARK_LEFT = 5,
456
            MARK_RIGHT = 3,
457
            MARK_LEFT_RIGHT = 2,
458
            MARK_NONE = 0
459
        }
460

  
461
        private void ModifyLinks(IEnumerable<HtmlNode> descendantsOriginal)
462
        {
463
            foreach (var descendant in descendantsOriginal)
464
            {
465
                if (descendant.Name == "a")
466
                {
467
                    if (descendant.Attributes.Contains("href"))
468
                    {
469
                        descendant.SetAttributeValue("href", "/link?url=" + descendant.Attributes["href"].Value);
470
                        descendant.SetAttributeValue("target", "_blank");
471
                    }
472
                }
473
            }
474
        }
475

  
476
        private void WrapTextInSpan(IEnumerable<HtmlNode> descendantsOriginal, HtmlDocument docToEdit)
477
        {
478
            // Special case for non-html documents
479
            if (descendantsOriginal.Count() == 2)
480
            {
481
                var documentNode = descendantsOriginal.ElementAt(0);
482
                var childNode = descendantsOriginal.ElementAt(1);
483
                if (documentNode.Name == "#document" && childNode.Name == "#text")
484
                {
485
                    HtmlNode coveringSpan = docToEdit.CreateElement("span");
486
                    coveringSpan.InnerHtml = childNode.InnerHtml;
487
                    TagStartPositions.Add(childNode.InnerStartIndex);
488
                    TagStartLengths.Add(0);
489
                    TagClosingPositions.Add(childNode.InnerStartIndex + childNode.InnerLength);
490
                    TagClosingLengths.Add(0);
491
                    coveringSpan.Attributes.Add(TAG_ID_ATTRIBUTE_NAME, (TagStartPositions.Count - 1).ToString());
492

  
493
                    var parent = NodeDict[documentNode];
494

  
495
                    parent.ChildNodes.RemoveAt(0);
496
                    parent.ChildNodes.Add(coveringSpan);
497

  
498
                    return;
499
                }
500
            }
501

  
502
            foreach (var node in descendantsOriginal)
503
            {
504
                var originalNode = node;
505
                var toEditNode = NodeDict[node];
506

  
507
                if (originalNode.Name.Contains("#"))
508
                {
509
                    continue;
510
                }
511
                else
512
                {
513
                    bool onlyText = true;
514
                    bool onlySubtags = true;
515

  
516
                    foreach (var child in node.ChildNodes)
517
                    {
518
                        if (child.Name.Contains("#"))
519
                        {
520
                            onlySubtags = false;
521
                        }
522
                        else
523
                        {
524
                            onlyText = false;
525
                        }
526
                    }
527

  
528
                    if (onlyText || onlySubtags)
529
                    {
530
                        continue;
531
                    }
532
                    else
533
                    {
534

  
535
                        foreach (var child in node.ChildNodes)
536
                        {
537
                            if (child.Name.Contains("#text"))
538
                            {
539
                                HtmlNode coveringSpan = docToEdit.CreateElement("span");
540
                                coveringSpan.InnerHtml = child.InnerHtml;
541
                                TagStartPositions.Add(child.InnerStartIndex);
542
                                TagStartLengths.Add(0);
543
                                TagClosingPositions.Add(child.InnerStartIndex + child.InnerLength);
544
                                TagClosingLengths.Add(0);
545
                                coveringSpan.Attributes.Add(TAG_ID_ATTRIBUTE_NAME, (TagStartPositions.Count - 1).ToString());
546

  
547
                                var parent = NodeDict[node];
548
                                var index = parent.ChildNodes.IndexOf(NodeDict[child]);
549

  
550
                                parent.ChildNodes.RemoveAt(index);
551
                                parent.ChildNodes.Insert(index, coveringSpan);
552
                            }
553
                        }
554
                    }
555
                }
556
            }
557
        }
558

  
559
        private void GenerateCSS(List<AnnotationTag> tags)
560
        {
561
            /*string inner = "span.annotation {border-bottom: 2px solid;}";
562
            inner += "span {line-height: 30px}\n";*/
563

  
564
            var tagPaddingDict = Intersections.ColorGraph(Intersections.FindIntersections(tags));
565
            foreach (var tag in tags.DistinctBy(t => t.Instance))
566
            {
567
                var padding = (tagPaddingDict[tag] + 1) * 2;
568
                TagInstanceCSS.Add(new()
569
                {
570
                    InstanceId = tag.Instance,
571
                    Color = tag.Tag.Color,
572
                    Padding = padding
573
                });
574
            }
575
        }
576

  
577
        private void AssignIdsToOriginalDocument(IEnumerable<HtmlNode> descendantsOriginal, ref int currentId)
578
        {
579
            foreach (var node in descendantsOriginal)
580
            {
581
                var originalNode = node;
582
                var toEditNode = NodeDict[node];
583

  
584
                if (originalNode.Name.Contains("#"))
585
                {
586
                    continue;
587
                }
588
                else
589
                {
590
                    TagStartPositions.Add(originalNode.OuterStartIndex);
591
                    TagStartLengths.Add(originalNode.InnerStartIndex - originalNode.OuterStartIndex);
592
                    currentId = TagStartPositions.Count - 1;
593
                    toEditNode.Attributes.Add(TAG_ID_ATTRIBUTE_NAME, currentId.ToString());
594

  
595
                    TagClosingPositions.Add(originalNode.InnerStartIndex + originalNode.InnerLength);
596
                    TagClosingLengths.Add((originalNode.OuterStartIndex + originalNode.OuterLength) - (originalNode.InnerStartIndex + originalNode.InnerLength));
597
                }
598
            }
599
        }
600

  
601
        private void FillNodeDict(IEnumerable<HtmlNode> descendantsOriginal, IEnumerable<HtmlNode> descendantsToEdit)
602
        {
603
            var zipped = descendantsOriginal.Zip(descendantsToEdit, (orig, toEdit) => new
604
            {
605
                Original = orig,
606
                ToEdit = toEdit
607
            });
608
            foreach (var node in zipped)
609
            {
610
                var originalNode = node.Original;
611
                var toEditNode = node.ToEdit;
612
                NodeDict.Add(originalNode, toEditNode);
613
            }
614
        }
615

  
616

  
617
        /*
618
         *      Full HTML Preprocessing -------------------------------------------------------------------------------
619
         */
620

  
621
        /*
622
         *      Partial HTML Preprocessing ----------------------------------------------------------------------------
623
         */
624

  
625
        private string PartialPreprocessHTMLAddTag(string htmlToEdit, string htmlOriginal, AnnotationTag tagToAdd, List<AnnotationTag> tags)
626
        {
627
            var docOriginal = new HtmlDocument();
628
            docOriginal.LoadHtml(htmlOriginal);
629
            var docToEdit = new HtmlDocument();
630
            docToEdit.LoadHtml(htmlToEdit);
631

  
632
            var descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
633

  
634
            int i = 0;
635
            List<HtmlNode> addedForSelection = new();
636

  
637
            int descendantsCount = descendantsToEdit.Count();
638
            while (i < descendantsCount)
639
            {
640
                for (; i < descendantsCount; i++)
641
                {
642
                    var node = descendantsToEdit.ElementAt(i);
643
                    if (!node.Name.Contains("#text") || addedForSelection.Contains(node) || addedForSelection.Contains(node.ParentNode) ||
644
                        node.ParentNode.Name == "style" || node.ParentNode.Name.StartsWith("#"))
645
                    {
646
                        continue;
647
                    }
648

  
649
                    int nodeId = node.ParentNode.GetAttributeValue(TAG_ID_ATTRIBUTE_NAME, -1);
650

  
651
                    var start = TagStartPositions[nodeId] + TagStartLengths[nodeId];
652
                    var end = TagClosingPositions[nodeId];
653

  
654
                    int selectionStart = tagToAdd.Position;
655
                    int selectionEnd = tagToAdd.Position + tagToAdd.Length;
656

  
657
                    if (selectionStart < end && selectionEnd > start)
658
                    {
659
                        if (selectionStart <= start && selectionEnd >= end)
660
                        {
661
                            addedForSelection.Add(SolveFullFill(node, selectionStart, selectionEnd, start, end, docToEdit, tagToAdd));
662
                        }
663
                        else if (selectionStart <= start)
664
                        {
665
                            addedForSelection.AddRange(SolveRightGap(node, selectionStart, selectionEnd, start, end, docToEdit, tagToAdd));
666
                        }
667
                        else if (selectionEnd >= end)
668
                        {
669
                            addedForSelection.AddRange(SolveLeftGap(node, selectionStart, selectionEnd, start, end, docToEdit, tagToAdd));
670
                        }
671
                        else
672
                        {
673
                            addedForSelection.AddRange(SolveLeftRightGap(node, selectionStart, selectionEnd, start, end, docToEdit, tagToAdd));
674
                        }
675
                        descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
676
                        descendantsCount = descendantsToEdit.Count();
677
                        break;
678
                    }
679
                }
680
            }
681

  
682
            GenerateCSS(tags);
683
            return docToEdit.DocumentNode.OuterHtml;
684
        }
685

  
686

  
687
        private string PartialPreprocessHTMLRemoveTag(string htmlToEdit, string htmlOriginal, AnnotationTag tagToRemove, List<AnnotationTag> tags)
688
        {
689
            var docOriginal = new HtmlDocument();
690
            docOriginal.LoadHtml(htmlOriginal);
691
            var docToEdit = new HtmlDocument();
692
            docToEdit.LoadHtml(htmlToEdit);
693

  
694
            var descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
695

  
696
            int i = 0;
697
            int descendantsCount = descendantsToEdit.Count();
698
            while (i < descendantsCount)
699
            {
700
                for (; i < descendantsCount; i++)
701
                {
702
                    var node = descendantsToEdit.ElementAt(i);
703
                    if (!node.Attributes.Contains(TAG_EF_ID_ATTRIBUTE_NAME))
704
                    {
705
                        continue;
706
                    }
707
                    else
708
                    {
709
                        if (node.Attributes[TAG_EF_ID_ATTRIBUTE_NAME].Value != tagToRemove.Id.ToString())
710
                        {
711
                            continue;
712
                        }
713

  
714
                        node.Attributes.Remove(TAG_EF_ID_ATTRIBUTE_NAME);
715
                        node.Attributes.Remove(TAG_INSTANCE_ATTRIBUTE_NAME);
716
                        node.Attributes.Remove("class");
717
                        node.Attributes.Remove("start");
718
                        node.Attributes.Remove("end");
719

  
720
                        /*var parent = node.ParentNode;
721
                        var contents = node.ChildNodes;
722
                        int index = parent.ChildNodes.IndexOf(node);
723
                        parent.ChildNodes.RemoveAt(index);
724

  
725
                        List<HtmlNode> newChildren = new();
726
                        for (int j = 0; j < index; j++)
727
                        {
728
                            newChildren.Add(parent.ChildNodes[j]);
729
                        }
730
                        for (int j = 0; j < contents.Count; j++)
731
                        {
732
                            newChildren.Add(contents[j]);
733
                        }
734
                        for (int j = index; j < parent.ChildNodes.Count; j++)
735
                        {
736
                            newChildren.Add(parent.ChildNodes[j]);
737
                        }
738

  
739
                        parent.ChildNodes.Clear();
740
                        foreach (var child in newChildren) { parent.ChildNodes.Add(child); }*/
741

  
742
                        descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
743
                        descendantsCount = descendantsToEdit.Count();
744
                        break;
745
                    }
746
                }
747
            }
748

  
749
            GenerateCSS(tags);
750
            return docToEdit.DocumentNode.OuterHtml;
751
        }
752

  
753
        /*
754
         *      Partial HTML Preprocessing ----------------------------------------------------------------------------
755
         */
756

  
757

  
758 230
        // TODO temporary
759 231
        private bool IsHtml(string text)
760 232
        {

Také k dispozici: Unified diff