Store CSS in string

Asked

Viewed 374 times

2

.ql-snow .ql-tooltip{background-color:#fff;border:1px solid #ccc;box-shadow:0 0 5px #ddd;color:#444;padding:5px 12px;white-space:nowrap}.ql-snow .ql-tooltip::before{content:"Visit URL:";line-height:26px;margin-right:8px}.ql-snow .ql-tooltip input[type=text]{display:none;border:1px solid #ccc;font-size:13px;height:26px;margin:0;padding:3px 5px;width:170px}.ql-snow .ql-tooltip a.ql-preview{display:inline-block;max-width:200px;overflow-x:hidden;text-overflow:ellipsis;vertical-align:top}.ql-snow .ql-tooltip a.ql-action::after{border-right:1px solid #ccc;content:'Edit';.ql-snow .ql-tooltip a.ql-action2::after{border-right:1px solid #ccc;content:"Cancel";}

Is there an online site that converts HTML, or CSS into string, to play everything within a variable? For example:

I put in the form that:

body {
 margin:0;
 padding: 0;
}

He’d return something like this to me, making all the escape arrangements:

var css = 'body{margin:0;padding:0}';

If it were in PHP, I would do so:

$var =  <<<END
   .ql-snow .ql-tooltip{background-color:#fff;border:1px solid #ccc;box-shadow:0 0 5px #ddd;color:#444;padding:5px 12px;white-space:nowrap}.ql-snow .ql-tooltip::before{content:"Visit URL:";line-height:26px;margin-right:8px}.ql-snow .ql-tooltip input[type=text]{display:none;border:1px solid #ccc;font-size:13px;height:26px;margin:0;padding:3px 5px;width:170px}.ql-snow .ql-tooltip a.ql-preview{display:inline-block;max-width:200px;overflow-x:hidden;text-overflow:ellipsis;vertical-align:top}.ql-snow .ql-tooltip a.ql-action::after{border-right:1px solid #ccc;content:'Edit';.ql-snow .ql-tooltip a.ql-action2::after{border-right:1px solid #ccc;content:"Cancel";}
END;

I need to do this to generate a PDF:

var valor1 = 'aqui viria o css minificado com as aspas escapada';
var valor2 = 'mais CSS aqui...';

 $('.main').prepend('<style type="text/css">' + [
                 valor1,
                 valor2
              ].join('') + '</style>');

OBS: My question is clear, the problem is that people did not understand, and so I’m having to add more information, to clarify for those who cannot understand what is obvious:

1 - I’m not looking for a solution to minify CSS

2 - I’m looking for a solution to play CSS minified inside a variable with a string inside quotes.

3 - I don’t want to do the string manually (that is, I don’t want to type it), I want to generate a string in the string format and not its value.

  • 1

    It is not capturing the value and removing line breaks and spaces?

  • Yes, I need this css in value... But not only this... manually, gives a lot of work.

  • I tried to on that website, but I found nothing.

  • It’s no trouble, see the jbueno’s answer.

3 answers

5

I don’t know if I understand what you want, because I don’t understand the usefulness of this, but I think all you need to do is remove the line breaks and spaces from the text.

document.getElementById('btn-remover').addEventListener('click', fnClick);

function fnClick(){
  var val = document.getElementById('txt-css').value;
  val = val.replace(/[\s]/g, ' ');
  console.log(val);
}
textarea {
  height: 300px;
  width: 300px;
}
<textarea id="txt-css"></textarea> <br>
<button id="btn-remover">Remover</button>

  • That’s about it, only it didn’t work.

  • 2

    Ué, explain what "did not work out" then for the author to improve the answer.

  • 1

    @Ivanferrer in this case you need to detail what did not work.

  • Yeah, I’m still waiting for the feedback XD

  • What did not work, the result of your filter, You can not paste between quotes. Edit and "Cancel" continue with the same quotes. If it is simple quotes, it should be 'Cancel' and "Cancel", conversely, "Cancel"and ' Cancel' .

2


I made this example based on the @jbueno response trying to touch what I think was your expectation. See if it suits you.

document.getElementById('btn-remover').addEventListener('click', fnClick);
var str = document.getElementsByTagName('style')[0].firstChild.data;
var textArea = document.getElementById('txt-css');

function fnClick() {
  var val = str.replace(/\s+/g, ' ');
  console.log(val);
  textArea.innerHTML += val;
}
textarea {
  height: 150px;
  width: 300px;
}
<textarea id="txt-css"></textarea> <br>
<button id="btn-remover">Remover</button>

ESCAPING QUOTES

Here the code changes all " for \":

document.getElementById('btn-remover').addEventListener('click', fnClick);
var str = document.getElementsByTagName('style')[0].firstChild.data;
var textArea = document.getElementById('txt-css');

function fnClick(){
  var val = str.replace(/\s+/g, ' ');
  var newVal = val.replace(/"/g, '\\"');
  console.log(newVal);
  textArea.innerHTML += newVal;
}
textarea {
  height: 150px;
  width: 300px;
}
.escaped {
  content: "http://urlAleatório.com";
}
<textarea id="txt-css"></textarea> <br>
<button id="btn-remover">Remover</button>

IN QUOTES

Here, in addition to changing the quotes, the variable is created already inside the ' '.

document.getElementById('btn-remover').addEventListener('click', fnClick);
var str = document.getElementsByTagName('style')[0].firstChild.data;
var textArea = document.getElementById('txt-css');

function fnClick(){
  var val = str.replace(/\s+/g, ' ');
  var escVal = val.replace(/"/g, '\\"');
  var newVal = "'" + escVal + "'";
  console.log(newVal);
  textArea.innerHTML += newVal;
}
textarea {
  height: 150px;
  width: 300px;
}
.escaped {
  content: "http://urlAleatório.com";
}
<textarea id="txt-css"></textarea> <br>
<button id="btn-remover">Remover</button>

SEPARATE STYLE SHEET

This option will work just using a separate style sheet and will do everything that the last option does. This function will also replace all single quotes in double escaped quotes. If you are doing it offline, Chrome may give security error as it does not let js do this function. In that case, you would need to start Chrome with websecurity disabled, or test it with online files.

document.getElementById('btn-remover').addEventListener('click', fnClick);
var textArea = document.getElementById('txt-css');
var str;
$.ajax({
  url: 'style.css',
  success: function(data) {
    str = data;
  }
});

function fnClick() {
  var val = str.replace(/\s+/g, ' ');
  var escVal = val.replace(/"/g, '\\"');
  var escVal = escVal.replace(/'/g, '\\"');
  var newVal = "'" + escVal + "'";
  console.log(newVal);
  textArea.innerHTML += newVal;
}
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>

<textarea id="txt-css"></textarea> <br>
<button id="btn-remover">Remover</button>

  • Your answer is the closest, however, you have removed the quotes, I would like to treat it to this format when it is double: " in this format.

  • @Ivanferrer I don’t understand. Would you like the variable to come with quotes around it? Type: 'body { repare nas aspas: 0px; }' ? Would that be?

  • @Ivanferrer Or you want to turn all " in \" in the created string?

  • @Ivanferrer I put an EDIT escaping the quotes. See if this is it.

  • {&#xA; "message": "Uncaught TypeError: Cannot read property 'replace' of undefined",&#xA; "filename": "https://stacksnippets.net/js",&#xA; "lineno": 30,&#xA; "colno": 16&#xA;}

  • @Ivanferrer Have you read my description? This last example is for separate style sheets. Logically it doesn’t work there. Take the example and use on your PC with your stylesheet and replace in the code the url: 'style.css', by the way and the name of your style sheet. I put 4 examples. Some should be what you are looking for. xD

  • Okay, I’ll test here.

  • I tested it here, he put the CSS in the textarea, he even put the backslash in the quotes, only it keeps breaking, because it has double quotes and single quotes, it didn’t work. I believe it is inverted..., it would be double quotes no, for this case... the string, closes with double quotes?

  • @Ivanferrer Complete for me. What exactly do you want to happen with each type of quotation marks? Simple quotation marks: ' should turn what? Double quotes: " must be what?

  • In this example I made it have simple quotes at the beginning and at the end, if I understand correctly, you want me to turn all internal quotes into \". Correct?

  • @Ivanferrer I made a new edition, please read what is written on top of the example. Note only that this code already puts simple quotes around the result, if you prefer you can take this option.

  • it worked, I just had to change it like this: var newVal = "\"" + escVal + "\";";

  • @Ivanferrer Show! Good luck there now! :)

Show 8 more comments

0

Maybe that’s what:

<!DOCTYPE html>
<html>
<head>
<style>
body {
    background-color: blue; 
    color: red;
}
p {
    background-color: green; 
    color: red;
}
</style>
</head>
<body>

<p>Click the button to display the style properties of this document.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementsByTagName("STYLE")[0];
    document.getElementById("demo").innerHTML = x.innerHTML;
}
</script>

</body>
</html>

Source

  • Your answer is interesting, however, my style is coming from a style.css file, so there’s no way I can do it this way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.