commit 4e12f0cc82298c00e496365941b6ba04e7f86cc6
Author: Vojtěch Bartička <aitakaitov@gmail.com>
Date:   Sun May 8 11:34:11 2022 +0200

    Caching generated CSS, returning it separate from HTML

diff --git a/Backend/Core/Entities/Annotation.cs b/Backend/Core/Entities/Annotation.cs
index e42f91f..dc1e52e 100644
--- a/Backend/Core/Entities/Annotation.cs
+++ b/Backend/Core/Entities/Annotation.cs
@@ -33,7 +33,7 @@ namespace Core.Entities
         public string CachedLengths { get; set; } = "";
         public string CachedClosingPositions { get; set; } = "";
         public string CachedClosingLengths { get; set; } = "";
-        //public string CachedNodeDict { get; set; } = "";
+        public string CachedCSS { get; set; } = "";
 
         public Guid? LastModifiedTagId { get; set; }
         public EModified ModifiedType { get; set; } = EModified.NONE;
diff --git a/Backend/Core/Entities/AnnotationClass.cs b/Backend/Core/Entities/AnnotationClass.cs
deleted file mode 100644
index 5314345..0000000
--- a/Backend/Core/Entities/AnnotationClass.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-﻿using System;
-using System.Collections.Generic;
-using System.ComponentModel.DataAnnotations;
-using System.ComponentModel.DataAnnotations.Schema;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Core.Entities
-{
-   /* public class AnnotationClass : BaseEntity
-    {
-        [ForeignKey("Class")]
-        public int ClassId { get; set; }
-        public Class Class { get; set; }
-
-        [ForeignKey("Annotation")]
-        public int AnnotationId { get; set; }
-        public Annotation Annotation { get; set; }
-    }*/
-}
diff --git a/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs b/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
index a9b9d6d..2e5adb5 100644
--- a/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
+++ b/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
@@ -162,7 +162,7 @@ namespace Core.Services.AnnotationService
                 annotation.CachedLengths = JsonConvert.SerializeObject(TagStartLengths);
                 annotation.CachedClosingPositions = JsonConvert.SerializeObject(TagClosingPositions);
                 annotation.CachedClosingLengths = JsonConvert.SerializeObject(TagClosingLengths);
-                //annotation.CachedNodeDict = JsonConvert.SerializeObject(NodeDict);
+                annotation.CachedCSS = JsonConvert.SerializeObject(TagInstanceCSS);
                 annotation.ModifiedType = EModified.NONE;
                 annotation.CachedDocumentHTML = docToRender;
                 context.SaveChanges();
@@ -222,6 +222,7 @@ namespace Core.Services.AnnotationService
         private List<int> TagClosingPositions = new();
         private List<int> TagClosingLengths = new();
         private Dictionary<HtmlNode, HtmlNode> NodeDict = new();
+        private List<TagInstanceCSSInfo> TagInstanceCSS = new();
 
         /*
          *      Full HTML Preprocessing -------------------------------------------------------------------------------
@@ -313,14 +314,9 @@ namespace Core.Services.AnnotationService
             {
                 sanitizer.AllowedTags.Add("style");
             }
-            docToRender = sanitizer.Sanitize(docToRender);
-
-            HtmlDocument doc = new HtmlDocument();
-            doc.LoadHtml(docToRender);
-            var cssNode = GenerateCSS(doc, tags);
-            doc.DocumentNode.ChildNodes.Insert(0, cssNode);
-
-            return doc.DocumentNode.OuterHtml;
+            docToRender = sanitizer.Sanitize(docToRender);        
+            GenerateCSS(tags);
+            return docToRender;
         }
 
         private HtmlNode SolveFullFill(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit, AnnotationTag tag)
@@ -541,26 +537,22 @@ namespace Core.Services.AnnotationService
             }
         }
 
-        private HtmlNode GenerateCSS(HtmlDocument docToEdit, List<AnnotationTag> tags)
+        private void GenerateCSS(List<AnnotationTag> tags)
         {
-            HtmlNode style = docToEdit.CreateElement("style");
-
-            string inner = "span.annotation {border-bottom: 2px solid;}";
-            inner += "span {line-height: 30px}\n";
+            /*string inner = "span.annotation {border-bottom: 2px solid;}";
+            inner += "span {line-height: 30px}\n";*/
 
             var tagPaddingDict = Intersections.ColorGraph(Intersections.FindIntersections(tags));
-
             foreach (var tag in tags)
             {
                 var padding = (tagPaddingDict[tag] + 1) * 2;
-                inner += $"span[{TAG_INSTANCE_ATTRIBUTE_NAME}=\"{tag.Instance}\"] {{ border-color:{tag.Tag.Color}; padding-bottom: {padding}px }}";
+                TagInstanceCSS.Add(new()
+                {
+                    InstanceId = tag.Instance,
+                    Color = tag.Tag.Color,
+                    Padding = padding
+                });             
             }
-
-            inner += "span[end=\"1\"] {border-end-end-radius: 0px; border-right: 1px solid darkgray}\n";
-            inner += "span[start=\"1\"] {border-end-start-radius: 0px; border-left: 1px solid darkgray}\n";
-
-            style.InnerHtml = inner;
-            return style;
         }
 
         private void AssignIdsToOriginalDocument(IEnumerable<HtmlNode> descendantsOriginal, ref int currentId)
@@ -619,7 +611,6 @@ namespace Core.Services.AnnotationService
             docToEdit.LoadHtml(htmlToEdit);
 
             var descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
-            RemoveCSS(docToEdit);
 
             int i = 0;
             List<HtmlNode> addedForSelection = new();
@@ -669,9 +660,7 @@ namespace Core.Services.AnnotationService
                 }
             }
 
-            var cssNode = GenerateCSS(docToEdit, tags);
-            docToEdit.DocumentNode.ChildNodes.Insert(0, cssNode);
-
+            GenerateCSS(tags);
             return docToEdit.DocumentNode.OuterHtml;
         }
 
@@ -684,7 +673,6 @@ namespace Core.Services.AnnotationService
             docToEdit.LoadHtml(htmlToEdit);
 
             var descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf().ToList();
-            RemoveCSS(docToEdit);
 
             int i = 0;
             int descendantsCount = descendantsToEdit.Count();
@@ -739,17 +727,10 @@ namespace Core.Services.AnnotationService
                 }
             }
 
-            var cssNode = GenerateCSS(docToEdit, tags);
-            docToEdit.DocumentNode.ChildNodes.Insert(0, cssNode);
-
+            GenerateCSS(tags);
             return docToEdit.DocumentNode.OuterHtml;
         }
 
-        private void RemoveCSS(HtmlDocument doc)
-        {
-            doc.DocumentNode.ChildNodes.RemoveAt(0);
-        }
-
         /*
          *      Partial HTML Preprocessing ----------------------------------------------------------------------------
          */
diff --git a/Backend/Models/Annotations/AnnotationInfo.cs b/Backend/Models/Annotations/AnnotationInfo.cs
index 68f232f..cce5eb6 100644
--- a/Backend/Models/Annotations/AnnotationInfo.cs
+++ b/Backend/Models/Annotations/AnnotationInfo.cs
@@ -18,5 +18,6 @@ namespace Models.Annotations
         public EDocumentType Type { get; set; }
         public string Note { get; set; }
         public List<TagInstanceInfo> TagInstances { get; set; } = new();
+        public List<TagInstanceCSSInfo> CSSInfo { get; set; } = new();
     }
 }
diff --git a/Backend/Models/Tags/TagInstanceCSSInfo.cs b/Backend/Models/Tags/TagInstanceCSSInfo.cs
new file mode 100644
index 0000000..7d4240d
--- /dev/null
+++ b/Backend/Models/Tags/TagInstanceCSSInfo.cs
@@ -0,0 +1,15 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Models.Tags
+{
+    public class TagInstanceCSSInfo
+    {
+        public Guid InstanceId { get; set; }
+        public string Color { get; set; }
+        public int Padding { get; set; }
+    }
+}
