Georgeios_lavei
Georgeios_lavei

Reputation: 13

jQuery html attributes with HTML?

I'm using jQuery to make sure that my users can't execute javascript through my chat app. This is how I append the data that I get from WebSocket server to the users:

obj = JSON.parse(e.data);

    $("<li>", {
        class: "list-group-item",
        text: obj.username + ": " + obj.message
    }).prependTo('#chatlog');

This works great, and seems to escape all XSS attacks, but here's my problem. I want to make obj.username bold, but I have no idea how to go about doing that since everything after text: becomes text. Very happy for any help on this!

Upvotes: 1

Views: 76

Answers (2)

Rohit Sharma
Rohit Sharma

Reputation: 3334

Then you can use another way:-

$("<li>", {
  class: "list-group-item",
}).append($('<span>', {
  class: 'font-weight-bold',
  text: obj.username
})).append($('<span>', {
  text: `: ${obj.message}`
})).prependTo('#chatlog');

See the snippet below:-

const obj = {
  username: 'John Doe',
  message: 'Hi!'
};

$("<li>", {
  class: "list-group-item",
}).append($('<span>', {
  class: 'font-weight-bold',
  text: obj.username
})).append($('<span>', {
  text: `: ${obj.message}`
})).prependTo('#chatlog');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<ul class="list-group" id="chatlog"></ul>

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115222

Set HTML content with span wrapped element where you can convert text by using a temporary element.

const escape = txt => $('<div/>').text(txt).html()    

$("<li>", {
  class: "list-group-item",
  html: '<span style="font-weight:bold">' + escape(obj.username) + "</span>: " + escape(obj.message)
}).prependTo('#chatlog');

const obj = {
  username: 'John Doe',
  message: 'Hi!<script>djsjdk<\/script>'
};

const escape = txt => $('<div/>').text(txt).html();

console.log(escape(obj.message));

$("<li>", {
  class: "list-group-item",
  html: '<span style="font-weight:bold">' + escape(obj.username) + "</span>: " + escape(obj.message)
}).prependTo('#chatlog');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="chatlog"></ul>

Or alternatively, you can create a span using jQuery and for message create a text node.

$("<li>", {
  class: "list-group-item",
  html: [
    $('<span>', {
      css: {
        fontWeight: 'bold'
      },
      text: obj.username
    }),
    document.createTextNode(': ' + obj.message)
  ]
}).prependTo('#chatlog');

const obj = {
  username: 'John Doe',
  message: 'Hi!<script>djsjdk<\/script>'
};


$("<li>", {
  class: "list-group-item",
  html: [
    $('<span>', {
      css: {
        fontWeight: 'bold'
      },
      text: obj.username
    }),
    document.createTextNode(': ' + obj.message)
  ]
}).prependTo('#chatlog');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="chatlog"></ul>

Upvotes: 2

Related Questions