Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

propose example attribute for documentation and other uses #1042

Closed
Nick-Levinson opened this issue Oct 9, 2017 · 14 comments
Closed

propose example attribute for documentation and other uses #1042

Nick-Levinson opened this issue Oct 9, 2017 · 14 comments

Comments

@Nick-Levinson
Copy link

For communicating about HTML, teaching it, or other purposes, with an example that should not cause an actual effect in a user agent, even a user agent programmed for private use, an attribute should be specified for the purpose and HTML should specify it. I propose "example" for an attribute for any element.

Case and the use of quotation marks would be as HTML specifies for other attributes. The content value could be any value, including null and absent (no equals sign) and would be formattable as for any other attribute.

The content or its absence would not be processed by a user agent.

HTML 5.1 2d ed. already has room for this ("[f]or generic extensions that are to be used by multiple independent tools, either this specification should be extended to provide the feature explicitly, or . . ." (section 3.2.5.9)).

An analogy is with domain names, such as example.com and *.test.

@chaals
Copy link
Collaborator

chaals commented Oct 10, 2017

There is the obsolete and non-conforming <xmp> element that does this.

It happens to work in a lot of browsers I tested.

But a better approach MHO is to use <code> (and <pre> if you want to preserve whitespace), with escaped characters.

@iandevlin
Copy link
Contributor

I agree with @chaals here, <code> currently serves this purpose well enough.

@Nick-Levinson
Copy link
Author

No; the misunderstanding was not intended. The code and pre elements do affect browser behavior; that is precisely why they won't work for this purpose. The xmp was inconsistently implemented, which means it was at least in some browsers causing effects; that's not wanted for this proposed element (from issue 1041 combined with this issue) and this proposed attribute. This is for use in documentation where someone, like a student, might copy it into a Web page and it should still have no effect.

By reserving it, future use in a spec and private use, if conflicting, would be barred.

Visit the website at example.com and you'll see no ads, tracking of user behavior, or other things that might confuse someone for whom that should only be an example website domain. It's why you can't use domain names like mydomain.com as if they were example.com. Example.com and a few others are reserved in an RFC so that you can't buy them. That's the analogy to this proposal.

@chaals
Copy link
Collaborator

chaals commented Oct 11, 2017

The problem is that you have an opt-in situation - unless a user agent understand example, it will interpret that as an unknown, and the content of it - most paticularly things like script elements - will be parsed as real content. Reserving the name won't have an effect until generations of browsers have passed...

That process has been happening for <xmp>, for years already.

I think it makes far more sense to consider whether <xmp> is sufficiently well deployed in reality that it can be safely resurrected than to start today trying to make something that does more or less the same thing - specifically because I think the proposal doesn't solve any of the significant problems differently.

The reason I prefer <code> (with or without <pre>) is that you can put semantic markup inside it, e.g. linking or empahsising bits of a code sample. You cannot do that with something like <xmp>.

I have written (in MS Word macro!) a converter to copy HTML source and paste it as escaped <code>. It's not very hard, and using DOM today you could do an even better job relatively easily - certainly easily enough for use in authoring tools.

@Nick-Levinson
Copy link
Author

Nick-Levinson commented Oct 11, 2017 via email

@chaals
Copy link
Collaborator

chaals commented Oct 11, 2017

@Nick-Levinson I think I am failing to explain myself.

The problem I see is this:

You want to put some HTML code in the source, and see that rendered as-is in the page. E.g.

<xmp>
 <script>DoEvilThings()</script>
 <iframe srcdoc="... ..."><!-- use this for something nasty --> </iframe>
</xmp>

Should (and actually does today) show the user

 <script>DoEvilThings()</script>
 <iframe srcdoc="... ..."><!-- use this for something nasty --> </iframe>

on the webpage, instead of running the script or putting in the iframe. xmp is a reserved keyword that HTML defines, although noting it is obsolete and you should use ampersand-escaped code in a code element. This element - unlike more or less any other - stops the content from being parsed, because it i

