public ItemCheckResult CheckItem(ParseItem item, ICssCheckerContext context) { UrlItem url = (UrlItem)item; if (!WESettings.Instance.Css.ValidateEmbedImages || !url.IsValid || url.UrlString == null || url.UrlString.Text.Contains("base64,") || context == null) { return(ItemCheckResult.Continue); } string fileName = ImageQuickInfo.GetFullUrl(url.UrlString.Text, EditorExtensionsPackage.DTE.ActiveDocument.FullName); if (string.IsNullOrEmpty(fileName) || fileName.Contains("://")) { return(ItemCheckResult.Continue); } FileInfo file = new FileInfo(fileName); if (file.Exists && file.Length < (1024 * 3)) { Declaration dec = url.FindType <Declaration>(); if (dec != null && dec.PropertyName != null && dec.PropertyName.Text[0] != '*' && dec.PropertyName.Text[0] != '_') { string error = string.Format(CultureInfo.CurrentCulture, Resources.PerformanceEmbedImageAsDataUri, file.Length); context.AddError(new SimpleErrorTag(url.UrlString, error)); } } return(ItemCheckResult.Continue); }
public async override void Invoke() { ITextBuffer textBuffer = this.HtmlSmartTag.TextBuffer; ElementNode element = this.HtmlSmartTag.Element; AttributeNode src = element.GetAttribute("src", true); ImageCompressor compressor = new ImageCompressor(); bool isDataUri = src.Value.StartsWith("data:image/", StringComparison.Ordinal); if (isDataUri) { string dataUri = await compressor.CompressDataUriAsync(src.Value); if (dataUri.Length < src.Value.Length) { using (WebEssentialsPackage.UndoContext("Optimize image")) { Span span = Span.FromBounds(src.ValueRangeUnquoted.Start, src.ValueRangeUnquoted.End); textBuffer.Replace(span, dataUri); } } } else { var fileName = ImageQuickInfo.GetFullUrl(src.Value, textBuffer); if (string.IsNullOrEmpty(fileName) || !ImageCompressor.IsFileSupported(fileName) || !File.Exists(fileName)) { return; } await compressor.CompressFilesAsync(fileName); } }
public async override void Invoke(CancellationToken cancellationToken) { ImageCompressor compressor = new ImageCompressor(); bool isDataUri = Attribute.Value.StartsWith("data:image/", StringComparison.Ordinal); if (isDataUri) { string dataUri = await compressor.CompressDataUriAsync(Attribute.Value); if (dataUri.Length < Attribute.Value.Length) { using (WebEssentialsPackage.UndoContext(this.DisplayText)) using (ITextEdit edit = TextBuffer.CreateEdit()) { Span span = Span.FromBounds(Attribute.ValueRangeUnquoted.Start, Attribute.ValueRangeUnquoted.End); edit.Replace(span, dataUri); edit.Apply(); } } } else { var fileName = ImageQuickInfo.GetFullUrl(Attribute.Value, TextBuffer); if (string.IsNullOrEmpty(fileName) || !ImageCompressor.IsFileSupported(fileName) || !File.Exists(fileName)) { return; } await compressor.CompressFilesAsync(fileName); } }
public void AugmentQuickInfoSession(IQuickInfoSession session, IList <object> qiContent, out ITrackingSpan applicableToSpan) { applicableToSpan = null; SnapshotPoint?point = session.GetTriggerPoint(session.TextView.TextBuffer.CurrentSnapshot); if (!point.HasValue) { return; } HtmlEditorTree tree = HtmlEditorDocument.TryFromTextView(session.TextView).HtmlEditorTree; if (tree == null) { return; } ElementNode node = null; AttributeNode attr = null; tree.GetPositionElement(point.Value.Position, out node, out attr); if (node == null || (!node.Name.Equals("img", StringComparison.OrdinalIgnoreCase) && !node.Name.Equals("source", StringComparison.OrdinalIgnoreCase))) { return; } if (attr == null || !attr.Name.Equals("src", StringComparison.OrdinalIgnoreCase)) { return; } string url = ImageQuickInfo.GetFullUrl(attr.Value, session.TextView.TextBuffer); if (string.IsNullOrEmpty(url)) { return; } applicableToSpan = session.TextView.TextBuffer.CurrentSnapshot.CreateTrackingSpan(point.Value.Position, 1, SpanTrackingMode.EdgeNegative); ImageQuickInfo.AddImageContent(qiContent, url); }
public ItemCheckResult CheckItem(ParseItem item, ICssCheckerContext context) { UrlItem url = (UrlItem)item; if (!WESettings.Instance.Css.ValidateEmbedImages || !url.IsValid || url.UrlString == null || url.UrlString.Text.Contains("base64,") || context == null) { return(ItemCheckResult.Continue); } string fileName = ImageQuickInfo.GetFullUrl(url.UrlString.Text, WebEssentialsPackage.DTE.ActiveDocument.FullName); if (string.IsNullOrEmpty(fileName) || fileName.Contains("://")) { return(ItemCheckResult.Continue); } // Remove parameters if any; c:/temp/myfile.ext?#iefix fileName = fileName.Split('?', '#')[0]; try { FileInfo file = new FileInfo(fileName); if (file.Exists && file.Length < (1024 * 3)) { Declaration dec = url.FindType <Declaration>(); if (dec != null && dec.PropertyName != null && dec.PropertyName.Text[0] != '*' && dec.PropertyName.Text[0] != '_') { string error = string.Format(CultureInfo.CurrentCulture, Resources.PerformanceEmbedImageAsDataUri, file.Length); context.AddError(new SimpleErrorTag(url.UrlString, error)); } } } catch { /* Ignore any error here */ } return(ItemCheckResult.Continue); }