Ejemplo n.º 1
0
        private static bool AddMissingVendorDeclarations(StringBuilder sb, CssEditorDocument doc, ICssSchemaInstance rootSchema, out int count)
        {
            bool hasChanged = false;
            var  visitor    = new CssItemCollector <Declaration>(true);

            doc.Tree.StyleSheet.Accept(visitor);
            count = 0;

            var items = visitor.Items.Where(d => d.IsValid && !d.IsVendorSpecific() && d.PropertyName.Text != "filter");

            foreach (Declaration dec in items.Reverse())
            {
                ICssSchemaInstance schema = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, dec);
                var missingEntries        = dec.GetMissingVendorSpecifics(schema);

                if (missingEntries.Any())
                {
                    var    missingPrefixes = missingEntries.Select(e => e.Substring(0, e.IndexOf('-', 1) + 1));
                    string vendors         = GetVendorDeclarations(missingPrefixes, dec);

                    sb.Insert(dec.Start, vendors);
                    count++;

                    hasChanged = true;
                }
            }

            return(hasChanged);
        }
        private string AddMissingStandardDeclaration(StringBuilder sb, CssEditorDocument doc, ICssSchemaInstance rootSchema)
        {
            var visitor = new CssItemCollector<RuleBlock>(true);
            doc.Tree.StyleSheet.Accept(visitor);

            //var items = visitor.Items.Where(d => d.IsValid && d.IsVendorSpecific());
            foreach (RuleBlock rule in visitor.Items.Reverse())
            {
                HashSet<string> list = new HashSet<string>();
                foreach (Declaration dec in rule.Declarations.Where(d => d.IsValid && d.IsVendorSpecific()).Reverse())
                {
                    ICssSchemaInstance schema = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, dec);
                    ICssCompletionListEntry entry = VendorHelpers.GetMatchingStandardEntry(dec, schema);

                    if (entry != null && !list.Contains(entry.DisplayText) && !rule.Declarations.Any(d => d.PropertyName != null && d.PropertyName.Text == entry.DisplayText))
                    {
                        int index = dec.Text.IndexOf(":", StringComparison.Ordinal);
                        string standard = entry.DisplayText + dec.Text.Substring(index);

                        sb.Insert(dec.AfterEnd, standard);
                        list.Add(entry.DisplayText);
                    }
                }
            }

            return sb.ToString();
        }
Ejemplo n.º 3
0
        public ItemCheckResult CheckItem(ParseItem item, ICssCheckerContext context)
        {
            if (!WESettings.GetBoolean(WESettings.Keys.ValidateVendorSpecifics))
            {
                return(ItemCheckResult.Continue);
            }

            if (!item.IsValid || context == null)
            {
                return(ItemCheckResult.Continue);
            }

            ICssSchemaInstance rootSchema = CssSchemaManager.SchemaManager.GetSchemaRoot(null);
            ICssSchemaInstance schema     = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, item);

            string normalized = item.Text.Trim(':');

            if (normalized.Length > 0 && normalized[0] == '-' && schema.GetPseudo(item.Text) == null)
            {
                string message = string.Format(Resources.ValidationVendorPseudo, item.Text);
                context.AddError(new SimpleErrorTag(item, message, CssErrorFlags.TaskListWarning | CssErrorFlags.UnderlineRed));
                return(ItemCheckResult.CancelCurrentItem);
            }

            return(ItemCheckResult.Continue);
        }
Ejemplo n.º 4
0
        //private HashSet<string> _deprecated = new HashSet<string>()
        //{
        //    "-moz-opacity",
        //    "-moz-outline",
        //    "-moz-outline-style",
        //};

        public ItemCheckResult CheckItem(ParseItem item, ICssCheckerContext context)
        {
            if (!WESettings.GetBoolean(WESettings.Keys.ValidateVendorSpecifics))
            {
                return(ItemCheckResult.Continue);
            }

            Declaration dec = (Declaration)item;

            if (!dec.IsValid || !dec.IsVendorSpecific() || context == null)
            {
                return(ItemCheckResult.Continue);
            }

            ICssSchemaInstance rootSchema = CssSchemaManager.SchemaManager.GetSchemaRoot(null);
            ICssSchemaInstance schema     = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, item);

            //if (_deprecated.Contains(dec.PropertyName.Text))
            //{
            //    string message = string.Format(Resources.ValidationDeprecatedVendorDeclaration, dec.PropertyName.Text);
            //    context.AddError(new SimpleErrorTag(dec.PropertyName, message));
            //    return ItemCheckResult.CancelCurrentItem;
            //}
            if (schema.GetProperty(dec.PropertyName.Text) == null)
            {
                string message = string.Format(Resources.ValidationVendorDeclarations, dec.PropertyName.Text);
                context.AddError(new SimpleErrorTag(dec.PropertyName, message, CssErrorFlags.TaskListWarning | CssErrorFlags.UnderlineRed));
                return(ItemCheckResult.CancelCurrentItem);
            }

            return(ItemCheckResult.Continue);
        }
        protected override bool Execute(CssCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            var point = TextView.GetSelection("css");

            if (point == null)
            {
                return(false);
            }

            ITextBuffer        buffer     = point.Value.Snapshot.TextBuffer;
            CssEditorDocument  doc        = CssEditorDocument.FromTextBuffer(buffer);
            ICssSchemaInstance rootSchema = CssSchemaManager.SchemaManager.GetSchemaRoot(null);

            StringBuilder sb             = new StringBuilder(buffer.CurrentSnapshot.GetText());
            int           scrollPosition = TextView.TextViewLines.FirstVisibleLine.Extent.Start.Position;

            using (EditorExtensionsPackage.UndoContext("Add Missing Standard Property"))
            {
                int    count;
                string result = AddMissingStandardDeclaration(sb, doc, rootSchema, out count);
                Span   span   = new Span(0, buffer.CurrentSnapshot.Length);
                buffer.Replace(span, result);

                var selection = EditorExtensionsPackage.DTE.ActiveDocument.Selection as TextSelection;
                selection.GotoLine(1);

                EditorExtensionsPackage.ExecuteCommand("Edit.FormatDocument");
                TextView.ViewScroller.ScrollViewportVerticallyByLines(ScrollDirection.Down, TextView.TextSnapshot.GetLineNumberFromPosition(scrollPosition));
                EditorExtensionsPackage.DTE.StatusBar.Text = count + " missing standard properties added";
            }

            return(true);
        }
