Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

fix(typeahead): Typeahead uses ngSanitize when present #3463

Closed

Conversation

fernando-sendMail
Copy link
Contributor

Hey there!

This pull request addresses the issue #2884 which mentions the use of the attribute of bind-html-unsafe attribute in the default template/typeahead/typeahead-match.html.

So basically, I changed a little the testing process, considering what @chrisirhc said about the fix, here's the implemented solution:

  1. Added the angular-sanitize.js dependency to the misc/test-lib/ folder (the angular-sanitize.js is the same version as the angular.js included).
  2. Changed the karma.conf.js file to add this dependency.
  3. Added a line in the demo file to ship it with the dist folder.
  4. Changed the typeaheadHilghlight filter:
    • Checks if $sanitize is available to use, if not then a warning is printed to the console using the $log service, this will happen when the filer is requested by angular. The ngSanitize can be imported as part of the module that's using the typeahead directive. The reason of why the ngSanitize module is not imported directly on the ui.bootstrap.typeahead module is because it will add another dependency to the project, needless to say it will not work if not present.
    • When $sanitize is present it will use it to sanitize the string itself if it contains any sequence of < followed by any combination of chars, and an ending >, if not, then the string is wrapped in an object that ng-bind-html need to be displayed with the $sce.trustAsHtml method.
    • And finally the <strong> tags are added to the matched characters.
  5. On the file src/typeahead/test/typeahead-highlight.spec.js the returned value of the filter need to be unwrapped.
  6. Created another spec file (src/typeahead/test/typeahead-highlight-ngsanitize.spec.js) that will include the ngSanitize module and test that tags and attributes are being escaped.

And that's it, I think that's all.

I'm looking forward to your feedback and comments. Thanks!

}

return function(matchItem, query) {
return query ? ('' + matchItem).replace(new RegExp(escapeRegexp(query), 'gi'), '<strong>$&</strong>') : matchItem;
if($sanitize) { // Is the $sanitize service available?
if(/<.*>/g.test(matchItem)){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this check isn't required. It's safe to $sanitize something even if it doesn't contain any HTML tags.

@chrisirhc
Copy link
Contributor

Did a review. Good work here. Also, PR needs rebasing.

@chrisirhc chrisirhc added this to the Backlog milestone Mar 31, 2015
@fernando-sendMail
Copy link
Contributor Author

Hi @chrisirhc
Thanks for your feedback, I polished the code according to your comments. I already pushed the commit.

@karianna
Copy link
Contributor

karianna commented Apr 3, 2015

Can't be merged at present

@fernando-sendMail
Copy link
Contributor Author

@karianna I'm sorry, I'm kind of new to this, can I or should I do something about that?

@karianna
Copy link
Contributor

karianna commented Apr 4, 2015

@fernando-sendMail you'll need to rebase, e.g.

  1. do a git pull angular-ui/bootstrap master (I'm assuming you've aliased the angular-ui bootstrap remote)
  2. resolve any conflicts
  3. git push into your remote branch again.

YMMV here, others rebase in different ways :-).

@fernando-sendMail
Copy link
Contributor Author

@karianna Thanks for the info!

@@ -18,6 +18,7 @@ module.exports = function(config) {
'misc/test-lib/jquery-1.8.2.min.js',
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'misc/test-lib/angular-sanitize.js',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should change this to use node_modules/angular-sanitize/angular-sanitize.js and modify package.json to include angular-sanitize as a module since we changed this in 5536e96 so that we don't need to include the angular files in this repository.

@chrisirhc
Copy link
Contributor

Thanks @fernando-sendMail for your work on this, I did some further review.

@chrisirhc
Copy link
Contributor

Also note, should remove typeahead's dependency on ui.bootstrap.bindHtml module as it's no longer used after this change ( 👍 hooray!)

@fernando-sendMail
Copy link
Contributor Author

@chrisirhc Thanks for the review, I'll be working on this over the week.

@fernando-sendMail
Copy link
Contributor Author

Hey @chrisirhc I committed the latest stuff with the fixes and modifications to the test.

@fernando-sendMail
Copy link
Contributor Author

@wesleycho Sure, I'll work on that.

@fernando-sendMail
Copy link
Contributor Author

Hey @wesleycho I think I resolved all the conflicts, the PR is ready for review again

@wesleycho
Copy link
Contributor

Can you squash into one commit @fernando-sendMail ?

@@ -1,38 +1,37 @@
{
"author": "https://github.com/angular-ui/bootstrap/graphs/contributors",
"name": "angular-ui-bootstrap",
"version": "0.13.4-SNAPSHOT",
"version": "0.13.0-SNAPSHOT",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this back to 4

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert all changes to this file excepting the addition of angular-sanitize

@wesleycho
Copy link
Contributor

With these minor reverts/changes, this should be good to merge. Squashing the commits makes it easier to replay the change on top of master when we merge it, which is very useful for us.

@fernando-sendMail
Copy link
Contributor Author

Hey @wesleycho I tried using git rebase -i <commit to squash> but that didn't worked for me, it created an aditional commit instead, am I doing something wrong?

@wesleycho
Copy link
Contributor

You can do git rebase -i HEAD~[number of commits without brackets]

@wesleycho
Copy link
Contributor

Keep in mind once you do this, you will have to force push to the remote to overwrite the history with the new version.

@fernando-sendMail
Copy link
Contributor Author

@wesleycho Finally I've made it, it's ready for review again.

@wesleycho wesleycho modified the milestones: 0.13.4 (Performance), 0.13.x Aug 17, 2015
@wesleycho wesleycho closed this in bb9fa1a Aug 17, 2015
@wesleycho
Copy link
Contributor

Thanks a lot for sticking with it through the time, this is good work!

@fernando-sendMail
Copy link
Contributor Author

No problem, thanks a lot for the support guys!
On Mon, Aug 17, 2015 at 7:18 AM Wesley Cho [email protected] wrote:

Thanks a lot for sticking with it through the time, this is good work!


Reply to this email directly or view it on GitHub
#3463 (comment)
.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants