Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #18724

Re: Clean up HTML content

From Cezary Tomczyk <cezary.tomczyk@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Clean up HTML content
Date 2013-03-05 20:25 +0100
Organization Aioe.org NNTP Server
Message-ID <kh5grn$bm0$1@speranza.aioe.org> (permalink)
References <kh4813$lkv$1@speranza.aioe.org> <2342178.BYbISWzQ9U@PointedEars.de>

Show all headers | View raw


W dniu 2013-03-05 17:55, Thomas 'PointedEars' Lahn pisze:
> Cezary Tomczyk wrote:
[...]

>> Live example: http://jsfiddle.net/rFRwp/
>
> This is Usenet, not the Web.  An URI to a test case can be helpful, but it
> does not suffice.

I know that this is Usenet, not the Web. I thought that code is too big 
and instead of posting code here it would be better to prepare a working 
example.

>> Unfortunately, doesn't work yet. Am I going into right way? Or there is
>> better methods to clean up HTML?
>
> Perhaps.  I cannot know for sure because I cannot scroll your script code on
> the Web, and you have not posted any code here.

Here's the code:

var is_block_style = function (el) {
     var el_style;
     if (window.getComputedStyle) {
         el_style = window.getComputedStyle(el, 
null).getPropertyValue('display');
     } else {
         el_style = el.currentStyle.display;
     }
     return el_style;
};

var imgClassName = 'testimg';

var tidyHTML = function (node) {
     var elms = node.childNodes,
         i = elms.length,
         text = '',
         elm;

     if (!node.hasChildNodes() && (node.nodeName.toLowerCase() === 'img' 
&& !(new RegExp('(^|\\s)' + imgClassName + 
'(\\s|$)')).test(node.className))) {
         node.parentNode.removeChild(node);
         return text;
     }

     while (i--) {
         elm = elms[i];

         if (elm.nodeType === 1) {
             text += tidyHTML(elm);
         } else if (elm.nodeType === 3) {
             text += elm.data;
         } else if (elm.nodeType !== 3 && elm.nodeType !== 1) {
             elm.parentNode.removeChild(elm);
         }
     }

     if (typeof text !== 'undefined') {
         if (is_block_style(node) === 'block') {
             node.parentNode.insertBefore(document.createElement('br'), 
node.nextSibling);
         }
         //node.parentNode.replaceChild(document.createTextNode(text), 
node);
     }
     return text;
};

Above version also on the web: http://jsfiddle.net/rFRwp/2/

-- 
Cezary Tomczyk
http://www.ctomczyk.pl/

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Clean up HTML content Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-03-05 08:48 +0100
  Re: Clean up HTML content Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-05 17:55 +0100
    Re: Clean up HTML content Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-03-05 20:25 +0100
      Re: Clean up HTML content Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-06 00:03 +0100
        Re: Clean up HTML content Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-06 00:08 +0100
          Re: Clean up HTML content Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-06 22:06 +0000
            Re: Clean up HTML content Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-03-10 01:14 +0100
        Re: Clean up HTML content Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-03-06 22:02 +0100
          Re: Clean up HTML content Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-06 23:45 +0100
            Re: Clean up HTML content Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-03-09 11:29 +0100
              Re: Clean up HTML content Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-09 16:11 +0100
  Re: Clean up HTML content Danny <dann90038@gmail.com> - 2013-03-07 14:28 -0800
    Re: Clean up HTML content Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-03-09 11:37 +0100
      Re: Clean up HTML content dann90038@gmail.com - 2013-03-09 11:44 -0800

csiph-web