Ejemplo n.º 6
0
        public ItemCheckResult CheckItem(ParseItem item, ICssCheckerContext context)
        {
            if (!WESettings.GetBoolean(WESettings.Keys.ValidateVendorSpecifics))
            {
                return(ItemCheckResult.Continue);
            }

            AtDirective dir = item as AtDirective;

            if (!dir.IsValid || !dir.IsVendorSpecific() || context == null)
            {
                return(ItemCheckResult.Continue);
            }


            ICssSchemaInstance rootSchema = CssSchemaManager.SchemaManager.GetSchemaRoot(null);
            ICssSchemaInstance schema     = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, dir);

            if (schema.GetAtDirective("@" + dir.Keyword.Text) == null)
            {
                string message = string.Format(Resources.ValidationVendorDirective, dir.Keyword.Text);
                context.AddError(new SimpleErrorTag(dir.Keyword, message, CssErrorFlags.TaskListWarning | CssErrorFlags.UnderlineRed));
                return(ItemCheckResult.CancelCurrentItem);
            }

            return(ItemCheckResult.Continue);
        }
        private static string AddMissingVendorDeclarations(StringBuilder sb, CssEditorDocument doc, ICssSchemaInstance rootSchema, out int count)
        {
            var visitor = new CssItemCollector<Declaration>(true);
            doc.Tree.StyleSheet.Accept(visitor);
            count = 0;

            var items = visitor.Items.Where(d => d.IsValid && !d.IsVendorSpecific() && d.PropertyName.Text != "filter");

            foreach (Declaration dec in items.Reverse())
            {
                ICssSchemaInstance schema = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, dec);
                var missingEntries = dec.GetMissingVendorSpecifics(schema);

                if (missingEntries.Any())
                {
                    var missingPrefixes = missingEntries.Select(e => e.Substring(0, e.IndexOf('-', 1) + 1));
                    string vendors = GetVendorDeclarations(missingPrefixes, dec);

                    sb.Insert(dec.Start, vendors);
                    count++;
                }
            }

            return sb.ToString();
        }
Ejemplo n.º 8
0
        public ItemCheckResult CheckItem(ParseItem item, ICssCheckerContext context)
        {
            if (!WESettings.Instance.Css.ValidateVendorSpecifics)
            {
                return(ItemCheckResult.Continue);
            }

            AtDirective directive = (AtDirective)item;

            if (!directive.IsValid || directive.IsVendorSpecific() || context == null)
            {
                return(ItemCheckResult.Continue);
            }

            ICssSchemaInstance schema = CssEditorChecker.GetSchemaForItem(context, item);
            var missingEntries        = directive.GetMissingVendorSpecifics(schema);

            if (missingEntries.Any())
            {
                string    error = string.Format(CultureInfo.InvariantCulture, Resources.BestPracticeAddMissingVendorSpecificDirective, directive.Keyword.Text, string.Join(", ", missingEntries));
                ICssError tag   = new SimpleErrorTag(directive.Keyword, error);
                context.AddError(tag);
                return(ItemCheckResult.CancelCurrentItem);
            }

            return(ItemCheckResult.Continue);
        }
Ejemplo n.º 9
0
        public static string[] GetPrefixes(ICssSchemaInstance schema)
        {
            int version = schema.Version.GetHashCode();
            int browser = schema.Filter.Name.GetHashCode();
            int hash = version ^ browser;

            if (!prefixes.ContainsKey(hash))
            {
                CssSchemaManager.SchemaManager.CurrentSchemaChanged += CurrentSchemaChanged;
                var properties = schema.Properties;
                List<string> list = new List<string>();

                foreach (ICssCompletionListEntry property in properties)
                {
                    string text = property.DisplayText;
                    if (text[0] == '-')
                    {
                        int end = text.IndexOf('-', 1);
                        if (end > -1)
                        {
                            string prefix = text.Substring(0, end + 1);
                            if (!list.Contains(prefix))
                            {
                                list.Add(prefix);
                            }
                        }
                    }
                }

                prefixes.Add(hash, list.ToArray());
            }

            return prefixes[hash];
        }
Ejemplo n.º 10
0
        private static bool AddMissingStandardDeclaration(StringBuilder sb, CssEditorDocument doc, ICssSchemaInstance rootSchema, out int count)
        {
            bool hasChanged = false;
            var  visitor    = new CssItemCollector <RuleBlock>(true);

            doc.Tree.StyleSheet.Accept(visitor);
            count = 0;

            //var items = visitor.Items.Where(d => d.IsValid && d.IsVendorSpecific());
            foreach (RuleBlock rule in visitor.Items.Reverse())
            {
                HashSet <string> list = new HashSet <string>();
                foreach (Declaration dec in rule.Declarations.Where(d => d.IsValid && d.IsVendorSpecific()).Reverse())
                {
                    ICssSchemaInstance      schema = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, dec);
                    ICssCompletionListEntry entry  = VendorHelpers.GetMatchingStandardEntry(dec, schema);

                    if (entry != null && !list.Contains(entry.DisplayText) && !rule.Declarations.Any(d => d.PropertyName != null && d.PropertyName.Text == entry.DisplayText))
                    {
                        int    index    = dec.Text.IndexOf(":", StringComparison.Ordinal);
                        string standard = entry.DisplayText + dec.Text.Substring(index);

                        sb.Insert(dec.AfterEnd, standard);
                        list.Add(entry.DisplayText);
                        count++;

                        hasChanged = true;
                    }
                }
            }

            return(hasChanged);
        }
