在< pre>内忽略换行符 [英] linebreak ignored inside <pre>

查看:69
本文介绍了在< pre>内忽略换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个HTML奇怪。也就是说,换行符将被忽略

< preif相邻行是标签。


请参阅此处的源代码和说明:

http://xahlee.org/js/linebreak_after_tag.html


如果有人知道XML案例的规范,请告诉我。谢谢。


Xah
xa*@xahlee.org

A ?? http://xahlee.org/

解决方案

Xah Lee< xa*@xahlee.orgwrote:


有一个HTML奇怪的。也就是说,换行符将被忽略

< preif相邻的行是标签。



问题是在

PRE块内以特殊方式处理换行符。在PRE块之外的行中断为正常空间

字符,因此它们应该在结束标记之前/之后被忽略或者

开始标记,因为通常会在其中添加换行符源代码

代码只是为了使代码更具可读性。此外,任何序列的

空格字符,制表符和换行符(所谓的空格)

将被视为单个空格字符。


在PRE块中,换行符和所有其他空格

字符的处理方式不同。它们不会复制成单个

空格字符,并且必须在它们出现的位置显示换行符并且

不会转换为空格字符。所以我认为Firefox和Safari

不能正确显示你的测试页。


问题似乎是display:table对于PRE标签,

令Firefox和Safari混乱。这可能是这些

浏览器中的一个错误。


我假设你想使用display:table来使盒子小一点
需要
。如果没有修改后的显示属性,框将完全跨越

可用宽度。你可以把这个盒子变成一个漂浮物而且

clear之后它会得到同样的效果。然后所有的浏览器

会显示正确的。


-

Alexander


Xah Lee写道:


有一个HTML奇怪的。也就是说,换行符将被忽略

< preif相邻行是标签。


请参阅此处的源代码和说明:

http://xahlee.org/js/linebreak_after_tag.html


如果有人知道XML案例的规范,请告诉我。谢谢。


Xah
xa*@xahlee.org

A ?? http://xahlee.org/



为什么这个构造?我很惊讶地看到大多数浏览器似乎都在处理它。我将这样的造型应用到显示器上似乎有点奇怪。

pre。我的经验实际上是唯一适用于预售

的样式是字体。


(BTW IE无法识别显示的表值)


为了让前置元素空间受限,你可以将样式关闭

pre并将其声明为div(或其他元素)


< div class ="歌词">

< pre>

line1

line2

< / pre>

< / div>


Louise


Xah Lee< xa*@xahlee.orgscripsit:


有一个HTML奇怪的。也就是说,换行符将被忽略

< preif相邻的行是标签。



在您的页面上解释问题,您必须在开始标记后立即引用文本换行符

,在结束标记之前,必须有一个换行符

来自HTML规范。然而,引用的

要求一般都被网络浏览器违反了,所以不要把它计算在内 - 但是如果有些浏览器也不会感到惊讶实际上

在某些情况下符合它。


最初的问题似乎是如何呈现诗歌。使用< preisn''t

真的是最好的工具。当你的样式表被忽略时,你是否希望你的诗以monospace

字体出现?


更好的方法是使用


< div class =" stanza">

< div>第一行< / div>

< div>第二行< / div>

...

< / div>


以及一块试图阻止的CSS换行符

(除了< divelements之间)并且可以添加颜色或任何你想要的颜色。

使用这种方法,每一行都是一个可设置样式的元素,你可以添加class

属性用于更精细的调整。此外,您可以添加,例如,

div.stanza div {margin-left:1em; text-indent:-1em; }

这样如果一条线被分割,延续线会出现一点点

缩进,这样读者仍然可以看到结构。

后续修剪。


-

Jukka K. Korpela(Yucca)
http://www.cs.tut.fi/~jkorpela/


there''s a HTML odditity. That is, line break will be ignored inside
<preif the adjacent lines are tags.

see the source code and description here:

http://xahlee.org/js/linebreak_after_tag.html

if anyone know the spec for XML case, please let me know. Thanks.

Xah
xa*@xahlee.org
a?? http://xahlee.org/

解决方案

Xah Lee <xa*@xahlee.orgwrote:

there''s a HTML odditity. That is, line break will be ignored inside
<preif the adjacent lines are tags.

The problem is that line breaks are treated in a special way inside a
PRE block. Outside a PRE block line breaks count as normal space
characters and therefore they should be ignored before/after end tags or
start tags because usually one would add line breaks into the source
code just there to make the code more readable. Also any sequence of
space characters, tabs and line breaks (the so called "white spaces")
will be treated as a single space character.

Inside a PRE block, line breaks characters and all other "white space"
characters are treated differently. They do not copllapse into a single
space character, and line breaks must be displayed where they occur and
won''t convert into a space character. So I think that Firefox and Safari
don''t display your test page correctly.

The problem seems to be the "display:table" for the PRE tag, which
is confusing Firefox and Safari. This is probably a bug in these
browsers.

I assume that you want to use display:table to make the box a small as
needed. Without the modified display property the box would span the
available width completly. You could make the box a float instead and
"clear" it afterwards to get the same effect. And then all browsers
would display it right.

--
Alexander


Xah Lee wrote:

there''s a HTML odditity. That is, line break will be ignored inside
<preif the adjacent lines are tags.

see the source code and description here:

http://xahlee.org/js/linebreak_after_tag.html

if anyone know the spec for XML case, please let me know. Thanks.

Xah
xa*@xahlee.org
a?? http://xahlee.org/

Why this construct? I was surprised to see that most browsers seem to
handle it. It seems somewhat odd to me apply such styling as display to
pre. My experience that practically the only styling appropriate to pre
is to the font.

(BTW IE does not recognise the table value for display)

To have the pre element space restrained you might take the styling off
pre and declare it in a div (or other element)

<div class="lyrics">
<pre>
line1
line2
</pre>
</div>

Louise


Xah Lee <xa*@xahlee.orgscripsit:

there''s a HTML odditity. That is, line break will be ignored inside
<preif the adjacent lines are tags.

On your page that explains the problem, you quote the text "a line break
immediately following a start tag must be ignored, as must a line break
immediately before an end tag" from the HTML specification. The cited
requirement has, however, generally been violated by web browsers, so don''t
count on it - but don''t get surprised either if some browser actually
complies with it in some situation.

The original problem appears to be how to present poetry. Using <preisn''t
really the best tool for it. Do you want your poem to appear in a monospace
font when your style sheet is ignored?

A better approach is to use

<div class="stanza">
<div>first line</div>
<div>second line</div>
...
</div>

together with a suitable piece of CSS that tries to prevent line breaks
(except between the <divelements) and may add colors or whatever you want.
Using this approach, each line is a stylable element, and you may add class
attributes for finer tuning. Moreover, you can add, say,
div.stanza div { margin-left: 1em; text-indent: -1em; }
so that if a line is divided, the continuation line appears with a little
indentation, so that the reader can still see the structure.
Followups trimmed.

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/


这篇关于在&lt; pre&gt;内忽略换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