1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-05 15:32:55 +02:00

fixed error in removing marker

This commit is contained in:
fchasen 2014-02-19 15:35:36 -08:00
parent ed82dfe736
commit 4cf8bbc161
9 changed files with 72 additions and 33 deletions

View file

@ -2607,6 +2607,11 @@ EPUBJS.Book.prototype.gotoCfi = function(cfiString, defer){
cfi = new EPUBJS.EpubCFI(cfiString); cfi = new EPUBJS.EpubCFI(cfiString);
spinePos = cfi.spinePos; spinePos = cfi.spinePos;
if(spinePos == -1) {
return false;
}
spineItem = this.spine[spinePos]; spineItem = this.spine[spinePos];
promise = deferred.promise; promise = deferred.promise;
this._moving = true; this._moving = true;
@ -3463,6 +3468,10 @@ EPUBJS.EpubCFI.prototype.parse = function(cfiStr) {
path, path,
end, end,
text; text;
if(typeof cfiStr !== "string") {
return {spinePos: -1};
}
cfi.str = cfiStr; cfi.str = cfiStr;
@ -3569,7 +3578,7 @@ EPUBJS.EpubCFI.prototype.addMarker = function(cfi, _doc, _marker) {
if(lastStep && lastStep.type === "text") { if(lastStep && lastStep.type === "text") {
text = parent.childNodes[lastStep.index]; text = parent.childNodes[lastStep.index];
if(cfi.characterOffset){ if(cfi.characterOffset){
split = text.splitText(); split = text.splitText(cfi.characterOffset);
marker.classList.add("EPUBJS-CFI-SPLIT"); marker.classList.add("EPUBJS-CFI-SPLIT");
parent.insertBefore(marker, split); parent.insertBefore(marker, split);
} else { } else {
@ -3595,20 +3604,22 @@ EPUBJS.EpubCFI.prototype.removeMarker = function(marker, _doc) {
var doc = _doc || document; var doc = _doc || document;
// var id = marker.id; // var id = marker.id;
// Remove only elements added as markers // Cleanup textnodes if they were split
if(marker.classList.contains("EPUBJS-CFI-MARKER")){
marker.parentElement.removeChild(marker);
}
// Cleanup textnodes
if(marker.classList.contains("EPUBJS-CFI-SPLIT")){ if(marker.classList.contains("EPUBJS-CFI-SPLIT")){
nextSib = marker.nextSibling; nextSib = marker.nextSibling;
prevSib = marker.previousSibling; prevSib = marker.previousSibling;
if(nextSib.nodeType === 3 && prevSib.nodeType === 3){ if(nextSib &&
prevSib &&
nextSib.nodeType === 3 &&
prevSib.nodeType === 3){
prevSib.innerText += nextSib.innerText; prevSib.innerText += nextSib.innerText;
marker.parentElement.removeChild(nextSib); marker.parentElement.removeChild(nextSib);
} }
marker.parentElement.removeChild(marker); marker.parentElement.removeChild(marker);
} else if(marker.classList.contains("EPUBJS-CFI-MARKER")) {
// Remove only elements added as markers
marker.parentElement.removeChild(marker);
} }
}; };

4
build/epub.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -2606,6 +2606,11 @@ EPUBJS.Book.prototype.gotoCfi = function(cfiString, defer){
cfi = new EPUBJS.EpubCFI(cfiString); cfi = new EPUBJS.EpubCFI(cfiString);
spinePos = cfi.spinePos; spinePos = cfi.spinePos;
if(spinePos == -1) {
return false;
}
spineItem = this.spine[spinePos]; spineItem = this.spine[spinePos];
promise = deferred.promise; promise = deferred.promise;
this._moving = true; this._moving = true;
@ -3462,6 +3467,10 @@ EPUBJS.EpubCFI.prototype.parse = function(cfiStr) {
path, path,
end, end,
text; text;
if(typeof cfiStr !== "string") {
return {spinePos: -1};
}
cfi.str = cfiStr; cfi.str = cfiStr;
@ -3568,7 +3577,7 @@ EPUBJS.EpubCFI.prototype.addMarker = function(cfi, _doc, _marker) {
if(lastStep && lastStep.type === "text") { if(lastStep && lastStep.type === "text") {
text = parent.childNodes[lastStep.index]; text = parent.childNodes[lastStep.index];
if(cfi.characterOffset){ if(cfi.characterOffset){
split = text.splitText(); split = text.splitText(cfi.characterOffset);
marker.classList.add("EPUBJS-CFI-SPLIT"); marker.classList.add("EPUBJS-CFI-SPLIT");
parent.insertBefore(marker, split); parent.insertBefore(marker, split);
} else { } else {
@ -3594,20 +3603,22 @@ EPUBJS.EpubCFI.prototype.removeMarker = function(marker, _doc) {
var doc = _doc || document; var doc = _doc || document;
// var id = marker.id; // var id = marker.id;
// Remove only elements added as markers // Cleanup textnodes if they were split
if(marker.classList.contains("EPUBJS-CFI-MARKER")){
marker.parentElement.removeChild(marker);
}
// Cleanup textnodes
if(marker.classList.contains("EPUBJS-CFI-SPLIT")){ if(marker.classList.contains("EPUBJS-CFI-SPLIT")){
nextSib = marker.nextSibling; nextSib = marker.nextSibling;
prevSib = marker.previousSibling; prevSib = marker.previousSibling;
if(nextSib.nodeType === 3 && prevSib.nodeType === 3){ if(nextSib &&
prevSib &&
nextSib.nodeType === 3 &&
prevSib.nodeType === 3){
prevSib.innerText += nextSib.innerText; prevSib.innerText += nextSib.innerText;
marker.parentElement.removeChild(nextSib); marker.parentElement.removeChild(nextSib);
} }
marker.parentElement.removeChild(marker); marker.parentElement.removeChild(marker);
} else if(marker.classList.contains("EPUBJS-CFI-MARKER")) {
// Remove only elements added as markers
marker.parentElement.removeChild(marker);
} }
}; };

View file

@ -403,6 +403,7 @@ EPUBJS.reader.ControlsController = function(book) {
}); });
book.on('renderer:locationChanged', function(cfi){ book.on('renderer:locationChanged', function(cfi){
var cfiFragment = "#" + cfi;
//-- Check if bookmarked //-- Check if bookmarked
var bookmarked = reader.isBookmarked(cfi); var bookmarked = reader.isBookmarked(cfi);
if(bookmarked === -1) { //-- Not bookmarked if(bookmarked === -1) { //-- Not bookmarked
@ -416,8 +417,10 @@ EPUBJS.reader.ControlsController = function(book) {
} }
// Update the History Location // Update the History Location
if(reader.settings.history) { if(reader.settings.history &&
history.pushState({}, '', "#"+cfi); window.location.hash != cfiFragment) {
// Add CFI fragment to the history
history.pushState({}, '', cfiFragment);
} }
}); });

4
demo/js/epub.min.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -80,6 +80,7 @@ EPUBJS.reader.ControlsController = function(book) {
}); });
book.on('renderer:locationChanged', function(cfi){ book.on('renderer:locationChanged', function(cfi){
var cfiFragment = "#" + cfi;
//-- Check if bookmarked //-- Check if bookmarked
var bookmarked = reader.isBookmarked(cfi); var bookmarked = reader.isBookmarked(cfi);
if(bookmarked === -1) { //-- Not bookmarked if(bookmarked === -1) { //-- Not bookmarked
@ -93,8 +94,10 @@ EPUBJS.reader.ControlsController = function(book) {
} }
// Update the History Location // Update the History Location
if(reader.settings.history) { if(reader.settings.history &&
history.pushState({}, '', "#"+cfi); window.location.hash != cfiFragment) {
// Add CFI fragment to the history
history.pushState({}, '', cfiFragment);
} }
}); });

View file

@ -831,6 +831,11 @@ EPUBJS.Book.prototype.gotoCfi = function(cfiString, defer){
cfi = new EPUBJS.EpubCFI(cfiString); cfi = new EPUBJS.EpubCFI(cfiString);
spinePos = cfi.spinePos; spinePos = cfi.spinePos;
if(spinePos == -1) {
return false;
}
spineItem = this.spine[spinePos]; spineItem = this.spine[spinePos];
promise = deferred.promise; promise = deferred.promise;
this._moving = true; this._moving = true;

View file

@ -97,6 +97,10 @@ EPUBJS.EpubCFI.prototype.parse = function(cfiStr) {
path, path,
end, end,
text; text;
if(typeof cfiStr !== "string") {
return {spinePos: -1};
}
cfi.str = cfiStr; cfi.str = cfiStr;
@ -203,7 +207,7 @@ EPUBJS.EpubCFI.prototype.addMarker = function(cfi, _doc, _marker) {
if(lastStep && lastStep.type === "text") { if(lastStep && lastStep.type === "text") {
text = parent.childNodes[lastStep.index]; text = parent.childNodes[lastStep.index];
if(cfi.characterOffset){ if(cfi.characterOffset){
split = text.splitText(); split = text.splitText(cfi.characterOffset);
marker.classList.add("EPUBJS-CFI-SPLIT"); marker.classList.add("EPUBJS-CFI-SPLIT");
parent.insertBefore(marker, split); parent.insertBefore(marker, split);
} else { } else {
@ -229,20 +233,22 @@ EPUBJS.EpubCFI.prototype.removeMarker = function(marker, _doc) {
var doc = _doc || document; var doc = _doc || document;
// var id = marker.id; // var id = marker.id;
// Remove only elements added as markers // Cleanup textnodes if they were split
if(marker.classList.contains("EPUBJS-CFI-MARKER")){
marker.parentElement.removeChild(marker);
}
// Cleanup textnodes
if(marker.classList.contains("EPUBJS-CFI-SPLIT")){ if(marker.classList.contains("EPUBJS-CFI-SPLIT")){
nextSib = marker.nextSibling; nextSib = marker.nextSibling;
prevSib = marker.previousSibling; prevSib = marker.previousSibling;
if(nextSib.nodeType === 3 && prevSib.nodeType === 3){ if(nextSib &&
prevSib &&
nextSib.nodeType === 3 &&
prevSib.nodeType === 3){
prevSib.innerText += nextSib.innerText; prevSib.innerText += nextSib.innerText;
marker.parentElement.removeChild(nextSib); marker.parentElement.removeChild(nextSib);
} }
marker.parentElement.removeChild(marker); marker.parentElement.removeChild(marker);
} else if(marker.classList.contains("EPUBJS-CFI-MARKER")) {
// Remove only elements added as markers
marker.parentElement.removeChild(marker);
} }
}; };