WriteAttribute() public method

Write the attribute to the output stream.
Writes the attribute to the output stream.
/// is null. /// /// The is not in a state that allows writing attributes. /// /// The has been disposed. ///
public WriteAttribute ( MimeKit.Text.HtmlAttribute attribute ) : void
attribute MimeKit.Text.HtmlAttribute The attribute.
return void
Esempio n. 1
0
        /// <summary>
        /// Write the HTML tag.
        /// </summary>
        /// <remarks>
        /// Writes the HTML tag to the given <see cref="HtmlWriter"/>.
        /// </remarks>
        /// <example>
        /// <code language="c#" source="Examples\MimeVisitorExamples.cs" region="HtmlPreviewVisitor" />
        /// </example>
        /// <param name="htmlWriter">The HTML writer.</param>
        /// <param name="writeAttributes"><c>true</c> if the <see cref="Attributes"/> should also be written; otherwise, <c>false</c>.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="htmlWriter"/> is <c>null</c>.
        /// </exception>
        public void WriteTag(HtmlWriter htmlWriter, bool writeAttributes)
        {
            if (htmlWriter == null)
            {
                throw new ArgumentNullException(nameof(htmlWriter));
            }

            if (IsEndTag)
            {
                htmlWriter.WriteEndTag(TagName);
                return;
            }

            if (IsEmptyElementTag)
            {
                htmlWriter.WriteEmptyElementTag(TagName);
            }
            else
            {
                htmlWriter.WriteStartTag(TagName);
            }

            if (writeAttributes)
            {
                for (int i = 0; i < Attributes.Count; i++)
                {
                    htmlWriter.WriteAttribute(Attributes[i]);
                }
            }
        }
Esempio n. 2
0
			public void HtmlTagCallback (HtmlTagContext ctx, HtmlWriter htmlWriter)
			{
				if (ctx.TagId != HtmlTagId.Image || ctx.IsEndTag) {
					ctx.WriteTag (htmlWriter, true);
					return;
				}

				// write the IMG tag, but don't write out the attributes.
				ctx.WriteTag (htmlWriter, false);

				// manually write the attributes so that we can replace the SRC attributes
				foreach (var attribute in ctx.Attributes) {
					if (attribute.Id == HtmlAttributeId.Src) {
						int index;
						Uri uri;

						// parse the <img src=...> attribute value into a Uri
						if (Uri.IsWellFormedUriString (attribute.Value, UriKind.Absolute))
							uri = new Uri (attribute.Value, UriKind.Absolute);
						else
							uri = new Uri (attribute.Value, UriKind.Relative);

						// locate the index of the attachment within the multipart/related (if it exists)
						if ((index = related.IndexOf (uri)) != -1) {
							var attachment = related[index] as MimePart;

							if (attachment == null) {
								// the body part is not a basic leaf part (IOW it's a multipart or message-part)
								htmlWriter.WriteAttribute (attribute);
								continue;
							}

							var data = GetDataUri (attachment);

							htmlWriter.WriteAttributeName (attribute.Name);
							htmlWriter.WriteAttributeValue (data);
						} else {
							htmlWriter.WriteAttribute (attribute);
						}
					}
				}
			}
        void HtmlTagCallback(HtmlTagContext ctx, HtmlWriter htmlWriter)
        {
            if (ctx.TagId == HtmlTagId.Image && !ctx.IsEndTag && _stack.Count > 0)
            {
                ctx.WriteTag(htmlWriter, false);

                // replace the src attribute with a file:// URL
                foreach (var attribute in ctx.Attributes)
                {
                    if (attribute.Id == HtmlAttributeId.Src)
                    {
                        MimePart image;

                        if (!TryGetImage(attribute.Value, out image))
                        {
                            htmlWriter.WriteAttribute(attribute);
                            continue;
                        }

                        var url = SaveImage(image, attribute.Value);

                        htmlWriter.WriteAttributeName(attribute.Name);
                        htmlWriter.WriteAttributeValue(url);
                    }
                    else
                        htmlWriter.WriteAttribute(attribute);
                }
            }
            else if (ctx.TagId == HtmlTagId.Body && !ctx.IsEndTag)
            {
                ctx.WriteTag(htmlWriter, false);

                // add and/or replace oncontextmenu="return false;"
                foreach (var attribute in ctx.Attributes)
                {
                    if (attribute.Name.ToLowerInvariant() == "oncontextmenu")
                        continue;

                    htmlWriter.WriteAttribute(attribute);
                }

                htmlWriter.WriteAttribute("oncontextmenu", "return false;");
            }
            else
            {
                // pass the tag through to the output
                ctx.WriteTag(htmlWriter, true);
            }
        }
