package com.github.manliogit.javatags.lang;

import static com.github.manliogit.javatags.lang.HtmlHelper.*;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

import org.junit.Test;

import com.github.manliogit.javatags.element.Element;

public class HtmlHelperTest {
 
	@Test
	public void htmlTagRenderDocTypeAlso() throws Exception {
		assertThat(html5().render(), is("<!DOCTYPE html><html></html>"));
	}
	
	@Test
	public void renderTextAsLastSibling() throws Exception {
		Element frag = div(
				   	  div(),
				   	  "text"
				   );
		
		Element frag2 = div(
				div(),
				div(),
				text("text")
			);
		
		assertThat(frag.render(), is("<div><div></div>text</div>"));
		assertThat(frag2.render(), is("<div><div></div><div></div>text</div>"));
	}
	
	@Test
	public void renderDivWithAttributeAndTest() throws Exception {
		assertThat(div(attr("id -> 123"), "some text").render(), is("<div id='123'>some text</div>"));
	}
	
	@Test
	public void nested() throws Exception {
		Element frag = html5(
						div(attr("class -> fa fa-up"),
							div(attr("id -> 123"), "some text")
						),
						div(
							br(),
							"otherText"
						)
					);

		String expected = "<!DOCTYPE html>"
						+ "<html>"
						+ 	"<div class='fa fa-up'>"
						+ 		"<div id='123'>some text</div>"
						+ 	"</div>"
						+ 	"<div>"
						+ 		"<br/>otherText"
						+ 	"</div>"
						+ "</html>";
		
		assertThat(frag.render(), is(expected));
	}
	
	@Test
	public void headBlock() throws Exception {
		Element frag = html5(attr("lang -> en"),
						head(
							meta(attr("http-equiv -> Content-Type", "content -> text/html; charset=UTF-8")),
							title("title"),
							link(attr("href -> xxx.css", "rel -> stylesheet"))
						)
					);
		
		String expected = "<!DOCTYPE html>"+
							"<html lang='en'>" +
								"<head>"+
								    "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>" +
								    "<title>title</title>" +
								    "<link href='xxx.css' rel='stylesheet'/>"+
								"</head>" +
						    "</html>";
		
		assertThat(frag.render(), is(expected));
	}
	
	@Test
	public void renderJsOnAttribute() throws Exception {
		Element frag = div(
						 a(attr("href -> xxx", "onclick -> alert(\"yay!\")"), "xxx")
					   );
		
		assertThat(frag.render(), is("<div><a href='xxx' onclick='alert(\"yay!\")'>xxx</a></div>" ));
	}
	
	@Test
	public void renderJsWithinScript() throws Exception {
		Element frag = script(attr("type -> text/javascript"),
							"//<![CDATA[" +
								"alert('yay!');" +
							"//]]>"
					   );
		
		assertThat(frag.render(), is("<script type='text/javascript'>//<![CDATA[alert('yay!');//]]></script>" ));
	}
}

@@ -98,17 +98,678 @@
 lHelper.
-*
+a;%0Aimport static com.github.manliogit.javatags.lang.HtmlHelper.attr;%0Aimport static com.github.manliogit.javatags.lang.HtmlHelper.br;%0Aimport static com.github.manliogit.javatags.lang.HtmlHelper.div;%0Aimport static com.github.manliogit.javatags.lang.HtmlHelper.head;%0Aimport static com.github.manliogit.javatags.lang.HtmlHelper.html5;%0Aimport static com.github.manliogit.javatags.lang.HtmlHelper.link;%0Aimport static com.github.manliogit.javatags.lang.HtmlHelper.meta;%0Aimport static com.github.manliogit.javatags.lang.HtmlHelper.script;%0Aimport static com.github.manliogit.javatags.lang.HtmlHelper.text;%0Aimport static com.github.manliogit.javatags.lang.HtmlHelper.title
 ;%0Aimport
@@ -836,9 +836,18 @@
 ert.
-*
+assertThat
 ;%0A%0Ai
@@ -3158,27 +3158,29 @@
 %09%09%09%09%09%09%09%22
-//%3C!%5BCDATA%5B
+function xxx%7B
 %22 +%0A%09%09%09%09
@@ -3210,21 +3210,17 @@
 %09%09%09%09%09%09%09%22
-//%5D%5D%3E
+%7D
 %22%0A%09%09%09%09%09 
@@ -3294,19 +3294,21 @@
 pt'%3E
-//%3C!%5BCDATA%5B
+function xxx%7B
 aler
@@ -3321,13 +3321,9 @@
 !');
-//%5D%5D%3E
+%7D
 %3C/sc

test render js as text