package br.com.caelum.brutal.sanitizer;

import static br.com.caelum.brutal.sanitizer.HtmlSanitizer.sanitize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Ignore;
import org.junit.Test;

public class HtmlSanitizerTest {

	
	@Test @Ignore
	public void should_escape_LTGT_wehen_possible() {
		String html = "< bla >";
		String expected = "&lt;bla&gt;";
		String sanitized = sanitize(html);
		assertEquals(expected, sanitized);
	}
	
	@Test
	public void shouldAddNoFollowIntoLinks() {
		String html = "<a href=\"http://www.teste.com.br\">teste</a>";
		String expected = "<a href=\"http://www.teste.com.br\" rel=\"nofollow\">teste</a>";
		String sanitized = sanitize(html);
		assertEquals(expected, sanitized);
	}

	@Test
	public void shouldRemoveLinksWithInvalidProtocol() {
		String html = "<a href=\"ftp://www.teste.com.br\">teste</a>";
		String expected = "teste";
		String sanitized = sanitize(html);
		assertEquals(expected, sanitized);
	}
	
	@Test
	public void shouldRemoveTagScript() {
		String html = "<script>function deleteAll(){document.getElementsByTagName(\"body\")[0].remove()}</script>";
		String sanitized = sanitize(html);
		assertTrue(sanitized.isEmpty());
	}
	
	@Test
	public void shouldRemoveInvalidAttributesOfALink() {
		String html = "<a class=\"my-class\" href=\"http://www.teste.com.br\">teste</a>";
		String expected = "<a href=\"http://www.teste.com.br\" rel=\"nofollow\">teste</a>";
		String sanitized = sanitize(html);
		assertEquals(expected, sanitized);
	}
	
	@Test
	public void shouldNotRemoveTagP() {
		String html = "<p>My text maroto</p>";
		String sanitized = sanitize(html);
		assertEquals(html, sanitized);
	}

	@Test
	public void shouldNotRemoveTagPre() {
		String html = "<pre>My code maroto</pre>";
		String sanitized = sanitize(html);
		assertEquals(html, sanitized);
	}
	
	@Test
	public void shouldNotRemoveTagCode() {
		String html = "<code>My inline code maroto</code>";
		String sanitized = sanitize(html);
		assertEquals(html, sanitized);
	}
	
	@Test
	public void shouldNotRemoveTagImg() {
		String html = "<img src=\"myimage.png\" alt=\"chrome\" />";
		String sanitized = sanitize(html);
		assertEquals(html, sanitized);
	}
	

	@Test
	public void shouldAllowImagesSize() {
		String html = "<img src=\"http://www.teste.com.br\" alt=\"x\" width=\"5\" height=\"3\" />\n";
		String sanitized = sanitize(html);
		assertEquals(html, sanitized);
	}
	
	@Test
	public void shouldNotAllowOtherAttributesForImages() {
		String html = "<img src=\"http://www.teste.com.br\" alt=\"x\" width=\"5\" height=\"3\" somethingElse=\"foo\" />";
		String expected = "<img src=\"http://www.teste.com.br\" alt=\"x\" width=\"5\" height=\"3\" />";
		String sanitized = sanitize(html);
		assertEquals(expected, sanitized);
	}
	
	
	
	@Test
	public void shouldNotRemoveTagKbd() {
		String html = "<kbd>shift</kbd>";
		String sanitized = sanitize(html);
		assertEquals(html, sanitized);
	}
	
	@Test
	public void shouldNotRemoveTagOl() {
		String html = "<ol><li>shift</li><li>ctrl</li></ol>";
		String sanitized = sanitize(html);
		assertEquals(html, sanitized);
	}
	
	@Test
	public void shouldNotRemoveTagUl() {
		String html = "<ul><li>shift</li><li>ctrl</li></ul>";
		String sanitized = sanitize(html);
		assertEquals(html, sanitized);
	}
	
	@Test
	public void shouldNotRemoveTagStrong() {
		String html = "<strong>leo</strong>";
		String sanitized = sanitize(html);
		assertEquals(html, sanitized);
	}
	
	@Test
	public void shouldNotRemoveTagH2() {
		String html = "<h2>Title</h2>";
		String sanitized = sanitize(html);
		assertEquals(html, sanitized);
	}
	
	@Test
	public void shouldNotRemoveTagBlockquote() {
		String html = "<blockquote>My quotation</blockquote>";
		String sanitized = sanitize(html);
		assertEquals(html, sanitized);
	}
	
	@Test
	public void shouldNotRemoveTagHr() {
		String html = "<hr />";
		String sanitized = sanitize(html);
		assertEquals(html, sanitized);
	}


}

@@ -483,32 +483,212 @@
 d);%0A%09%7D%0A%09%0A%09@Test%0A
+%09public void should_remove_textarea() %7B%0A%09%09String html = %22%3Ctextarea%3E%22;%0A%09%09String expected = %22%22;%0A%09%09String sanitized = sanitize(html);%0A%09%09assertEquals(expected, sanitized);%0A%09%7D%0A%09%0A%09@Test%0A
 %09public void sho

teste de remocao de tags indesejadas, como textarea