Skip to content

Commit

Permalink
Add: Added the function call in the index for the entity replacer
Browse files Browse the repository at this point in the history
Delete: got rid of old tests that didnt apply
Delete: got rid of innerHtml assigning blank string
Update: Simplified the decodehtml function within the tests
  • Loading branch information
smilegodly committed Oct 6, 2020
1 parent 4204e92 commit 47c7617
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 108 deletions.
3 changes: 3 additions & 0 deletions src/backend/utils/html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const sanitize = require('./sanitize');
const fixIFrameWidth = require('./fix-iframe-width');
const lazyLoad = require('./lazy-load');
const syntaxHighlight = require('./syntax-highlight');
const replaceCodeEntities = require('./replace-entities');
const toDOM = require('./dom');

/**
Expand All @@ -26,6 +27,8 @@ module.exports = function process(html) {
fixIFrameWidth(dom);
// Update <img> and <iframe> elements to use native lazy loading.
lazyLoad(dom);
// Replace <code> elements with encoded entities to use characters
replaceCodeEntities(dom);

// Return the resulting HTML
return dom.window.document.body.innerHTML;
Expand Down
8 changes: 3 additions & 5 deletions src/backend/utils/html/replace-entities.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const entities = require('entities');

function decode(codeElement) {
// decode twice
// decode twice for double encoded entities
const result = entities.decodeHTML(codeElement.innerHTML);
return entities.decodeHTML(result);
}
// This function is meant query code elements, decode the entities, and then change the dom with those changes

module.exports = function (dom) {
if (!(dom && dom.window && dom.window.document)) {
return null;
Expand All @@ -14,10 +14,8 @@ module.exports = function (dom) {
let fixedCodeElement = {};

dom.window.document.querySelectorAll('code').forEach((code) => {
console.log('replace-entities: INSIDE OF THE FOREACH FUNCTION!');
fixedCodeElement = decode(code);
code.innerHTML = '';
code.innerHTML = fixedCodeElement;
});
return String(fixedCodeElement);
return String(fixedCodeElement); // return result to the tests
};
106 changes: 3 additions & 103 deletions test/replace-entities.test.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,32 @@
// const entities = require('entities');
const toDOM = require('../src/backend/utils/html/dom');
const replaceEntity = require('../src/backend/utils/html/replace-entities');

function decodeHTML(htmlData) {
console.log('Inside of the decodeHTML function');

// convert data to jsdom
const dom = toDOM(htmlData);
const fixedCodeElement = replaceEntity(dom);

// get the data for tests
let fixedCodeElement = replaceEntity(dom);
fixedCodeElement = `<code>${fixedCodeElement}</code>`;
const encodedHtml = dom.window.document.body.innerHTML;
const decodedContent = dom.window.document.body.textContent;

// make an array
const data = Array.from([fixedCodeElement, encodedHtml, decodedContent]);

// put the code blocks back onto elements 0 & 1
for (let i = 0; i < 1; i += 1) {
data[i] = `<code>${data[i]}</code>`;
}

return data;
return Array.from([fixedCodeElement, encodedHtml]);
}

test('Encoded string with no code block entities should not be changed', () => {
const htmlData = '<code><p><b>Hello World</b></p></code>';
const decoded = '<code><p><b>Hello World</b></p></code>';
const content = 'Hello World';

// decode the data
const data = decodeHTML(htmlData);

expect(Array.isArray(data)).toBe(true);
expect(data[0]).toBe(decoded);
expect(data[1]).toBe(htmlData);
expect(data[2]).toBe(content);
});

test('Encoded string with non-breaking space entities should be decoded', () => {
const htmlData =
'<code><p><b>Lorem&nbsp;ipsum&nbsp;dolor&nbsp;sit&nbsp;amet,&nbsp;consectetur&nbsp;adipiscing&nbsp;elit.</b></p></code>';
const decoded =
'<code><p><b>Lorem\xa0ipsum\xa0dolor\xa0sit\xa0amet,\xa0consectetur\xa0adipiscing\xa0elit.</b></p></code>';
const content = 'Lorem\xa0ipsum\xa0dolor\xa0sit\xa0amet,\xa0consectetur\xa0adipiscing\xa0elit.';

// decode the data
const data = decodeHTML(htmlData);

expect(Array.isArray(data)).toBe(true);
expect(data[0]).toBe(decoded);
expect(data[1]).toBe(htmlData);
expect(data[2]).toBe(content);
});

test('Encoded string with greater than sign should be decoded twice (double encoded)', () => {
Expand Down Expand Up @@ -91,26 +66,10 @@ test('Encoded string with greater than sign should be decoded twice (double enco
}
}
</code>`;
const content = `
// if -f filename.txt, take all files and put into the files variables.
if (yargs.argv._.length >
1) {
for (
let i =
1; i > yargs.argv._.length; i++) {
files.push(yargs.argv._[i]);
}
}
}
`;

// decode the data
const data = decodeHTML(htmlData);

expect(Array.isArray(data)).toBe(true);
expect(data[0]).toBe(decoded);
expect(data[1]).toBe(encodedHtml);
expect(data[2]).toBe(content);
});

test('Encoded string with less than sign should be decoded twice (double encoded)', () => {
Expand Down Expand Up @@ -150,56 +109,23 @@ test('Encoded string with less than sign should be decoded twice (double encoded
}
}
</code>`;
const content = `
// if -f filename.txt, take all files and put into the files variables.
if (yargs.argv._.length <
1) {
for (
let i =
1; i < yargs.argv._.length; i++) {
files.push(yargs.argv._[i]);
}
}
}
`;

// decode the data
const data = decodeHTML(htmlData);

expect(Array.isArray(data)).toBe(true);
expect(data[0]).toBe(decoded);
expect(data[1]).toBe(encodedHtml);
expect(data[2]).toBe(content);
});

test('Encoded string with ampersand entities should be decoded', () => {
const htmlData =
'<code><p><b>Lorem&amp;ipsum&amp;dolor&amp;sit&amp;amet,&amp;consectetur&amp;adipiscing&amp;elit.</b></p></code>';
const decoded =
'<code><p><b>Lorem&ipsum&dolor&sit&amet,&consectetur&adipiscing&elit.</b></p></code>';
const content = 'Lorem&ipsum&dolor&sit&amet,&consectetur&adipiscing&elit.';

// decode the data
const data = decodeHTML(htmlData);

expect(Array.isArray(data)).toBe(true);
expect(data[0]).toBe(decoded);
expect(data[1]).toBe(htmlData);
expect(data[2]).toBe(content);
});

// test('test to see if the string can get rid of double quote', () => {

// });

// test('test to see if the string can get rid of single quote', () => {

// });

// test('test to see if the string can get rid of apostrophe', () => {

// });

test('Encoded string with arrow function should be decoded', () => {
const htmlData = `<code>
<span class="hljs-string">', ...].map((number) =&gt;;
Expand All @@ -213,22 +139,10 @@ test('Encoded string with arrow function should be decoded', () => {
body: '...'
</span>
</code>`;
const content = `
', ...].map((number) =>;
twilio.messages.create({
body: '...'
`;

// decode the data
const data = decodeHTML(htmlData);

console.log(data[2]);

expect(Array.isArray(data)).toBe(true);
expect(data[0]).toBe(decoded);
expect(data[1]).toBe(htmlData);
expect(data[2]).toBe(content);
});

test('Encoded string with &amp;gt; should be decoded twice (double encoded)', () => {
Expand Down Expand Up @@ -256,22 +170,8 @@ test('Encoded string with &amp;gt; should be decoded twice (double encoded)', ()
<span class="hljs-symbol">></span>
<span class="hljs-symbol">></span> gus.tavo('example.html')\n
</code>`;
const content = `
>
>
> import gus
>
>
> gus.tavo('example.html')
`;

// decode the data
const data = decodeHTML(htmlData);

expect(Array.isArray(data)).toBe(true);
expect(data[0]).toBe(decoded);
expect(data[1]).toBe(encodedHtml);
expect(data[2]).toBe(content);
});

0 comments on commit 47c7617

Please sign in to comment.