From 285482804afbad2e64f01f1560cd3d4152c2093a Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 30 Jan 2021 12:19:43 +0000 Subject: [PATCH] Converted htmlUtils to TypeScript --- .eslintignore | 3 +++ .gitignore | 3 +++ .../tests/{htmlUtils.js => htmlUtils.ts} | 18 ++++++++---------- packages/lib/htmlUtils.ts | 17 +++++++++-------- 4 files changed, 23 insertions(+), 18 deletions(-) rename packages/app-cli/tests/{htmlUtils.js => htmlUtils.ts} (88%) diff --git a/.eslintignore b/.eslintignore index 29dae636a..9d5e0376e 100644 --- a/.eslintignore +++ b/.eslintignore @@ -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 diff --git a/.gitignore b/.gitignore index 85cc14303..66e964c79 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/packages/app-cli/tests/htmlUtils.js b/packages/app-cli/tests/htmlUtils.ts similarity index 88% rename from packages/app-cli/tests/htmlUtils.js rename to packages/app-cli/tests/htmlUtils.ts index 70c2d3ef8..bbe69af9b 100644 --- a/packages/app-cli/tests/htmlUtils.js +++ b/packages/app-cli/tests/htmlUtils.ts @@ -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() { ['testing', ['http://other.com/img.png'], '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()); } })); diff --git a/packages/lib/htmlUtils.ts b/packages/lib/htmlUtils.ts index ffce08a4b..6dd03e400 100644 --- a/packages/lib/htmlUtils.ts +++ b/packages/lib/htmlUtils.ts @@ -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[] = [];