Ejemplo n.º 11
0
        public static string[] GetPrefixes(ICssSchemaInstance schema)
        {
            int version = schema.Version.GetHashCode();
            int browser = schema.Filter.Name.GetHashCode();
            int hash    = version ^ browser;

            if (!prefixes.ContainsKey(hash))
            {
                CssSchemaManager.SchemaManager.CurrentSchemaChanged += CurrentSchemaChanged;
                var           properties = schema.Properties;
                List <string> list       = new List <string>();

                foreach (ICssCompletionListEntry property in properties)
                {
                    string text = property.DisplayText;
                    if (text[0] == '-')
                    {
                        int end = text.IndexOf('-', 1);
                        if (end > -1)
                        {
                            string prefix = text.Substring(0, end + 1);
                            if (!list.Contains(prefix))
                            {
                                list.Add(prefix);
                            }
                        }
                    }
                }

                prefixes.Add(hash, list.ToArray());
            }

            return(prefixes[hash]);
        }
Ejemplo n.º 12
0
        public ItemCheckResult CheckItem(ParseItem item, ICssCheckerContext context)
        {
            if (!WESettings.Instance.Css.ValidateVendorSpecifics)
            {
                return(ItemCheckResult.Continue);
            }

            Declaration dec = (Declaration)item;

            if (!dec.IsValid || dec.IsVendorSpecific() || IgnoreProperty(dec) || context == null)
            {
                return(ItemCheckResult.Continue);
            }

            ICssSchemaInstance schema = CssEditorChecker.GetSchemaForItem(context, item);
            var missingEntries        = dec.GetMissingVendorSpecifics(schema);

            if (missingEntries.ToArray().Length > 0)
            {
                var       missingPrefixes = missingEntries.Select(e => e.Substring(0, e.IndexOf('-', 1) + 1));
                string    error           = string.Format(CultureInfo.InvariantCulture, Resources.BestPracticeAddMissingVendorSpecific, dec.PropertyName.Text, string.Join(", ", missingPrefixes));
                ICssError tag             = new SimpleErrorTag(dec.PropertyName, error);
                context.AddError(tag);
                return(ItemCheckResult.CancelCurrentItem);
            }

            return(ItemCheckResult.Continue);
        }
        public IEnumerable <ISmartTagAction> GetSmartTagActions(ParseItem item, int position, ITrackingSpan itemTrackingSpan, ITextView view)
        {
            var dec = (Declaration)item;

            if (!item.IsValid || position > dec.Colon.Start || dec.IsVendorSpecific())
            {
                yield break;
            }

            switch (dec.PropertyName.Text)
            {
            case "filter":
            case "zoom":
            case "behavior":
                yield break;
            }

            ICssSchemaInstance   schema         = CssSchemaManager.SchemaManager.GetSchemaRoot(null);//.GetSchemaRootForBuffer(view.TextBuffer);
            IEnumerable <string> missingEntries = dec.GetMissingVendorSpecifics(schema);

            if (missingEntries.Any())
            {
                var missingPrefixes = missingEntries.Select(e => e.Substring(0, e.IndexOf('-', 1) + 1));
                yield return(new VendorSmartTagAction(itemTrackingSpan, dec, missingPrefixes, view));
            }
        }
        protected override bool Execute(uint commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            ITextBuffer        buffer     = TextView.TextBuffer;
            CssEditorDocument  doc        = new CssEditorDocument(buffer);
            ICssSchemaInstance rootSchema = CssSchemaManager.SchemaManager.GetSchemaRoot(null);

            StringBuilder sb = new StringBuilder(buffer.CurrentSnapshot.Length);

            sb.Append(buffer.CurrentSnapshot.GetText());

            EditorExtensionsPackage.DTE.UndoContext.Open("Add Missing Standard Property");

            string result = AddMissingStandardDeclaration(sb, doc, rootSchema);
            Span   span   = new Span(0, buffer.CurrentSnapshot.Length);

            buffer.Replace(span, result);

            var selection = EditorExtensionsPackage.DTE.ActiveDocument.Selection as TextSelection;

            selection.GotoLine(1);

            EditorExtensionsPackage.DTE.ExecuteCommand("Edit.FormatDocument");
            EditorExtensionsPackage.DTE.UndoContext.Close();

            return(true);
        }
Ejemplo n.º 15
0
        public IEnumerable <ISmartTagAction> GetSmartTagActions(ParseItem item, int position, ITrackingSpan itemTrackingSpan, ITextView view)
        {
            Declaration dec = (Declaration)item;

            if (!item.IsValid || position > dec.Colon.Start || !view.TextBuffer.ContentType.IsOfType("css"))
            {
                yield break;
            }

            RuleBlock rule = dec.FindType <RuleBlock>();

            if (!rule.IsValid)
            {
                yield break;
            }

            ICssSchemaInstance schema = CssSchemaManager.SchemaManager.GetSchemaRootForBuffer(view.TextBuffer);

            if (!dec.IsVendorSpecific())
            {
                IEnumerable <Declaration> vendors = VendorHelpers.GetMatchingVendorEntriesInRule(dec, rule, schema);
                if (vendors.Any(v => v.Start > dec.Start))
                {
                    yield return(new VendorOrderSmartTagAction(itemTrackingSpan, vendors.Last(), dec, view));
                }
            }
            else
            {
                ICssCompletionListEntry entry = VendorHelpers.GetMatchingStandardEntry(dec, schema);
                if (entry != null && !rule.Declarations.Any(d => d.PropertyName != null && d.PropertyName.Text == entry.DisplayText))
                {
                    yield return(new MissingStandardSmartTagAction(itemTrackingSpan, dec, entry.DisplayText, view));
                }
            }
        }
Ejemplo n.º 16
0
        public static ICssCompletionListEntry GetMatchingStandardEntry(Declaration declaration, ICssSchemaInstance rootSchema)
        {
            string standardName;
            if (declaration.TryGetStandardPropertyName(out standardName, rootSchema))
            {
                ICssSchemaInstance schema = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, declaration);
                return schema.GetProperty(standardName);
            }

            return null;
        }
        public static IEnumerable<string> GetPossibleVendorSpecifics(this AtDirective directive, ICssSchemaInstance schema)
        {
            string text = directive.Keyword.Text;

            foreach (string prefix in VendorHelpers.GetPrefixes(schema).Where(p => p != "-o-")) // Remove -o- since the parser doesn't recognize -o-keyframes
            {
                ICssCompletionListEntry entry = schema.GetAtDirective("@" + prefix + text);
                if (entry != null)
                    yield return entry.DisplayText;
            }
        }
Ejemplo n.º 18
0
        public static ICssCompletionListEntry GetMatchingStandardEntry(AtDirective directive, ICssSchemaInstance rootSchema)
        {
            string standardName;
            if (directive.TryGetStandardPropertyName(out standardName, rootSchema))
            {
                ICssSchemaInstance schema = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, directive);
                return schema.GetAtDirective("@" + standardName);
            }

            return null;
        }
        public static IEnumerable<string> GetMissingVendorSpecifics(this Declaration declaration, ICssSchemaInstance schema)
        {
            RuleBlock rule = declaration.FindType<RuleBlock>();
            IEnumerable<string> possible = GetPossibleVendorSpecifics(declaration, schema);

            foreach (string item in possible)
            {
                if (!rule.Declarations.Any(d => d.PropertyName != null && d.PropertyName.Text == item))
                    yield return item;
            }
        }
        public static IEnumerable<string> GetPossibleVendorSpecifics(this Declaration declaration, ICssSchemaInstance schema)
        {
            string text = declaration.PropertyName.Text;

            foreach (string prefix in VendorHelpers.GetPrefixes(schema))
            {
                ICssCompletionListEntry entry = schema.GetProperty(prefix + text);
                if (entry != null)
                    yield return entry.DisplayText;
            }
        }
Ejemplo n.º 21
0
        private static bool SchemaLookup(ParseItem item, ITextBuffer buffer)
        {
            if (item is ClassSelector || item is IdSelector || item is ItemName || item.Parent is RuleBlock || item.Parent is StyleSheet)
            {
                return(false);
            }

            ICssSchemaInstance schema = CssSchemaManager.SchemaManager.GetSchemaRootForBuffer(buffer);

            Declaration dec = item.FindType <Declaration>();

            if (dec != null && dec.PropertyName != null)
            {
                return(OpenReferenceUrl(schema.GetProperty, dec.PropertyName.Text, "http://realworldvalidator.com/css/properties/"));
            }

            PseudoClassFunctionSelector pseudoClassFunction = item.FindType <PseudoClassFunctionSelector>();

            if (pseudoClassFunction != null)
            {
                return(OpenReferenceUrl(schema.GetPseudo, pseudoClassFunction.Colon.Text + pseudoClassFunction.Function.FunctionName.Text + ")", "http://realworldvalidator.com/css/pseudoclasses/"));
            }

            PseudoElementFunctionSelector pseudoElementFunction = item.FindType <PseudoElementFunctionSelector>();

            if (pseudoElementFunction != null)
            {
                return(OpenReferenceUrl(schema.GetPseudo, pseudoElementFunction.DoubleColon.Text + pseudoElementFunction.Function.FunctionName.Text + ")", "http://realworldvalidator.com/css/pseudoelements/"));
            }

            PseudoElementSelector pseudoElement = item.FindType <PseudoElementSelector>();

            if (pseudoElement != null && pseudoElement.PseudoElement != null)
            {
                return(OpenReferenceUrl(schema.GetPseudo, pseudoElement.DoubleColon.Text + pseudoElement.PseudoElement.Text, "http://realworldvalidator.com/css/pseudoelements/"));
            }

            PseudoClassSelector pseudoClass = item.FindType <PseudoClassSelector>();

            if (pseudoClass != null && pseudoClass.PseudoClass != null)
            {
                return(OpenReferenceUrl(schema.GetPseudo, pseudoClass.Colon.Text + pseudoClass.PseudoClass.Text, "http://realworldvalidator.com/css/pseudoclasses/"));
            }

            AtDirective directive = item.FindType <AtDirective>();

            if (directive != null)
            {
                return(OpenReferenceUrl(schema.GetAtDirective, directive.At.Text + directive.Keyword.Text, "http://realworldvalidator.com/css/atdirectives/"));
            }

            return(false);
        }
Ejemplo n.º 22
0
 public static IEnumerable<Declaration> GetMatchingVendorEntriesInRule(Declaration declaration, RuleBlock rule, ICssSchemaInstance schema)
 {
     foreach (Declaration d in rule.Declarations.Where(d => d.IsValid && d.IsVendorSpecific()))
         foreach (string prefix in GetPrefixes(schema))
         {
             if (d.PropertyName.Text == prefix + declaration.PropertyName.Text)
             {
                 yield return d;
                 break;
             }
         }
 }
Ejemplo n.º 23
0
        public static ICssCompletionListEntry GetMatchingStandardEntry(Declaration declaration, ICssSchemaInstance rootSchema)
        {
            string standardName;

            if (declaration.TryGetStandardPropertyName(out standardName, rootSchema))
            {
                ICssSchemaInstance schema = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, declaration);
                return(schema.GetProperty(standardName));
            }

            return(null);
        }
Ejemplo n.º 24
0
        public static ICssCompletionListEntry GetMatchingStandardEntry(AtDirective directive, ICssCheckerContext context)
        {
            string standardName;

            if (directive.TryGetStandardPropertyName(out standardName, CssEditorChecker.GetSchemaForItem(context, directive)))
            {
                ICssSchemaInstance schema = CssEditorChecker.GetSchemaForItem(context, directive);
                return(schema.GetAtDirective("@" + standardName));
            }

            return(null);
        }
Ejemplo n.º 25
0
        public static ICssCompletionListEntry GetMatchingStandardEntry(AtDirective directive, ICssSchemaInstance rootSchema)
        {
            string standardName;

            if (directive.TryGetStandardPropertyName(out standardName, rootSchema))
            {
                ICssSchemaInstance schema = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, directive);
                return(schema.GetAtDirective("@" + standardName));
            }

            return(null);
        }
Ejemplo n.º 26
0
        public static ICssCompletionListEntry GetMatchingStandardEntry(Declaration declaration, ICssCheckerContext context)
        {
            string standardName;

            if (declaration.TryGetStandardPropertyName(out standardName, CssEditorChecker.GetSchemaForItem(context, declaration)))
            {
                ICssSchemaInstance schema = CssEditorChecker.GetSchemaForItem(context, declaration);
                return(schema.GetProperty(standardName));
            }

            return(null);
        }
        public static IEnumerable<string> GetMissingVendorSpecifics(this AtDirective directive, ICssSchemaInstance schema)
        {
            IEnumerable<string> possible = GetPossibleVendorSpecifics(directive, schema);

            var visitorRules = new CssItemCollector<AtDirective>();
            directive.Parent.Accept(visitorRules);

            foreach (string item in possible)
            {
                if (!visitorRules.Items.Any(d => d.Keyword != null && "@" + d.Keyword.Text == item))
                    yield return item;
            }
        }
        public static bool TryGetStandardPropertyName(this AtDirective directive, out string standardName, ICssSchemaInstance schema)
        {
            standardName = null;

            string propText = directive.Keyword.Text;
            string prefix = VendorHelpers.GetPrefixes(schema).SingleOrDefault(p => propText.IndexOf(p, StringComparison.Ordinal) == 0);
            if (prefix != null)
            {
                standardName = propText.Substring(prefix.Length);
                return true;
            }

            return false;
        }
        public bool EnsureInitialized(ITextBuffer buffer)
        {
            if (_schema == null)
            {
                try
                {
                    _schema = CssSchemaManager.SchemaManager.GetSchemaRootForBuffer(buffer);
                }
                catch (Exception)
                {
                }
            }

            return _schema != null;
        }
Ejemplo n.º 30
0
        public bool EnsureInitialized(ITextBuffer buffer)
        {
            if (_schema == null)
            {
                try
                {
                    _schema = CssSchemaManager.SchemaManager.GetSchemaRootForBuffer(buffer);
                }
                catch (Exception)
                {
                }
            }

            return(_schema != null);
        }
Ejemplo n.º 31
0
        public void FilterCompletionList(IList <CssCompletionEntry> completions, CssCompletionContext context)
        {
            ICssSchemaInstance rootSchema = CssSchemaManager.SchemaManager.GetSchemaRoot(null);
            ICssSchemaInstance schema     = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, context.ContextItem);

            foreach (CssCompletionEntry entry in completions)
            {
                ICssCompletionListEntry prop = GetSchemaEntry(schema, context, entry);

                if (prop != null && !string.IsNullOrEmpty(prop.GetAttribute("obsolete")))
                {
                    entry.FilterType = CompletionEntryFilterTypes.NeverVisible;
                }
            }
        }
Ejemplo n.º 32
0
        private static ICssCompletionListEntry GetSchemaEntry(ICssSchemaInstance schema, CssCompletionContext context, CssCompletionEntry entry)
        {
            switch (context.ContextType)
            {
            case CssCompletionContextType.AtDirectiveName:
                return(schema.GetAtDirective(entry.DisplayText));

            case CssCompletionContextType.PropertyName:
                return(schema.GetProperty(entry.DisplayText));

            case CssCompletionContextType.PseudoClassOrElement:
                return(schema.GetPseudo(entry.DisplayText));
            }

            return(null);
        }
Ejemplo n.º 33
0
        private IEnumerable <ICssSchemaInstance> GetAllSchemas(ICssSchemaInstance rootSchema)
        {
            foreach (ICssCompletionListEntry directive in rootSchema.AtDirectives)
            {
                if (_vs.Contains(directive.DisplayText))
                {
                    continue;
                }

                ICssSchemaInstance schema = rootSchema.GetAtDirectiveSchemaInstance(directive.DisplayText);
                if (schema != null && schema.Properties.Count() != rootSchema.Properties.Count())
                {
                    yield return(schema);
                }
            }
        }
        private static ICssCompletionListEntry GetSchemaEntry(ICssSchemaInstance schema, CssCompletionContext context, CssCompletionEntry entry)
        {
            switch (context.ContextType)
            {
                case CssCompletionContextType.AtDirectiveName:
                    return schema.GetAtDirective(entry.DisplayText);

                case CssCompletionContextType.PropertyName:
                    return schema.GetProperty(entry.DisplayText);

                case CssCompletionContextType.PseudoClassOrElement:
                    return schema.GetPseudo(entry.DisplayText);
            }

            return null;
        }
        public static bool TryGetStandardPropertyName(this Declaration declaration, out string standardName, ICssSchemaInstance schema)
        {
            standardName = null;

            if (declaration.IsVendorSpecific())
            {
                string propText = declaration.PropertyName.Text;
                string prefix = VendorHelpers.GetPrefixes(schema).SingleOrDefault(p => propText.StartsWith(p, StringComparison.Ordinal));
                if (prefix != null)
                {
                    standardName = propText.Substring(prefix.Length);
                    return true;
                }
            }

            return false;
        }
Ejemplo n.º 36
0
        public IEnumerable <ISmartTagAction> GetSmartTagActions(ParseItem item, int position, ITrackingSpan itemTrackingSpan, ITextView view)
        {
            var directive = (AtDirective)item;

            if (!item.IsValid || directive.IsVendorSpecific())
            {
                yield break;
            }

            ICssSchemaInstance   schema         = CssSchemaManager.SchemaManager.GetSchemaRoot(null);//.GetSchemaRootForBuffer(view.TextBuffer);
            IEnumerable <string> missingEntries = directive.GetMissingVendorSpecifics(schema);

            if (missingEntries.Any())
            {
                yield return(new VendorDirectiveSmartTagAction(itemTrackingSpan, directive, missingEntries));
            }
        }
