Only allow certain tags in contenteditable div












0














I have a contenteditable div and using keyboard shortcuts like ctrl+i the user is able to format the text. And as they type the innerHTML changes reflecting the tags i.e:



Hello&nbsp;<i>thanks for&nbsp;<br><br>for showing up<b>&nbsp;y'all b</b></i>



This is fine, and works well for my purposes. but the issue arises that when I go to print the html in a different div IF a user adds any other html tags, they could really mess up the application.



For instance, if they added a <script> tag or style etc.. How do I make it that the user is only allowed to add <i>, <br>, <b>, <s>, and &nbsp; without being able to add anything else?



Any ideas? Thank you










share|improve this question






















  • The easiest way to accomplish your goal is to maintain control over the input by using document.execCommand(). developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
    – Randy Casburn
    Nov 18 '18 at 1:47










  • @RandyCasburn I'm sorry. I'm not quite following how that helps me?
    – Jane Doe
    Nov 18 '18 at 3:21










  • I'm not sure it does directly other than allowing you to create a custom editor that allows them to type whatever text they want into the editor, and if they want to add emphasis, italics, bold, etc. you provide them a user interface to control that. The only other option is to parse the string and eliminate any thing that looks like a tag you don't want. That is inexact and may or may not achieve quality results for you.
    – Randy Casburn
    Nov 18 '18 at 3:24
















0














I have a contenteditable div and using keyboard shortcuts like ctrl+i the user is able to format the text. And as they type the innerHTML changes reflecting the tags i.e:



Hello&nbsp;<i>thanks for&nbsp;<br><br>for showing up<b>&nbsp;y'all b</b></i>



This is fine, and works well for my purposes. but the issue arises that when I go to print the html in a different div IF a user adds any other html tags, they could really mess up the application.



For instance, if they added a <script> tag or style etc.. How do I make it that the user is only allowed to add <i>, <br>, <b>, <s>, and &nbsp; without being able to add anything else?



Any ideas? Thank you










share|improve this question






















  • The easiest way to accomplish your goal is to maintain control over the input by using document.execCommand(). developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
    – Randy Casburn
    Nov 18 '18 at 1:47










  • @RandyCasburn I'm sorry. I'm not quite following how that helps me?
    – Jane Doe
    Nov 18 '18 at 3:21










  • I'm not sure it does directly other than allowing you to create a custom editor that allows them to type whatever text they want into the editor, and if they want to add emphasis, italics, bold, etc. you provide them a user interface to control that. The only other option is to parse the string and eliminate any thing that looks like a tag you don't want. That is inexact and may or may not achieve quality results for you.
    – Randy Casburn
    Nov 18 '18 at 3:24














0












0








0







I have a contenteditable div and using keyboard shortcuts like ctrl+i the user is able to format the text. And as they type the innerHTML changes reflecting the tags i.e:



Hello&nbsp;<i>thanks for&nbsp;<br><br>for showing up<b>&nbsp;y'all b</b></i>



This is fine, and works well for my purposes. but the issue arises that when I go to print the html in a different div IF a user adds any other html tags, they could really mess up the application.



For instance, if they added a <script> tag or style etc.. How do I make it that the user is only allowed to add <i>, <br>, <b>, <s>, and &nbsp; without being able to add anything else?



Any ideas? Thank you










share|improve this question













I have a contenteditable div and using keyboard shortcuts like ctrl+i the user is able to format the text. And as they type the innerHTML changes reflecting the tags i.e:



Hello&nbsp;<i>thanks for&nbsp;<br><br>for showing up<b>&nbsp;y'all b</b></i>



This is fine, and works well for my purposes. but the issue arises that when I go to print the html in a different div IF a user adds any other html tags, they could really mess up the application.



For instance, if they added a <script> tag or style etc.. How do I make it that the user is only allowed to add <i>, <br>, <b>, <s>, and &nbsp; without being able to add anything else?



Any ideas? Thank you







javascript jquery angular






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 '18 at 1:36









Jane DoeJane Doe

691931




691931












  • The easiest way to accomplish your goal is to maintain control over the input by using document.execCommand(). developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
    – Randy Casburn
    Nov 18 '18 at 1:47










  • @RandyCasburn I'm sorry. I'm not quite following how that helps me?
    – Jane Doe
    Nov 18 '18 at 3:21










  • I'm not sure it does directly other than allowing you to create a custom editor that allows them to type whatever text they want into the editor, and if they want to add emphasis, italics, bold, etc. you provide them a user interface to control that. The only other option is to parse the string and eliminate any thing that looks like a tag you don't want. That is inexact and may or may not achieve quality results for you.
    – Randy Casburn
    Nov 18 '18 at 3:24


















  • The easiest way to accomplish your goal is to maintain control over the input by using document.execCommand(). developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
    – Randy Casburn
    Nov 18 '18 at 1:47










  • @RandyCasburn I'm sorry. I'm not quite following how that helps me?
    – Jane Doe
    Nov 18 '18 at 3:21










  • I'm not sure it does directly other than allowing you to create a custom editor that allows them to type whatever text they want into the editor, and if they want to add emphasis, italics, bold, etc. you provide them a user interface to control that. The only other option is to parse the string and eliminate any thing that looks like a tag you don't want. That is inexact and may or may not achieve quality results for you.
    – Randy Casburn
    Nov 18 '18 at 3:24
















