0

I am working on an application using Node.js, Express, React & Redux that enables users to write code into a text-editor, when the user hits 'run' to submit their code the code is then compiled using the JDoodle API.

As part of the JSON response returned from JDoodle I get the compiled output, so for instance:

System.out.println("Hello World!");
System.out.println("Hello Universe!");

would return:

Hello World!\nHello Universe!\n

The issue I am having is that I am rendering this output in an Iframe within a React component, and when I do so it is rendering the new line character \n as a single space. So for instance the above code would produce the following in the Iframe:

Hello World! Hello Universe! 

I have tried giving the Iframe a className of iFrame and then in CSS doing the following:

.iFrame {
  white-space: pre-wrap;
}

but no success! I also seen a few others mention escaping the backslash so replacing \n for \n:

.replaceAll('\n', '\\n')

but I cannot seem to get this working as every time I use the backslash character in quotes Javascript thinks I'm escaping the previous character.

I also tried

.split('\\') 

but having the same issue as above.

Any help would be much appreciated, thank you!

EDIT: I had read Override body style for content in an iframe which this question was marked as a duplicate for but it does not address my issue in any way. I have now resolved my problem and if the question could be reopened I can hopefully post the solution which may in turn help others facing the same issue. Thank you!

seshkebab
  • 37
  • 6
  • "Iframe a className of iFrame" — Styles applied to an iframe do not influence the document loaded into the iframe. To style that document you have to do so explicitly. – Quentin Aug 31 '21 at 14:01
  • _"I have tried giving the Iframe a className of iFrame and then in CSS doing the following"_ - of course that does not work; you do not want to format the `iframe` element itself, you want to format parts of the document that gets loaded into the iframe - so _that_ is where you have to apply any formatting. – CBroe Aug 31 '21 at 14:01
  • The output in the response from the API comes back with new line characters in it and when I log this output in the console it converts any new line characters to a carriage return. It is **only** in the iframe where the new line character is converted to single space so I'm unsure how I can possibly format the output before loading it into the iframe if it's already in the format I want it to be in before being rendered in the iframe. Hence why I feel like it's problem to do with how the new line character is being handled in the iframe – seshkebab Aug 31 '21 at 14:18
  • @seshkebab Well, what was causing the problem? – Martin Sep 15 '21 at 13:08

0 Answers0