Ejemplo n.º 37
0
        public void Parse()
        {
            ICssSchemaInstance root = CssSchemaManager.SchemaManager.GetSchemaRoot(null);
            IEnumerable <ICssSchemaInstance> schemas = GetAllSchemas(root);

            using (XmlWriter writer = XmlWriter.Create(_fileName))
            {
                writer.WriteStartElement("css");

                // @-Directives
                List <ICssCompletionListEntry> directives = new List <ICssCompletionListEntry>(root.AtDirectives);
                directives.AddRange(schemas.SelectMany(s => s.AtDirectives));
                directives = RemoveDuplicates(directives);

                writer.WriteStartElement("atDirectives");
                WriteSection(writer, directives);
                writer.WriteEndElement();

                // Pseudos
                List <ICssCompletionListEntry> pseudos = new List <ICssCompletionListEntry>(root.PseudoClassesAndElements);
                pseudos.AddRange(schemas.SelectMany(s => s.PseudoClassesAndElements));
                pseudos = RemoveDuplicates(pseudos);

                writer.WriteStartElement("pseudoClasses");
                WriteSection(writer, pseudos.Where(p => p.DisplayText[1] != ':'));
                writer.WriteEndElement();

                writer.WriteStartElement("pseudoElements");
                WriteSection(writer, pseudos.Where(p => p.DisplayText[1] == ':'));
                writer.WriteEndElement();

                // Properties
                List <ICssCompletionListEntry> properties = new List <ICssCompletionListEntry>(root.Properties);
                properties.AddRange(schemas.SelectMany(s => s.Properties));
                properties = RemoveDuplicates(properties);

                writer.WriteStartElement("properties");
                WriteProperties(writer, properties, root);
                writer.WriteEndElement();

                writer.WriteEndElement();
            }
        }
Ejemplo n.º 38
0
        public ItemCheckResult CheckItem(ParseItem item, ICssCheckerContext context)
        {
            Declaration dec = (Declaration)item;

            if (context == null || !dec.IsValid)
            {
                return(ItemCheckResult.Continue);
            }

            RuleBlock rule = dec.FindType <RuleBlock>();

            if (!rule.IsValid)
            {
                return(ItemCheckResult.Continue);
            }

            if (!dec.IsVendorSpecific())
            {
                ICssSchemaInstance schema = CssEditorChecker.GetSchemaForItem(context, item);
                bool hasVendor            = VendorHelpers.HasVendorLaterInRule(dec, schema);
                if (hasVendor)
                {
                    context.AddError(new SimpleErrorTag(dec.PropertyName, Resources.BestPracticeStandardPropertyOrder));
                    return(ItemCheckResult.CancelCurrentItem);
                }
            }
            else
            {
                ICssCompletionListEntry entry = VendorHelpers.GetMatchingStandardEntry(dec, context);
                if (entry != null && !rule.Declarations.Any(d => d.PropertyName != null && d.PropertyName.Text == entry.DisplayText))
                {
                    if (entry.DisplayText != "filter" && entry.DisplayText != "zoom" && entry.DisplayText != "behavior")
                    {
                        string message = string.Format(CultureInfo.InvariantCulture, Resources.BestPracticeAddMissingStandardProperty, entry.DisplayText);
                        context.AddError(new SimpleErrorTag(dec.PropertyName, message));
                        return(ItemCheckResult.CancelCurrentItem);
                    }
                }
            }

            return(ItemCheckResult.Continue);
        }
Ejemplo n.º 39
0
        public static bool HasVendorLaterInRule(Declaration declaration, ICssSchemaInstance schema)
        {
            Declaration next = declaration.NextSibling as Declaration;

            while (next != null)
            {
                if (next.IsValid && next.IsVendorSpecific())
                {
                    foreach (string prefix in GetPrefixes(schema))
                    {
                        if (next.PropertyName.Text == prefix + declaration.PropertyName.Text)
                            return true;
                    }
                }

                next = next.NextSibling as Declaration;
            }

            return false;
        }
        public IEnumerable <ISmartTagAction> GetSmartTagActions(ParseItem item, int position, ITrackingSpan itemTrackingSpan, ITextView view)
        {
            AtDirective directive = (AtDirective)item;

            if (!item.IsValid || !directive.IsVendorSpecific())
            {
                yield break;
            }

            ICssSchemaInstance schema = CssSchemaManager.SchemaManager.GetSchemaRootForBuffer(view.TextBuffer);
            var visitor = new CssItemCollector <AtDirective>();

            directive.Parent.Accept(visitor);

            ICssCompletionListEntry entry = VendorHelpers.GetMatchingStandardEntry(directive, schema);

            if (entry != null && !visitor.Items.Any(d => d.Keyword != null && "@" + d.Keyword.Text == entry.DisplayText))
            {
                yield return(new MissingStandardDirectiveSmartTagAction(itemTrackingSpan, directive, entry.DisplayText));
            }
        }
        public ItemCheckResult CheckItem(ParseItem item, ICssCheckerContext context)
        {
            if (!WESettings.Instance.Css.ValidateVendorSpecifics)
            {
                return(ItemCheckResult.Continue);
            }

            AtDirective dir = item as AtDirective;

            if (!dir.IsValid || !dir.IsVendorSpecific() || context == null)
            {
                return(ItemCheckResult.Continue);
            }


            ICssSchemaInstance rootSchema = CssSchemaManager.SchemaManager.GetSchemaRoot(null);
            ICssSchemaInstance schema     = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, dir);

            ICssCompletionListEntry at = schema.GetAtDirective("@" + dir.Keyword.Text);

            if (at == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, Resources.ValidationVendorDirective, dir.Keyword.Text);
                context.AddError(new SimpleErrorTag(dir.Keyword, message, CssErrorFlags.TaskListWarning | CssErrorFlags.UnderlineRed));
                return(ItemCheckResult.CancelCurrentItem);
            }
            else
            {
                string obsolete = at.GetAttribute("obsolete");
                if (!string.IsNullOrEmpty(obsolete))
                {
                    string message = string.Format(CultureInfo.CurrentCulture, Resources.BestPracticeRemoveObsolete, "@" + dir.Keyword.Text, obsolete);
                    context.AddError(new SimpleErrorTag(dir.Keyword, message, CssErrorFlags.TaskListMessage));
                    return(ItemCheckResult.CancelCurrentItem);
                }
            }

            return(ItemCheckResult.Continue);
        }
