The link kindly provided by @animuson in the comments explains why it happens with eight blanks instead of four, but my question is, why does something which is not markup, but looks a bit like markup, get treated as markup.

I had assumed that questions regularly included code minus whitespace (it saves the code so much time to write, if not someone to read later) and thought it may be unique to the editing box. It is not.

So, the question remains, why does a<b, with no closing < even, get treated as markup. Is it possible for the edit box to say "this isn't really markup, so I'll display it"?

In addition, the second set of code in a "list" in the same question does not behave in the same way. This only require four leading spaces, not eight. Is it because it is 1.text rather than `1. text'. Yes, looking now, it seems so.

The list formatting requires a space after the full-stop/period. Is this correct? The OP edited both "item 2's", but only one broke.

If "1. Text" is to be automatically treated as a formal list, without original author or editor probably being aware, should "1.Text" be treated in the same way?

This suggested edit, https://stackoverflow.com/review/suggested-edits/3881672, looked like the suggester had killed some code (look at the first point 2).

Having previously learned that looks can be deceiving, and to use both the views on the Review screen, it seems that all the scrunched-up code got the <kidsArray.length;k++){ treated as the start of an html-style tag. So the rest of that code block disappeared.

Tricky, and the editor should have looked at the preview part of the edit screen and noticed the problem. Correctly rejected, and I edited/rolled back to confirm that the edit pickles the question.

The original seems to work because it has eight leading blanks. Change that to four, and it goes to pot. In fact, change it to seven. It is fun toggling between seven and eight leading blanks and watching the code disappear and reappear. OK, fun for a while.

There's even more fun. In attempting to prepare some screenshots of the here's-the-code-where's-the-code? scenario, it seems that the automatic list numbering may be causing a side-effect, or it may be the tabs in the original or something else, or some combination. Basically lots of different things happen when I mess with the post in the Edit box.

I can get the code to disappear (from the < onwards). I can get it to reappear again, or reappear as plain lines of text.

It is such a mess, I don't have time to do justice to the screenshots.

If others have a play (I was deleting most of the text above and below to make a smaller screenshot, replacing tabs by space, removing spaces (or tabs, not sure) on the second line of the code block) perhaps I can get some confirmation that I'm not going nuts. There's at least four different behaviours, and life isn't long enough for one person to recreate them all when they should be cooking dinner :-)

  • Just noticed the irony of the words following the code in question: I thought I was missing something – Bill Woodger Jan 23 '14 at 18:35
  • 1
    So what exactly is your question here? If you're concerned about the spaces, it does require 8 spaces in order to be formatted as code when it's directly under a list (or inside one). Using only 4 spaces will indent it as part of the list item. See: Code block is not properly formatted when placed immediately after a list item – animuson Jan 23 '14 at 18:59

Ok, let's pick the part where I think your actual confusion is at:

So, the question remains, why does a<b, with no closing > even, get treated as markup. Is it possible for the edit box to say "this isn't really markup, so I'll display it"?

To explain it, let's just take the first line of that code: for (var k=0;k<kids.length;k++)

Now, just that line does not contain any HTML - true. However, it gets inserted into HTML, which could cause some problems. Once run through the Markdown parser and rendered, the full HTML structure ends up looking something like this (again, simplified to make it easier):

<p>for (var k=0;k<kids.length;k++)</p>

Now, HTML is not that sophisticated. In the simplest of terms, it's a form of XML, which means anything within angled brackets could potentially be an element when you don't escape it properly. The first <p> is obvious. However, the < sign in the code, to any parser in the world, begins a new HTML element. So in reality, the entire <kids.length;k++)</p> would get interpreted as a single start tag of a new element. That's certainly not ideal. So, an easy way to fix that is to just remove it. Invalid HTML elements, as well as tags that start but do not ever end, need to be removed so as not to interfere with the other display elements of the post.

In other environments where HTML is not allowed at all in the non-rendered version of the post, you could simply run the entire post through an HTML escaping function to replace the < with &lt; and be happy. Unfortunately in an environment which does allow some actual HTML to be used, that's not possible, because then you'd be escaping real HTML elements which are meant to be rendered. It's far easier to just remove random things that shouldn't be used or just utterly don't make sense than to try and look at each HTML tag in a post and determine if it was used properly, if it has an appropriate end tag, etc, etc - HTML parsing is not any fun.

| improve this answer | |
  • OK, thanks. Get it now. And the list thing? "1. Text" being treated as a list and "1.Text" not? I doubt it happens often enough to be a worry, but confused me on this example... – Bill Woodger Jan 23 '14 at 23:09
  • The Markdown parser does require a space to be there. Imagine if a decimal number like 1.4853 got randomly converted to a list. – animuson Jan 23 '14 at 23:10
  • Mmmm... fourth time this week I've ended up at that post, including from the question saying that it gets linked to too often... – Bill Woodger Jan 23 '14 at 23:10
  • Yep. Satisfied bunny. Thanks. – Bill Woodger Jan 23 '14 at 23:11

Not the answer you're looking for? Browse other questions tagged .