internal static void ImportXml(string xmlPath, out VisualTreeAsset vta) { vta = ScriptableObject.CreateInstance <VisualTreeAsset>(); vta.visualElementAssets = new List <VisualElementAsset>(); vta.templateAssets = new List <TemplateAsset>(); XDocument doc; try { doc = XDocument.Load(xmlPath, LoadOptions.SetLineInfo); } catch (Exception e) { logger.LogError(ImportErrorType.Syntax, ImportErrorCode.InvalidXml, e, Error.Level.Fatal, null); return; } StyleSheetBuilder ssb = new StyleSheetBuilder(); LoadXmlRoot(doc, vta, ssb); StyleSheet inlineSheet = ScriptableObject.CreateInstance <StyleSheet>(); inlineSheet.name = "inlineStyle"; ssb.BuildTo(inlineSheet); vta.inlineSheet = inlineSheet; }
private static void LoadXmlRoot(XDocument doc, VisualTreeAsset vta, StyleSheetBuilder ssb) { XElement elt = doc.Root; if (!string.Equals(elt.Name.LocalName, k_TemplateNode, k_Comparison)) { logger.LogError(ImportErrorType.Semantic, ImportErrorCode.InvalidRootElement, elt.Name, Error.Level.Fatal, elt); return; } foreach (var child in elt.Elements()) { switch (child.Name.LocalName) { case k_UsingNode: LoadUsingNode(vta, elt, child); break; default: LoadXml(child, null, vta, ssb); continue; } } }
public static StyleSheetBuilder WithUnstyledColor( this StyleSheetBuilder @this, Color color) { @this._styleSheet.UnstyledColor = color; return(@this); }
private static void LoadXmlRoot(XDocument doc, VisualTreeAsset vta, StyleSheetBuilder ssb) { XElement root = doc.Root; if (!string.Equals(root.Name.LocalName, "UXML", StringComparison.InvariantCulture)) { UIElementsViewImporter.logger.LogError(ImportErrorType.Semantic, ImportErrorCode.InvalidRootElement, root.Name, UIElementsViewImporter.Error.Level.Fatal, root); } else { foreach (XElement current in root.Elements()) { string localName = current.Name.LocalName; if (localName != null) { if (localName == "Using") { UIElementsViewImporter.LoadUsingNode(vta, root, current); continue; } } UIElementsViewImporter.LoadXml(current, null, vta, ssb); } } }
public static StyleSheetBuilder WithStyle( this StyleSheetBuilder @this, [RegexPattern] string pattern, Color color) { @this._styleSheet.AddStyle(pattern, color); return(@this); }
public static StyleSheetBuilder WithStyle( this StyleSheetBuilder @this, [RegexPattern] string pattern, Color color, MatchFoundLite matchTransformer) { @this._styleSheet.AddStyle(pattern, color, matchTransformer); return(@this); }
private static EngineBuilder EnableCssMoz(this EngineBuilder builder) { var stream = Assembly.GetExecutingAssembly() .GetManifestResourceStream("Knyaz.Optimus.Tests.Resources.moz_default.css"); var defaultCss = StyleSheetBuilder.CreateStyleSheet(new StreamReader(stream)); builder.EnableCss(config => config.UserAgentStyleSheet = defaultCss); return(builder); }
internal StyleValueImporter() { m_Context = null; m_AssetPath = null; m_Parser = new Parser(); m_Builder = new StyleSheetBuilder(); m_Errors = new StyleSheetImportErrors(); m_Validator = new StyleValidator(); }
public void DefaultComputedStyle() { var defaultStyleSheet = StyleSheetBuilder.CreateStyleSheet(new StringReader("DIV {display:block;font-size:16px;}"), s => null); var styling = new DocumentStyling(_document, defaultStyleSheet, null); _document.Body.AppendChild(_div); styling.GetComputedStyle(_div).Assert(style => style.GetPropertyValue("display") == "block" && style.GetPropertyValue("font-size") == "16px"); }
public StyleValueImporter(UnityEditor.AssetImporters.AssetImportContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } m_Context = context; m_AssetPath = context.assetPath; m_Parser = new Parser(); m_Builder = new StyleSheetBuilder(); m_Errors = new StyleSheetImportErrors(); m_Validator = new StyleValidator(); }
public void ImportTest(string css) { var ss = StyleSheetBuilder.CreateStyleSheet( new StringReader(css), s => s == "a.css" ? new StringReader("div{color:green}") : null); ss.Assert(styleSheet => styleSheet.CssRules.Count == 2 && ((CssStyleRule)styleSheet.CssRules[0]).SelectorText == "div" && ((CssStyleRule)styleSheet.CssRules[0]).Style.Length == 1 && ((CssStyleRule)styleSheet.CssRules[0]).Style.GetPropertyValue("color") == "green" && ((CssStyleRule)styleSheet.CssRules[1]).SelectorText == "div" && ((CssStyleRule)styleSheet.CssRules[1]).Style.Length == 1 && ((CssStyleRule)styleSheet.CssRules[1]).Style.GetPropertyValue("background-color") == "red"); }
public StyleSheet ParseQLSStream(TextReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } var inputStream = new AntlrInputStream(reader); var lexer = new QLSLexer(inputStream); var tokens = new CommonTokenStream(lexer); var parser = new QLSParser(tokens); var visitor = new StyleSheetBuilder(); return visitor.Visit(parser.stylesheet()); }
public MemoryStream CreateReport(DataTable result, string reportName = "") { var memoryStream = new MemoryStream(); var spreadsheetDocument = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook); var workbookPart = spreadsheetDocument.AddWorkbookPart(); workbookPart.Workbook = new Workbook(); var worksheetPart = workbookPart.AddNewPart <WorksheetPart>(); var sheetData = new SheetData(); WorkbookStylesPart workbookStylesPart = workbookPart.AddNewPart <WorkbookStylesPart>(); workbookStylesPart.Stylesheet = StyleSheetBuilder.CreateStylesheet(); workbookStylesPart.Stylesheet.Save(); var workSheet = new Worksheet(); var columns = BuildColumns(result); workSheet.Append(columns); workSheet.Append(sheetData); worksheetPart.Worksheet = workSheet; _reportName = StringExtensions.Coalesce(_reportName, reportName); var sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(new Sheets()); var sheet = new Sheet { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = _reportName }; sheets.AppendChild(sheet); BuildWorksheetHeader(sheetData); BuildHeaderRow(sheetData, result.Columns); BuildDataRows(sheetData, result.Rows); workbookPart.Workbook.Save(); spreadsheetDocument.Close(); memoryStream.Seek(0, SeekOrigin.Begin); return(memoryStream); }
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); }
private static void LoadXml(XElement elt, VisualElementAsset parent, VisualTreeAsset vta, StyleSheetBuilder ssb) { VisualElementAsset vea; if (!ResolveType(elt, vta, out vea)) { logger.LogError(ImportErrorType.Semantic, ImportErrorCode.UnknownElement, elt.Name.LocalName, Error.Level.Fatal, elt); return; } var parentId = (parent == null ? 0 : parent.id); // id includes the parent id, meaning it's dependent on the whole direct hierarchy int id = (parentId << 1) ^ vea.GetHashCode(); vea.parentId = parentId; vea.id = id; bool startedRule = ParseAttributes(elt, vea, ssb, vta, parent); // each vea will creates 0 or 1 style rule, with one or more properties // they don't have selectors and are directly referenced by index // it's then applied during tree cloning vea.ruleIndex = startedRule ? ssb.EndRule() : -1; if (vea is TemplateAsset) { vta.templateAssets.Add((TemplateAsset)vea); } else { vta.visualElementAssets.Add(vea); } if (elt.HasElements) { foreach (XElement child in elt.Elements()) { if (child.Name.LocalName == k_StyleReferenceNode) { LoadStyleReferenceNode(vea, child); } else { LoadXml(child, vea, vta, ssb); } } } }
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; }
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 CssStyleSheet Build(string css) { return(StyleSheetBuilder.CreateStyleSheet(new StringReader(css), s => null)); }
private static void LoadXml(XElement elt, VisualElementAsset parent, VisualTreeAsset vta, StyleSheetBuilder ssb) { VisualElementAsset visualElementAsset; if (!UIElementsViewImporter.ResolveType(elt, vta, out visualElementAsset)) { UIElementsViewImporter.logger.LogError(ImportErrorType.Semantic, ImportErrorCode.UnknownElement, elt.Name.LocalName, UIElementsViewImporter.Error.Level.Fatal, elt); } else { int num = (parent != null) ? parent.id : 0; int id = num << 1 ^ visualElementAsset.GetHashCode(); visualElementAsset.parentId = num; visualElementAsset.id = id; bool flag = UIElementsViewImporter.ParseAttributes(elt, visualElementAsset, ssb, vta, parent); visualElementAsset.ruleIndex = ((!flag) ? -1 : ssb.EndRule()); if (visualElementAsset is TemplateAsset) { vta.templateAssets.Add((TemplateAsset)visualElementAsset); } else { vta.visualElementAssets.Add(visualElementAsset); } if (elt.HasElements) { foreach (XElement current in elt.Elements()) { if (current.Name.LocalName == "Style") { UIElementsViewImporter.LoadStyleReferenceNode(visualElementAsset, current); } else { UIElementsViewImporter.LoadXml(current, visualElementAsset, vta, ssb); } } } } }