Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to parse an url("...") inside a @font-face src declaration? #126

Closed
UweKeim opened this issue Jan 11, 2023 · 2 comments
Closed

Comments

@UweKeim
Copy link

UweKeim commented Jan 11, 2023

Having a @font-face that might look like this:

@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 400;
  src: url(https://example.org/some-font.ttf) format('truetype');
}

I am able to successfully parse this to an ICssFontFaceRule and access the Source property:

url(https://example.org/some-font.ttf) format('truetype');

I'm interested in the raw value of the url() part:

https://example.org/some-font.ttf

Since I found no way that AngleSharp.Css might help in parsing this, I'm about to fire up some Regex to extract the URL.

Just to be extra sure that I didn't miss this functionality I'm asking this question:

Is there any built-in functionality inside AngleSharp.Css to extract the raw URL of an url(...) ... string?

@UweKeim
Copy link
Author

UweKeim commented Jan 11, 2023

A quickly assembled Regex that seems to work for my use-case is:

private static string extractUrl(string raw)
{
    var m = Regex.Match(raw, @"url\s*\(\s*['""]{0,1}(.*?)['""]{0,1}\s*\)", RegexOptions.IgnoreCase);

    return m.Success ? m.Groups[1].Value : null;
}

Probably not the most bullet-proof thing but good enough for my scenario.

@FlorianRappl
Copy link
Contributor

Yes, you can navigate through the CSSOM - but its at the moment rather cumbersome and not very elegant.

If you grabbed already the font face rule fontFace you can:

var src = fontFace.GetProperty("src").RawValue;
// there can be multiple sources (first list) and each source might have multiple properties (second list)
var url = ((src as ICssMultipleValue)[0] as ICssMultipleValue)[0] as CssUrlValue;

// do something with url.Path, which is in the example above:
Assert.AreEqual("https://example.org/some-font.ttf", url.Path);

FlorianRappl added a commit that referenced this issue Jun 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants