/// <summary> /// Detects URLs in styles. /// </summary> /// <param name="baseUri">The base URI.</param> /// <param name="attributeName">Name of the attribute.</param> /// <param name="attributeValue">The attribute value.</param> /// <returns></returns> private List <UriResourceInformation> ExtractStyleUrls( Uri baseUri, string attributeName, string attributeValue) { List <UriResourceInformation> result = new List <UriResourceInformation>(); if (string.Compare(attributeName, @"style", true) == 0) { if (attributeValue != null && attributeValue.Trim().Length > 0) { MatchCollection matchs = Regex.Matches( attributeValue, @"url\s*\(\s*([^\)\s]+)\s*\)", RegexOptions.Singleline | RegexOptions.IgnoreCase); if (matchs.Count > 0) { foreach (Match match in matchs) { if (match != null && match.Success) { string url = match.Groups[1].Value; UriResourceInformation ui = new UriResourceInformation( _settings.Options, url, new Uri(url, UriKind.RelativeOrAbsolute), baseUri, UriType.Resource); bool isOnSameSite = ui.IsOnSameSite(baseUri); if ((isOnSameSite || !_settings.Options.StayOnSite) && ui.IsProcessableUri) { result.Add(ui); } } } } } } return(result); }
// ------------------------------------------------------------------ #endregion #region Private methods. // ------------------------------------------------------------------ /// <summary> /// Does the extract links. /// </summary> /// <param name="xml">The XML.</param> /// <param name="uriInfo">The URI info.</param> /// <returns></returns> private List<UriResourceInformation> DoExtractLinks( XmlReader xml, UriResourceInformation uriInfo ) { List<UriResourceInformation> links = new List<UriResourceInformation>(); while ( xml.Read() ) { switch ( xml.NodeType ) { // Added 2006-03-27: Inside comments, too. case XmlNodeType.Comment: XmlReader childXml = GetDocReader( xml.Value, uriInfo.BaseUri ); List<UriResourceInformation> childLinks = DoExtractLinks( childXml, uriInfo ); links.AddRange( childLinks ); break; // A node element. case XmlNodeType.Element: string[] linkAttributeNames; UriType linkType; // If this is a link element, store the URLs to modify. if ( IsLinkElement( xml.Name, out linkAttributeNames, out linkType ) ) { while ( xml.MoveToNextAttribute() ) { links.AddRange( ExtractStyleUrls( uriInfo.BaseUriWithFolder, xml.Name, xml.Value ) ); foreach ( string a in linkAttributeNames ) { if ( string.Compare( a, xml.Name, true ) == 0 ) { string url = xml.Value; UriResourceInformation ui = new UriResourceInformation( _settings.Options, url, new Uri( url, UriKind.RelativeOrAbsolute ), uriInfo.BaseUriWithFolder, linkType ); bool isOnSameSite = ui.IsOnSameSite( uriInfo.BaseUri ); if ( (isOnSameSite || !_settings.Options.StayOnSite) && ui.IsProcessableUri ) { links.Add( ui ); } } } } } else { // Also, look for style attributes. while ( xml.MoveToNextAttribute() ) { links.AddRange( ExtractStyleUrls( uriInfo.BaseUriWithFolder, xml.Name, xml.Value ) ); } } break; } } return links; }
/// <summary> /// Detects URLs in styles. /// </summary> /// <param name="baseUri">The base URI.</param> /// <param name="attributeName">Name of the attribute.</param> /// <param name="attributeValue">The attribute value.</param> /// <returns></returns> private List<UriResourceInformation> ExtractStyleUrls( Uri baseUri, string attributeName, string attributeValue ) { List<UriResourceInformation> result = new List<UriResourceInformation>(); if ( string.Compare( attributeName, @"style", true ) == 0 ) { if ( attributeValue != null && attributeValue.Trim().Length > 0 ) { MatchCollection matchs = Regex.Matches( attributeValue, @"url\s*\(\s*([^\)\s]+)\s*\)", RegexOptions.Singleline | RegexOptions.IgnoreCase ); if ( matchs.Count > 0 ) { foreach ( Match match in matchs ) { if ( match != null && match.Success ) { string url = match.Groups[1].Value; UriResourceInformation ui = new UriResourceInformation( _settings.Options, url, new Uri( url, UriKind.RelativeOrAbsolute ), baseUri, UriType.Resource ); bool isOnSameSite = ui.IsOnSameSite( baseUri ); if ( (isOnSameSite || !_settings.Options.StayOnSite) && ui.IsProcessableUri ) { result.Add( ui ); } } } } } } return result; }
// ------------------------------------------------------------------ #endregion #region Private methods. // ------------------------------------------------------------------ /// <summary> /// Does the extract links. /// </summary> /// <param name="xml">The XML.</param> /// <param name="uriInfo">The URI info.</param> /// <returns></returns> private List <UriResourceInformation> DoExtractLinks( XmlReader xml, UriResourceInformation uriInfo) { List <UriResourceInformation> links = new List <UriResourceInformation>(); while (xml.Read()) { switch (xml.NodeType) { // Added 2006-03-27: Inside comments, too. case XmlNodeType.Comment: XmlReader childXml = GetDocReader(xml.Value, uriInfo.BaseUri); List <UriResourceInformation> childLinks = DoExtractLinks(childXml, uriInfo); links.AddRange(childLinks); break; // A node element. case XmlNodeType.Element: string[] linkAttributeNames; UriType linkType; // If this is a link element, store the URLs to modify. if (IsLinkElement( xml.Name, out linkAttributeNames, out linkType)) { while (xml.MoveToNextAttribute()) { links.AddRange( ExtractStyleUrls( uriInfo.BaseUriWithFolder, xml.Name, xml.Value)); foreach (string a in linkAttributeNames) { if (string.Compare(a, xml.Name, true) == 0) { string url = xml.Value; UriResourceInformation ui = new UriResourceInformation( _settings.Options, url, new Uri(url, UriKind.RelativeOrAbsolute), uriInfo.BaseUriWithFolder, linkType); bool isOnSameSite = ui.IsOnSameSite(uriInfo.BaseUri); if ((isOnSameSite || !_settings.Options.StayOnSite) && ui.IsProcessableUri) { links.Add(ui); } } } } } else { // Also, look for style attributes. while (xml.MoveToNextAttribute()) { links.AddRange( ExtractStyleUrls( uriInfo.BaseUriWithFolder, xml.Name, xml.Value)); } } break; } } return(links); }