RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1069830
Accepted
Дмитрий Полянин
Дмитрий Полянин
Asked:2020-01-14 21:48:33 +0000 UTC2020-01-14 21:48:33 +0000 UTC 2020-01-14 21:48:33 +0000 UTC

如何检查通过 JavaScript 输入表单的 CSS 代码的有效性

  • 772

文本输入到表单CSS中,你需要在浏览器端检查它的有效性,或者扔掉所有无效的部分,只留下有效的部分。我怎样才能做到这一点?

#textarea {
  width: 100%;
  height: 120px;
}
<textarea id="textarea">.selector1 {
   color: red;
}
.selector2 div {
   font-weight: bold;
}
some_strange_code #^</textarea>
<button onclick="test()">Проверить</button>

对我来说更好的解决方案是不使用第三方库,但是使用第三方库的解决方案也是合适的,优先考虑库的轻便性,最好是一个你可以复制给自己的功能。

也许某种浏览器解决方案将输入css的内容连接到页面内部,浏览器可能会更正这个css内容,并丢弃它不理解的所有内容。该解决方案也适合我,主要是输出将是正常有效的css。
将输入添加css到页面中没有问题,但我仍然不明白如何提取它。
如果您将其添加到标签内,style它将以与您添加它相同的形式提取它而无需更正。可能有必要以不同的方式提取,而不是通过 dom。

javascript
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. k0mar
    2020-01-14T22:01:47Z2020-01-14T22:01:47Z

    我用这个lib,不用拉很多包,重量小,很灵活,可以用

    • 1
  2. Best Answer
    Qwertiy
    2020-01-14T23:05:19Z2020-01-14T23:05:19Z

    如果您需要将代码清理为当前浏览器可以理解的内容

    请注意,虽然块@supports仍然存在,但如果当前浏览器不支持适当的样式,则这些属性将从规则中删除。供应商前缀也是如此。

    由于 SO 上的安全设置,在片段中不起作用,但在 jsfiddle 上一切正常:https ://jsfiddle.net/usjqmhf8/2/

    document.querySelector('button').addEventListener('click', () => {
      var css = document.querySelector('textarea').value
    
      var doc = document.querySelector('iframe').contentDocument
      doc.open()
      doc.write(`<!DOCTYPE html><title>CSS</title><style></style>`)
      doc.close()
      doc.querySelector('style').textContent = css
      
      var res = [...doc.styleSheets[0].cssRules].map(s => s.cssText).join("\n")
      document.querySelector('pre').textContent = res;
    })
    [hidden] { display: none !important; }
    html, body, textarea, pre { height: 100%; margin: 0; box-sizing: border-box; }
    textarea, pre { width: 50%; float: left; overflow: auto; resize: none; }
    button { position: absolute; top: 8px; right: 25px; }
    <textarea>.selector1 {
       text-overflow: -o-ellipsis-lastline;
       -webkit-appearance: none;
       -moz-appearance: none;
       appearance: none;
       color: red;
    }
    .selector2 div {
       font-weight: bold;
       rubbish
    }
    @supports (qqq: www) {
      @media only print {
        qwe:zxc {
          abc: xyz;
        }
        qwe:hover {
          qqq: www;
          color: silver
        }
      }
    }
    some_strange_code #^
    </style><script>alert(1)</script>
    {} a { color: red }</textarea>
    <button>Проверить</button>
    <iframe hidden sandbox="allow-same-origin"></iframe>
    <pre></pre>

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5