lib/assets/htmlMatcher.js
function findClosingPair(open, matcher) {
		var stack = [], tag = null;
		var text = matcher.text();
		
		for (var pos = open.range.end, len = text.length; pos < len; pos++) {
			if (matches(text, pos, '<!--')) {
				// skip to end of comment
				for (var j = pos; j < len; j++) {
					if (matches(text, j, '-->')) {
						pos = j + 3;
						break;
					}
				}
			}
			
			if ((tag = matcher.matches(pos))) {
				if (tag.type == 'open' && !tag.selfClose) {
					stack.push(tag.name);
				} else if (tag.type == 'close') {
					if (!stack.length) { // found valid pair?
						return tag.name == open.name ? tag : null;
					}
					
					// check if current closing tag matches previously opened one
					if (stack[stack.length - 1] == tag.name) {
						stack.pop();
					} else {
						var found = false;
						while (stack.length && !found) {
							var last = stack.pop();
							if (last == tag.name) {
								found = true;
							}
						}
						
						if (!stack.length && !found) {
							return tag.name == open.name ? tag : null;
						}
					}
				}

				pos = tag.range.end - 1;
			}
		}
	}
function findClosingPair(open, matcher) {
		var stack = [], tag = null;
		var text = matcher.text();
		
		for (var pos = open.range.end, len = text.length; pos < len; pos++) {
			if (matches(text, pos, '<!--')) {
				// skip to end of comment
				for (var j = pos; j < len; j++) {
					if (matches(text, j, '-->')) {
						pos = j + 3;
						break;
					}
				}
			}
			
			if ((tag = matcher.matches(pos))) {
				if (tag.type == 'open' && !tag.selfClose) {
					stack.push(tag.name);
				} else if (tag.type == 'close') {
					if (!stack.length) { // found valid pair?
						return tag.name == open.name ? tag : null;
					}
					
					// check if current closing tag matches previously opened one
					if (stack[stack.length - 1] == tag.name) {
						stack.pop();
					} else {
						var found = false;
						while (stack.length && !found) {
							var last = stack.pop();
							if (last == tag.name) {
								found = true;
							}
						}
						
						if (!stack.length && !found) {
							return tag.name == open.name ? tag : null;
						}
					}
				}

				pos = tag.range.end - 1;
			}
		}
	}
function findClosingPair ( open , matcher ) { var stack = [ ] , tag = null ; var text = matcher . text ( ) ; for ( var pos = open . range . end , len = text . length ; pos < len ; pos ++ ) { if ( matches ( text , pos , '<!--' ) ) { // skip to end of comment for ( var j = pos ; j < len ; j ++ ) { if ( matches ( text , j , '-->' ) ) { pos = j + 3 ; break ; } } } if ( ( tag = matcher . matches ( pos ) ) ) { if ( tag . type == 'open' && ! tag . selfClose ) { stack . push ( tag . name ) ; } else if ( tag . type == 'close' ) { if ( ! stack . length ) { // found valid pair? return tag . name == open . name ? tag : null ; } // check if current closing tag matches previously opened one if ( stack [ stack . length - 1 ] == tag . name ) { stack . pop ( ) ; } else { var found = false ; while ( stack . length && ! found ) { var last = stack . pop ( ) ; if ( last == tag . name ) { found = true ; } } if ( ! stack . length && ! found ) { return tag . name == open . name ? tag : null ; } } } pos = tag . range . end - 1 ; } } }
Search for closing pair of opening tag
@param {Object} open Open tag instance
@param {Object} matcher Matcher instance
Search for closing pair of opening tag