JavaScript-将HTML标记插入给定样式索引的字符串中
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaScript-将HTML标记插入给定样式索引的字符串中,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3359字,纯文字阅读大概需要5分钟。
内容图文
我有一段文字.
"This is a test to see whether nested style spans work properly."
我将样式信息作为JSON对象,例如
0: {start: 22, end: 54, type: "strong"}
1: {start: 30, end: 36, type: "hyperlink", data: {…}}
2: {start: 37, end: 48, type: "em"}
3: {start: 43, end: 48, type: "hyperlink", data: {…}}
文字看起来应该像
<p>This is a test to see <strong>whether <a href="https://www.google.co.za">nested</a> <em>style <a href="https://www.google.co.za">spans</a></em> work </strong>properly.</p>
算法的一般方法应该是什么?问题是,一旦我开始在文本中插入标记以设置样式,样式信息中包含的索引就会明显过时.
我试图跟踪要通过缓冲区插入的字符的长度(以便可以根据缓冲区的长度来调整索引),但是这成为嵌套标签的问题.我的整个“解决方案”都显得笨拙和笨拙,我确信必须有一个更好的方法.
这是我尝试过的代码.
NewsUtils.styleSpanCSS = span => {
let styledSpan = {};
switch (span.type) {
case "em":
styledSpan.opening = "<span";
styledSpan.style = ' class="italic">';
styledSpan.closing = "</span>";
break;
case "hyperlink":
styledSpan.opening = `<a href="${span.data.url}">`;
styledSpan.style = "";
styledSpan.closing = "</a>";
break;
case "strong":
styledSpan.opening = "<span";
styledSpan.style = ' class="bold">';
styledSpan.closing = "</span>";
break;
default:
styledSpan.opening = "";
styledSpan.style = "";
styledSpan.closing = "";
}
styledSpan.length =
styledSpan.opening.length +
styledSpan.style.length +
styledSpan.closing.length;
return styledSpan;
};
NewsUtils.styleParagraph = elem => {
if (elem.spans.length > 0) {
let buffer = 0;
elem.spans.map(span => {
let elementToInsert = NewsUtils.styleSpanCSS(span);
let spanLength =
elementToInsert.opening.length +
elementToInsert.style.length +
elementToInsert.closing.length;
elem.text =
elem.text.substring(0, span.start + buffer) +
elementToInsert.opening +
elementToInsert.style +
elem.text.substring(span.start + buffer, span.end + buffer) +
elementToInsert.closing +
elem.text.substring(span.end + buffer, elem.text.length + buffer);
buffer += spanLength;);
});
return <p dangerouslySetInnerHTML={{ __html: elem.text }} />;
}
return <p dangerouslySetInnerHTML={{ __html: elem.text }} />;
};
NewsUtils.markupParagraphs = post => {
const postDetails = post.data.text.map(elem => {
switch (elem.type) {
case "paragraph":
return NewsUtils.styleParagraph(elem);
case "image":
return (
<img
src={elem.url}
width={elem.dimensions.width}
height={elem.dimensions.height}
/>
);
case "embed":
let url = elem.oembed.embed_url;
url = url.substring(0, url.indexOf("&"));
url = url.replace("watch?v=", "embed/");
url = url.replace("vimeo.com", "player.vimeo.com/video");
return <iframe src={url} frameBorder="0" allowFullScreen />;
default:
return null;
}
});
return postDetails;
};
};
解决方法:
这是一个基本的实现.我摆脱了超链接的特殊处理,以演示算法本身,但是将逻辑添加回去应该很容易:
const text = 'This is a test to see whether nested style spans work properly.'
const styling = [
{start: 22, end: 54, type: "strong"},
{start: 30, end: 36, type: "a"},
{start: 37, end: 48, type: "em"},
{start: 43, end: 48, type: "a"}
];
const result = [...text].reduce((a, v, i) => {
styling.filter(s => s.start === i).forEach(s => a += `<${s.type}>`);
styling.filter(s => s.end === i).forEach(s => a += `</${s.type}>`);
return a + v;
}, '');
document.body.innerHTML = result;
输出:
This is a test to see <strong>whether <a>nested</a> <em>style <a>spans</em></a> work </strong>properly.
如果输入和样式数组很大,则可能需要创建临时查找对象以提高性能.
内容总结
以上是互联网集市为您收集整理的JavaScript-将HTML标记插入给定样式索引的字符串中全部内容,希望文章能够帮你解决JavaScript-将HTML标记插入给定样式索引的字符串中所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。