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);
        }
Пример #2
0
            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);
                }
            }
Пример #3
0
        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);
            }
        }
Пример #4
0
        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);
        }
Пример #5
0
        public IEnumerable <ITagSpan <IOutliningRegionTag> > GetTags(NormalizedSnapshotSpanCollection spans)
        {
            if (spans.Count == 0 || !EnsureInitialized())
            {
                yield break;
            }

            var visitor = new CssItemCollector <UrlItem>();

            _tree.StyleSheet.Accept(visitor);

            foreach (UrlItem url in visitor.Items.Where(u => u.UrlString != null && u.Start >= spans[0].Start))
            {
                if (url.UrlString.Text.IndexOf("base64,") > -1 && buffer.CurrentSnapshot.Length >= url.UrlString.AfterEnd)
                {
                    var items = new List <object>();
                    ImageQuickInfo.AddImageContent(items, url.UrlString.Text.Trim('"', '\''));

                    // Replace any TextBuffers into strings for the tooltip to display.
                    // This works because base64 images are loaded synchronously, so we
                    // can compute the size before returning.  If they do change, we'll
                    // need to replace them with TextBlocks & handle the Changed event.
                    for (int i = 0; i < items.Count; i++)
                    {
                        var tipBuffer = items[i] as ITextBuffer;
                        if (tipBuffer == null)
                        {
                            continue;
                        }
                        items[i] = tipBuffer.CurrentSnapshot.GetText();
                    }
                    var content = new ItemsControl {
                        ItemsSource = items
                    };

                    var span = new SnapshotSpan(new SnapshotPoint(buffer.CurrentSnapshot, url.UrlString.Start), url.UrlString.Length);
                    var tag  = new OutliningRegionTag(true, true, url.UrlString.Length + " characters", content);
                    yield return(new TagSpan <IOutliningRegionTag>(span, tag));
                }
            }
        }
Пример #6
0
        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);
        }