Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML input affecting entire page

Tags:

html

jquery

I am making a page where you can enter HTML and see the output in a div to the side.
Code:

$("#tryit").keyup(function() {
  var input = $(this).val();
  $("#readout").html(input);
});
#readout {
  height: 400px;
  width: 48%;
  border: 1px solid black;
  overflow: auto;
  float: right;
  clear: none;
  display: inline-block;
}
#tryit {
  height: 396px;
  width: 50%;
  float: left;
  clear: none;
  display: inline-block;
  font-family: monospace;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea name="tryit" id="tryit" rows="10">&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;New Webpage&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;/body&gt; &lt;/html&gt;
</textarea>

<div id="readout"></div>

It works as it should: it outputs the HTML to the #readout. What goes wrong is when a user adds a <style> tag in the head. Then the style added there affects not only the HTML in the text input, but in the entire page.
How can I make the CSS affect only the user's input, and not the entire page?
Here is the Problem Page.

like image 501
yainspan Avatar asked Jul 16 '15 22:07

yainspan


People also ask

How do you set limits in input type text?

To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.

Which input type defines a slider control?

The <input type="range"> defines a control for entering a number whose exact value is not important (like a slider control). Default range is 0 to 100. However, you can set restrictions on what numbers are accepted with the attributes below.

How do you center an input field in HTML?

Use the CSS text-align Property to Center a Form in HTML We can set the value to center to center the form. For example, apply the text-align property to the form tag in the style attribute, and set the property to center . Next, create input tags with the type text and then submit .


2 Answers

You should really consider using a different approach to achieve this.

If you would really want to go with what you have so far you would have to replace a lot of stuff within the textarea value. Just to give you an idea, in the below snippet i am only replacing the html and body style declarations with the id (#readout) to apply the styles to the #readout div and not to the body of the page. It does work for the body, but what is with all the other possible declarations?

Sure i am a real noob when it comes to regex and i am sure this can be done a lot better, but all of this is not necessary.

$("#tryit").keyup(function() {
    var style = $(this).val().match(/<style>(.*)<\/style>/);
    if (style !== null && style.length > 0) {
        var bodyReplace = style[1].replace(/(^|\s)(html)|(body)(\s|$)/ig, '#readout');
    }
  var input = $(this).val();
  $("#readout").html(input.replace(/<style>.*<\/style>/, '<style>'+bodyReplace+'</style>'));
});

$("#tryit").keyup();
#readout {
  height: 400px;
  width: 48%;
  border: 1px solid black;
  overflow: auto;
  float: right;
  clear: none;
  display: inline-block;
}
#tryit {
  height: 396px;
  width: 50%;
  float: left;
  clear: none;
  display: inline-block;
  font-family: monospace;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea name="tryit" id="tryit" rows="10">&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;New Webpage&lt;/title&gt;<style>body {background: black; color: red }</style> &lt;/head&gt; &lt;body&gt;<h1>Hello</h1> &lt;/body&gt; &lt;/html&gt;
</textarea>
<div id="readout"></div>

An other problem is that the <doctype>, <html>, <head> and <body> elements in your textarea do not really exist. Take a look at the source and you will find out that they are not there and they do not belong there. That being said i would go with a solution mentioned in a comment by Barmar, who suggested using an iframe.

It is actually not that hard to achieve. One quick and dirty way is to crate a blob from the textarea value and add that to the source attribute of an iframe. That would make it easy to even separate your HTML and CSS by having another textarea for the CSS and injecting it's value as a style-sheet into the head of the iframe. Since i can't use the iframe thing in the SO snippet tool because of sandbox security stuff check this

JSFIDDLE

and i am sure you will get the idea.

like image 195
DavidDomain Avatar answered Oct 17 '22 14:10

DavidDomain


In your css make it to where it selects your div input with a specific id then select its child elements:

<style>    
#readout input{
     color: red;
}
</style>

I added the style tag since you are putting the css directly in your html editor. The above css will only affect every input element in your div with the id of readout.

If you are looking for something different please say so

Look at this fiddle

like image 26
code Avatar answered Oct 17 '22 14:10

code