Commit 721f5c32 authored by Fred Chasen's avatar Fred Chasen

Merge branch 'hyphen_fixes' into 'master'

Hyphen Fixes

See merge request !42
parents 69705652 53469e15
Pipeline #173 passed with stage
in 3 minutes and 37 seconds
...@@ -302,6 +302,11 @@ class Layout { ...@@ -302,6 +302,11 @@ class Layout {
parent = findElement(renderedNode, source); parent = findElement(renderedNode, source);
index = indexOfTextNode(container, parent); index = indexOfTextNode(container, parent);
if (index === -1) {
return;
}
node = child(parent, index); node = child(parent, index);
offset += node.textContent.indexOf(container.textContent); offset += node.textContent.indexOf(container.textContent);
...@@ -407,7 +412,7 @@ class Layout { ...@@ -407,7 +412,7 @@ class Layout {
left = 0; left = 0;
for (var i = 0; i != rects.length; i++) { for (var i = 0; i != rects.length; i++) {
rect = rects[i]; rect = rects[i];
if (!left || rect.left > left) { if (rect.width > 0 && (!left || rect.left > left)) {
left = rect.left; left = rect.left;
} }
} }
...@@ -542,21 +547,23 @@ class Layout { ...@@ -542,21 +547,23 @@ class Layout {
} }
removeOverflow(overflow) { removeOverflow(overflow) {
this.hyphenateAtBreak(overflow); let {startContainer} = overflow;
let extracted = overflow.extractContents();
this.hyphenateAtBreak(startContainer);
return overflow.extractContents(); return extracted;
} }
hyphenateAtBreak(overflow) { hyphenateAtBreak(startContainer) {
if (isText(overflow.startContainer) && overflow.startOffset > 0) { if (isText(startContainer)) {
let startText = overflow.startContainer.textContent; let startText = startContainer.textContent;
let startOffset = overflow.startOffset; let prevLetter = startText[startText.length-1];
let prevLetter = startText[startOffset-1];
// Add a hyphen if previous character is a letter or soft hyphen // Add a hyphen if previous character is a letter or soft hyphen
if (/^\w|\u00AD$/.test(prevLetter)) { if (/^\w|\u00AD$/.test(prevLetter)) {
overflow.startContainer.textContent = startText.slice(0, startOffset) + "\u2010"; startContainer.parentNode.classList.add("pagedjs_hyphen");
overflow.setStart(overflow.startContainer, startOffset + 1); startContainer.textContent += "\u2011";
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment