Only allow certain tags in contenteditable div
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 <i>thanks for <br><br>for showing up<b> 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
without being able to add anything else?
Any ideas? Thank you
javascript jquery angular
add a comment |
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 <i>thanks for <br><br>for showing up<b> 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
without being able to add anything else?
Any ideas? Thank you
javascript jquery angular
The easiest way to accomplish your goal is to maintain control over the input by usingdocument.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
add a comment |
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 <i>thanks for <br><br>for showing up<b> 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
without being able to add anything else?
Any ideas? Thank you
javascript jquery angular
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 <i>thanks for <br><br>for showing up<b> 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
without being able to add anything else?
Any ideas? Thank you
javascript jquery angular
javascript jquery angular
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 usingdocument.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
add a comment |
The easiest way to accomplish your goal is to maintain control over the input by usingdocument.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
add a comment |
1 Answer
1
active
oldest
votes
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,"<$1>")
.replace(/</(?!br|i|u)((w+))>/gm,'</$1>');
}
See stackblitz
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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,"<$1>")
.replace(/</(?!br|i|u)((w+))>/gm,'</$1>');
}
See stackblitz
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
add a comment |
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,"<$1>")
.replace(/</(?!br|i|u)((w+))>/gm,'</$1>');
}
See stackblitz
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
add a comment |
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,"<$1>")
.replace(/</(?!br|i|u)((w+))>/gm,'</$1>');
}
See stackblitz
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,"<$1>")
.replace(/</(?!br|i|u)((w+))>/gm,'</$1>');
}
See stackblitz
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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