PageSourceSearch

https://ehuixue.cn/static/common/htmldecode.js

js ehuixue.cn collected 2026-10-05 01:59:39 UTC 3,758 bytes, 135 lines download raw bytes

1
2var HtmlUtil = {
3  /*1.用正则表达式实现html转码*/
4  htmlEncodeByRegExp: function (str) {
5    var s = "";
6    if (str.length == 0) return "";
7    s = str.replace(/&/g, "&");
8    s = s.replace(/</g, "&lt;");
9    s = s.replace(/>/g, "&gt;");
10    s = s.replace(/\'/g, "&#039;");
11    s = s.replace(/\"/g, "&quot;");
12    return s;
13  },
14  /*2.用正则表达式实现html解码*/
15  htmlDecodeByRegExp: function (str) {
16    var s = "";
17    if (str.length == 0) return "";
18    s = str.replace(/&amp;/g, "&");
19    s = s.replace(/&lt;/g, "<");
20    s = s.replace(/&gt;/g, ">");
21    s = s.replace(/&#039;/g, "\'");
22    s = s.replace(/&#39;/g, "\'");
23    s = s.replace(/&quot;/g, "\"");
24    return s;
25  }
26};
27
28/**
29 * 转码
30 * @param  {[type]} str [description]
31 * @return {[type]}     [description]
32 */
33function htmlDecode(str) {
34  var reg = /[><]/;
35  if (reg.test(str)) {
36    var decodeHTML = HtmlUtil.htmlDecodeByRegExp(str);
37    // alert(str);
38    return decodeHTML;
39  } else {
40
41    var temp = document.createElement("div");
42    //2.然后将要转换的字符串设置为这个元素的innerHTML(ie,火狐,google都支持)
43    temp.innerHTML = str;
44    //3.最后返回这个元素的innerText(ie支持)或者textContent(火狐,google支持),即得到经过HTML解码的字符串了。
45    var output = temp.innerText || temp.textContent;
46    temp = null;
47    // alert(output) ;
48    return output;
49  }
50
51}
52
53/**
54* 反转码(编码)
55* @param  {[type]} str [description]
56* @return {[type]}     [description]
57*/
58function htmlEncode(str) {
59  var reg = /[<>&"']/;
60  if (reg.test(str)) {
61    var encodeHTML = HtmlUtil.htmlEncodeByRegExp(str);
62    return encodeHTML;
63  } else {
64
65    var temp = document.createElement("div");
66    //2.然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(火狐,google支持)
67    temp.innerText = str;
68    //3.最后返回这个元素的innerHTML,即得到经过HTML编码的字符串了。
69    var output = temp.innerHTML;
70    temp = null;
71    return output;
72  }
73
74}
75
76/**
77 * [changestr2 description]截取字符串
78 * @param  {[type]} str [description]字符串
79 * @param  {[type]} len [description]截取长度
80 * @return {[type]}     [description]
81 */
82function changestr2(str, len) {
83  if (str.length > len) {
84    var name = str.substr(0, len) + "...";
85  } else {
86    var name = str;
87  }
88  return name;
89}
90
91/**
92 * [changestr description]截取字符串
93 * @param  {[type]} str [description]字符串
94 * @param  {[type]} len [description]截取长度
95 * @return {[type]}     [description]
96 */
97function changestr(str, len) {
98  if (str.length > len) {
99    var name = str.substr(0, len);
100  } else {
101    var name = str;
102  }
103  return name;
104}
105
106// 注册 Vue 全局过滤器,复用 htmlDecode 解码 HTML 实体
107// 用法:{{ var | html_entity_decode }}
108// 兼容 htmldecode.js 在 vue.js 之前或之后加载
109(function () {
110  function registerFilter(V) {
111    if (V && typeof V.filter === 'function' && !V.options.filters['html_entity_decode']) {
112      V.filter('html_entity_decode', function (str) {
113        if (!str || typeof str !== 'string') return str;
114        return htmlDecode(str);
115      });
116    }
117  }
118
119  if (typeof Vue !== 'undefined') {
120    // Vue 已加载,立即注册
121    registerFilter(Vue);
122  } else {
123    // Vue 尚未加载,拦截 window.Vue 赋值,在 Vue 定义时自动注册
124    var _vue;
125    Object.defineProperty(window, 'Vue', {
126      configurable: true,
127      enumerable: true,
128      get: function () { return _vue; },
129      set: function (v) {
130        _vue = v;
131        registerFilter(v);
132      }
133    });
134  }
135})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.