Example #1
0
            public override void OnBeginElementTest(VisualElement element, List <RuleMatcher> ruleMatchers)
            {
                if (element.IsDirty(ChangeType.Styles))
                {
                    element.triggerPseudoMask    = (PseudoStates)0;
                    element.dependencyPseudoMask = (PseudoStates)0;
                }
                if (element != null && element.styleSheets != null)
                {
                    for (int i = 0; i < element.styleSheets.Count; i++)
                    {
                        StyleSheet             styleSheet       = element.styleSheets[i];
                        StyleComplexSelector[] complexSelectors = styleSheet.complexSelectors;
                        int val = ruleMatchers.Count + complexSelectors.Length;
                        ruleMatchers.Capacity = Math.Max(ruleMatchers.Capacity, val);
                        for (int j = 0; j < complexSelectors.Length; j++)
                        {
                            StyleComplexSelector complexSelector = complexSelectors[j];
                            ruleMatchers.Add(new RuleMatcher
                            {
                                sheet           = styleSheet,
                                complexSelector = complexSelector
                            });
                        }
                    }
                }
                this.m_MatchedRules.Clear();
                string fullTypeName = element.fullTypeName;
                long   num          = (long)fullTypeName.GetHashCode();

                this.m_MatchingRulesHash = (num * 397L ^ (long)this.currentPixelsPerPoint.GetHashCode());
            }
        public static void AddFakeSelector(VisualElement selectorElement)
        {
            if (selectorElement == null)
            {
                return;
            }

            var styleSheet = selectorElement.GetClosestStyleSheet();

            if (styleSheet == null)
            {
                return;
            }

            StyleComplexSelector complexSelector = selectorElement.GetProperty(BuilderConstants.ElementLinkedStyleSelectorVEPropertyName) as StyleComplexSelector;
            var fakeSelectorString = BuilderConstants.UssSelectorNameSymbol + selectorElement.name;

            var fakeSelector = styleSheet.FindSelector(fakeSelectorString); // May already exist because of Undo/Redo

            if (fakeSelector == null)
            {
                fakeSelector = styleSheet.AddSelector(fakeSelectorString);
            }

            fakeSelector.rule      = complexSelector.rule;
            fakeSelector.ruleIndex = complexSelector.ruleIndex; // shared index
            selectorElement.SetProperty(BuilderConstants.ElementLinkedFakeStyleSelectorVEPropertyName, fakeSelector);
            // To ensure that the fake selector is removed from the stylesheet if the builder gets closed with a selector still selected
            selectorElement.RegisterCallback <DetachFromPanelEvent>(OnSelectorElementDetachedFromPanel);
            selectorElement.IncrementVersion((VersionChangeType)(-1));
        }
Example #3
0
        public static bool CompareSelectors(StyleComplexSelector lhs, StyleComplexSelector rhs)
        {
            if (lhs.isSimple != rhs.isSimple ||
                lhs.specificity != rhs.specificity ||
                lhs.selectors.Length != rhs.selectors.Length)
            {
                return(false);
            }

            for (var i = 0; i < lhs.selectors.Length; ++i)
            {
                var lSelector = lhs.selectors[i];
                var rSelector = rhs.selectors[i];

                if (lSelector.parts.Length != rSelector.parts.Length)
                {
                    return(false);
                }

                if (lSelector.previousRelationship != rSelector.previousRelationship)
                {
                    return(false);
                }

                for (var j = 0; j < lSelector.parts.Length; ++j)
                {
                    if (!EqualityComparer <StyleSelectorPart> .Default.Equals(lSelector.parts[j], rSelector.parts[j]))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
 public Rule(string selectorName, StyleComplexSelector selector)
 {
     SelectorName     = selectorName;
     Selector         = selector;
     Properties       = new Dictionary <string, Property>();
     PseudoStateRules = new List <string>();
 }
Example #5
0
        internal static bool InitializeSelector(StyleComplexSelector complexSelector, string complexSelectorStr)
        {
            // Set specificity.
            int fullSpecificity = CSSSpecCopy.GetSelectorSpecificity(complexSelectorStr);

            if (fullSpecificity == 0)
            {
                Debug.LogError("Failed to calculate selector specificity: " + complexSelectorStr);
                return(false);
            }
            complexSelector.specificity = fullSpecificity;

            // Remove extra whitespace.
            var selectorSplit = complexSelectorStr.Split(' ');

            complexSelectorStr = String.Join(" ", selectorSplit);

            var simpleSelectors     = new List <StyleSelector>();
            var previousRelationsip = StyleSelectorRelationship.None;

            foreach (var simpleSelectorStr in selectorSplit)
            {
                if (simpleSelectorStr == s_DecendantSymbol)
                {
                    previousRelationsip = StyleSelectorRelationship.Child;
                    continue;
                }

                StyleSelectorPart[] parts;
                if (!CSSSpecCopy.ParseSelector(simpleSelectorStr, out parts))
                {
                    Debug.LogError(StyleSheetImportErrorCode.UnsupportedSelectorFormat + ": " + complexSelectorStr);
                    return(false);
                }
                if (parts.Any(p => p.type == StyleSelectorType.Unknown))
                {
                    Debug.LogError(StyleSheetImportErrorCode.UnsupportedSelectorFormat + ": " + complexSelectorStr);
                    return(false);
                }
                if (parts.Any(p => p.type == StyleSelectorType.RecursivePseudoClass))
                {
                    Debug.LogError(StyleSheetImportErrorCode.RecursiveSelectorDetected + ": " + complexSelectorStr);
                    return(false);
                }

                var simpleSelector = new StyleSelector();
                simpleSelector.parts = parts;
                simpleSelector.previousRelationship = previousRelationsip;
                simpleSelectors.Add(simpleSelector);

                // This is the default (if no > came before).
                previousRelationsip = StyleSelectorRelationship.Descendent;
            }

            complexSelector.selectors = simpleSelectors.ToArray();

            return(true);
        }
 public StyleSheetBuilder.ComplexSelectorScope BeginComplexSelector(int specificity)
 {
     StyleSheetBuilder.Log("Begin complex selector with specificity " + specificity);
     this.m_BuilderState                       = StyleSheetBuilder.BuilderState.ComplexSelector;
     this.m_CurrentComplexSelector             = new StyleComplexSelector();
     this.m_CurrentComplexSelector.specificity = specificity;
     this.m_CurrentComplexSelector.ruleIndex   = this.m_Rules.Count;
     return(new StyleSheetBuilder.ComplexSelectorScope(this));
 }
Example #7
0
        public void SetContext(StyleSheet sheet, StyleComplexSelector selector, StyleVariableContext varContext)
        {
            m_Sheet       = sheet;
            m_Properties  = selector.rule.properties;
            m_PropertyIds = StyleSheetCache.GetPropertyIds(sheet, selector.ruleIndex);
            m_Resolver.variableContext = varContext;

            LoadProperties();
        }
Example #8
0
 public void SetContext(StyleSheet sheet, StyleComplexSelector selector, StyleVariableContext varContext, float dpiScaling = 1f)
 {
     this.m_Sheet       = sheet;
     this.m_Properties  = selector.rule.properties;
     this.m_PropertyIds = StyleSheetCache.GetPropertyIds(sheet, selector.ruleIndex);
     this.m_Resolver.variableContext = varContext;
     this.dpiScaling = dpiScaling;
     this.LoadProperties();
 }
        public static void CachePseudoStateMasks(this StyleComplexSelector complexSelector)
        {
            // If we have already cached data on this selector, skip it
            if (complexSelector.selectors[0].pseudoStateMask != -1)
            {
                return;
            }

            // lazily build a cache of pseudo state names
            if (s_PseudoStates == null)
            {
                s_PseudoStates             = new Dictionary <string, PseudoStateData>();
                s_PseudoStates["active"]   = new PseudoStateData(PseudoStates.Active, false);
                s_PseudoStates["hover"]    = new PseudoStateData(PseudoStates.Hover, false);
                s_PseudoStates["checked"]  = new PseudoStateData(PseudoStates.Checked, false);
                s_PseudoStates["selected"] = new PseudoStateData(PseudoStates.Selected, false);
                s_PseudoStates["disabled"] = new PseudoStateData(PseudoStates.Disabled, false);
                s_PseudoStates["focus"]    = new PseudoStateData(PseudoStates.Focus, false);

                // A few substates can be negated, meaning them match if the flag is not set
                s_PseudoStates["inactive"] = new PseudoStateData(PseudoStates.Active, true);
                s_PseudoStates["enabled"]  = new PseudoStateData(PseudoStates.Disabled, true);
            }

            for (int j = 0, subCount = complexSelector.selectors.Length; j < subCount; j++)
            {
                StyleSelector       selector               = complexSelector.selectors[j];
                StyleSelectorPart[] parts                  = selector.parts;
                PseudoStates        pseudoClassMask        = 0;
                PseudoStates        negatedPseudoClassMask = 0;
                for (int i = 0; i < selector.parts.Length; i++)
                {
                    if (selector.parts[i].type == StyleSelectorType.PseudoClass)
                    {
                        PseudoStateData data;
                        if (s_PseudoStates.TryGetValue(parts[i].value, out data))
                        {
                            if (!data.negate)
                            {
                                pseudoClassMask |= data.state;
                            }
                            else
                            {
                                negatedPseudoClassMask |= data.state;
                            }
                        }
                        else
                        {
                            Debug.LogWarningFormat("Unknown pseudo class \"{0}\"", parts[i].value);
                        }
                    }
                }
                selector.pseudoStateMask        = (int)pseudoClassMask;
                selector.negatedPseudoStateMask = (int)negatedPseudoClassMask;
            }
        }
        public static bool MatchRightToLeft(VisualElement element, StyleComplexSelector complexSelector, Action <VisualElement, MatchResultInfo> processResult)
        {
            VisualElement visualElement  = element;
            int           i              = complexSelector.selectors.Length - 1;
            VisualElement visualElement2 = null;
            int           num            = -1;
            bool          result;

            while (i >= 0)
            {
                bool flag = visualElement == null;
                if (flag)
                {
                    break;
                }
                MatchResultInfo matchResultInfo = StyleSelectorHelper.MatchesSelector(visualElement, complexSelector.selectors[i]);
                processResult(visualElement, matchResultInfo);
                bool flag2 = !matchResultInfo.success;
                if (flag2)
                {
                    bool flag3 = i < complexSelector.selectors.Length - 1 && complexSelector.selectors[i + 1].previousRelationship == StyleSelectorRelationship.Descendent;
                    if (flag3)
                    {
                        visualElement = visualElement.parent;
                    }
                    else
                    {
                        bool flag4 = visualElement2 != null;
                        if (!flag4)
                        {
                            break;
                        }
                        visualElement = visualElement2;
                        i             = num;
                    }
                }
                else
                {
                    bool flag5 = i < complexSelector.selectors.Length - 1 && complexSelector.selectors[i + 1].previousRelationship == StyleSelectorRelationship.Descendent;
                    if (flag5)
                    {
                        visualElement2 = visualElement.parent;
                        num            = i;
                    }
                    bool flag6 = --i < 0;
                    if (flag6)
                    {
                        result = true;
                        return(result);
                    }
                    visualElement = visualElement.parent;
                }
            }
            result = false;
            return(result);
        }
Example #11
0
        public static string FindExtend(this StyleComplexSelector selector, StyleSheet sheet)
        {
            var property = FindProperty(selector, k_Extend);

            if (property == null)
            {
                return(null);
            }
            return(sheet.ReadString(property.values[0]));
        }
Example #12
0
        public static string FindStyleName(this StyleComplexSelector selector, string selectorStr, StyleSheet sheet)
        {
            var property = FindProperty(selector, k_Name);

            if (property == null)
            {
                return(ToStyleName(selectorStr));
            }
            return(sheet.ReadString(property.values[0]));
        }
Example #13
0
        public void SetContext(StyleSheet sheet, StyleComplexSelector selector, StyleVariableContext varContext)
        {
            m_Sheet       = sheet;
            m_Properties  = selector.rule.properties;
            m_PropertyIDs = StyleSheetCache.GetPropertyIDs(sheet, selector.ruleIndex);
            m_Resolver.variableContext = varContext;

            specificity = sheet.isUnityStyleSheet ? StyleValueExtensions.UnitySpecificity : selector.specificity;
            LoadProperties();
        }
        static VisualElement CreateNewSelectorElement(StyleSheet styleSheet, StyleComplexSelector complexSelector)
        {
            var ssVE     = new VisualElement();
            var ssVEName = BuilderConstants.StyleSelectorElementName + complexSelector.ruleIndex;

            ssVE.name = ssVEName;
            ssVE.SetProperty(BuilderConstants.ElementLinkedStyleSelectorVEPropertyName, complexSelector);

            return(ssVE);
        }
Example #15
0
 public static void BuildSelector(StyleComplexSelector complexSelector, StyleSheetBuilderHelper helper)
 {
     using (helper.builder.BeginComplexSelector(complexSelector.specificity))
     {
         foreach (var selector in complexSelector.selectors)
         {
             helper.builder.AddSimpleSelector(selector.parts, selector.previousRelationship);
         }
     }
 }
        public static string ToUssSelector(StyleComplexSelector complexSelector)
        {
            var sb = new StringBuilder();

            foreach (var selector in complexSelector.selectors)
            {
                ToUssString(selector.previousRelationship, selector.parts, sb);
            }
            return(sb.ToString());
        }
        public static string ToUssString(StyleSheet sheet, StyleComplexSelector complexSelector, StringBuilder stringBuilder = null)
        {
            var inlineBuilder = stringBuilder == null ? new StringBuilder() : stringBuilder;

            ToUssString(sheet, new UssExportOptions(), complexSelector, inlineBuilder);

            var result = inlineBuilder.ToString();

            return(result);
        }
Example #18
0
 public static void CachePseudoStateMasks(this StyleComplexSelector complexSelector)
 {
     if (complexSelector.selectors[0].pseudoStateMask == -1)
     {
         if (StyleComplexSelectorExtensions.s_PseudoStates == null)
         {
             StyleComplexSelectorExtensions.s_PseudoStates             = new Dictionary <string, StyleComplexSelectorExtensions.PseudoStateData>();
             StyleComplexSelectorExtensions.s_PseudoStates["active"]   = new StyleComplexSelectorExtensions.PseudoStateData(PseudoStates.Active, false);
             StyleComplexSelectorExtensions.s_PseudoStates["hover"]    = new StyleComplexSelectorExtensions.PseudoStateData(PseudoStates.Hover, false);
             StyleComplexSelectorExtensions.s_PseudoStates["checked"]  = new StyleComplexSelectorExtensions.PseudoStateData(PseudoStates.Checked, false);
             StyleComplexSelectorExtensions.s_PseudoStates["selected"] = new StyleComplexSelectorExtensions.PseudoStateData(PseudoStates.Selected, false);
             StyleComplexSelectorExtensions.s_PseudoStates["disabled"] = new StyleComplexSelectorExtensions.PseudoStateData(PseudoStates.Disabled, false);
             StyleComplexSelectorExtensions.s_PseudoStates["focus"]    = new StyleComplexSelectorExtensions.PseudoStateData(PseudoStates.Focus, false);
             StyleComplexSelectorExtensions.s_PseudoStates["inactive"] = new StyleComplexSelectorExtensions.PseudoStateData(PseudoStates.Active, true);
             StyleComplexSelectorExtensions.s_PseudoStates["enabled"]  = new StyleComplexSelectorExtensions.PseudoStateData(PseudoStates.Disabled, true);
         }
         int i   = 0;
         int num = complexSelector.selectors.Length;
         while (i < num)
         {
             StyleSelector       styleSelector = complexSelector.selectors[i];
             StyleSelectorPart[] parts         = styleSelector.parts;
             PseudoStates        pseudoStates  = (PseudoStates)0;
             PseudoStates        pseudoStates2 = (PseudoStates)0;
             for (int j = 0; j < styleSelector.parts.Length; j++)
             {
                 if (styleSelector.parts[j].type == StyleSelectorType.PseudoClass)
                 {
                     StyleComplexSelectorExtensions.PseudoStateData pseudoStateData;
                     if (StyleComplexSelectorExtensions.s_PseudoStates.TryGetValue(parts[j].value, out pseudoStateData))
                     {
                         if (!pseudoStateData.negate)
                         {
                             pseudoStates |= pseudoStateData.state;
                         }
                         else
                         {
                             pseudoStates2 |= pseudoStateData.state;
                         }
                     }
                     else
                     {
                         Debug.LogWarningFormat("Unknown pseudo class \"{0}\"", new object[]
                         {
                             parts[j].value
                         });
                     }
                 }
             }
             styleSelector.pseudoStateMask        = (int)pseudoStates;
             styleSelector.negatedPseudoStateMask = (int)pseudoStates2;
             i++;
         }
     }
 }
        public static void AddStyleComplexSelectorToSelection(StyleSheet styleSheet, StyleComplexSelector scs)
        {
            var selectionProp = styleSheet.AddProperty(
                scs,
                BuilderConstants.SelectedStyleRulePropertyName,
                BuilderConstants.ChangeSelectionUndoMessage);

            // Need to add at least one dummy value because lots of code will die
            // if it encounters a style property with no values.
            styleSheet.AddValue(
                selectionProp, 42.0f, BuilderConstants.ChangeSelectionUndoMessage);
        }
        public ComplexSelectorScope BeginComplexSelector(int specificity)
        {
            Log("Begin complex selector with specificity " + specificity);
            Debug.Assert(m_BuilderState == BuilderState.Rule);
            m_BuilderState = BuilderState.ComplexSelector;

            m_CurrentComplexSelector             = new StyleComplexSelector();
            m_CurrentComplexSelector.specificity = specificity;
            m_CurrentComplexSelector.ruleIndex   = m_Rules.Count;

            return(new ComplexSelectorScope(this));
        }
Example #21
0
        public static void CopySelector(StyleSheet sheet, UssComments comments, StyleComplexSelector complexSelector, StyleSheetBuilderHelper helper)
        {
            helper.BeginRule(comments.Get(complexSelector.rule));
            BuildSelector(complexSelector, helper);

            foreach (var property in complexSelector.rule.properties)
            {
                CopyProperty(sheet, comments, property, helper);
            }

            helper.EndRule();
        }
Example #22
0
        public static StyleComplexSelector CreateSimpleSelector(string styleName)
        {
            var cs = new StyleComplexSelector();

            StyleSelectorPart[] parts;
            CSSSpec.ParseSelector(styleName, out parts);
            var selector = new StyleSelector();

            selector.parts = parts;
            cs.selectors   = new[] { selector };
            return(cs);
        }
 public void EndComplexSelector()
 {
     StyleSheetBuilder.Log("Ending complex selector");
     this.m_BuilderState = StyleSheetBuilder.BuilderState.Rule;
     if (this.m_CurrentSelectors.Count > 0)
     {
         this.m_CurrentComplexSelector.selectors = this.m_CurrentSelectors.ToArray();
         this.m_ComplexSelectors.Add(this.m_CurrentComplexSelector);
         this.m_CurrentSelectors.Clear();
     }
     this.m_CurrentComplexSelector = null;
 }
Example #24
0
 private void FinishSelector()
 {
     FinishCurrentSelector();
     if (styleSelectors.Count > 0)
     {
         var selector = new StyleComplexSelector();
         selector.selectors = styleSelectors.ToArray();
         styleSelectors.Clear();
         m_Matchers.Add(new RuleMatcher {
             complexSelector = selector
         });
     }
 }
Example #25
0
        public static void ToUssString(StyleSheet sheet, UssExportOptions options, StyleComplexSelector complexSelector, StringBuilder sb)
        {
            foreach (var selector in complexSelector.selectors)
            {
                ToUssString(selector.previousRelationship, selector.parts, sb);
            }

            sb.Append(" {\n");

            ToUssString(sheet, options, complexSelector.rule, sb);

            sb.Append("}");
            sb.Append("\n");
        }
Example #26
0
 private void FinishSelector()
 {
     this.FinishCurrentSelector();
     if (this.styleSelectors.Count > 0)
     {
         StyleComplexSelector styleComplexSelector = new StyleComplexSelector();
         styleComplexSelector.selectors = this.styleSelectors.ToArray();
         this.styleSelectors.Clear();
         this.m_Matchers.Add(new RuleMatcher
         {
             complexSelector = styleComplexSelector
         });
     }
 }
        public static void ToUssString(StyleSheet sheet, UssExportOptions options, StyleComplexSelector complexSelector, StringBuilder sb)
        {
            foreach (var selector in complexSelector.selectors)
            {
                ToUssString(selector.previousRelationship, selector.parts, sb);
            }

            sb.Append(" {");
            sb.Append(BuilderConstants.newlineCharFromEditorSettings);

            ToUssString(sheet, options, complexSelector.rule, sb);

            sb.Append("}");
            sb.Append(BuilderConstants.newlineCharFromEditorSettings);
        }
        public void EndComplexSelector()
        {
            Log("Ending complex selector");

            Debug.Assert(m_BuilderState == BuilderState.ComplexSelector);
            m_BuilderState = BuilderState.Rule;

            if (m_CurrentSelectors.Count > 0)
            {
                m_CurrentComplexSelector.selectors = m_CurrentSelectors.ToArray();
                m_ComplexSelectors.Add(m_CurrentComplexSelector);
                m_CurrentSelectors.Clear();
            }
            m_CurrentComplexSelector = null;
        }
        public static StyleComplexSelector AddSelector(
            this StyleSheet styleSheet, string complexSelectorStr, string undoMessage = null)
        {
            // Undo/Redo
            if (string.IsNullOrEmpty(undoMessage))
            {
                undoMessage = "New UI Style Selector";
            }
            Undo.RegisterCompleteObjectUndo(styleSheet, undoMessage);

            // Remove extra whitespace.
            var selectorSplit = complexSelectorStr.Split(' ');

            complexSelectorStr = String.Join(" ", selectorSplit);

            // Create rule.
            var rule = new StyleRule {
                line = -1
            };

            rule.properties = new StyleProperty[0];

            // Create selector.
            var complexSelector = new StyleComplexSelector();

            complexSelector.rule = rule;
            var initResult = StyleComplexSelectorExtensions.InitializeSelector(complexSelector, complexSelectorStr);

            if (!initResult)
            {
                return(null);
            }

            // Add rule to StyleSheet.
            var rulesList = styleSheet.rules.ToList();

            rulesList.Add(rule);
            styleSheet.rules          = rulesList.ToArray();
            complexSelector.ruleIndex = styleSheet.rules.Length - 1;

            // Add complex selector to list in stylesheet.
            var complexSelectorsList = styleSheet.complexSelectors.ToList();

            complexSelectorsList.Add(complexSelector);
            styleSheet.complexSelectors = complexSelectorsList.ToArray();

            return(complexSelector);
        }
Example #30
0
        private void PushStyleSheet(StyleSheet styleSheetData)
        {
            StyleComplexSelector[] complexSelectors = styleSheetData.complexSelectors;
            int val = this.ruleMatchers.Count + complexSelectors.Length;

            this.ruleMatchers.Capacity = Math.Max(this.ruleMatchers.Capacity, val);
            for (int i = 0; i < complexSelectors.Length; i++)
            {
                StyleComplexSelector complexSelector = complexSelectors[i];
                this.ruleMatchers.Add(new RuleMatcher
                {
                    sheet           = styleSheetData,
                    complexSelector = complexSelector
                });
            }
        }