简单的解析font标签

来源:互联网 发布:linux系统中文版 编辑:程序博客网 时间:2024/06/06 01:52

利用font标签来给某些字符设定字号、颜色、和是否加粗。

var log = console.log;var str = "abc<font color='#ffee00'>123</font>oooo<b>oo</b>ooooo<font color='#ff2300' size='26'>ppppppp<b>我擦</b>ppp<b>嗷嗷嗷</b>p</font>点点点";function Data(str, color, size, isBold) {this.str = str;this.color = color || "#ffffff";this.size = size || 30;this.isBold = isBold || false;}// 解析font标签中的属性:size、colorfunction parseFont(html) {var size = undefined;var color = undefined;if(html[0] == '<' && html[1] == 'f') {for(var i = 0; i < html.length; i++) {if(html[i] == 's') {// 目前需求只需要解析size和color所以只有判断字符是不是s就知道是不是检测到有size这个属性var j = i;while(html[j] != "'") {j++;}var start = j + 1;j = start;while(html[j] != "'") {j++;}var end = j;var num = html.substring(start, end);var size = parseInt(num);}if(html[i] == 'c') {var j = i;while(html[j] != "'") {j++;}var start = j + 1;j = start;while(html[j] != "'") {j++;}var end = j;var hex = html.substring(start, end);var color = hex;}if(html[i] == '>') {break;}}} return {"color":color, "size":size};}function parse(html) {var chList = [];var dataList = [];var size = undefined;var color = undefined;var bold = undefined;log(html)for(var i = 0; i < html.length; i++) {if(html[i] == '<') {if(chList.length > 0) {var str = chList.join("");var data = new Data(str, color, size, bold);dataList.push(data);chList = [];}if(html[i + 1] == '/') {if(html[i + 2] == 'f') {size = undefined;color = undefined;} else if(html[i + 2] == 'b') {bold = undefined;}var j = i;while(html[j] != '>') {j++;}i = j;} else if(html[i + 1] == 'f') {var d = parseFont(html.substring(i, html.length));size = d["size"];color = d["color"];} else if(html[i + 1] == 'b') {bold = true;}var j = i;while(html[j] != '>') {j++;}i = j;} else {chList.push(html[i]);}}if(chList.length > 0) {var str = chList.join("");var data = new Data(str, color, size, bold);dataList.push(data);chList = [];}return dataList;}log(parse(str));


0 0
原创粉丝点击