mirror of
https://github.com/kiwix/kiwix-js.git
synced 2025-09-27 23:17:37 -04:00
parent
26112de66c
commit
95afdeecb6
@ -807,10 +807,12 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
|
|||||||
var regexpZIMUrlWithNamespace = /(?:^|\/)([-ABIJMUVWX]\/.+)/;
|
var regexpZIMUrlWithNamespace = /(?:^|\/)([-ABIJMUVWX]\/.+)/;
|
||||||
// Pattern to match a local anchor in a href
|
// Pattern to match a local anchor in a href
|
||||||
var regexpLocalAnchorHref = /^#/;
|
var regexpLocalAnchorHref = /^#/;
|
||||||
// These regular expressions match both relative and absolute URLs
|
// Regex below finds images, scripts and stylesheets with ZIM-type metadata and image namespaces [kiwix-js #378]
|
||||||
// Since late 2014, all ZIM files should use relative URLs
|
// It first searches for <img, <script, or <link, then scans forward to find, on a word boundary, either src=["']
|
||||||
var regexpImageUrl = /^(?:\.\.\/|\/)+(I\/.*)$/;
|
// OR href=["'] (ignoring any extra whitespace), and it then tests everything up to the next ["'] against a pattern that
|
||||||
var regexpMetadataUrl = /^(?:\.\.\/|\/)+(-\/.*)$/;
|
// matches ZIM URLs with namespaces [-I] ("-" = metadata or "I" = image). Finally it removes the relative or absolute path.
|
||||||
|
// DEV: If you want to support more namespaces, add them to the END of the character set [-I] (not to the beginning)
|
||||||
|
var regexpTagsWithZimUrl = /(<(?:img|script|link)\s+[^>]*?\b)(?:src|href)(\s*=\s*["']\s*)(?:\.\.\/|\/)+([-I]\/[^"']*)/ig;
|
||||||
|
|
||||||
// Cache for CSS styles contained in ZIM.
|
// Cache for CSS styles contained in ZIM.
|
||||||
// It significantly speeds up subsequent page display. See kiwix-js issue #335
|
// It significantly speeds up subsequent page display. See kiwix-js issue #335
|
||||||
@ -828,8 +830,9 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
|
|||||||
$("#articleContent").contents().scrollTop(0);
|
$("#articleContent").contents().scrollTop(0);
|
||||||
|
|
||||||
if (contentInjectionMode === 'jquery') {
|
if (contentInjectionMode === 'jquery') {
|
||||||
// Fast-replace img src with data-kiwixsrc [kiwix-js #272]
|
// Replaces ZIM-style URLs of img, script and link tags with a data-url to prevent 404 errors [kiwix-js #272 #376]
|
||||||
htmlArticle = htmlArticle.replace(/(<img\s+[^>]*\b)src(\s*=)/ig, '$1data-kiwixsrc$2');
|
// This replacement also processes the URL to remove the path so that the URL is ready for subsequent jQuery functions
|
||||||
|
htmlArticle = htmlArticle.replace(regexpTagsWithZimUrl, "$1data-kiwixurl$2$3");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute base URL
|
// Compute base URL
|
||||||
@ -917,13 +920,10 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
|
|||||||
}
|
}
|
||||||
|
|
||||||
function loadImagesJQuery() {
|
function loadImagesJQuery() {
|
||||||
$('#articleContent').contents().find('body').find('img').each(function() {
|
$('#articleContent').contents().find('body').find('img[data-kiwixurl]').each(function() {
|
||||||
var image = $(this);
|
var image = $(this);
|
||||||
// It's a standard image contained in the ZIM file
|
var imageUrl = image.attr("data-kiwixurl");
|
||||||
// We try to find its name (from an absolute or relative URL)
|
var title = decodeURIComponent(imageUrl);
|
||||||
var imageMatch = image.attr("data-kiwixsrc").match(regexpImageUrl); //kiwix-js #272
|
|
||||||
if (imageMatch) {
|
|
||||||
var title = decodeURIComponent(imageMatch[1]);
|
|
||||||
selectedArchive.getDirEntryByTitle(title).then(function(dirEntry) {
|
selectedArchive.getDirEntryByTitle(title).then(function(dirEntry) {
|
||||||
selectedArchive.readBinaryFile(dirEntry, function (fileDirEntry, content) {
|
selectedArchive.readBinaryFile(dirEntry, function (fileDirEntry, content) {
|
||||||
// TODO : use the complete MIME-type of the image (as read from the ZIM file)
|
// TODO : use the complete MIME-type of the image (as read from the ZIM file)
|
||||||
@ -932,7 +932,6 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
|
|||||||
}).fail(function (e) {
|
}).fail(function (e) {
|
||||||
console.error("could not find DirEntry for image:" + title, e);
|
console.error("could not find DirEntry for image:" + title, e);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -948,13 +947,10 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
|
|||||||
collapsedBlocks[i].classList.add('open-block');
|
collapsedBlocks[i].classList.add('open-block');
|
||||||
}
|
}
|
||||||
|
|
||||||
$('#articleContent').contents().find('link[rel=stylesheet]').each(function() {
|
$('#articleContent').contents().find('link[data-kiwixurl]').each(function() {
|
||||||
var link = $(this);
|
var link = $(this);
|
||||||
// We try to find its name (from an absolute or relative URL)
|
var linkUrl = link.attr("data-kiwixurl");
|
||||||
var hrefMatch = link.attr("href").match(regexpMetadataUrl);
|
var title = uiUtil.removeUrlParameters(decodeURIComponent(linkUrl));
|
||||||
if (hrefMatch) {
|
|
||||||
// It's a CSS file contained in the ZIM file
|
|
||||||
var title = uiUtil.removeUrlParameters(decodeURIComponent(hrefMatch[1]));
|
|
||||||
if (cssCache && cssCache.has(title)) {
|
if (cssCache && cssCache.has(title)) {
|
||||||
var cssContent = cssCache.get(title);
|
var cssContent = cssCache.get(title);
|
||||||
uiUtil.replaceCSSLinkWithInlineCSS(link, cssContent);
|
uiUtil.replaceCSSLinkWithInlineCSS(link, cssContent);
|
||||||
@ -972,19 +968,15 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
|
|||||||
console.error("could not find DirEntry for CSS : " + title, e);
|
console.error("could not find DirEntry for CSS : " + title, e);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadJavaScriptJQuery() {
|
function loadJavaScriptJQuery() {
|
||||||
$('#articleContent').contents().find('script').each(function() {
|
$('#articleContent').contents().find('script[data-kiwixurl]').each(function() {
|
||||||
var script = $(this);
|
var script = $(this);
|
||||||
// We try to find its name (from an absolute or relative URL)
|
var scriptUrl = script.attr("data-kiwixurl");
|
||||||
var srcMatch = script.attr("src") ? script.attr("src").match(regexpMetadataUrl) : null;
|
|
||||||
// TODO check that the type of the script is text/javascript or application/javascript
|
// TODO check that the type of the script is text/javascript or application/javascript
|
||||||
if (srcMatch) {
|
var title = uiUtil.removeUrlParameters(decodeURIComponent(scriptUrl));
|
||||||
// It's a Javascript file contained in the ZIM file
|
|
||||||
var title = uiUtil.removeUrlParameters(decodeURIComponent(srcMatch[1]));
|
|
||||||
selectedArchive.getDirEntryByTitle(title).then(function(dirEntry) {
|
selectedArchive.getDirEntryByTitle(title).then(function(dirEntry) {
|
||||||
if (dirEntry === null) {
|
if (dirEntry === null) {
|
||||||
console.log("Error: js file not found: " + title);
|
console.log("Error: js file not found: " + title);
|
||||||
@ -997,7 +989,6 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
|
|||||||
}).fail(function (e) {
|
}).fail(function (e) {
|
||||||
console.error("could not find DirEntry for javascript : " + title, e);
|
console.error("could not find DirEntry for javascript : " + title, e);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user