Esempio n. 4
0
		void ReplaceUrlsWithFileNames (HtmlTagContext ctx, HtmlWriter htmlWriter)
		{
			if (ctx.TagId == HtmlTagId.Image) {
				htmlWriter.WriteEmptyElementTag (ctx.TagName);
				ctx.DeleteEndTag = true;

				for (int i = 0; i < ctx.Attributes.Count; i++) {
					var attr = ctx.Attributes[i];

					if (attr.Id == HtmlAttributeId.Src) {
						var fileName = Path.GetFileName (attr.Value);
						htmlWriter.WriteAttributeName (attr.Name);
						htmlWriter.WriteAttributeValue (fileName);
					} else {
						htmlWriter.WriteAttribute (attr);
					}
				}
			} else {
				ctx.WriteTag (htmlWriter, true);
			}
		}
Esempio n. 5
0
		void HtmlTagCallback (HtmlTagContext ctx, HtmlWriter htmlWriter)
		{
			if (ctx.TagId == HtmlTagId.Body && !ctx.IsEmptyElementTag) {
				if (ctx.IsEndTag) {
					// end our opening <blockquote>
					htmlWriter.WriteEndTag (HtmlTagId.BlockQuote);

					// pass the </body> tag through to the output
					ctx.WriteTag (htmlWriter, true);
				} else {
					// pass the <body> tag through to the output
					ctx.WriteTag (htmlWriter, true);

					// prepend the HTML reply with "On {DATE}, {SENDER} wrote:"
					htmlWriter.WriteStartTag (HtmlTagId.P);
					htmlWriter.WriteText (GetOnDateSenderWrote (message));
					htmlWriter.WriteEndTag (HtmlTagId.P);

					// Wrap the original content in a <blockquote>
					htmlWriter.WriteStartTag (HtmlTagId.BlockQuote);
					htmlWriter.WriteAttribute (HtmlAttributeId.Style, "border-left: 1px #ccc solid; margin: 0 0 0 .8ex; padding-left: 1ex;");

					ctx.InvokeCallbackForEndTag = true;
				}
			} else {
				// pass the tag through to the output
				ctx.WriteTag (htmlWriter, true);
			}
		}
Esempio n. 6
0
		/// <summary>
		/// Write the HTML tag.
		/// </summary>
		/// <remarks>
		/// Writes the HTML tag to the given <see cref="HtmlWriter"/>.
		/// </remarks>
		/// <example>
		/// <code language="c#" source="Examples\MimeVisitorExamples.cs" region="HtmlPreviewVisitor" />
		/// </example>
		/// <param name="htmlWriter">The HTML writer.</param>
		/// <param name="writeAttributes"><c>true</c> if the <see cref="Attributes"/> should also be written; otherwise, <c>false</c>.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="htmlWriter"/> is <c>null</c>.
		/// </exception>
		public void WriteTag (HtmlWriter htmlWriter, bool writeAttributes)
		{
			if (htmlWriter == null)
				throw new ArgumentNullException ("htmlWriter");

			if (IsEndTag) {
				htmlWriter.WriteEndTag (TagName);
				return;
			}

			if (IsEmptyElementTag)
				htmlWriter.WriteEmptyElementTag (TagName);
			else
				htmlWriter.WriteStartTag (TagName);
			
			if (writeAttributes) {
				for (int i = 0; i < Attributes.Count; i++)
					htmlWriter.WriteAttribute (Attributes[i]);
			}
		}
