Skip to content

Commit

Permalink
Add ability to ignore elements if they already contain   characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy-set-studio committed May 10, 2018
1 parent 5697a21 commit 35c1761
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -66,6 +66,7 @@ That settings object now allows `<h2>` elements within `article#content` to be p
| `minWords` | Number | The minimum amount of words that have to be present in an element's content before TypeMate will process it | 4 |
| `selector` | String | The selector string that's passed to `querySelectorAll` | 'p' |
| `ignoreClass` | String | The CSS class that can be added to an element to mark itself as ignorable to TypeMate | 'js&#8288;-&#8288;typemate__ignore' |
| `ignoreExistingSpaceChars` | Boolean | Determine if elements should be ignored if they already contain an `&nbsp;` character | false |

## Codepen example

Expand Down
2 changes: 1 addition & 1 deletion dist/typemate.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion src/typemate.js
Expand Up @@ -14,7 +14,8 @@ class TypeMate {
self.settings = Object.assign({
minWords: 4,
selector: 'p',
ignoreClass: 'js-typemate__ignore'
ignoreClass: 'js-typemate__ignore',
ignoreExistingSpaceChars: false
}, settings);

// Either load from root or the passed parent element
Expand All @@ -40,6 +41,11 @@ class TypeMate {
return false;
}

// Run the ignore checker nd bail if required
if(self.shouldElementBeIgnored(elem)) {
return false;
}

// The result string will be tacked on to this
var result = '';

Expand Down Expand Up @@ -88,9 +94,33 @@ class TypeMate {
let self = this;

self.elems.map(elem => {

// Run the ignore checker nd bail if required
if(self.shouldElementBeIgnored(elem)) {
return false;
}

elem.innerHTML = elem.innerHTML.replace(/&nbsp;/g, ' ');
});
}

/**
* Run checks to see if the passed element should be skipped
*
* @param {HTMLElement} elem
* @returns boolean
*/
shouldElementBeIgnored(elem) {
let self = this;

// Check if the element already contains 1 or more &nbsp; characters and the
// ignore setting is true. If so: bail.
if((elem.innerHTML.indexOf('&nbsp;') > -1) && self.settings.ignoreExistingSpaceChars) {
return true;
}

return false;
}
};

module.exports = function(parent, settings = {}) {
Expand Down
7 changes: 7 additions & 0 deletions tests.json
Expand Up @@ -36,5 +36,12 @@
},
"init": "<p class=\"ignore\">Etiam porta sem malesuada.</p>",
"apply": "<p class=\"ignore\">Etiam porta sem malesuada.</p>"
},
"Will ignore element if there's 1 or more &nbsp; characters present": {
"settings": {
"ignoreExistingSpaceChars": true
},
"init": "<p>Some text with an existing&nbsp;character</p>",
"apply": "<p>Some text with an existing&nbsp;character</p>"
}
}

0 comments on commit 35c1761

Please sign in to comment.