예제 #1
0
        public StyleSheet CompileStyleSheetContent(string styleSheetContent, bool disableValidation, bool reportErrors)
        {
            var importer   = new StyleSheetImporterImpl();
            var styleSheet = ScriptableObject.CreateInstance <StyleSheet>();

            importer.disableValidation = disableValidation;
            importer.Import(styleSheet, styleSheetContent);
            if (reportErrors)
            {
                foreach (var err in importer.importErrors)
                {
                    Debug.LogFormat(LogType.Warning, LogOption.NoStacktrace, styleSheet, err.ToString());
                }
            }
            return(styleSheet);
        }
예제 #2
0
        private static bool ParseAttributes(XElement elt, VisualElementAsset res, StyleSheetBuilder ssb, VisualTreeAsset vta, VisualElementAsset parent)
        {
            // underscore means "unnamed element but it would not look pretty in the project window without one"
            res.name = "_" + res.GetType().Name;

            bool startedRule = false;

            foreach (XAttribute xattr in elt.Attributes())
            {
                string attrName = xattr.Name.LocalName;

                // start with special cases
                switch (attrName)
                {
                case "name":
                    res.name = xattr.Value;
                    continue;

                case "text":
                    res.text = xattr.Value;
                    continue;

                case "class":
                    res.classes = xattr.Value.Split(' ');
                    continue;

                case "contentContainer":
                    if (vta.contentContainerId != 0)
                    {
                        logger.LogError(ImportErrorType.Semantic, ImportErrorCode.DuplicateContentContainer, null, Error.Level.Fatal, elt);
                        continue;
                    }
                    vta.contentContainerId = res.id;
                    continue;

                case k_SlotDefinitionAttr:
                    if (String.IsNullOrEmpty(xattr.Value))
                    {
                        logger.LogError(ImportErrorType.Semantic, ImportErrorCode.SlotDefinitionHasEmptyName, null, Error.Level.Fatal, elt);
                    }
                    else if (!vta.AddSlotDefinition(xattr.Value, res.id))
                    {
                        logger.LogError(ImportErrorType.Semantic, ImportErrorCode.DuplicateSlotDefinition, xattr.Value, Error.Level.Fatal, elt);
                    }
                    continue;

                case k_SlotUsageAttr:
                    var templateAsset = parent as TemplateAsset;
                    if (!(templateAsset != null))
                    {
                        logger.LogError(ImportErrorType.Semantic, ImportErrorCode.SlotUsageInNonTemplate, parent, Error.Level.Fatal, elt);
                        continue;
                    }
                    if (string.IsNullOrEmpty(xattr.Value))
                    {
                        logger.LogError(ImportErrorType.Semantic, ImportErrorCode.SlotUsageHasEmptyName, null, Error.Level.Fatal, elt);
                        continue;
                    }
                    templateAsset.AddSlotUsage(xattr.Value, res.id);
                    continue;

                case "style":
                    ExCSS.StyleSheet parsed = new Parser().Parse("* { " + xattr.Value + " }");
                    if (parsed.Errors.Count != 0)
                    {
                        logger.LogError(
                            ImportErrorType.Semantic,
                            ImportErrorCode.InvalidCssInStyleAttribute,
                            parsed.Errors.Aggregate("", (s, error) => s + error.ToString() + "\n"),
                            Error.Level.Warning,
                            xattr);
                        continue;
                    }
                    if (parsed.StyleRules.Count != 1)
                    {
                        logger.LogError(
                            ImportErrorType.Semantic,
                            ImportErrorCode.InvalidCssInStyleAttribute,
                            "Expected one style rule, found " + parsed.StyleRules.Count,
                            Error.Level.Warning,
                            xattr);
                        continue;
                    }
                    ssb.BeginRule(-1);
                    startedRule = true;
                    StyleSheetImportErrors errors = new StyleSheetImportErrors();
                    foreach (Property prop in parsed.StyleRules[0].Declarations)
                    {
                        ssb.BeginProperty(prop.Name);
                        StyleSheetImporterImpl.VisitValue(errors, ssb, prop.Term);
                        ssb.EndProperty();
                    }

                    // Don't call ssb.EndRule() here, it's done in LoadXml to get the rule index at the same time !
                    continue;

                case "pickingMode":
                    if (!Enum.IsDefined(typeof(PickingMode), xattr.Value))
                    {
                        Debug.LogErrorFormat("Could not parse value of '{0}', because it isn't defined in the PickingMode enum.", xattr.Value);
                        continue;
                    }
                    res.pickingMode = (PickingMode)Enum.Parse(typeof(PickingMode), xattr.Value);
                    continue;
                }

                res.AddProperty(xattr.Name.LocalName, xattr.Value);
            }
            return(startedRule);
        }
