public static void FindMatches(StyleMatchingContext context, List <SelectorMatchRecord> matchedSelectors)
        {
            // To support having the root pseudo states set for style sheets added onto an element
            // we need to find which sheets belongs to the element itself.
            VisualElement element          = context.currentElement;
            int           parentSheetIndex = context.styleSheetCount - 1;

            if (element.styleSheetList != null)
            {
                // The number of style sheet for an element is the count of the styleSheetList + all imported style sheet
                int elementSheetCount = element.styleSheetList.Count;
                for (var i = 0; i < element.styleSheetList.Count; i++)
                {
                    var elementSheet = element.styleSheetList[i];
                    if (elementSheet.flattenedRecursiveImports != null)
                    {
                        elementSheetCount += elementSheet.flattenedRecursiveImports.Count;
                    }
                }

                parentSheetIndex -= elementSheetCount;
            }

            FindMatches(context, matchedSelectors, parentSheetIndex);
        }
        private void FindStyleSheets(VisualElement cursor, StyleMatchingContext matchingContext)
        {
            if (cursor.shadow.parent != null)
            {
                FindStyleSheets(cursor.shadow.parent, matchingContext);
            }

            if (cursor.styleSheets != null)
            {
                foreach (StyleSheet sheet in cursor.styleSheets)
                {
                    selectedElementStylesheets.Add(AssetDatabase.GetAssetPath(sheet) ?? "<unknown>");
                    matchingContext.styleSheetStack.Add(sheet);
                }
            }
        }
        private void SetupParents(VisualElement cursor, StyleMatchingContext matchingContext)
        {
            if (cursor.hierarchy.parent != null)
            {
                SetupParents(cursor.hierarchy.parent, matchingContext);
            }

            // We populate the ancestor filter in order for the Bloom filter detection to work.
            matchingContext.ancestorFilter.PushElement(cursor);

            if (cursor.styleSheetList == null)
            {
                return;
            }

            foreach (StyleSheet sheet in cursor.styleSheetList)
            {
                // Skip deleted style sheets
                if (sheet == null)
                {
                    continue;
                }

                string name = AssetDatabase.GetAssetPath(sheet);
                if (string.IsNullOrEmpty(name) || sheet.isDefaultStyleSheet)
                {
                    name = sheet.name;
                }

                void RecursivePrintStyleSheetNames(StyleSheet importedSheet)
                {
                    for (int i = 0; i < importedSheet.imports.Length; i++)
                    {
                        var thisImportedSheet = importedSheet.imports[i].styleSheet;
                        name += "\n(" + thisImportedSheet.name + ")";
                        matchingContext.AddStyleSheet(thisImportedSheet);
                        RecursivePrintStyleSheetNames(thisImportedSheet);
                    }
                }

                RecursivePrintStyleSheetNames(sheet);

                selectedElementStylesheets.Add(name);
                matchingContext.AddStyleSheet(sheet);
            }
        }
        public void FindMatchingRules(VisualElement target)
        {
            var matchingContext = new StyleMatchingContext((element, info) => {});

            matchingContext.currentElement = target;
            FindStyleSheets(target, matchingContext);

            List <SelectorMatchRecord> matches = new List <SelectorMatchRecord>();

            StyleSelectorHelper.FindMatches(matchingContext, matches);

            matches.Sort(SelectorMatchRecord.Compare);

            foreach (var record in matches)
            {
                selectedElementRules.Add(new MatchedRule(record));
            }
        }
        public void FindMatchingRules(VisualElement target)
        {
            var matchingContext = new StyleMatchingContext((element, info) => {})
            {
                currentElement = target
            };

            SetupParents(target, matchingContext);

            matchRecords.Clear();
            StyleSelectorHelper.FindMatches(matchingContext, matchRecords);

            matchRecords.Sort(SelectorMatchRecord.Compare);

            foreach (var record in matchRecords)
            {
                selectedElementRules.Add(new MatchedRule(record));
            }
        }
        public static void FindMatches(StyleMatchingContext context, List <SelectorMatchRecord> matchedSelectors, int parentSheetIndex)
        {
            Debug.Assert(matchedSelectors.Count == 0);

            Debug.Assert(context.currentElement != null, "context.currentElement != null");

            VisualElement element     = context.currentElement;
            bool          toggledRoot = false;

            for (int i = 0; i < context.styleSheetStack.Count; i++)
            {
                // If the sheet is added on the element consider it as :root
                if (!toggledRoot && i > parentSheetIndex)
                {
                    element.pseudoStates |= PseudoStates.Root;
                    toggledRoot           = true;
                }

                StyleSheet          styleSheet = context.styleSheetStack[i];
                SelectorMatchRecord record     = new SelectorMatchRecord(styleSheet, i);

                FastLookup(styleSheet.orderedTypeSelectors, matchedSelectors, context, element.typeName, ref record);
                FastLookup(styleSheet.orderedTypeSelectors, matchedSelectors, context, "*", ref record);

                if (!string.IsNullOrEmpty(element.name))
                {
                    FastLookup(styleSheet.orderedNameSelectors, matchedSelectors, context, element.name, ref record);
                }

                foreach (string @class in element.GetClassesForIteration())
                {
                    FastLookup(styleSheet.orderedClassSelectors, matchedSelectors, context, @class, ref record);
                }
            }

            if (toggledRoot)
            {
                element.pseudoStates &= ~PseudoStates.Root;
            }
        }
        public static void FindMatches(StyleMatchingContext context, List <SelectorMatchRecord> matchedSelectors)
        {
            Debug.Assert(matchedSelectors.Count == 0);
            Debug.Assert(context.currentElement != null, "context.currentElement != null");
            VisualElement currentElement = context.currentElement;

            for (int i = 0; i < context.styleSheetStack.Count; i++)
            {
                StyleSheet          styleSheet          = context.styleSheetStack[i];
                SelectorMatchRecord selectorMatchRecord = new SelectorMatchRecord(styleSheet, i);
                StyleSelectorHelper.FastLookup(styleSheet.orderedTypeSelectors, matchedSelectors, context, currentElement.typeName, ref selectorMatchRecord);
                StyleSelectorHelper.FastLookup(styleSheet.orderedTypeSelectors, matchedSelectors, context, "*", ref selectorMatchRecord);
                bool flag = !string.IsNullOrEmpty(currentElement.name);
                if (flag)
                {
                    StyleSelectorHelper.FastLookup(styleSheet.orderedNameSelectors, matchedSelectors, context, currentElement.name, ref selectorMatchRecord);
                }
                foreach (string current in currentElement.GetClassesForIteration())
                {
                    StyleSelectorHelper.FastLookup(styleSheet.orderedClassSelectors, matchedSelectors, context, current, ref selectorMatchRecord);
                }
            }
        }
        static void FastLookup(IDictionary <string, StyleComplexSelector> table, List <SelectorMatchRecord> matchedSelectors, StyleMatchingContext context, string input, ref SelectorMatchRecord record)
        {
            StyleComplexSelector currentComplexSelector;

            if (table.TryGetValue(input, out currentComplexSelector))
            {
                while (currentComplexSelector != null)
                {
                    bool isCandidate        = true;
                    bool isMatchRightToLeft = false;

                    if (!currentComplexSelector.isSimple)
                    {
                        isCandidate = context.ancestorFilter.IsCandidate(currentComplexSelector);
                    }

                    if (isCandidate || s_VerifyBloomIntegrity)
                    {
                        isMatchRightToLeft = MatchRightToLeft(context.currentElement, currentComplexSelector, context.processResult);
                    }

                    // This verifies that the Bloom filter never rejects a valid complex selector.
                    if (s_VerifyBloomIntegrity)
                    {
                        Assert.IsTrue(isCandidate || !isMatchRightToLeft, "The Bloom filter returned a false negative match.");
                    }

                    if (isMatchRightToLeft)
                    {
                        record.complexSelector = currentComplexSelector;
                        matchedSelectors.Add(record);
                    }

                    currentComplexSelector = currentComplexSelector.nextInTable;
                }
            }
        }
        private static void FastLookup(IDictionary <string, StyleComplexSelector> table, List <SelectorMatchRecord> matchedSelectors, StyleMatchingContext context, string input, ref SelectorMatchRecord record)
        {
            StyleComplexSelector nextInTable;
            bool flag = table.TryGetValue(input, out nextInTable);

            if (flag)
            {
                while (nextInTable != null)
                {
                    bool flag2 = StyleSelectorHelper.MatchRightToLeft(context.currentElement, nextInTable, context.processResult);
                    if (flag2)
                    {
                        record.complexSelector = nextInTable;
                        matchedSelectors.Add(record);
                    }
                    nextInTable = nextInTable.nextInTable;
                }
            }
        }