Kareadita/Kavita · warning · KavitaException

Image element does not have a valid source attribute

Error message

Image element does not have a valid source attribute

What it means

Thrown by BookService.CopyImageToTempFromBook (line 1268) when the selected <img>/<image> node has neither a 'src' attribute nor an 'xlink:href' attribute (the only two src attribute names GetImageSrcAttributeName recognizes). The image element exists but Kavita cannot determine its source.

Solutions

  1. Repair the epub so image elements use standard src (or xlink:href for SVG image).
  2. Extend GetImageSrcAttributeName to recognize additional common attributes (data-src) if appropriate for your corpus.

Example fix

// before
private static string? GetImageSrcAttributeName(HtmlNode targetImage) {
    if (targetImage.Attributes["src"] != null) return "src";
    if (targetImage.Attributes["xlink:href"] != null) return "xlink:href";
    return null;
}

// after — also accept lazy-load variants
if (targetImage.Attributes["data-src"] != null) return "data-src";
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check the image element's source attribute on the client
const hasSrc = img.hasAttribute('src') || img.hasAttribute('xlink:href');
if (!hasSrc) { /* don't offer copy-image for this element */ }

Prevention

When it happens

Trigger: An epub page whose image element uses a non-standard attribute (e.g. data-src, poster) or a malformed/empty image tag with no src/xlink:href.

Common situations: Hand-authored or converter-produced epub with lazy-loading 'data-src' attributes; SVG <image> missing xlink:href.

Related errors


AI-assisted analysis of Kareadita/Kavita@9c3e540000 (2026-08-13). Data as JSON: /api/errors/9a8f7ff156213453. Report an issue: GitHub.

Appendix: source

Thrown at Kavita.Services/BookService.cs:1268

            if (images == null || images.Count == 0)
            {
                throw new KavitaException("No images found on the specified page");
            }

            if (bookmarkDto.ImageOffset >= images.Count)
            {
                throw new KavitaException($"Image index {bookmarkDto.ImageOffset} is out of range. Page has {images.Count} images");
            }

            var targetImage = images[bookmarkDto.ImageOffset];

            // Get the image source attribute
            var srcAttributeName = GetImageSrcAttributeName(targetImage);

            if (string.IsNullOrEmpty(srcAttributeName))
            {
                throw new KavitaException("Image element does not have a valid source attribute");
            }

            var imageSource = targetImage.Attributes[srcAttributeName].Value;

            // Clean and get the correct key for the image
            var imageKey = CleanContentKeys(GetKeyForImage(book, imageSource));

            // Check if it's an external URL
            if (imageKey.StartsWith("http"))
            {
                throw new KavitaException("Cannot copy external images");
            }

            // Get the image file from the epub

            if (!book.Content.Images.TryGetLocalFileRefByKey(imageKey, out var imageFile))
            {
                throw new KavitaException($"Image file not found in epub: {imageKey}");

View on GitHub (pinned to 9c3e540000)