Make is(Extraneous|Collapsed)LineBreak more robust
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Fri, 12 Aug 2011 14:47:34 -0600
changeset 523 f18ac9086744
parent 522 7b8688822cdd
child 524 7c7f25c5dbb9
Make is(Extraneous|Collapsed)LineBreak more robust

This makes insertLineBreak behave more correctly in editor.html.
implementation.js
--- a/implementation.js	Fri Aug 12 14:34:11 2011 -0600
+++ b/implementation.js	Fri Aug 12 14:47:34 2011 -0600
@@ -807,6 +807,10 @@
 	while (getComputedStyle(ref).display == "inline") {
 		ref = ref.parentNode;
 	}
+	var refStyle = ref.hasAttribute("style") ? ref.getAttribute("style") : null;
+	ref.style.height = "auto";
+	ref.style.maxHeight = "none";
+	ref.style.minHeight = "0";
 	var space = document.createTextNode("\u200b");
 	var origHeight = ref.offsetHeight;
 	if (origHeight == 0) {
@@ -815,6 +819,14 @@
 	br.parentNode.insertBefore(space, br.nextSibling);
 	var finalHeight = ref.offsetHeight;
 	space.parentNode.removeChild(space);
+	if (refStyle === null) {
+		// Without the setAttribute() line, removeAttribute() doesn't work in
+		// Chrome 14 dev.  I have no idea why.
+		ref.setAttribute("style", "");
+		ref.removeAttribute("style");
+	} else {
+		ref.setAttribute("style", refStyle);
+	}
 
 	// Allow some leeway in case the zwsp didn't create a whole new line, but
 	// only made an existing line slightly higher.  Firefox 6.0a2 shows this
@@ -838,21 +850,37 @@
 		return false;
 	}
 
+	// Make the line break disappear and see if that changes the block's
+	// height.  Yes, this is an absurd hack.  We have to reset height etc. on
+	// the reference node because otherwise its height won't change if it's not
+	// auto.
 	var ref = br.parentNode;
 	while (getComputedStyle(ref).display == "inline") {
 		ref = ref.parentNode;
 	}
-	var style = br.hasAttribute("style") ? br.getAttribute("style") : null;
+	var refStyle = ref.hasAttribute("style") ? ref.getAttribute("style") : null;
+	ref.style.height = "auto";
+	ref.style.maxHeight = "none";
+	ref.style.minHeight = "0";
+	var brStyle = br.hasAttribute("style") ? br.getAttribute("style") : null;
 	var origHeight = ref.offsetHeight;
 	if (origHeight == 0) {
 		throw "isExtraneousLineBreak: original height is zero, bug?";
 	}
 	br.setAttribute("style", "display:none");
 	var finalHeight = ref.offsetHeight;
-	if (style === null) {
+	if (refStyle === null) {
+		// Without the setAttribute() line, removeAttribute() doesn't work in
+		// Chrome 14 dev.  I have no idea why.
+		ref.setAttribute("style", "");
+		ref.removeAttribute("style");
+	} else {
+		ref.setAttribute("style", refStyle);
+	}
+	if (brStyle === null) {
 		br.removeAttribute("style");
 	} else {
-		br.setAttribute("style", style);
+		br.setAttribute("style", brStyle);
 	}
 
 	return origHeight == finalHeight;