Skip to content

Commit

Permalink
Fix for span tags (#105)
Browse files Browse the repository at this point in the history
* FIX-FOR Added Fix

* FIX-FOR Cleanup

* FIX-FOR Cleanup

* FIX-FOR Added Fix For Span Tags

* FIX-FOR Cleanup

* FIX-FOR Added Tests

* FIX-FOR Cleanup

* FIX-FOR Cleanup

* FIX-FOR Cleanup

* FIX-FOR Cleanup
  • Loading branch information
hrai committed May 6, 2021
1 parent 73a7747 commit 0a7e4e0
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 44 deletions.
9 changes: 2 additions & 7 deletions distribution/dependencies/bootstrap.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions distribution/dependencies/jquery.min.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
<h2>Testing page</h2>
</div>
<div>
<p id='pt' contenteditable="true">Editable tag</p>
<span id='span' contenteditable="true">Editable span tag</span>
</div>
<div>
<p id='pt' contenteditable="true">Editable p tag</p>
</div>
<div>
<label for="Textarea" style="vertical-align: top;">Textarea:</label>
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "auto-capitalise-extension",
"title": "Auto Capitalise Extension",
"version": "2.0.0",
"description": "Firefox add-on to automatically capitalise the first letter of a sentence while typing.",
"description": "Browser add-on to automatically capitalise the first letter of a sentence while typing.",
"scripts": {
"start": "run-script-os",
"start:win32": "yarn build && web-ext run --firefox=\"c:/Program Files/Firefox Developer Edition/firefox.exe\" --start-url localhost:3000",
"start:darwin:linux": "yarn build && web-ext run --firefox=firefox-developer --start-url localhost:3000",
"start:chrome": "yarn build && cd distribution && web-ext run -t chromium --start-url localhost:3000",
"start:win32": "yarn build-dev && web-ext run --firefox=\"c:/Program Files/Firefox Developer Edition/firefox.exe\" --start-url localhost:3000",
"start:darwin:linux": "yarn build-dev && web-ext run --firefox=firefox-developer --start-url localhost:3000",
"start:chrome": "yarn build-dev && cd distribution && web-ext run -t chromium --start-url localhost:3000",
"build": "yarn webpack",
"build-dev": "yarn webpack --mode=development",
"watch": "yarn build --watch",
Expand Down
26 changes: 26 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let keyValueDictionary = {
[abbreviationsKeyVal]: {},
[locationsKeyVal]: {},
};
const nbsp = '&nbsp;';

export function capitaliseText(
element,
Expand Down Expand Up @@ -221,9 +222,30 @@ export function getText(htmlControl, tagName) {
return htmlControl.value ? htmlControl.value : '';
}

if (htmlControl.innerHTML && tagName.toUpperCase() === 'SPAN') {
return getTextForSpanTag(htmlControl.innerHTML);
}

return htmlControl.innerHTML ? htmlControl.innerHTML : '';
}

export function getTextForSpanTag(text) {
if (text && getNbspCount(text) === 1) {
let result= replaceLastOccurrenceInString(text, nbsp, ' ');
return result;
}

return text;
}

export function replaceLastOccurrenceInString(originalText,textToMatch, replacement) {
return originalText.replace(new RegExp(textToMatch + '$'), replacement);
}

export function getNbspCount(text) {
return (text.match(new RegExp(nbsp, 'g')) || []).length;
}

export function setText(htmlControl, tagName, updatedStr, shouldAppendBr) {
if (
tagName.toUpperCase() === 'INPUT' ||
Expand All @@ -233,6 +255,10 @@ export function setText(htmlControl, tagName, updatedStr, shouldAppendBr) {
return;
}

if (tagName.toUpperCase() === 'SPAN') {
updatedStr = replaceLastOccurrenceInString(updatedStr, ' ', nbsp);
}

if (shouldAppendBr) {
updatedStr += '<br>';
}
Expand Down
95 changes: 92 additions & 3 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ describe('utilities test', function () {
}).toThrow();
});

test('getCapitalisedContentForI', () => {
expect(utils.getCapitalisedContentForI('i ')).toBe('I ');
expect(utils.getCapitalisedContentForI('i\'')).toBe('I\'');
expect(utils.getCapitalisedContentForI('Hi this is i ')).toBe(
'Hi this is I '
);
});

test('shouldCapitaliseForI', () => {
expect(utils.shouldCapitaliseForI('war i ')).toBe(true);
expect(utils.shouldCapitaliseForI(' i ')).toBe(true);
Expand Down Expand Up @@ -97,7 +105,7 @@ describe('utilities test', function () {
'<div>' +
' <input type="text" id="username" value="Bingo" />' +
' <textarea id="about-me" rows="8" cols="40"></textarea> ' +
' <span id="address">Please enter your address.</span> ' +
' <span id="address">Please enter your address&nbsp;</span> ' +
' <button id="button" />' +
'</div>';
}
Expand Down Expand Up @@ -131,16 +139,66 @@ describe('utilities test', function () {
setInnerHtml();
const element = $('#address');
expect(utils.getText(element[0], 'span')).toBe(
'Please enter your address.'
'Please enter your address '
);
expect(utils.getText(element[0], 'input')).toBe('');
expect(utils.getText(element[0], '')).toBe('Please enter your address.');
expect(utils.getText(element[0], '')).toBe(
'Please enter your address&nbsp;'
);
expect(() => {
utils.getText(element);
}).toThrow();
});
});

describe('getNbspCount', () => {
test('getNbspCount', () => {
expect(utils.getNbspCount('test&nbsp;')).toBe(1);
expect(utils.getNbspCount('test&nbsp;&nbsp;')).toBe(2);
expect(utils.getNbspCount('test')).toBe(0);
});
});

describe('replaceLastOccurrenceInString', () => {
test('replaceLastOccurrenceInString_ReplacesText', () => {
expect(
utils.replaceLastOccurrenceInString('test&nbsp;&nbsp;', '&nbsp;', 'me')
).toBe('test&nbsp;me');
expect(
utils.replaceLastOccurrenceInString(
'test&nbsp;&nbsp;&nbsp;',
'&nbsp;',
'me'
)
).toBe('test&nbsp;&nbsp;me');
expect(
utils.replaceLastOccurrenceInString('test this this', 'this', 'and')
).toBe('test this and');
});

test('replaceLastOccurrenceInString_DoesNotReplaceText', () => {
expect(
utils.replaceLastOccurrenceInString('test me', 'bro', 'this')
).toBe('test me');
expect(
utils.replaceLastOccurrenceInString('test me ', 'me', 'this')
).toBe('test me ');
});
});

describe('getTextForSpanTag', () => {
test('getTextForSpanTag_ReplacesTagForSingleOccurrence', () => {
expect(utils.getTextForSpanTag('test&nbsp;')).toBe('test ');
});

test('getTextForSpanTag_DoesNotReplaceTagForMultipleOccurrences', () => {
expect(utils.getTextForSpanTag('test&nbsp;&nbsp;')).toBe(
'test&nbsp;&nbsp;'
);
expect(utils.getTextForSpanTag('test')).toBe('test');
});
});

describe('setText', () => {
function resetHtml() {
document.body.innerHTML =
Expand Down Expand Up @@ -178,6 +236,32 @@ describe('utilities test', function () {
}).toThrow();
});

test('setText_SpanTagWithNbsp', () => {
const updatedStr = 'testing this ';

resetHtml();
let element = $('#address');

utils.setText(element[0], 'span', updatedStr, false);
expect(element.html()).toBe('testing this&nbsp;');

resetHtml();
element = $('#address');
utils.setText(element[0], 'p', '', false);
expect(element.html()).toBe('');

resetHtml();
element = $('#address');
utils.setText(element[0], 'span', '', false);
expect(element.html()).toBe('');

expect(() => {
resetHtml();
element = $('#address');
utils.setText(element[0]);
}).toThrow();
});

test('setText_TextareaTag', () => {
const updatedStr = 'This is my life.';

Expand Down Expand Up @@ -216,6 +300,11 @@ describe('utilities test', function () {
utils.setText(element[0], 'p', updatedStr, false);
expect(element.html()).toBe('This is my life.');

resetHtml();
element = $('#address');
utils.setText(element[0], 'p', 'test space ', false);
expect(element.html()).toBe('test space ');

resetHtml();
element = $('#address');
utils.setText(element[0], 'span', '', false);
Expand Down
13 changes: 0 additions & 13 deletions zip_everything.sh

This file was deleted.

14 changes: 0 additions & 14 deletions zip_files.sh

This file was deleted.

0 comments on commit 0a7e4e0

Please sign in to comment.