1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Converted htmlUtils to TypeScript

This commit is contained in:
Laurent Cozic 2021-01-30 12:19:43 +00:00
parent 73314447b9
commit 285482804a
4 changed files with 23 additions and 18 deletions

View File

@ -103,6 +103,9 @@ packages/app-cli/tests/Synchronizer.tags.js.map
packages/app-cli/tests/fsDriver.d.ts
packages/app-cli/tests/fsDriver.js
packages/app-cli/tests/fsDriver.js.map
packages/app-cli/tests/htmlUtils.d.ts
packages/app-cli/tests/htmlUtils.js
packages/app-cli/tests/htmlUtils.js.map
packages/app-cli/tests/models_Folder.d.ts
packages/app-cli/tests/models_Folder.js
packages/app-cli/tests/models_Folder.js.map

3
.gitignore vendored
View File

@ -90,6 +90,9 @@ packages/app-cli/tests/Synchronizer.tags.js.map
packages/app-cli/tests/fsDriver.d.ts
packages/app-cli/tests/fsDriver.js
packages/app-cli/tests/fsDriver.js.map
packages/app-cli/tests/htmlUtils.d.ts
packages/app-cli/tests/htmlUtils.js
packages/app-cli/tests/htmlUtils.js.map
packages/app-cli/tests/models_Folder.d.ts
packages/app-cli/tests/models_Folder.js
packages/app-cli/tests/models_Folder.js.map

View File

@ -1,6 +1,4 @@
/* eslint-disable no-unused-vars */
const htmlUtils = require('@joplin/lib/htmlUtils.js');
import htmlUtils from '@joplin/lib/htmlUtils';
describe('htmlUtils', function() {
@ -19,8 +17,8 @@ describe('htmlUtils', function() {
];
for (let i = 0; i < testCases.length; i++) {
const md = testCases[i][0];
const expected = testCases[i][1];
const md = testCases[i][0] as string;
const expected = testCases[i][1] as string[];
expect(htmlUtils.extractImageUrls(md).join(' ')).toBe(expected.join(' '));
}
@ -33,19 +31,19 @@ describe('htmlUtils', function() {
['<img src="http://test.com/img.png" alt="testing" >', ['http://other.com/img.png'], '<img src="http://other.com/img.png" alt="testing" >'],
];
const callback = (urls) => {
const callback = (urls: string[]) => {
let i = -1;
return function(src) {
return function(_src: string) {
i++;
return urls[i];
};
};
for (let i = 0; i < testCases.length; i++) {
const md = testCases[i][0];
const r = htmlUtils.replaceImageUrls(md, callback(testCases[i][1]));
expect(r.trim()).toBe(testCases[i][2].trim());
const md = testCases[i][0] as string;
const r = htmlUtils.replaceImageUrls(md, callback(testCases[i][1] as string[]));
expect(r.trim()).toBe((testCases[i][2] as string).trim());
}
}));

View File

@ -31,19 +31,20 @@ const selfClosingElements = [
];
class HtmlUtils {
headAndBodyHtml(doc: any) {
public headAndBodyHtml(doc: any) {
const output = [];
if (doc.head) output.push(doc.head.innerHTML);
if (doc.body) output.push(doc.body.innerHTML);
return output.join('\n');
}
isSelfClosingTag(tagName: string) {
public isSelfClosingTag(tagName: string) {
return selfClosingElements.includes(tagName.toLowerCase());
}
// Returns the **encoded** URLs, so to be useful they should be decoded again before use.
extractImageUrls(html: string) {
public extractImageUrls(html: string) {
if (!html) return [];
const output = [];
@ -55,7 +56,7 @@ class HtmlUtils {
return output.filter(url => !!url);
}
replaceImageUrls(html: string, callback: Function) {
public replaceImageUrls(html: string, callback: Function) {
return this.processImageTags(html, (data: any) => {
const newSrc = callback(data.src);
return {
@ -65,7 +66,7 @@ class HtmlUtils {
});
}
processImageTags(html: string, callback: Function) {
public processImageTags(html: string, callback: Function) {
if (!html) return '';
return html.replace(imageRegex, (_v: string, before: string, src: string, after: string) => {
@ -90,7 +91,7 @@ class HtmlUtils {
});
}
prependBaseUrl(html: string, baseUrl: string) {
public prependBaseUrl(html: string, baseUrl: string) {
if (!html) return '';
return html.replace(anchorRegex, (_v: string, before: string, href: string, after: string) => {
@ -99,7 +100,7 @@ class HtmlUtils {
});
}
attributesHtml(attr: any) {
public attributesHtml(attr: any) {
const output = [];
for (const n in attr) {
@ -110,7 +111,7 @@ class HtmlUtils {
return output.join(' ');
}
stripHtml(html: string) {
public stripHtml(html: string) {
const output: string[] = [];
const tagStack: any[] = [];