// Class: openfl._internal.text.HTMLParser

var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this

$global.Object.defineProperty(exports, "__esModule", {value: true});

var __map_reserved = {};

// Imports

var $hxClasses = require("./../../../hxClasses_stub").default;
var $import = require("./../../../import_stub").default;
function EReg() {return require("./../../../EReg");}
function HxOverrides() {return require("./../../../HxOverrides");}
function openfl__$internal_text_TextFormatRange() {return require("./../../../openfl/_internal/text/TextFormatRange");}
function Std() {return require("./../../../Std");}

// Constructor

var HTMLParser = function(){}

// Meta

HTMLParser.__name__ = ["openfl","_internal","text","HTMLParser"];
HTMLParser.prototype = {
	
};
HTMLParser.prototype.__class__ = $hxClasses["openfl._internal.text.HTMLParser"] = HTMLParser;

// Init



// Statics

HTMLParser.parse = function(value,textFormat,textFormatRanges) {
	value = HTMLParser.__regexBreakTag.replace(value,"\n");
	value = HTMLParser.__regexEntities[0].replace(value,"\"");
	value = HTMLParser.__regexEntities[1].replace(value,"'");
	value = HTMLParser.__regexEntities[2].replace(value,"&");
	value = HTMLParser.__regexEntities[5].replace(value," ");
	var segments = value.split("<");
	if(segments.length == 1) {
		value = HTMLParser.__regexHTMLTag.replace(value,"");
		if(textFormatRanges.get_length() > 1) {
			textFormatRanges.splice(1,textFormatRanges.get_length() - 1);
		}
		value = HTMLParser.__regexEntities[3].replace(value,"<");
		value = HTMLParser.__regexEntities[4].replace(value,">");
		var range = textFormatRanges.get(0);
		range.format = textFormat;
		range.start = 0;
		range.end = value.length;
		return value;
	} else {
		textFormatRanges.splice(0,textFormatRanges.get_length());
		value = "";
		var segment;
		var _g1 = 0;
		var _g = segments.length;
		while(_g1 < _g) {
			var i = _g1++;
			segment = segments[i];
			segment = HTMLParser.__regexEntities[3].replace(segment,"<");
			segment = HTMLParser.__regexEntities[4].replace(segment,">");
			segments[i] = segment;
		}
		var formatStack = [textFormat.clone()];
		var sub;
		var noLineBreak = false;
		var _g2 = 0;
		while(_g2 < segments.length) {
			var segment1 = segments[_g2];
			++_g2;
			if(segment1 == "") {
				continue;
			}
			var isClosingTag = (HxOverrides().default).substr(segment1,0,1) == "/";
			var tagEndIndex = segment1.indexOf(">");
			var start = tagEndIndex + 1;
			var spaceIndex = segment1.indexOf(" ");
			var tagName = segment1.substring(isClosingTag ? 1 : 0,spaceIndex > -1 && spaceIndex < tagEndIndex ? spaceIndex : tagEndIndex);
			var format;
			if(isClosingTag) {
				formatStack.pop();
				format = formatStack[formatStack.length - 1].clone();
				if(tagName.toLowerCase() == "p" && textFormatRanges.get_length() > 0) {
					value += "\n";
					noLineBreak = true;
				}
				if(start < segment1.length) {
					sub = (HxOverrides().default).substr(segment1,start,null);
					textFormatRanges.push(new (openfl__$internal_text_TextFormatRange().default)(format,value.length,value.length + sub.length));
					value += sub;
					noLineBreak = false;
				}
			} else {
				format = formatStack[formatStack.length - 1].clone();
				if(tagEndIndex > -1) {
					var _g11 = tagName.toLowerCase();
					switch(_g11) {
					case "a":
						if(HTMLParser.__regexHref.match(segment1)) {
							format.url = HTMLParser.__getAttributeMatch(HTMLParser.__regexHref);
						}
						break;
					case "b":
						format.bold = true;
						break;
					case "em":case "i":
						format.italic = true;
						break;
					case "font":
						if(HTMLParser.__regexFace.match(segment1)) {
							format.font = HTMLParser.__getAttributeMatch(HTMLParser.__regexFace);
						}
						if(HTMLParser.__regexColor.match(segment1)) {
							format.color = (Std().default).parseInt("0x" + HTMLParser.__getAttributeMatch(HTMLParser.__regexColor));
						}
						if(HTMLParser.__regexSize.match(segment1)) {
							var sizeAttr = HTMLParser.__getAttributeMatch(HTMLParser.__regexSize);
							var firstChar = (HxOverrides().default).cca(sizeAttr,0);
							if(firstChar == 43 || firstChar == 45) {
								var parentFormat = formatStack.length >= 2 ? formatStack[formatStack.length - 2] : textFormat;
								format.size = parentFormat.size + (Std().default).parseInt(sizeAttr);
							} else {
								format.size = (Std().default).parseInt(sizeAttr);
							}
						}
						break;
					case "p":
						if(textFormatRanges.get_length() > 0 && !noLineBreak) {
							value += "\n";
						}
						if(HTMLParser.__regexAlign.match(segment1)) {
							format.align = HTMLParser.__getAttributeMatch(HTMLParser.__regexAlign).toLowerCase();
						}
						break;
					case "textformat":
						if(HTMLParser.__regexBlockIndent.match(segment1)) {
							format.blockIndent = (Std().default).parseInt(HTMLParser.__getAttributeMatch(HTMLParser.__regexBlockIndent));
						}
						if(HTMLParser.__regexIndent.match(segment1)) {
							format.indent = (Std().default).parseInt(HTMLParser.__getAttributeMatch(HTMLParser.__regexIndent));
						}
						if(HTMLParser.__regexLeading.match(segment1)) {
							format.leading = (Std().default).parseInt(HTMLParser.__getAttributeMatch(HTMLParser.__regexLeading));
						}
						if(HTMLParser.__regexLeftMargin.match(segment1)) {
							format.leftMargin = (Std().default).parseInt(HTMLParser.__getAttributeMatch(HTMLParser.__regexLeftMargin));
						}
						if(HTMLParser.__regexRightMargin.match(segment1)) {
							format.rightMargin = (Std().default).parseInt(HTMLParser.__getAttributeMatch(HTMLParser.__regexRightMargin));
						}
						if(HTMLParser.__regexTabStops.match(segment1)) {
							var values = HTMLParser.__getAttributeMatch(HTMLParser.__regexTabStops).split(" ");
							var tabStops = [];
							var _g12 = 0;
							while(_g12 < values.length) {
								var stop = values[_g12];
								++_g12;
								tabStops.push((Std().default).parseInt(stop));
							}
							format.tabStops = tabStops;
						}
						break;
					case "u":
						format.underline = true;
						break;
					}
					formatStack.push(format);
					if(start < segment1.length) {
						sub = segment1.substring(start);
						textFormatRanges.push(new (openfl__$internal_text_TextFormatRange().default)(format,value.length,value.length + sub.length));
						value += sub;
						noLineBreak = false;
					}
				} else {
					textFormatRanges.push(new (openfl__$internal_text_TextFormatRange().default)(format,value.length,value.length + segment1.length));
					value += segment1;
					noLineBreak = false;
				}
			}
		}
		if(textFormatRanges.get_length() == 0) {
			textFormatRanges.push(new (openfl__$internal_text_TextFormatRange().default)(formatStack[0],0,0));
		}
	}
	return value;
}
HTMLParser.__getAttributeMatch = function(regex) {
	if(regex.matched(2) != null) {
		return regex.matched(2);
	} else {
		return regex.matched(3);
	}
}
HTMLParser.__regexAlign = new (EReg().default)("align=(\"([^\"]+)\"|'([^']+)')","i")
HTMLParser.__regexBreakTag = new (EReg().default)("<br\\s*/?>","gi")
HTMLParser.__regexBlockIndent = new (EReg().default)("blockindent=(\"([^\"]+)\"|'([^']+)')","i")
HTMLParser.__regexColor = new (EReg().default)("color=(\"#([^\"]+)\"|'#([^']+)')","i")
HTMLParser.__regexEntities = [new (EReg().default)("&quot;","g"),new (EReg().default)("&apos;","g"),new (EReg().default)("&amp;","g"),new (EReg().default)("&lt;","g"),new (EReg().default)("&gt;","g"),new (EReg().default)("&nbsp;","g")]
HTMLParser.__regexFace = new (EReg().default)("face=(\"([^\"]+)\"|'([^']+)')","i")
HTMLParser.__regexHTMLTag = new (EReg().default)("<.*?>","g")
HTMLParser.__regexHref = new (EReg().default)("href=(\"([^\"]+)\"|'([^']+)')","i")
HTMLParser.__regexIndent = new (EReg().default)(" indent=(\"([^\"]+)\"|'([^']+)')","i")
HTMLParser.__regexLeading = new (EReg().default)("leading=(\"([^\"]+)\"|'([^']+)')","i")
HTMLParser.__regexLeftMargin = new (EReg().default)("leftmargin=(\"([^\"]+)\"|'([^']+)')","i")
HTMLParser.__regexRightMargin = new (EReg().default)("rightmargin=(\"([^\"]+)\"|'([^']+)')","i")
HTMLParser.__regexSize = new (EReg().default)("size=(\"([^\"]+)\"|'([^']+)')","i")
HTMLParser.__regexTabStops = new (EReg().default)("tabstops=(\"([^\"]+)\"|'([^']+)')","i")

// Export

exports.default = HTMLParser;