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:
parent
73314447b9
commit
285482804a
@ -103,6 +103,9 @@ packages/app-cli/tests/Synchronizer.tags.js.map
|
|||||||
packages/app-cli/tests/fsDriver.d.ts
|
packages/app-cli/tests/fsDriver.d.ts
|
||||||
packages/app-cli/tests/fsDriver.js
|
packages/app-cli/tests/fsDriver.js
|
||||||
packages/app-cli/tests/fsDriver.js.map
|
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.d.ts
|
||||||
packages/app-cli/tests/models_Folder.js
|
packages/app-cli/tests/models_Folder.js
|
||||||
packages/app-cli/tests/models_Folder.js.map
|
packages/app-cli/tests/models_Folder.js.map
|
||||||
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -90,6 +90,9 @@ packages/app-cli/tests/Synchronizer.tags.js.map
|
|||||||
packages/app-cli/tests/fsDriver.d.ts
|
packages/app-cli/tests/fsDriver.d.ts
|
||||||
packages/app-cli/tests/fsDriver.js
|
packages/app-cli/tests/fsDriver.js
|
||||||
packages/app-cli/tests/fsDriver.js.map
|
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.d.ts
|
||||||
packages/app-cli/tests/models_Folder.js
|
packages/app-cli/tests/models_Folder.js
|
||||||
packages/app-cli/tests/models_Folder.js.map
|
packages/app-cli/tests/models_Folder.js.map
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
/* eslint-disable no-unused-vars */
|
import htmlUtils from '@joplin/lib/htmlUtils';
|
||||||
|
|
||||||
const htmlUtils = require('@joplin/lib/htmlUtils.js');
|
|
||||||
|
|
||||||
describe('htmlUtils', function() {
|
describe('htmlUtils', function() {
|
||||||
|
|
||||||
@ -19,8 +17,8 @@ describe('htmlUtils', function() {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for (let i = 0; i < testCases.length; i++) {
|
for (let i = 0; i < testCases.length; i++) {
|
||||||
const md = testCases[i][0];
|
const md = testCases[i][0] as string;
|
||||||
const expected = testCases[i][1];
|
const expected = testCases[i][1] as string[];
|
||||||
|
|
||||||
expect(htmlUtils.extractImageUrls(md).join(' ')).toBe(expected.join(' '));
|
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" >'],
|
['<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;
|
let i = -1;
|
||||||
|
|
||||||
return function(src) {
|
return function(_src: string) {
|
||||||
i++;
|
i++;
|
||||||
return urls[i];
|
return urls[i];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
for (let i = 0; i < testCases.length; i++) {
|
for (let i = 0; i < testCases.length; i++) {
|
||||||
const md = testCases[i][0];
|
const md = testCases[i][0] as string;
|
||||||
const r = htmlUtils.replaceImageUrls(md, callback(testCases[i][1]));
|
const r = htmlUtils.replaceImageUrls(md, callback(testCases[i][1] as string[]));
|
||||||
expect(r.trim()).toBe(testCases[i][2].trim());
|
expect(r.trim()).toBe((testCases[i][2] as string).trim());
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
@ -31,19 +31,20 @@ const selfClosingElements = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
class HtmlUtils {
|
class HtmlUtils {
|
||||||
headAndBodyHtml(doc: any) {
|
|
||||||
|
public headAndBodyHtml(doc: any) {
|
||||||
const output = [];
|
const output = [];
|
||||||
if (doc.head) output.push(doc.head.innerHTML);
|
if (doc.head) output.push(doc.head.innerHTML);
|
||||||
if (doc.body) output.push(doc.body.innerHTML);
|
if (doc.body) output.push(doc.body.innerHTML);
|
||||||
return output.join('\n');
|
return output.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
isSelfClosingTag(tagName: string) {
|
public isSelfClosingTag(tagName: string) {
|
||||||
return selfClosingElements.includes(tagName.toLowerCase());
|
return selfClosingElements.includes(tagName.toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the **encoded** URLs, so to be useful they should be decoded again before use.
|
// 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 [];
|
if (!html) return [];
|
||||||
|
|
||||||
const output = [];
|
const output = [];
|
||||||
@ -55,7 +56,7 @@ class HtmlUtils {
|
|||||||
return output.filter(url => !!url);
|
return output.filter(url => !!url);
|
||||||
}
|
}
|
||||||
|
|
||||||
replaceImageUrls(html: string, callback: Function) {
|
public replaceImageUrls(html: string, callback: Function) {
|
||||||
return this.processImageTags(html, (data: any) => {
|
return this.processImageTags(html, (data: any) => {
|
||||||
const newSrc = callback(data.src);
|
const newSrc = callback(data.src);
|
||||||
return {
|
return {
|
||||||
@ -65,7 +66,7 @@ class HtmlUtils {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
processImageTags(html: string, callback: Function) {
|
public processImageTags(html: string, callback: Function) {
|
||||||
if (!html) return '';
|
if (!html) return '';
|
||||||
|
|
||||||
return html.replace(imageRegex, (_v: string, before: string, src: string, after: string) => {
|
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 '';
|
if (!html) return '';
|
||||||
|
|
||||||
return html.replace(anchorRegex, (_v: string, before: string, href: string, after: string) => {
|
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 = [];
|
const output = [];
|
||||||
|
|
||||||
for (const n in attr) {
|
for (const n in attr) {
|
||||||
@ -110,7 +111,7 @@ class HtmlUtils {
|
|||||||
return output.join(' ');
|
return output.join(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
stripHtml(html: string) {
|
public stripHtml(html: string) {
|
||||||
const output: string[] = [];
|
const output: string[] = [];
|
||||||
|
|
||||||
const tagStack: any[] = [];
|
const tagStack: any[] = [];
|
||||||
|
Loading…
Reference in New Issue
Block a user