Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for newlines inside <pre> tags on IE8 and below #69

Closed
wants to merge 1 commit into from
Closed

Fix for newlines inside <pre> tags on IE8 and below #69

wants to merge 1 commit into from

Conversation

loopj
Copy link
Contributor

@loopj loopj commented Jul 2, 2012

On IE8 and below, using innerHTML to set code inside a pre tag causes whitespace preservation to get hosed (see http://www.quirksmode.org/bugreports/archives/2004/11/innerhtml_and_t.html).

This pull request adds a fix which makes sure newlines still work on older IEs.

block.innerHTML = code;
// fill in the code, the outerHTML/innerHTML logic is a fix for old IEs that break newlines
// see http://www.quirksmode.org/bugreports/archives/2004/11/innerhtml_and_t.html
if ((block.tagName == "CODE" || block.tagName == "PRE") && "outerHTML" in block) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does block.tagName == "CODE" need to be included here? According to that article

This causes incorrect display of whitespace in elements that ought to preserve formatting

Since code is a span element that does not preserve formatting is this necessary?

@loopj
Copy link
Contributor Author

loopj commented Jul 4, 2012

All good points. Maybe we should loop over all the attributes in the element and re-apply them to the new element?

@ccampbell
Copy link
Owner

Yeah, I just spent some time trying to get this working and the attributes are not the only problem. The thing is that the entire pre block needs to be recreated and replaced I think.

Using outerHTML causes the Rainbow.onHighlight call to break in good browsers. It seems that the block loses reference to itself in the DOM after calling outerHTML. I noticed this in Chrome.

It's also tricky cause

<pre data-language="javascript">code here</pre>

and

<pre>
    <code data-language="javascript">code here</code>
</pre>

and

<pre data-language="javascript">
    <code>some code here</code>
    <code>more code here</code>
</pre>

Are all supported formats.

I took some look at the competition and saw a really intense hack
https://github.com/isagalaev/highlight.js/blob/master/src/highlight.js#L535-544

I tried that, and it is closer, but for some reason I am still finding that some new lines are mysteriously absent on IE.

@ccampbell
Copy link
Owner

Closing this out for now since this fix doesn't quite work. Opened #80 to track the issue.

@ccampbell ccampbell closed this Jul 21, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants