Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 剥离html标记&x27;RegExp自由通道';_Javascript_Html_Html Sanitizing - Fatal编程技术网

Javascript 剥离html标记&x27;RegExp自由通道';

Javascript 剥离html标记&x27;RegExp自由通道';,javascript,html,html-sanitizing,Javascript,Html,Html Sanitizing,我最近有点偏执,因为有一些解决方案通过使用正则表达式“清理”html字符串来处理这项任务。它们在很大程度上取决于正则表达式的“防弹性”。所以,我想出了这个片段,希望能从社区得到一些反馈。谢谢 // // #notags String.prototype.notags = (function (doc) { var mkel = doc.createElement.bind(doc); var hasown = Function.prototype.call.bind(Obj

我最近有点偏执,因为有一些解决方案通过使用正则表达式“清理”html字符串来处理这项任务。它们在很大程度上取决于正则表达式的“防弹性”。所以,我想出了这个片段,希望能从社区得到一些反馈。谢谢

//
// #notags
String.prototype.notags = (function (doc) {

  var mkel     = doc.createElement.bind(doc);
  var hasown   = Function.prototype.call.bind(Object.prototype.hasOwnProperty);

  // #textlike-nodes
  var textlike = {

    12 : "NOTATION_NODE", 
    3  : "TEXT_NODE", 
    4  : "CDATA_SECTION_NODE", 
    5  : "ENTITY_REFERENCE_NODE", 
    6  : "ENTITY_NODE", 
    7  : "PROCESSING_INSTRUCTION_NODE", 
    8  : "COMMENT_NODE"

  };

  // #_notags
  // main function
  var _notags = function (tagedstring) {

    var div;
    var istxt = istextnode;
    var nodes;
    var nodetxt = getxt;
    var res;

    div = mkel('div');
    div.innerHTML = (''+ tagedstring);


    // get the div's descendants 
    // and read their text content 
    // until all of its childern are plain 
    // text nodes...

    nodes = descendants(div);

    while (!nodes.every(istxt)) {
      div.innerHTML = nodetxt(div);
      nodes = descendants(div);
    }

    res = div.innerHTML;

    // get rid of temporary div
    // prevents mem. leaks
    div.innerHTML = '';
    delete div;

    return res;
  };


  // #save
  // String#notags
  return function () {
    return _notags(this);
  };



  ////////////////
  ////// #helpers

  // #istextnode
  function istextnode (node) {
    return !(3 - node.nodeType);
  }

  // #descendants
  function descendants (startnode, _nodels) {

    _nodels || (_nodels = []);

    var node = startnode.firstChild;

    while (node) {

      _nodels.push(node);
      descendants(node, _nodels);

      node = node.nextSibling;
    }

    return _nodels;
  }

  // #getxt
  // loop each node's descendant 
  // and fetch it' text content
  function getxt (node) {

    var _ = {
      str: '', 
      txt: textlike
    };

    descendants(node)
    .forEach(getnodetext, _);

    return _.str;
  }

  // #getnodetext
  function getnodetext (node) {
    //this: {str, txt}
    if (hasown(this.txt, node.nodeType))
      this.str += (node.data || node.textContent || node.nodeValue);
  }


})(document);

// /eof

这个问题似乎离题了;可能适合。我应该把它转到那里吗?这不是一堆代码…只是清理服务器上的输入。这是你无论如何都必须做的事情,所以在客户机上做这件事真的没有任何好处。@nikolav:我知道我遗漏了一些东西(绑定技巧),非常酷,我不确定我以前是否见过使用过。它甚至可以在IE8上工作(带有
函数#bind
垫片)。漂亮!