Ejemplo n.º 42
0
        public static bool HasVendorLaterInRule(Declaration declaration, ICssSchemaInstance schema)
        {
            Declaration next = declaration.NextSibling as Declaration;

            while (next != null)
            {
                if (next.IsValid && next.IsVendorSpecific())
                {
                    foreach (string prefix in GetPrefixes(schema))
                    {
                        if (next.PropertyName.Text == prefix + declaration.PropertyName.Text)
                        {
                            return(true);
                        }
                    }
                }

                next = next.NextSibling as Declaration;
            }

            return(false);
        }
        public ItemCheckResult CheckItem(ParseItem item, ICssCheckerContext context)
        {
            if (!WESettings.Instance.Css.ValidateVendorSpecifics)
            {
                return(ItemCheckResult.Continue);
            }

            Declaration dec = (Declaration)item;

            if (!dec.IsValid || !dec.IsVendorSpecific() || context == null)
            {
                return(ItemCheckResult.Continue);
            }

            ICssSchemaInstance rootSchema = CssSchemaManager.SchemaManager.GetSchemaRoot(null);
            ICssSchemaInstance schema     = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, item);

            ICssCompletionListEntry prop = schema.GetProperty(dec.PropertyName.Text);

            if (prop == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, Resources.ValidationVendorDeclarations, dec.PropertyName.Text);
                context.AddError(new SimpleErrorTag(dec.PropertyName, message, CssErrorFlags.TaskListWarning | CssErrorFlags.UnderlineRed));
                return(ItemCheckResult.CancelCurrentItem);
            }
            else
            {
                string obsolete = prop.GetAttribute("obsolete");
                if (!string.IsNullOrEmpty(obsolete))
                {
                    string message = string.Format(CultureInfo.CurrentCulture, Resources.BestPracticeRemoveObsolete, dec.PropertyName.Text, obsolete);
                    context.AddError(new SimpleErrorTag(dec.PropertyName, message, CssErrorFlags.TaskListMessage));
                    return(ItemCheckResult.CancelCurrentItem);
                }
            }

            return(ItemCheckResult.Continue);
        }
