1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-04 15:09:16 +02:00

Fixed spaces in Section

This commit is contained in:
Fred Chasen 2017-08-07 16:41:12 -07:00
parent c2b3284259
commit 8bfed1593c
2 changed files with 54 additions and 53 deletions

View file

@ -52,6 +52,7 @@
rendition.on("rendered", function(section){ rendition.on("rendered", function(section){
console.log("rendered", section);
var nextSection = section.next(); var nextSection = section.next();
var prevSection = section.prev(); var prevSection = section.prev();

View file

@ -54,7 +54,7 @@ class Section {
// var directory = new Url(this.url).directory; // var directory = new Url(this.url).directory;
this.document = xml; this.document = xml;
this.contents = xml.documentElement; this.contents = xml.documentElement;
return this.hooks.content.trigger(this.document, this); return this.hooks.content.trigger(this.document, this);
}.bind(this)) }.bind(this))
@ -96,9 +96,9 @@ class Section {
Serializer = require("xmldom").XMLSerializer; Serializer = require("xmldom").XMLSerializer;
} else { } else {
Serializer = XMLSerializer; Serializer = XMLSerializer;
} }
var serializer = new Serializer(); var serializer = new Serializer();
this.output = serializer.serializeToString(contents); this.output = serializer.serializeToString(contents);
return this.output; return this.output;
}.bind(this)). }.bind(this)).
then(function(){ then(function(){
@ -114,64 +114,64 @@ class Section {
return rendered; return rendered;
} }
/** /**
* Find a string in a section * Find a string in a section
* @param {string} _query The query string to find * @param {string} _query The query string to find
* @return {object[]} A list of matches, with form {cfi, excerpt} * @return {object[]} A list of matches, with form {cfi, excerpt}
*/ */
find(_query){ find(_query){
var section = this; var section = this;
var matches = []; var matches = [];
var query = _query.toLowerCase(); var query = _query.toLowerCase();
var find = function(node){ var find = function(node){
var text = node.textContent.toLowerCase(); var text = node.textContent.toLowerCase();
var range = section.document.createRange(); var range = section.document.createRange();
var cfi; var cfi;
var pos; var pos;
var last = -1; var last = -1;
var excerpt; var excerpt;
var limit = 150; var limit = 150;
while (pos != -1) { while (pos != -1) {
// Search for the query // Search for the query
pos = text.indexOf(query, last + 1); pos = text.indexOf(query, last + 1);
if (pos != -1) { if (pos != -1) {
// We found it! Generate a CFI // We found it! Generate a CFI
range = section.document.createRange(); range = section.document.createRange();
range.setStart(node, pos); range.setStart(node, pos);
range.setEnd(node, pos + query.length); range.setEnd(node, pos + query.length);
cfi = section.cfiFromRange(range); cfi = section.cfiFromRange(range);
// Generate the excerpt // Generate the excerpt
if (node.textContent.length < limit) { if (node.textContent.length < limit) {
excerpt = node.textContent; excerpt = node.textContent;
} }
else { else {
excerpt = node.textContent.substring(pos - limit/2, pos + limit/2); excerpt = node.textContent.substring(pos - limit/2, pos + limit/2);
excerpt = "..." + excerpt + "..."; excerpt = "..." + excerpt + "...";
} }
// Add the CFI to the matches list // Add the CFI to the matches list
matches.push({ matches.push({
cfi: cfi, cfi: cfi,
excerpt: excerpt excerpt: excerpt
}); });
} }
last = pos; last = pos;
} }
}; };
sprint(section.document, function(node) { sprint(section.document, function(node) {
find(node); find(node);
}); });
return matches; return matches;
}; };
/** /**
* Reconciles the current chapters layout properies with * Reconciles the current chapters layout properies with
* the global layout properities. * the global layout properities.
* @param {object} global The globa layout settings object, chapter properties string * @param {object} global The globa layout settings object, chapter properties string