Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.
C# annotationscbzcomicinfocomicscomics-readercross-platformcsharpepubepub-readerfreelinuxmangamedia-servermetadataopdsopds-feedpdfself-hostedubooquitywindows
EPUB CSS Parsing Issues (#690)
Joseph Milazzo60dd66f6ae28
diff --git a/API.Tests/Parser/BookParserTests.cs b/API.Tests/Parser/BookParserTests.csindex b33ef1f54..7f6975fe5 100644--- a/API.Tests/Parser/BookParserTests.cs+++ b/API.Tests/Parser/BookParserTests.cs@@ -17,5 +17,24 @@ namespace API.Tests.Parser{Assert.Equal(expected, API.Parser.Parser.ParseVolume(filename));}++ // [Theory]+ // [InlineData("@font-face{font-family:'syyskuu_repaleinen';src:url(data:font/opentype;base64,AAEAAAA", "@font-face{font-family:'syyskuu_repaleinen';src:url(data:font/opentype;base64,AAEAAAA")]+ // [InlineData("@font-face{font-family:'syyskuu_repaleinen';src:url('fonts/font.css')", "@font-face{font-family:'syyskuu_repaleinen';src:url('TEST/fonts/font.css')")]+ // public void ReplaceFontSrcUrl(string input, string expected)+ // {+ // var apiBase = "TEST/";+ // var actual = API.Parser.Parser.FontSrcUrlRegex.Replace(input, "$1" + apiBase + "$2" + "$3");+ // Assert.Equal(expected, actual);+ // }+ //+ // [Theory]+ // [InlineData("@import url('font.css');", "@import url('TEST/font.css');")]+ // public void ReplaceImportSrcUrl(string input, string expected)+ // {+ // var apiBase = "TEST/";+ // var actual = API.Parser.Parser.CssImportUrlRegex.Replace(input, "$1" + apiBase + "$2" + "$3");+ // Assert.Equal(expected, actual);+ // }}}diff --git a/API/Parser/Parser.cs b/API/Parser/Parser.csindex b219e264f..51a07a835 100644--- a/API/Parser/Parser.cs+++ b/API/Parser/Parser.cs@@ -24,11 +24,25 @@ namespace API.Parserprivate const RegexOptions MatchOptions =RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant;- public static readonly Regex FontSrcUrlRegex = new Regex(@"(src:url\(.{1})" + "([^\"']*)" + @"(.{1}\))",+ /// <summary>+ /// Matches against font-family css syntax. Does not match if url import has data: starting, as that is binary data+ /// </summary>+ /// <remarks>See here for some examples https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face</remarks>+ public static readonly Regex FontSrcUrlRegex = new Regex(@"(?<Start>(src:\s?)?url\((?!data:).(?!data:))" + "(?<Filename>(?!data:)[^\"']*)" + @"(?<End>.{1}\))",MatchOptions, RegexTimeout);- public static readonly Regex CssImportUrlRegex = new Regex("@import\\s([\"|']|url\\([\"|'])(?<Filename>[^'\"]+)[\"|']\\)?;",+ /// <summary>+ /// https://developer.mozilla.org/en-US/docs/Web/CSS/@import+ /// </summary>+ public static readonly Regex CssImportUrlRegex = new Regex("(@import\\s([\"|']|url\\([\"|']))(?<Filename>[^'\"]+)([\"|']\\)?);",+ MatchOptions | RegexOptions.Multiline, RegexTimeout);+ /// <summary>+ /// Misc css image references, like background-image: url(), border-image, or list-style-image+ /// </summary>+ /// Original prepend: (background|border|list-style)-image:\s?)?+ public static readonly Regex CssImageUrlRegex = new Regex(@"(url\((?!data:).(?!data:))" + "(?<Filename>(?!data:)[^\"']*)" + @"(.\))",MatchOptions, RegexTimeout);+private static readonly string XmlRegexExtensions = @"\.xml";private static readonly Regex ImageRegex = new Regex(ImageFileExtensions,MatchOptions, RegexTimeout);diff --git a/API/Services/BookService.cs b/API/Services/BookService.csindex a114b3bd6..4449b6834 100644--- a/API/Services/BookService.cs+++ b/API/Services/BookService.cs@@ -140,13 +140,18 @@ namespace API.Services}stylesheetHtml = stylesheetHtml.Insert(0, importBuilder.ToString());- stylesheetHtml =- Parser.Parser.CssImportUrlRegex.Replace(stylesheetHtml, "$1" + apiBase + prepend + "$2" + "$3");+ var importMatches = Parser.Parser.CssImportUrlRegex.Matches(stylesheetHtml);+ foreach (Match match in importMatches)+ {+ if (!match.Success) continue;+ var importFile = match.Groups["Filename"].Value;+ stylesheetHtml = stylesheetHtml.Replace(importFile, apiBase + prepend + importFile);+ }- var styleContent = RemoveWhiteSpaceFromStylesheets(stylesheetHtml);- styleContent =- Parser.Parser.FontSrcUrlRegex.Replace(styleContent, "$1" + apiBase + "$2" + "$3");+ // Check if there are any background images and rewrite those urls+ EscapeCssImageReferences(ref stylesheetHtml, apiBase, book);+ var styleContent = RemoveWhiteSpaceFromStylesheets(stylesheetHtml);styleContent = styleContent.Replace("body", ".reading-section");var stylesheet = await _cssParser.ParseAsync(styleContent);@@ -165,6 +170,21 @@ namespace API.Servicesreturn RemoveWhiteSpaceFromStylesheets(stylesheet.ToCss());}+ private static void EscapeCssImageReferences(ref string stylesheetHtml, string apiBase, EpubBookRef book)+ {+ var matches = Parser.Parser.CssImageUrlRegex.Matches(stylesheetHtml);+ foreach (Match match in matches)+ {+ if (!match.Success) continue;++ var importFile = match.Groups["Filename"].Value;+ var key = CleanContentKeys(importFile);+ if (!book.Content.AllFiles.ContainsKey(key)) continue;++ stylesheetHtml = stylesheetHtml.Replace(importFile, apiBase + key);+ }+ }+public ComicInfo GetComicInfo(string filePath){if (!IsValidFile(filePath) || Parser.Parser.IsPdf(filePath)) return null;diff --git a/UI/Web/src/app/book-reader/book-reader/book-reader.component.html b/UI/Web/src/app/book-reader/book-reader/book-reader.component.htmlindex 02ea43531..5f209490e 100644--- a/UI/Web/src/app/book-reader/book-reader/book-reader.component.html+++ b/UI/Web/src/app/book-reader/book-reader/book-reader.component.html@@ -111,6 +111,9 @@<ng-container [ngTemplateOutlet]="actionBar"></ng-container></div></div>+ <!-- <div *ngIf="page !== undefined && scrollbarNeeded">+ <ng-container [ngTemplateOutlet]="actionBar"></ng-container>+ </div> --><ng-template #actionBar><div class="reading-bar row no-gutters justify-content-between">diff --git a/UI/Web/src/app/book-reader/book-reader/book-reader.component.scss b/UI/Web/src/app/book-reader/book-reader/book-reader.component.scssindex f7c62217e..7132626f9 100644--- a/UI/Web/src/app/book-reader/book-reader/book-reader.component.scss+++ b/UI/Web/src/app/book-reader/book-reader/book-reader.component.scss@@ -155,6 +155,11 @@ $primary-color: #0062cc;.reading-section {height: 100vh;+ width: 100%;+}++.book-content {+ position: relative;}.drawer-body {diff --git a/UI/Web/src/app/book-reader/book-reader/book-reader.component.ts b/UI/Web/src/app/book-reader/book-reader/book-reader.component.tsindex 5faeaef7e..0eee0bfb0 100644--- a/UI/Web/src/app/book-reader/book-reader/book-reader.component.ts+++ b/UI/Web/src/app/book-reader/book-reader/book-reader.component.ts@@ -160,7 +160,11 @@ export class BookReaderComponent implements OnInit, AfterViewInit, OnDestroy {readerStyles: string = '';darkModeStyleElem!: HTMLElement;topOffset: number = 0; // Offset for drawer and rendering canvas- scrollbarNeeded = false; // Used for showing/hiding bottom action bar+ /**+ * Used for showing/hiding bottom action bar. Calculates if there is enough scroll to show it.+ * Will hide if all content in book is absolute positioned+ */+ scrollbarNeeded = false;readingDirection: ReadingDirection = ReadingDirection.LeftToRight;private readonly onDestroy = new Subject<void>();@@ -715,6 +719,14 @@ export class BookReaderComponent implements OnInit, AfterViewInit, OnDestroy {this.isLoading = false;this.scrollbarNeeded = this.readingSectionElemRef.nativeElement.scrollHeight > this.readingSectionElemRef.nativeElement.clientHeight;+ const itemsOnScreen = Array.from(this.readingHtml.nativeElement.querySelectorAll('*')).filter(elem => (elem as HTMLElement).nodeName != 'STYLE');+ const itemsWithAbsolutePositioning = itemsOnScreen.filter(elem => (elem as HTMLElement).style.getPropertyValue('position') === 'absolute').length;++ if (itemsWithAbsolutePositioning >= itemsOnScreen.length) {+ // Supress bottom actionbar. This is because of how the html is structured, with abs positioning, it will render inside images, etc.+ this.scrollbarNeeded = false;+ }+// Find all the part ids and their top offsetthis.setupPageAnchors();