private HtmlText makeText() throws MalformedHtmlException{
		assert !tree.isEmpty() : "Missing HtmlDocument from root";
		assert chars.length()>0 : "Empty buffer";
		
		//Construct text and chop trailing whitespaces if necessary
		String text = chars.toString();
		chars.setLength(0);
		lastCharIsWS = true;
		//Len must be 
		int len = text.length();
		if(text.charAt(len-1) == ' '){
			text = text.substring(0, --len);
		}
		
		//Text is inside some element
		HtmlMidNode n = tree.peek();
		if(n.is(ELEMENT)){
			//This is an HtmlElement
			HtmlElement e = n.asElement();
			
			//TODO: implement real legal parent/children check
			if (e.getType() == HTML || e.getType() == HEAD){
				//Text inside invalid element!
				err("Text inside an illegal element");
			}
			return new HtmlText(e, text);
		}else{
			//This is an HtmlDocument
			err("Text inside root");
			return new HtmlText(n, text);
		}
	}