|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="zh-hans"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>双向文本空格丢失</title> |
| 6 | +<meta name="description" content="为什么我的浏览器会折叠拉丁字母和阿拉伯字母/希伯来字母之间的空格,如何修复这个问题?"> |
| 7 | +<script> |
| 8 | +var f = { } |
| 9 | + |
| 10 | +// AUTHORS should fill in these assignments: |
| 11 | +f.directory = 'questions'+'/' // the path to this file, not including /International or the file name |
| 12 | +f.filename = 'qa-bidi-space' // the file name WITHOUT extensions |
| 13 | +f.authors = 'Richard Ishida, W3C' // author(s) and affiliations |
| 14 | +f.previousauthors = '' // as above |
| 15 | +f.modifiers = '' // people making substantive changes, and their affiliation |
| 16 | +f.searchString = 'qa-bidi-space' // blog search string - usually the filename without extensions |
| 17 | +f.firstPubDate = '2003-07-04' // date of the first publication of the document (after review) |
| 18 | +f.lastSubstUpdate = { date:'2016-02-07', time:'22:47'} // date and time of latest substantive changes to this document |
| 19 | +f.status = 'published' // should be one of draft, review, published, notreviewed or obsolete |
| 20 | +f.path = '../' // what you need to prepend to a URL to get to the /International directory |
| 21 | + |
| 22 | +// AUTHORS AND TRANSLATORS should fill in these assignments: |
| 23 | +f.thisVersion = { date:'2025-07-31', time:'18:23'} // date and time of latest edits to this document/translation |
| 24 | +f.contributors = '' // people providing useful contributions or feedback during review or at other times |
| 25 | +// also make sure that the lang attribute on the html tag is correct! |
| 26 | +f.sources = '' // describes sources of information |
| 27 | + |
| 28 | +// TRANSLATORS should fill in these assignments: |
| 29 | +f.translators = '薛富侨' // translator(s) and their affiliation - a elements allowed, but use double quotes for attributes |
| 30 | + |
| 31 | +f.breadcrumb = 'characters' |
| 32 | + |
| 33 | +f.additionalLinks = '' |
| 34 | +</script> |
| 35 | +<script src="qa-bidi-space-data/translations.js"> </script> |
| 36 | +<script src="../javascript/doc-structure/article-dt.js"> </script> |
| 37 | +<script src="../javascript/boilerplate-text/boilerplate-zh-hans.js"> </script> |
| 38 | +<!--TRANSLATORS must change -en in the line just above to the subtag for their language! --> |
| 39 | +<script src="../javascript/doc-structure/article-2022.js"> </script> |
| 40 | +<script src="../javascript/articletoc-2022.js"></script> |
| 41 | +<link rel="stylesheet" href="../style/article-2022.css"> |
| 42 | +<link rel="copyright" href="#copyright"> |
| 43 | +<script src="../javascript/prism.js"></script> |
| 44 | +<link rel="stylesheet" href="../style/prism.css"> |
| 45 | +<style> |
| 46 | +.el { color: #D61B1B; font-size: 100%; font-weight: 300; margin: 0 2px; } |
| 47 | +</style> |
| 48 | +<script> |
| 49 | +function highlightCode () { |
| 50 | + var examples = document.querySelectorAll('figure.example, div.example') |
| 51 | + for (var i=0;i<examples.length;i++) { |
| 52 | + var re = /<([^&]+)>/ |
| 53 | + var content = examples[i].innerHTML |
| 54 | + out = content.replace(/<([^&]+)>/g,'<span class="el"><$1></span>') |
| 55 | + examples[i].innerHTML = out |
| 56 | + } |
| 57 | + } |
| 58 | +</script> |
| 59 | +</head> |
| 60 | + |
| 61 | +<body> |
| 62 | +<header> |
| 63 | + <nav id="mainNavigation"></nav><script>document.getElementById('mainNavigation').innerHTML = mainNavigation</script> |
| 64 | + |
| 65 | + <h1>双向文本空格丢失</h1> |
| 66 | +</header> |
| 67 | + |
| 68 | + |
| 69 | + <div id="audience"> |
| 70 | + <div id="updateInfo"></div><script>document.getElementById('updateInfo').innerHTML = g.updated</script> |
| 71 | + </div> |
| 72 | + |
| 73 | +<section id="question"> |
| 74 | +<h2>问题</h2> |
| 75 | + <p class="question">为什么我的浏览器会折叠拉丁字母和阿拉伯字母/希伯来字母之间的空格,如何修复这个问题?</p> |
| 76 | +</section> |
| 77 | + |
| 78 | + |
| 79 | +<section id="answer"> |
| 80 | +<h2>答案</h2> |
| 81 | + |
| 82 | + <section id="cause"> |
| 83 | +<h3>可能的原因</h3> |
| 84 | + <div class="insideinfonote"> |
| 85 | + <p class="info">代码示例中从右向左的文本在这里用大写字母表示,从左向右的文本用小写字母表示。</p> |
| 86 | + </div> |
| 87 | + <p>如果文本后面跟着一个行内元素,带有<code class="kw" translate="no">dir</code>属性,并且含有空白字符,拉丁字母和阿拉伯字母/希伯来字母之间的空格可能会被折叠。</p> |
| 88 | + <p>在以下的代码模式中,彩色矩形表示有问题的空格。(大写字母表示RTL文本,小写内容表示LTR。)</p> |
| 89 | + <figure class="example"> |
| 90 | + <p><code><p dir="rtl">RTL_TEXT <span dir="ltr">ltr_text<span style="background-color: orange;"> </span></span>RTL_TEXT</p> </code></p> |
| 91 | +</figure> |
| 92 | + <p>如果我们用阿拉伯字母和英文替换内容,上述代码会产生以下结果。</p> |
| 93 | + <figure class="example" dir="rtl"> |
| 94 | + <img src="qa-bidi-space-data/missing-space-1.png" alt="结果图片,显示拉丁字母本左侧没有空格。"> |
| 95 | + <div class="demolink" dir="ltr"><a href="qa-bidi-space-data/eg-space-problem" target="text">查看实时演示</a></div> |
| 96 | + </figure> |
| 97 | + <p>请注意,当从右到左的文本嵌入到从左到右的段落中时,也会出现这种效果。</p> |
| 98 | + </section> |
| 99 | + |
| 100 | + <section id="whattodo"> |
| 101 | +<h3>如何修复</h3> |
| 102 | + <p>如果前面的部分描述了你代码的样子,解决方案是删除行内元素结束标签前的所有空格,或者删除<code class="kw" translate="no">dir</code>属性(如果合适的话)。</p> |
| 103 | + <p>新的代码模式:</p> |
| 104 | + <figure class="example"> |
| 105 | + <p><code><p dir="rtl">RTL_TEXT <span dir="ltr">ltr_text</span><span style="background-color: orange;"> </span>RTL_TEXT</p> </code></p> |
| 106 | + </figure> |
| 107 | + <p>例如,在上面的例子里,把<span class="qterm">W3C</span>后面的空格移到<code class="kw" translate="no">span</code>的外面,会产生预期的结果。</p> |
| 108 | + <figure class="example" dir="rtl"> |
| 109 | + <img src="qa-bidi-space-data/missing-space-2.png" alt="结果图片,显示拉丁字母本两侧都有空格。"> |
| 110 | + <div class="demolink" dir="ltr"><a href="qa-bidi-space-data/eg-space-ok" target="text">查看实时演示</a></div> |
| 111 | + <!--<p dir="rtl"> العالمية (W3C) تخلق قواعد</p>--> |
| 112 | + </figure> |
| 113 | + <p>在这种情况下,我们并不需要<span class="qterm">W3C</span>周围的<code class="kw" translate="no">span</code>元素来产生正确的显示顺序。省略属性或整个<code class="kw" translate="no">span</code>元素也可以解决问题(尽管我们通常建议标记所有相反方向的文本)。</p> |
| 114 | + </section> |
| 115 | +</section> |
| 116 | + |
| 117 | + |
| 118 | +<section id="additionalinfo"> |
| 119 | +<h2>其他信息</h2> |
| 120 | + |
| 121 | +<section id="why"> |
| 122 | +<h3>为什么会发生这种情况?</h3> |
| 123 | + <p>如果你想了解为什么会发生这种情况的技术细节时,可以阅读本节。</p> |
| 124 | + <p>文本显示时的预期行为在HTML规范中没有详细描述,但在CSS规范中<strong>有</strong>描述。虽然本页面的示例没有用CSS,但同样的原则适用。以下内容摘自<a href="https://www.w3.org/TR/css-text-3/#white-space-phase-1">CSS文本模块第3版候选推荐标准</a>:</p> |
| 125 | + <blockquote> |
| 126 | + <p>任何紧跟在另一个可折叠空格之后的可折叠空格(即使该空格位于包含该空格的行内边界之外,只要这两个空格位于同一个行内格式化上下文中)都会被折叠,使其前进宽度为零。(它是不可见的,但保留了其软换行的机会,如果有的话。)</p> |
| 127 | + </blockquote> |
| 128 | + <p>给定如下场景,其中颜色表示空格(U+0020):</p> |
| 129 | + <blockquote> |
| 130 | + <p><ltr>a<span style="background-color: red;"> </span><rtl><span style="background-color: orange;"> </span>B<span style="background-color: blue;"> </span></rtl><span style="background-color:green;"> </span>c</ltr></p> |
| 131 | + </blockquote> |
| 132 | + <p>规范说A后面的空格被保留,B前面的空格被删除,B后面的空格被保留,C前面的空格被删除,这给我们留下:</p> |
| 133 | + <blockquote> |
| 134 | + <p><ltr>a<span style="background-color: red;"> </span><rtl>B<span style="background-color: blue;"> </span></rtl>c</ltr></p> |
| 135 | + </blockquote> |
| 136 | + <p>然后根据Unicode双向算法进行渲染,最终结果是:</p> |
| 137 | + <blockquote> |
| 138 | + <p>a<span style="background-color: red;"> </span><span style="background-color: blue;"> </span>Bc</p> |
| 139 | + </blockquote> |
| 140 | + <p>请注意,A和B之间实际上有两个空格。嵌入级别可以表示如下:</p> |
| 141 | + <blockquote> |
| 142 | + <p>0<span style="background-color: red; color:white;">0</span><span style="background-color: blue; color: white;">1</span>10</p> |
| 143 | + </blockquote> |
| 144 | +</section> |
| 145 | + |
| 146 | + <section id="test"> |
| 147 | +<h3>在我的浏览器中会发生什么?</h3> |
| 148 | + <p>下面的框显示了代码示例,和代码在此页面上的实现。你可以测试你的浏览器的行为。所有例子的上下文都是从右到左的。垂直的橙色条表示空格字符的位置。</p> |
| 149 | + <figure> |
| 150 | + <div class="example" style="margin-left:0; margin-right: 0;"> |
| 151 | + <p>ARABIC<span style="background-color:orange;"> </span><span dir="ltr">latin<span style="background-color:orange;"> </span></span>ARABIC</p> |
| 152 | + <p dir="rtl">صفحة الترجمة لموقع <span dir="ltr">W3C </span>على الرابط</p> |
| 153 | + </div> |
| 154 | + </figure> |
| 155 | + <figure> |
| 156 | + <div class="example" style="margin-left:0; margin-right: 0;"> |
| 157 | + <p>ARABIC<span style="background-color:orange;"> </span><span dir="ltr">latin<span style="background-color:orange;"> </span></span><span style="background-color:orange;"> </span>ARABIC</p> |
| 158 | + <p dir="rtl">صفحة الترجمة لموقع <span dir="ltr">W3C </span> على الرابط</p> |
| 159 | + </div> |
| 160 | + </figure> |
| 161 | + <figure> |
| 162 | + <div class="example" style="margin-left:0; margin-right: 0;"> |
| 163 | + <p>ARABIC<span style="background-color:orange;"> </span><span dir="ltr">latin</span><span style="background-color:orange;"> </span>ARABIC</p> |
| 164 | + <p dir="rtl">صفحة الترجمة لموقع <span dir="ltr">W3C</span> على الرابط</p> |
| 165 | + </div> |
| 166 | +</figure> |
| 167 | + <figure> |
| 168 | + <div class="example" style="margin-left:0; margin-right: 0;"> |
| 169 | + <p>ARABIC<span style="background-color:orange;"> </span><span>latin<span style="background-color:orange;"> </span></span>ARABIC</p> |
| 170 | + <p dir="rtl">صفحة الترجمة لموقع <span>W3C </span>على الرابط</p> |
| 171 | + </div> |
| 172 | + </figure> |
| 173 | + <figure> |
| 174 | + <div class="example" style="margin-left:0; margin-right: 0;"> |
| 175 | + <p>ARABIC<span dir="ltr"><span style="background-color:orange;"> </span>latin</span><span style="background-color:orange;"> </span>ARABIC</p> |
| 176 | + <p dir="rtl">صفحة الترجمة لموقع<span> W3C</span> على الرابط</p> |
| 177 | + </div> |
| 178 | + </figure> |
| 179 | + </section> |
| 180 | +</section> |
| 181 | + |
| 182 | + |
| 183 | +<section id="endlinks"> |
| 184 | +<h2>延伸阅读</h2> |
| 185 | +<aside class="section" id="survey"> </aside><script>document.getElementById('survey').innerHTML = g.survey</script> |
| 186 | +<ul id="full-links"> |
| 187 | + <li> |
| 188 | + <p>相关链接:<cite>编写网页</cite></p> |
| 189 | + <ul> |
| 190 | + <li><a href="https://www.w3.org/International/techniques/authoring-html?collapse&open=direction">文本方向</a></li> |
| 191 | + <li><a href="https://www.w3.org/International/techniques/authoring-html#inline">行内混合文本方向</a></li> |
| 192 | + </ul> |
| 193 | + </li> |
| 194 | +</ul> |
| 195 | +</section> |
| 196 | + |
| 197 | +<footer id="thefooter"></footer><script>document.getElementById('thefooter').innerHTML = g.bottomOfPage</script> |
| 198 | +<script>completePage(); highlightCode();</script> |
| 199 | +</body> |
| 200 | +</html> |
0 commit comments