Esempio n. 7
0
		public void TestHtmlWriter ()
		{
			const string expected = "<html ltr=\"true\"><head/><body><p class=\"paragraph\">" +
				"special characters in this text should get encoded: &lt;&gt;&#39;&amp;\n" +
				"special characters should not get encoded: &lt;&gt;" +
				"</p></body></html>";
			var actual = new StringBuilder ();

			using (var html = new HtmlWriter (new StringWriter (actual))) {
				Assert.AreEqual (HtmlWriterState.Default, html.WriterState);

				// make sure we can't start by writing an attribute since we are in the wrong state
				Assert.Throws<InvalidOperationException> (() => html.WriteAttribute (new HtmlAttribute (HtmlAttributeId.Action, "invalid state")));
				Assert.Throws<InvalidOperationException> (() => html.WriteAttribute (HtmlAttributeId.Action, "invalid state"));
				Assert.Throws<InvalidOperationException> (() => html.WriteAttribute ("action", "invalid state"));

				// write a tag
				html.WriteStartTag (HtmlTagId.Html);
				Assert.AreEqual (HtmlWriterState.Tag, html.WriterState);

				// *now* we should be able to write an attribute
				html.WriteAttribute (new HtmlAttribute ("ltr", "true"));

				// write en empty element tag, this should change the state to Default
				html.WriteEmptyElementTag (HtmlTagId.Head);
				Assert.AreEqual (HtmlWriterState.Tag, html.WriterState);

				html.WriteStartTag ("body");
				Assert.AreEqual (HtmlWriterState.Tag, html.WriterState);

				html.WriteStartTag (HtmlTagId.P);
				Assert.AreEqual (HtmlWriterState.Tag, html.WriterState);

				// make sure that we can't write an attribute value yet
				Assert.Throws<InvalidOperationException> (() => html.WriteAttributeValue ("attrValue"));
				Assert.Throws<InvalidOperationException> (() => html.WriteAttributeValue ("attrValue".ToCharArray (), 0, 9));

				html.WriteAttributeName (HtmlAttributeId.Class);
				Assert.AreEqual (HtmlWriterState.Attribute, html.WriterState);

				html.WriteAttributeValue ("paragraph");
				Assert.AreEqual (HtmlWriterState.Tag, html.WriterState);

				html.WriteText ("special characters in this text should get encoded: <>'&\n");
				html.WriteMarkupText ("special characters should not get encoded: &lt;&gt;");
				Assert.AreEqual (HtmlWriterState.Default, html.WriterState);

				html.WriteEndTag (HtmlTagId.P);

				html.WriteEndTag (HtmlTagId.Body);
				html.WriteEndTag ("html");
			}

			Assert.AreEqual (expected, actual.ToString ());
		}
Esempio n. 8
0
		public void TestArgumentExceptions ()
		{
			Assert.Throws<ArgumentNullException> (() => new HtmlWriter (null, Encoding.UTF8));
			Assert.Throws<ArgumentNullException> (() => new HtmlWriter (new MemoryStream (), null));
			Assert.Throws<ArgumentNullException> (() => new HtmlWriter (null));

			using (var html = new HtmlWriter (new StringWriter ())) {
				Assert.Throws<ArgumentNullException> (() => html.WriteAttribute (null));
				Assert.Throws<ArgumentNullException> (() => html.WriteAttribute (null, string.Empty));
				Assert.Throws<ArgumentNullException> (() => html.WriteAttribute ("name", null));
				Assert.Throws<ArgumentException> (() => html.WriteAttribute (string.Empty, null));
				Assert.Throws<ArgumentException> (() => html.WriteAttribute ("a b c", null));
				Assert.Throws<ArgumentNullException> (() => html.WriteAttribute (null, new char[1], 0, 1));
				Assert.Throws<ArgumentNullException> (() => html.WriteAttribute ("name", null, 0, 0));
				Assert.Throws<ArgumentOutOfRangeException> (() => html.WriteAttribute ("name", new char[0], -1, 0));
				Assert.Throws<ArgumentOutOfRangeException> (() => html.WriteAttribute ("name", new char[0], 0, 1));
				Assert.Throws<ArgumentException> (() => html.WriteAttribute (HtmlAttributeId.Unknown, new char[1], 0, 1));
				Assert.Throws<ArgumentNullException> (() => html.WriteAttribute (HtmlAttributeId.Alt, null, 0, 0));
				Assert.Throws<ArgumentOutOfRangeException> (() => html.WriteAttribute (HtmlAttributeId.Alt, new char[0], -1, 0));
				Assert.Throws<ArgumentOutOfRangeException> (() => html.WriteAttribute (HtmlAttributeId.Alt, new char[0], 0, 1));

				Assert.Throws<ArgumentException> (() => html.WriteAttributeName (HtmlAttributeId.Unknown));
				Assert.Throws<ArgumentNullException> (() => html.WriteAttributeName (null));

				Assert.Throws<ArgumentNullException> (() => html.WriteAttributeValue (null));
				Assert.Throws<ArgumentNullException> (() => html.WriteAttributeValue (null, 0, 0));
				Assert.Throws<ArgumentOutOfRangeException> (() => html.WriteAttributeValue (new char[0], -1, 0));
				Assert.Throws<ArgumentOutOfRangeException> (() => html.WriteAttributeValue (new char[0], 0, 1));

				Assert.Throws<ArgumentException> (() => html.WriteEmptyElementTag (HtmlTagId.Unknown));
				Assert.Throws<ArgumentNullException> (() => html.WriteEmptyElementTag (null));
				Assert.Throws<ArgumentException> (() => html.WriteEmptyElementTag (string.Empty));
				Assert.Throws<ArgumentException> (() => html.WriteEmptyElementTag ("a b c"));

				Assert.Throws<ArgumentException> (() => html.WriteEndTag (HtmlTagId.Unknown));
				Assert.Throws<ArgumentNullException> (() => html.WriteEndTag (null));
				Assert.Throws<ArgumentException> (() => html.WriteEndTag (string.Empty));
				Assert.Throws<ArgumentException> (() => html.WriteEndTag ("a b c"));

				Assert.Throws<ArgumentNullException> (() => html.WriteMarkupText (null));
				Assert.Throws<ArgumentNullException> (() => html.WriteMarkupText (null, 0, 0));
				Assert.Throws<ArgumentOutOfRangeException> (() => html.WriteMarkupText (new char[0], -1, 0));
				Assert.Throws<ArgumentOutOfRangeException> (() => html.WriteMarkupText (new char[0], 0, 1));

				Assert.Throws<ArgumentException> (() => html.WriteStartTag (HtmlTagId.Unknown));
				Assert.Throws<ArgumentNullException> (() => html.WriteStartTag (null));
				Assert.Throws<ArgumentException> (() => html.WriteStartTag (string.Empty));
				Assert.Throws<ArgumentException> (() => html.WriteStartTag ("a b c"));

				Assert.Throws<ArgumentNullException> (() => html.WriteText (null));
				Assert.Throws<ArgumentNullException> (() => html.WriteText (null, 0, 0));
				Assert.Throws<ArgumentOutOfRangeException> (() => html.WriteText (new char[0], -1, 0));
				Assert.Throws<ArgumentOutOfRangeException> (() => html.WriteText (new char[0], 0, 1));

				Assert.Throws<ArgumentNullException> (() => html.WriteToken (null));
			}
		}