Ejemplo n.º 44
0
        protected override bool Execute(CssCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (_point == null)
            {
                return(false);
            }

            ITextBuffer        buffer     = _point.Value.Snapshot.TextBuffer;
            CssEditorDocument  doc        = CssEditorDocument.FromTextBuffer(buffer);
            ICssSchemaInstance rootSchema = CssSchemaManager.SchemaManager.GetSchemaRoot(null);
            StringBuilder      sb         = new StringBuilder(buffer.CurrentSnapshot.GetText());
            int scrollPosition            = TextView.TextViewLines.FirstVisibleLine.Extent.Start.Position;

            using (WebEssentialsPackage.UndoContext("Add Missing Vendor Specifics"))
            {
                int  count;
                bool hasChanged = AddMissingVendorDeclarations(sb, doc, rootSchema, out count);

                if (hasChanged)
                {
                    buffer.SetText(sb.ToString()
                                   .Replace("/* BEGIN EXTERNAL SOURCE */\r\n", string.Empty)
                                   .Replace("\r\n/* END EXTERNAL SOURCE */\r\n", string.Empty));
                }

                if (TextView.Caret.Position.BufferPosition.Snapshot == buffer.CurrentSnapshot)
                {
                    (WebEssentialsPackage.DTE.ActiveDocument.Selection as TextSelection).GotoLine(1);
                }

                WebEssentialsPackage.ExecuteCommand("Edit.FormatDocument");
                TextView.ViewScroller.ScrollViewportVerticallyByLines(ScrollDirection.Down, TextView.TextSnapshot.GetLineNumberFromPosition(scrollPosition));
                WebEssentialsPackage.DTE.StatusBar.Text = count + " missing vendor specific properties added";
            }

            return(true);
        }
        public ItemCheckResult CheckItem(ParseItem item, ICssCheckerContext context)
        {
            if (!WESettings.Instance.Css.ValidateVendorSpecifics)
            {
                return(ItemCheckResult.Continue);
            }

            if (!item.IsValid || context == null)
            {
                return(ItemCheckResult.Continue);
            }

            ICssSchemaInstance rootSchema = CssSchemaManager.SchemaManager.GetSchemaRoot(null);
            ICssSchemaInstance schema     = CssSchemaManager.SchemaManager.GetSchemaForItem(rootSchema, item);

            string normalized = item.Text.Trim(':');
            ICssCompletionListEntry pseudo = schema.GetPseudo(item.Text);

            if (normalized.Length > 0 && normalized[0] == '-' && pseudo == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, Resources.ValidationVendorPseudo, item.Text);
                context.AddError(new SimpleErrorTag(item, message, CssErrorFlags.TaskListWarning | CssErrorFlags.UnderlineRed));
                return(ItemCheckResult.CancelCurrentItem);
            }
            else if (pseudo != null)
            {
                string obsolete = pseudo.GetAttribute("obsolete");
                if (!string.IsNullOrEmpty(obsolete))
                {
                    string message = string.Format(CultureInfo.CurrentCulture, Resources.BestPracticeRemoveObsolete, normalized, obsolete);
                    context.AddError(new SimpleErrorTag(item, message, CssErrorFlags.TaskListMessage));
                    return(ItemCheckResult.CancelCurrentItem);
                }
            }

            return(ItemCheckResult.Continue);
        }
 public DeclarationQuickInfo(ITextBuffer buffer)
 {
     _buffer = buffer;
     _rootSchema = CssSchemaManager.SchemaManager.GetSchemaRootForBuffer(buffer);
 }
        private IEnumerable<ICssSchemaInstance> GetAllSchemas(ICssSchemaInstance rootSchema)
        {
            foreach (ICssCompletionListEntry directive in rootSchema.AtDirectives)
            {
                if (vs.Contains(directive.DisplayText))
                    continue;

                ICssSchemaInstance schema = rootSchema.GetAtDirectiveSchemaInstance(directive.DisplayText);
                if (schema != null && schema.Properties.Count() != rootSchema.Properties.Count())
                    yield return schema;
            }
        }
        private static Tuple<ParseItem, ICssCompletionListEntry> GetEntriesAndPoint(ParseItem item, SnapshotPoint point, ICssSchemaInstance schema)
        {
            // Declaration
            Declaration dec = item.FindType<Declaration>();

            if (dec != null && dec.PropertyName != null && dec.PropertyName.ContainsRange(point.Position, 1))
            {
                return Tuple.Create<ParseItem, ICssCompletionListEntry>(dec.PropertyName, schema.GetProperty(dec.PropertyName.Text));
            }

            if (dec != null && dec.IsValid && dec.Values.TextStart <= point.Position && dec.Values.TextAfterEnd >= point.Position)
            {
                var entry = schema.GetProperty(dec.PropertyName.Text);

                if (entry != null)
                {
                    var list = schema.GetPropertyValues(entry.DisplayText);
                    var theOne = dec.StyleSheet.ItemFromRange(point.Position, 0);

                    return Tuple.Create<ParseItem, ICssCompletionListEntry>(theOne,
                    list.SingleOrDefault(r => r.DisplayText.Equals(theOne.Text, StringComparison.OrdinalIgnoreCase)));
                }
            }

            // Pseudo class
            PseudoClassSelector pseudoClass = item.FindType<PseudoClassSelector>();

            if (pseudoClass != null)
            {
                return Tuple.Create<ParseItem, ICssCompletionListEntry>(pseudoClass, schema.GetPseudo(pseudoClass.Text));
            }

            // Pseudo class function
            PseudoClassFunctionSelector pseudoClassFunction = item.FindType<PseudoClassFunctionSelector>();

            if (pseudoClassFunction != null)
            {
                return Tuple.Create<ParseItem, ICssCompletionListEntry>(pseudoClassFunction, schema.GetPseudo(pseudoClassFunction.Text));
            }

            // Pseudo element
            PseudoElementSelector pseudoElement = item.FindType<PseudoElementSelector>();

            if (pseudoElement != null)
            {
                return Tuple.Create<ParseItem, ICssCompletionListEntry>(pseudoElement, schema.GetPseudo(pseudoElement.Text));
            }

            // Pseudo element function
            PseudoElementFunctionSelector pseudoElementFunction = item.FindType<PseudoElementFunctionSelector>();

            if (pseudoElementFunction != null)
            {
                return Tuple.Create<ParseItem, ICssCompletionListEntry>(pseudoElementFunction, schema.GetPseudo(pseudoElementFunction.Text));
            }

            // @-directive
            AtDirective atDirective = item.Parent as AtDirective;

            if (atDirective != null && atDirective.Keyword != null)
            {
                return Tuple.Create<ParseItem, ICssCompletionListEntry>(atDirective.Keyword, schema.GetAtDirective("@" + atDirective.Keyword.Text));
            }

            return null;
        }
        private void WriteProperties(XmlWriter writer, IEnumerable<ICssCompletionListEntry> entries, ICssSchemaInstance schema)
        {
            foreach (ICssCompletionListEntry entry in entries.OrderBy(e => e.DisplayText))
            {
                writer.WriteStartElement("entry");
                writer.WriteAttributeString("name", entry.DisplayText);
                writer.WriteAttributeString("restriction", entry.GetAttribute("restriction"));
                writer.WriteAttributeString("version", entry.GetAttribute("version"));
                WriteBrowserSupport(writer, entry);

                if (!string.IsNullOrEmpty(entry.GetAttribute("standard-reference")))
                    writer.WriteAttributeString("ref", entry.GetAttribute("standard-reference"));

                if (!string.IsNullOrEmpty(entry.GetAttribute("syntax")))
                    writer.WriteAttributeString("syntax", entry.GetAttribute("syntax"));

                if (!string.IsNullOrEmpty(entry.GetAttribute("description")))
                    writer.WriteElementString("desc", entry.GetAttribute("description"));

                var values = schema.GetPropertyValues(entry.DisplayText);
                if (values.Count() > 2)
                {
                    writer.WriteStartElement("values");
                    foreach (ICssCompletionListEntry value in values.OrderBy(v => v.DisplayText))
                    {
                        if (value.DisplayText == "initial" || value.DisplayText == "inherit")
                            continue;

                        writer.WriteStartElement("value");
                        writer.WriteAttributeString("name", value.DisplayText);
                        writer.WriteAttributeString("version", value.GetAttribute("version") != string.Empty ? value.GetAttribute("version") : entry.GetAttribute("version"));
                        WriteBrowserSupport(writer, value);

                        if (!string.IsNullOrEmpty(value.GetAttribute("description")))
                            writer.WriteElementString("desc", value.GetAttribute("description"));

                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
            }
        }
 public DeclarationQuickInfo(DeclarationQuickInfoSourceProvider provider, ITextBuffer buffer)
 {
     _provider = provider;
     _buffer = buffer;
     _rootSchema = CssSchemaManager.SchemaManager.GetSchemaRootForBuffer(buffer);
 }