【问题标题】:CSS3 Making :before content not highlightableCSS3 制作:在内容不可高亮之前
【发布时间】:2017-08-13 11:11:38
【问题描述】:

== 在 60.0.3112.90 版本中由 Chrome 修复 ==

-Chrome 错误-

错误报告:https://bugs.chromium.org/p/chromium/issues/detail?id=703807&can=2&start=0&num=100&q=&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Component%20Status%20Owner%20Summary%20OS%20Modified&groupby=&sort=

好的,所以我一直在从事一个具有 CSS 行号的项目。当用户突出显示/选择文本时,数字也会突出显示/选择。幸运的是 :before 内容没有被复制,但它仍然让我烦恼。有没有办法解决这个问题。

注意:Pre 是 contenteditable='true'

我尝试过的:

user-select: none;

也一样

pre code:before::selection {

    background: transparent;

}

这是一些示例代码:

<pre contenteditable='true'><code>bla</code><code>bla</code><code>bla</code></pre>

<style>
    pre {
        margin: 0px;
        outline: none;
    }

    pre code {
        display: block;
        color: #a9b7c6;
        counter-increment: line;
    }

    pre code:before {
        content: '\00a0'counter(line)'\00a0';
        margin-right: 2px;
        border-right: solid #4b4b4b 1px;
    }
</style>

现在的样子:

它应该是什么样子:

【问题讨论】:

  • 我认为之前实际上使元素无法选择。你的情况如何相反
  • 我完全不知道,文本复制功能很好,但看起来很烦人。
  • Chrome 错误报告链接:bugs.chromium.org/p/chromium/issues/…

标签: html css web


【解决方案1】:

您应该在问题中包含MCVE。现在还不清楚出了什么问题。我无法重现您的问题,请参阅下面的 sn-p。

请注意,将pre 设置为contentEditable 与将每一行设置为单独的code 元素并不能很好地结合。我假设您正在通过 JavaScript 处理这个?还是我误解了你的工作?

无论如何,下面的(甚至不使用user-select: none)似乎对我有用(在Firefox 和Chrome中,屏幕截图是在Firefox中拍摄的)。

pre, code {
  margin: 0;
  padding: 0;
}

pre {
  font-size: 0;
}

pre code {
  counter-increment: line;
  font-size: 1rem;
}

pre code:before {
  content: counter(line)".";
  margin-right: .5rem;
  padding-right: .2rem;
  border-right: 1px solid #ccc;
}
<pre contenteditable="true">
  <code>a line 1</code>
  <code>a line 2</code>
  <code>a line 3</code>
  <code>a line 4</code>
  <code>a line 5</code>
  <code>a line 6</code>
</pre>

编辑。我实际上可以在 Chrome 中重现该问题。看起来只有计数器被选择突出显示,其他任何内容都没有。也就是说,在下面的 sn-p 中,点没有突出显示。如果我将content: counter(line)"." 替换为content: "1.",则选择会按预期忽略伪元素。始终选择计数器。它也不受::selection 样式的影响。对我来说,这闻起来像是 Chrome 中的一个错误——尽管 CSS 规范中的文本选择当然没有任何保证(user-select 和 ::selection 都只在 CSS4-UI 的草稿中)......但是伪文本元素真的不应该是可选择的。

你可以open a bug report。看看他们怎么说。

我唯一可以为您提供的是一种解决方法。希望它仍然有帮助。它将行号放在一个完全独立的元素中。现在需要使用 JavaScript 维护行号。既然你说你已经有脚本来处理编辑,我想为什么不写这个呢?

* {
  margin: 0;
  padding: 0;
  background: #fff;
  color: #000;
}

.editor {
  position: relative;
}

html {
  font-size: 18px; /* define font size for rem */
}

pre {
  padding-left: 2rem;
  font-size: 0;
}

pre code {
  font-size: 1rem;
  line-height: 1.3rem;
}

lines {
  position: absolute;
  top: 0;
  left: 0;
}

line {
  display: block;
  padding-right: .2rem;
  border-right: 1px solid #ccc;
  font-family: monospace;
  font-size: 1rem;
  line-height: 1.3rem;
  
     -moz-user-select: none;
      -ms-user-select: none;
  -webkit-user-select: none;
          user-select: none;
}
<div class="editor">
  <pre contenteditable="true">
    <code>a line 1</code>
    <code>a line 2</code>
    <code>a line 3</code>
    <code>a line 4</code>
    <code>a line 5</code>
    <code>a line 6</code>
  </pre>
  <lines>
    <line>1.</line>
    <line>2.</line>
    <line>3.</line>
    <line>4.</line>
    <line>5.</line>
    <line>6.</line>
  </lines>
</div>

【讨论】:

  • 我在 javascript 中有很多 pre 和 code 标签的处理程序,并且必须在内容可编辑的主标签中。这是最新版 chrome 的屏幕截图。 imgur.com/a/7mTZE这可能是在浏览器中删除用户选择吗?
  • @DrBrad 你的截图不是来自我的回答,是吗?我很肯定 user-select 没有从 CSS 规范中删除。
  • 很遗憾,这来自您的回答。
  • @DrBrad 对,我被黑暗的主题弄糊涂了。我现在正在使用移动设备,稍后会仔细查看。
  • 那是我前一阵子做的扩展哈哈。谢谢
【解决方案2】:

它可能不起作用,因为您忘记了前缀:

pre code::before {
  content: counter(line);
  -webkit-user-select: none;
  -moz-user-select: none;
  user-select: none;
}

如果这不起作用:

pre code::before {
  content: counter(line);
  pointer-events: none;
}

【讨论】:

  • 遗憾的是,从 :before 开始的没有变化但突出显示/选择不再可能。不过谢谢你的回答。
  • @DrBrad 我已经编辑了答案以包含另一个解决方案
猜你喜欢
  • 2021-04-17
  • 2014-02-03
  • 2011-03-29
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多