The easiest way to accomplish your goal is to maintain control over the input by using document.execCommand(). developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
– Randy Casburn
Nov 18 '18 at 1:47




The easiest way to accomplish your goal is to maintain control over the input by using document.execCommand(). developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
– Randy Casburn
Nov 18 '18 at 1:47












@RandyCasburn I'm sorry. I'm not quite following how that helps me?
– Jane Doe
Nov 18 '18 at 3:21




@RandyCasburn I'm sorry. I'm not quite following how that helps me?
– Jane Doe
Nov 18 '18 at 3:21












I'm not sure it does directly other than allowing you to create a custom editor that allows them to type whatever text they want into the editor, and if they want to add emphasis, italics, bold, etc. you provide them a user interface to control that. The only other option is to parse the string and eliminate any thing that looks like a tag you don't want. That is inexact and may or may not achieve quality results for you.
– Randy Casburn
Nov 18 '18 at 3:24




I'm not sure it does directly other than allowing you to create a custom editor that allows them to type whatever text they want into the editor, and if they want to add emphasis, italics, bold, etc. you provide them a user interface to control that. The only other option is to parse the string and eliminate any thing that looks like a tag you don't want. That is inexact and may or may not achieve quality results for you.
– Randy Casburn
Nov 18 '18 at 3:24












1 Answer
1






active

oldest

votes


















1














I think that you can use a regExpresion to avoid the "indeseables" tags. Some like



<textarea #data [(ngModel)]="value" (input)="replace(data)"></textarea>
<div [innerHtml]="valueParse">
</div>

replace(control:any)
{
this.valueParse=control.value.replace(/<(?!br|i|u)((w+))>/gm,"&lt$1&gt")
.replace(/</(?!br|i|u)((w+))>/gm,'&lt/$1&gt');
}


See stackblitz






share|improve this answer























  • This works well. I also found this post that seems to work really well: stackoverflow.com/a/46483672/2230430
    – Jane Doe
    Nov 18 '18 at 17:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357161%2fonly-allow-certain-tags-in-contenteditable-div%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I think that you can use a regExpresion to avoid the "indeseables" tags. Some like



<textarea #data [(ngModel)]="value" (input)="replace(data)"></textarea>
<div [innerHtml]="valueParse">
</div>

replace(control:any)
{
this.valueParse=control.value.replace(/<(?!br|i|u)((w+))>/gm,"&lt$1&gt")
.replace(/</(?!br|i|u)((w+))>/gm,'&lt/$1&gt');
}


See stackblitz






share|improve this answer























  • This works well. I also found this post that seems to work really well: stackoverflow.com/a/46483672/2230430
    – Jane Doe
    Nov 18 '18 at 17:16
















1














I think that you can use a regExpresion to avoid the "indeseables" tags. Some like



<textarea #data [(ngModel)]="value" (input)="replace(data)"></textarea>
<div [innerHtml]="valueParse">
</div>

replace(control:any)
{
this.valueParse=control.value.replace(/<(?!br|i|u)((w+))>/gm,"&lt$1&gt")
.replace(/</(?!br|i|u)((w+))>/gm,'&lt/$1&gt');
}


See stackblitz






share|improve this answer























  • This works well. I also found this post that seems to work really well: stackoverflow.com/a/46483672/2230430
    – Jane Doe
    Nov 18 '18 at 17:16














1












1








1






I think that you can use a regExpresion to avoid the "indeseables" tags. Some like



<textarea #data [(ngModel)]="value" (input)="replace(data)"></textarea>
<div [innerHtml]="valueParse">
</div>

replace(control:any)
{
this.valueParse=control.value.replace(/<(?!br|i|u)((w+))>/gm,"&lt$1&gt")
.replace(/</(?!br|i|u)((w+))>/gm,'&lt/$1&gt');
}


See stackblitz






share|improve this answer














I think that you can use a regExpresion to avoid the "indeseables" tags. Some like



<textarea #data [(ngModel)]="value" (input)="replace(data)"></textarea>
<div [innerHtml]="valueParse">
</div>

replace(control:any)
{
this.valueParse=control.value.replace(/<(?!br|i|u)((w+))>/gm,"&lt$1&gt")
.replace(/</(?!br|i|u)((w+))>/gm,'&lt/$1&gt');
}


See stackblitz







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 '18 at 14:13

























answered Nov 18 '18 at 13:56









EliseoEliseo

5,4791312




5,4791312












  • This works well. I also found this post that seems to work really well: stackoverflow.com/a/46483672/2230430
    – Jane Doe
    Nov 18 '18 at 17:16


















  • This works well. I also found this post that seems to work really well: stackoverflow.com/a/46483672/2230430
    – Jane Doe
    Nov 18 '18 at 17:16
















This works well. I also found this post that seems to work really well: stackoverflow.com/a/46483672/2230430
– Jane Doe
Nov 18 '18 at 17:16




This works well. I also found this post that seems to work really well: stackoverflow.com/a/46483672/2230430
– Jane Doe
Nov 18 '18 at 17:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357161%2fonly-allow-certain-tags-in-contenteditable-div%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?