예제 #3
0
        private static bool ParseAttributes(XElement elt, VisualElementAsset res, StyleSheetBuilder ssb, VisualTreeAsset vta, VisualElementAsset parent)
        {
            res.name = "_" + res.GetType().Name;
            bool result = false;

            foreach (XAttribute current in elt.Attributes())
            {
                string localName = current.Name.LocalName;
                switch (localName)
                {
                case "name":
                    res.name = current.Value;
                    continue;

                case "text":
                    res.text = current.Value;
                    continue;

                case "class":
                    res.classes = current.Value.Split(new char[]
                    {
                        ' '
                    });
                    continue;

                case "contentContainer":
                    if (vta.contentContainerId != 0)
                    {
                        UIElementsViewImporter.logger.LogError(ImportErrorType.Semantic, ImportErrorCode.DuplicateContentContainer, null, UIElementsViewImporter.Error.Level.Fatal, elt);
                        continue;
                    }
                    vta.contentContainerId = res.id;
                    continue;

                case "slot-name":
                    if (string.IsNullOrEmpty(current.Value))
                    {
                        UIElementsViewImporter.logger.LogError(ImportErrorType.Semantic, ImportErrorCode.SlotDefinitionHasEmptyName, null, UIElementsViewImporter.Error.Level.Fatal, elt);
                    }
                    else if (!vta.AddSlotDefinition(current.Value, res.id))
                    {
                        UIElementsViewImporter.logger.LogError(ImportErrorType.Semantic, ImportErrorCode.DuplicateSlotDefinition, current.Value, UIElementsViewImporter.Error.Level.Fatal, elt);
                    }
                    continue;

                case "slot":
                {
                    TemplateAsset templateAsset = parent as TemplateAsset;
                    if (templateAsset == null)
                    {
                        UIElementsViewImporter.logger.LogError(ImportErrorType.Semantic, ImportErrorCode.SlotUsageInNonTemplate, parent, UIElementsViewImporter.Error.Level.Fatal, elt);
                        continue;
                    }
                    if (string.IsNullOrEmpty(current.Value))
                    {
                        UIElementsViewImporter.logger.LogError(ImportErrorType.Semantic, ImportErrorCode.SlotUsageHasEmptyName, null, UIElementsViewImporter.Error.Level.Fatal, elt);
                        continue;
                    }
                    templateAsset.AddSlotUsage(current.Value, res.id);
                    continue;
                }

                case "style":
                {
                    StyleSheet styleSheet = new Parser().Parse("* { " + current.Value + " }");
                    if (styleSheet.Errors.Count != 0)
                    {
                        UIElementsViewImporter.logger.LogError(ImportErrorType.Semantic, ImportErrorCode.InvalidCssInStyleAttribute, styleSheet.Errors.Aggregate("", (string s, StylesheetParseError error) => s + error.ToString() + "\n"), UIElementsViewImporter.Error.Level.Warning, current);
                        continue;
                    }
                    if (styleSheet.StyleRules.Count != 1)
                    {
                        UIElementsViewImporter.logger.LogError(ImportErrorType.Semantic, ImportErrorCode.InvalidCssInStyleAttribute, "Expected one style rule, found " + styleSheet.StyleRules.Count, UIElementsViewImporter.Error.Level.Warning, current);
                        continue;
                    }
                    ssb.BeginRule(-1);
                    result = true;
                    StyleSheetImportErrors errors = new StyleSheetImportErrors();
                    foreach (Property current2 in styleSheet.StyleRules[0].Declarations)
                    {
                        ssb.BeginProperty(current2.Name);
                        StyleSheetImporterImpl.VisitValue(errors, ssb, current2.Term);
                        ssb.EndProperty();
                    }
                    continue;
                }

                case "pickingMode":
                    if (!Enum.IsDefined(typeof(PickingMode), current.Value))
                    {
                        Debug.LogErrorFormat("Could not parse value of '{0}', because it isn't defined in the PickingMode enum.", new object[]
                        {
                            current.Value
                        });
                        continue;
                    }
                    res.pickingMode = (PickingMode)Enum.Parse(typeof(PickingMode), current.Value);
                    continue;
                }
                res.AddProperty(current.Name.LocalName, current.Value);
            }
            return(result);
        }
        private static bool ParseAttributes(XElement elt, VisualElementAsset res, StyleSheetBuilder ssb, VisualTreeAsset vta, VisualElementAsset parent)
        {
            bool startedRule = false;

            foreach (XAttribute xattr in elt.Attributes())
            {
                string attrName = xattr.Name.LocalName;

                // start with special cases
                switch (attrName)
                {
                    case "class":
                        res.classes = xattr.Value.Split(' ');
                        continue;
                    case "contentContainer":
                        if (vta.contentContainerId != 0)
                        {
                            logger.LogError(ImportErrorType.Semantic, ImportErrorCode.DuplicateContentContainer, null, Error.Level.Fatal, elt);
                            continue;
                        }
                        vta.contentContainerId = res.id;
                        continue;
                    case k_SlotDefinitionAttr:
                        if (String.IsNullOrEmpty(xattr.Value))
                            logger.LogError(ImportErrorType.Semantic, ImportErrorCode.SlotDefinitionHasEmptyName, null, Error.Level.Fatal, elt);
                        else if (!vta.AddSlotDefinition(xattr.Value, res.id))
                            logger.LogError(ImportErrorType.Semantic, ImportErrorCode.DuplicateSlotDefinition, xattr.Value, Error.Level.Fatal, elt);
                        continue;
                    case k_SlotUsageAttr:
                        var templateAsset = parent as TemplateAsset;
                        if (templateAsset == null)
                        {
                            logger.LogError(ImportErrorType.Semantic, ImportErrorCode.SlotUsageInNonTemplate, parent, Error.Level.Fatal, elt);
                            continue;
                        }
                        if (string.IsNullOrEmpty(xattr.Value))
                        {
                            logger.LogError(ImportErrorType.Semantic, ImportErrorCode.SlotUsageHasEmptyName, null, Error.Level.Fatal, elt);
                            continue;
                        }
                        templateAsset.AddSlotUsage(xattr.Value, res.id);
                        continue;
                    case "style":
                        ExCSS.StyleSheet parsed = new Parser().Parse("* { " + xattr.Value + " }");
                        if (parsed.Errors.Count != 0)
                        {
                            logger.LogError(
                                ImportErrorType.Semantic,
                                ImportErrorCode.InvalidCssInStyleAttribute,
                                parsed.Errors.Aggregate("", (s, error) => s + error.ToString() + "\n"),
                                Error.Level.Warning,
                                xattr);
                            continue;
                        }
                        if (parsed.StyleRules.Count != 1)
                        {
                            logger.LogError(
                                ImportErrorType.Semantic,
                                ImportErrorCode.InvalidCssInStyleAttribute,
                                "Expected one style rule, found " + parsed.StyleRules.Count,
                                Error.Level.Warning,
                                xattr);
                            continue;
                        }
                        ssb.BeginRule(-1);
                        startedRule = true;
                        StyleSheetImportErrors errors = new StyleSheetImportErrors();
                        foreach (Property prop in parsed.StyleRules[0].Declarations)
                        {
                            ssb.BeginProperty(prop.Name);
                            StyleSheetImporterImpl.VisitValue(errors, ssb, prop.Term);
                            ssb.EndProperty();
                        }

                        // Don't call ssb.EndRule() here, it's done in LoadXml to get the rule index at the same time !
                        continue;
                }

                res.AddProperty(xattr.Name.LocalName, xattr.Value);
            }
            return startedRule;
        }