If we created a new element, then any browser built before it was implemented would do nothing with the element, but would parse its content. So changing xmp in the above example to anythingNew would try to execute the script instead of showing the script tag and its content.

I think you are asking for the behaviour of xmp. That can be achieved, but is best done with the xmp element, since just reserving an element name doesn't stop its content being parsed. But I would argue that ampersand-escaped code is actually better - because you can still use HTML semantics to add markup inside your examples, making them more powerful while still ensuring they are not parsed as part of the document they are inside.

@Nick-Levinson
Copy link
Author

Nick-Levinson commented Oct 12, 2017 via email

@unor
Copy link

unor commented Oct 17, 2017

If the goal is to display HTML as plain text, without having to escape < or > (which means it’s not possible to use HTML within), the script element can be used:

<script type="text/plain">
<p>Example <abbr>HTML</abbr> that doesn’t get parsed.</p>
</script>

(It can’t contain </script> or variations, of course.)

As it’s typically visually hidden in the CSS, you need to make it visible:

script[type="text/plain"] {display:block;}

@Nick-Levinson
Copy link
Author

Nick-Levinson commented Oct 17, 2017 via email

@unor
Copy link

unor commented Oct 17, 2017

@Nick-Levinson

I think this use of the script element would have to be defined in the spec so it doesn't fail validation […]

It is, of course, defined in the spec (I also quoted the spec in my answer linked above):

The script element allows authors to include […] data blocks in their documents.

[…] means that the script is a data block, which is not processed.

@scottaohara
Copy link
Member

Reading through this thread, I also agree with @chaals and @iandevlin.

Especially in the context of "For communicating about HTML, teaching it, or other purposes...", I would imagine one might want the ability to highlight/mark particular parts of a code example, which wouldn't be possible if the wrapped content was not processed by user agents.

I've created the this codepen example to illustrate using the mark element to highlight a portion of the example I want to draw attention to. This is not possible in the script type="text/plain" example, or if using xmp (not shown in the codepen)

Additionally, the script type="text/plain" would have one last quirk, where one could not have a nested </script>.

@chaals
Copy link
Collaborator

chaals commented Oct 18, 2017

@Nick-Levinson wrote

Is it not possible for the HTML spec to require that a given element shall not be parsed by a conforming browser (already the case for an unspecified element, such as a misspelling)? If it's not possible, then my proposal is a bad one.

And the answer is, in the terms I believe you mean, "It is possible, but only for new browsers, and it would cause very big problems if people used it in content". An unknown element will be created, and behave much like a <span>. In particular, its children will be parsed.

I had written:

[the <xmp>] element - unlike more or less any other - stops the content from being parsed, because it i

...sorry I failed to complete that. I meant

...because it is already recognised, and specially treated by browsers and has been for some time. Since not implementing that could expose a big security issue, a la

<p>For example, don't do this:</p>

<xmp>
 <script>doSomethingTerrible()</script>
</xmp>

Whereas if the example were &lt;script&gt;somethingEvenWorse()&lt;/script&gt; it doesn't even look like it should be run. (Although I am sure there are examples in reality of someone not coding carefully enough and getting caught out. Code is written by people and people make mistakes in describing what they think they want).

And you could do something like

<p>For example, don't do this:</p>

<pre><code>
 <em class="bad">&lt;script&gt;doSomethingTerrible()&lt;/script&gt;</em>
</code></pre>

So it seems you (@Nick-Levinson) agree, given the proposal does not behave as you had hoped in browsers, that we should drop it. I have marked it wontfix - feel free to close it, or if no further comment is forthcoming I will do so by and by.

@Nick-Levinson
Copy link
Author

Nick-Levinson commented Oct 19, 2017 via email

@chaals
Copy link
Collaborator

chaals commented Oct 19, 2017

They are doing what the spec says - the element turns into an unknown Element - but crucially for this case its children get parsed if they are recognised. Please be careful apportioning blame for things you don't like - this group aims to avoid such things as ad hominem attacks and deal with technical issues.

@chaals chaals closed this as completed Oct 19, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants