Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display links as <a> tags but don't allow other tags?

So, I am using Vue.js and want to display some text that contains links. The text is input by the users.

Example:

This is a site www.example.com which is better than www.oldexample.com

I want it to appear as:

This is a site www.example.com which is better than www.oldexample.com

So, I want the links to be hyperlinked. The problem is that this is user input, so it has to be XSS protected. I currently display it as text, so I can't use HTML tags in it. If I were to use v-html and wrap all links in a tags it would be too unsafe as users could enter any HTML.

I was thinking about splitting the string by URLs, and wrapping every part that is not a link in a span tag and every link in a a tag. But this seems a bit too much processing to be done just to hyperlink the URLs.

Is there a simpler method for this, that is XSS safe (users can't enter other tags except of <a>) ?

like image 847
XCS Avatar asked Feb 10 '17 21:02

XCS


People also ask

How do you make an A tag do nothing?

To make an anchor tag refer to nothing, use “javascript: void(0)”. The following link does nothing because the expression "0" has no effect in JavaScript. Here the expression "0" is evaluated, but it is not loaded back into the current document.

How do I make a link non clickable?

How do I make a link non-clickable? In order to disable a HTML Anchor Link (HyperLink), the value of its HREF attribute is copied to the REL attribute and the value of HREF attribute is set to an empty JavaScript function. This makes the HTML Anchor Link (HyperLink) disabled i.e. non-clickable.

How do I hide a link in a tag?

Change the "display" or "visibility". Changing your visibility to "hidden" will hide the link without influencing the page layout. Your code for this stage should simply look like: display: none. visibility: hidden.


1 Answers

Use vue-linkify it create a v-linkified directive that do exactly what you need

new Vue({
  el:'#app',
  data:{
    input:'Hello from vuejs.org'
  }
})
<div id="app">
  <input v-model="input"/>

  <div v-html="input" v-linkified>
  </div>

</div>
like image 139
AldoRomo88 Avatar answered Oct 05 '22 22:10

AldoRomo88