我试图让用户编辑 html / css ,但阻止他们进行 在 <?php 块
中修改代码到目前为止,我一直在测试codemirror,并能够 通过将 markText 与只读参数一起使用,可以获得预期的结果。
我想知道是否有其他方法或不同的方法 基于浏览器的编辑器?
这是我的例子:
window.onload = () => {
// js syntax
function init(ok) {
return ok;
}
}
var htmlEditor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
mode: "htmlmixed",
theme: 'default'
});
htmlEditor.markText({line:4,ch:1},{line:7,ch:1},{readOnly:true});
下面是整个代码段,可防止 <?php 块被编辑
*, *:after, *:before {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
height: 100%;
}
html {
background: #fff;
text-rendering: optimizeLegibility;
line-height: 1.5em;
letter-spacing: .3px;
font-family: 'Poppins', sans-serif;
-webkit-text-size-adjust: 100%;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
body {
color: #151515;
}
html {
font-family: 'Geneva', sans-serif;
}
/* ----------
WRAPPERS & CONTAINERS
---------- */
section {
position: relative;
padding: 2.5rem 0;
}
.container {
position: relative;
margin: auto;
padding: 0 20px;
width: 100%;
max-width: 970px;
}
section:after, .container:after, .row:after {
display: table;
content: '';
clear: both;
}
/* ----------
ANHOR TAG
---------- */
a {
cursor: pointer;
outline: 0;
}
h1 > a,
h2 > a,
h3 > a,
h4 > a,
h5 > a,
h6 > a,
p > a {
color: #ed9d0a;
text-decoration: none;
}
h1 > a:hover,
h2 > a:hover,
h3 > a:hover,
h4 > a:hover,
h5 > a:hover,
h6 > a:hover,
p > a:hover {
text-decoration: underline;
}
/* ----------
TYPOGRAPHY
---------- */
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
ul {
margin-bottom: 1rem;
-ms-word-break: break-word;
-ms-word-wrap: break-word;
-webkit-word-break: break-word;
-webkit-word-wrap: break-word;
word-break: break-word;
word-wrap: break-word;
}
h1:last-child,
h2:last-child,
h3:last-child,
h4:last-child,
h5:last-child,
h6:last-child,
p:last-child,
blockquote:last-child,
ul:last-child {
margin-bottom: 0;
}
h1,
h2,
h3,
h4,
h5,
h6 {
line-height: 1.25em;
letter-spacing: 1px;
font-weight: 600;
}
h1:not(:first-child),
h2:not(:first-child),
h3:not(:first-child),
h4:not(:first-child),
h5:not(:first-child),
h6:not(:first-child) {
margin-top: 1.5em;
}
h1 {
letter-spacing: 2px;
font-size: 1.75rem;
}
h2 {
font-size: 1.5rem;
}
h1 strong,
h2 strong,
h3 strong,
h4 strong,
h5 strong,
h6 strong {
font-weight: 700;
}
p {
line-height: 1.5em;
}
ul {
padding-left: 1.5rem;
}
hr {
border: 0;
border-top: 1px solid #e1e1e1;
}
/* ----------
CODE BLOCK
---------- */
.code-container {
position: relative;
margin-bottom: 1.5rem;
overflow: hidden;
border-radius: 3px;
box-shadow: 3px 3px 6px rgba(0, 0, 0, .3);
}
.code-container:last-child {
margin-bottom: 0;
}
.btn {
background-color: #ed9d0a;
color: #fff;
padding: 4px 10px;
text-decoration: none;
border-radius: 3px;
box-shadow: 0 8px 6px -6px rgba(0, 0, 0, .15);
-webkit-backface-visibility: hidden;
-webkit-transition: 200ms -webkit-transform ease, 200ms transform ease, 200ms box-shadow ease;
transition: 200ms -webkit-transform ease, 200ms transform ease, 200ms box-shadow ease;
}
.btn:hover {
box-shadow: 0 6px 6px -4px rgba(0, 0, 0, .2);
-webkit-transform: translateY(-2px);
transform: translateY(-2px);
}
.btn.btn-left {
float: left;
}
.btn.btn-right {
float: right;
}
.row {
margin-bottom: 1.5rem;
width: 100%;
}
.row:last-child {
margin-bottom: 0;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.32.0/codemirror.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.32.0/codemirror.min.js"></script>
<section>
<div class="container">
<h1>Trying to mix HTML and PHP</h1>
<div class="code-container">
<textarea id="code">
<!-- xml/html syntax -->
<!DOCTYPE html>
<html>
<head>
<?php
phpinfo();
?>
<!-- meta -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- title -->
<title>Example website</title>
</head>
<body>
<section>
<div class="container">
<h1>Example website</h1>
<p>This is just a HTML example</p>
</div>
</section>
</body>
</html>
</textarea>
</div>
</div>
</section>
addAttribute