示例#1
0
		public Template() {
			members[MainRenderFunctionName] = new RenderFunctionMember(MainRenderFunctionName, "");
			#if SERVER
				serverUsings["System"] = null;
				serverUsings["System.Collections.Generic"] = null;
				serverUsings["System.Text"] = null;
				serverUsings["Saltarelle"] = null;
				serverUsings["Saltarelle.Ioc"] = null;

				clientUsings["System"] = null;
				clientUsings["System.Collections"] = null;
				clientUsings["System.Collections.Generic"] = null;
				clientUsings["System.Html"] = null;
				clientUsings["Saltarelle"] = null;
				clientUsings["Saltarelle.Ioc"] = null;
			#endif
		}
		internal static void ProcessDefFragment(IDocumentProcessor docProcessor, XmlNode node, ITemplate template) {
			XmlAttribute nameAttr   = (XmlAttribute)node.Attributes.GetNamedItem("name");
			XmlAttribute paramsAttr = (XmlAttribute)node.Attributes.GetNamedItem("params");
			
			if (nameAttr == null)
				throw ParserUtils.TemplateErrorException("The <def-fragment> element must have the name attribute specified.");
			string name = nameAttr.Value;
			if (!ParserUtils.IsValidUnqualifiedName(name))
				throw ParserUtils.TemplateErrorException("The name " + name + " is not a valid unqualified identifier.");
			if (template.HasMember(name))
				throw ParserUtils.TemplateErrorException("Duplicate definition of member " + name + ".");

			RenderFunctionMember m = new RenderFunctionMember(nameAttr.Value, paramsAttr != null ? paramsAttr.Value : "");

			Utils.DoForEachChild(node, delegate(XmlNode n) {
				docProcessor.ProcessRecursive(n, template, m);
			});

			if (template.HasMember(name))
				throw ParserUtils.TemplateErrorException("Duplicate definition of member " + name + "."); // Just in case it has already been added during the recursive call.
			template.AddMember(m);
		}
		private void TestWriteDefinition_Works(bool server) {
			ITemplate tpl = mocks.StrictMock<ITemplate>();
			if (!server)
				Expect.Call(tpl.EnableClientCreate).Return(true);
			mocks.ReplayAll();
		
			var cb = new CodeBuilder();

			var f1 = mocks.StrictMock<IFragment>();
			var f2 = mocks.StrictMock<IFragment>();
			
			using (mocks.Ordered()) {
				Expect.Call(f1.TryMergeWithNext(f2)).Return(null);
				Expect.Call(() => f1.WriteCode(tpl, server ? FragmentCodePoint.ServerRender : FragmentCodePoint.ClientRender, cb)).Do((Action<ITemplate, FragmentCodePoint, CodeBuilder>)((_, __, x) => x.AppendLine("[a]")));
				Expect.Call(() => f2.WriteCode(tpl, server ? FragmentCodePoint.ServerRender : FragmentCodePoint.ClientRender, cb)).Do((Action<ITemplate, FragmentCodePoint, CodeBuilder>)((_, __, x) => x.AppendLine("[b]")));
			}
		
			string expected = "private string Method(int p1, string param2) {" + Environment.NewLine
			                +  "	StringBuilder sb = new StringBuilder();" + Environment.NewLine
			                +  "	[a]" + Environment.NewLine
			                +  "	[b]" + Environment.NewLine
			                +  "	return sb.ToString();" + Environment.NewLine
			                +  "}" + Environment.NewLine + Environment.NewLine;
			                       
			mocks.ReplayAll();

			var member = new RenderFunctionMember("Method", "int p1, string param2");
			member.AddFragment(f1);
			member.AddFragment(f2);
			if (server)
				member.WriteCode(tpl, MemberCodePoint.ServerDefinition, cb);
			else
				member.WriteCode(tpl, MemberCodePoint.ClientDefinition, cb);
			Assert.AreEqual(expected, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			
			mocks.VerifyAll();
		}
示例#4
0
        public override bool Equals(object obj)
        {
            RenderFunctionMember other = obj as RenderFunctionMember;

            if (other == null || other.name != name || other.parameters != parameters || other.fragments.Count != fragments.Count || other.dependencies.Count != dependencies.Count)
            {
                return(false);
            }
            for (int i = 0; i < fragments.Count; i++)
            {
                if (!other.fragments[i].Equals(fragments[i]))
                {
                    return(false);
                }
            }
            for (int i = 0; i < dependencies.Count; i++)
            {
                if (other.dependencies[i] != dependencies[i])
                {
                    return(false);
                }
            }
            return(true);
        }
		public bool TryProcess(IDocumentProcessor docProcessor, XmlNode node, bool isRoot, ITemplate template, IRenderFunction currentRenderFunction) {
			if (node.NodeType != XmlNodeType.Element || node.Name != "control")
				return false;
			
			if (isRoot)
				throw ParserUtils.TemplateErrorException("The root element can not be a control.");

			string id = null;
			string type = null;
			bool customInstantiate = false;
			Dictionary<string, TypedMarkupData> additionalProperties = new Dictionary<string, TypedMarkupData>();

			Utils.DoForEachAttribute(node, delegate(XmlAttribute attr) {
				if (attr.Name == "id") {
					if (!ParserUtils.IsValidUnqualifiedName(attr.Value))
						throw ParserUtils.TemplateErrorException("The id '" + attr.Value + "' is not a valid identifier.");
					id = attr.Value;
				}
				else if (attr.Name == "type") {
					if (string.IsNullOrEmpty(attr.Value))
						throw ParserUtils.TemplateErrorException("The control type '" + attr.Value + "' is invalid.");
					type = attr.Value;
				}
				else if (attr.Name == "customInstantiate") {
					string v = attr.Value.ToLowerCase();
					customInstantiate = Utils.ParseBool(v);
				}
				else {
					additionalProperties[attr.Name] = docProcessor.ParseTypedMarkup(attr.Value);
				}
			});
			
			if (customInstantiate && additionalProperties.Count > 0)
				throw ParserUtils.TemplateErrorException("There can not be any property assignments when customInstantiate is true.");

			if (type == null)
				throw ParserUtils.TemplateErrorException("The control '" + id + "' does not have a type specified.");
			if (id == null)
				id = template.GetUniqueId();
			if (template.HasMember(id))
				throw ParserUtils.TemplateErrorException("Duplicate definition of member " + id);

			var dependencies = new List<IMember>();
			int numInnerFragments = 0;
			if (Utils.GetNumChildNodes(node) > 0) {
				Utils.DoForEachChild(node, delegate(XmlNode n) {
					if (n.OuterXml.Trim() != "") {
						numInnerFragments++;
						string innerName = id + "_inner" + Utils.ToStringInvariantInt(numInnerFragments);
						if (template.HasMember(innerName))
							throw ParserUtils.TemplateErrorException("The internal name " + innerName + " is already in use.");
						IRenderFunction innerFunction = new RenderFunctionMember(innerName, "");
						template.AddMember((IMember)innerFunction);
						docProcessor.ProcessRecursive(n, template, innerFunction);
						dependencies.Add(innerFunction);
					}
				});
			}
			
			if (!template.HasMember("Container"))
				template.AddMember(new PropertyMember("Container", "IContainer", "IContainer", AccessModifier._Public, "_container", "IContainer", "IContainer", true, true, null, true));

			IMember controlMember = new InstantiatedControlMember(id, type, customInstantiate, additionalProperties, dependencies);
			template.AddMember(controlMember);

			currentRenderFunction.AddFragment(new InstantiatedControlFragment(id, customInstantiate, numInnerFragments));
			currentRenderFunction.AddDependency(controlMember);

			return true;
		}
		public void TestRender_Works() {
			var tpl = mocks.StrictMock<ITemplate>();
			var ctl = mocks.StrictMock<IInstantiatedTemplateControl>();
			var f1  = mocks.StrictMock<IFragment>();
			var f2  = mocks.StrictMock<IFragment>();
			Expect.Call(f1.TryMergeWithNext(f2)).Return(null);
			Expect.Call(() => f1.Render(null, null, null)).IgnoreArguments().Constraints(Is.Same(tpl), Is.Same(ctl), Is.NotNull()).Do((Action<ITemplate, IInstantiatedTemplateControl, StringBuilder>)((_, __, sb) => sb.Append("[a]")));
			Expect.Call(() => f2.Render(null, null, null)).IgnoreArguments().Constraints(Is.Same(tpl), Is.Same(ctl), Is.NotNull()).Do((Action<ITemplate, IInstantiatedTemplateControl, StringBuilder>)((_, __, sb) => sb.Append("[b]")));
			mocks.ReplayAll();
			var m = new RenderFunctionMember("Test", "");
			m.AddFragment(f1);
			m.AddFragment(f2);
			Assert.AreEqual("[a][b]", m.Render(tpl, ctl));
			mocks.VerifyAll();
		}
		private RenderFunctionMember CreateRenderFunction(string name, string parameters, params IFragment[] fragments) {
			var result = new RenderFunctionMember(name, parameters);
			foreach (IFragment f in fragments)
				result.AddFragment(f);
			return result;
		}