Bloom.Book.HtmlDom.GetImageElementUrl C# (CSharp) Method

GetImageElementUrl() public static method

Gets the url for the image, either from an img element or any other element that has an inline style with background-image set.
public static GetImageElementUrl ( ElementProxy imgOrDivWithBackgroundImage ) : UrlPathString
imgOrDivWithBackgroundImage ElementProxy
return UrlPathString
        public static UrlPathString GetImageElementUrl(ElementProxy imgOrDivWithBackgroundImage)
        {
            if(imgOrDivWithBackgroundImage.Name.ToLower() == "img")
            {
                var src = imgOrDivWithBackgroundImage.GetAttribute("src");
                return UrlPathString.CreateFromUrlEncodedString(src);
            }
            else
            {
                var styleRule = imgOrDivWithBackgroundImage.GetAttribute("style") ?? "";
                var regex = new Regex("background-image\\s*:\\s*url\\((.*)\\)", RegexOptions.IgnoreCase);
                var match = regex.Match(styleRule);
                if(match.Groups.Count == 2)
                {
                    return UrlPathString.CreateFromUrlEncodedString(match.Groups[1].Value.Trim(new[] {'\'', '"'}));
                }
            }
            //we choose to return this instead of null to reduce errors created by things like
            // HtmlDom.GetImageElementUrl(element).UrlEncoded. If we just returned null, that has to be written
            // as something that checks for null, like:
            //  var url = HtmlDom.GetImageElementUrl(element). if(url!=null) url.UrlEncoded
            return UrlPathString.CreateFromUnencodedString(string.Empty);
        }

Same methods

HtmlDom::GetImageElementUrl ( GeckoHtmlElement imageElement ) : UrlPathString
HtmlDom::GetImageElementUrl ( XmlElement imageElement ) : UrlPathString

Usage Example

Example #1
0
        public static void UpdateImgMetadataAttributesToMatchImage(string folderPath, XmlElement imgElement, IProgress progress, Metadata metadata)
        {
            //see also PageEditingModel.UpdateMetadataAttributesOnImage(), which does the same thing but on the browser dom
            var    url      = HtmlDom.GetImageElementUrl(new ElementProxy(imgElement));
            string fileName = url.PathOnly.NotEncoded;

            if (fileName.ToLowerInvariant() == "placeholder.png" || fileName.ToLowerInvariant() == "license.png")
            {
                return;
            }
            if (string.IsNullOrEmpty(fileName))
            {
                Logger.WriteEvent("Book.UpdateImgMetdataAttributesToMatchImage() Warning: img has no or empty src attribute");
                //Debug.Fail(" (Debug only) img has no or empty src attribute");
                return;                 // they have bigger problems, which aren't appropriate to deal with here.
            }

            if (metadata == null)
            {
                // The fileName might be URL encoded.  See https://silbloom.myjetbrains.com/youtrack/issue/BL-3901.
                var path = UrlPathString.GetFullyDecodedPath(folderPath, ref fileName);
                progress.WriteStatus("Reading metadata from " + fileName);
                if (!RobustFile.Exists(path))                 // they have bigger problems, which aren't appropriate to deal with here.
                {
                    imgElement.RemoveAttribute("data-copyright");
                    imgElement.RemoveAttribute("data-creator");
                    imgElement.RemoveAttribute("data-license");
                    Logger.WriteEvent("Book.UpdateImgMetdataAttributesToMatchImage()  Image " + path + " is missing");
                    //Debug.Fail(" (Debug only) Image " + path + " is missing");
                    return;
                }
                try
                {
                    metadata = RobustIO.MetadataFromFile(path);
                }
                catch (UnauthorizedAccessException e)
                {
                    throw new BloomUnauthorizedAccessException(path, e);
                }
            }

            progress.WriteStatus("Writing metadata to HTML for " + fileName);

            imgElement.SetAttribute("data-copyright",
                                    String.IsNullOrEmpty(metadata.CopyrightNotice) ? "" : metadata.CopyrightNotice);
            imgElement.SetAttribute("data-creator", String.IsNullOrEmpty(metadata.Creator) ? "" : metadata.Creator);
            imgElement.SetAttribute("data-license", metadata.License == null ? "" : metadata.License.ToString());
        }