Last active
March 8, 2018 15:07
-
-
Save adam-knights/36566222df480f247ca5 to your computer and use it in GitHub Desktop.
html safe string phantomjs issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName:'Ember Twiddle' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
// Inputs | |
// params[0] - Text to add emphasis parts too | |
// params[1] - searchText to use in emphasizing | |
// params[2] - (Optional) Emphasizing element i.e. i for <i>, defaults to <b> | |
export function autoCompleteEmphasizer(params) { | |
if (params === undefined || params === null || params.length === 0) { | |
return false; | |
} | |
var result = params[0]; | |
// If they only provide the string to search in then return it unaltered | |
if (params.length === 1) { | |
return result; | |
} | |
var searchText = params[1].toLowerCase().replace(/\(|\)/g, ''); | |
if (searchText.length === 0) { | |
return result; | |
} | |
// Get search words and make sure search wordsare unique and non empty | |
var searchWords = searchText.split(' ').uniq().reject(w => w === ''); | |
var emphasisElement = params[2] || 'b'; | |
// If any of the search words match the emphasis element then do that word first | |
if (searchWords.contains(emphasisElement)) { | |
searchWords.removeObject(emphasisElement); | |
searchWords.insertAt(0, emphasisElement); | |
} | |
// Make sure result is html safe and contains nothing malicious | |
result = Ember.Handlebars.Utils.escapeExpression(result); | |
searchWords.forEach(w => result = result.replace(new RegExp('(^|)(' + w + ')(|$)','ig'), '$1<' + emphasisElement + '>$2</' + emphasisElement + '>$3')); | |
// Make result as html safe as we know it contains no malicious html | |
return Ember.String.htmlSafe(result); | |
} | |
export default Ember.Helper.helper(autoCompleteEmphasizer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.4.14", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember.debug.js", | |
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.1.0/ember-data.js", | |
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember-template-compiler.js" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment