Skip to content

[QUESTION] <link/> handling #1509

Open
Open

Description

@renanyoy

How to handle < l i n k /> tag and append external css content ?

Activity

  1. changed the title [-][QUESTION] <style></style> handling[/-] [+][QUESTION] <link/> handling[/+] on May 19, 2026
  2. renanyoy commented on May 19, 2026

    @renanyoy
    Author

    for now I preprocess the html before. but will be more handy to have a callback onCssRequested.
    And maybe same for fonts declaration in css (@font-face), a callback onFontFamillyRequested.

     void prepareText() {
        text = widget.spine.file.asText.nbspFormat;
        int i = 0;
        // load external css
        while (i < text.length) {
          i = text.indexOf('<link', i);
          if (i < 0) break;
          final n0 = text.indexOf('/>', i);
          final n1 = text.indexOf('</link>', i);
          int n = 0;
          if (n0 > 0 && n1 > 0) {
            n = min(n0, n1);
            n += n == n0 ? 2 : 7;
          } else if (n0 > 0) {
            n = n0 + 2;
          } else if (n1 > 0) {
            n = n1 + 7;
          } else {
            break;
          }
          try {
            final xml = XmlDocument.parse(text.substring(i, n));
            final href = xml.rootElement.getAttribute('href');
            if (href != null && href.isFileExt('css')) {
              final aref = widget.spine.combine(href: href);
              final bytes = widget.epub.files[aref]?.content;
              if (bytes != null) {
                final style = '<style>${utf8.decode(bytes)}</style>';
                text = '${text.substring(0, i)}$style${text.substring(n)}';
                n = i + style.length;
              }
            }
          } catch (error) {
            Debug.info(error);
          }
          i = n;
        }
      }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions