@@ -8,6 +8,12 @@ var htmlparser = require("htmlparser");
88var helper = require ( './helper' ) ;
99var format = require ( './formatter' ) ;
1010
11+ // Which type of tags should not be parsed
12+ var SKIP_TYPES = [
13+ 'style' ,
14+ 'script'
15+ ] ;
16+
1117function htmlToText ( html , options ) {
1218 options = options || { } ;
1319 _ . defaults ( options , {
@@ -17,9 +23,9 @@ function htmlToText(html, options) {
1723
1824 var handler = new htmlparser . DefaultHandler ( function ( error , dom ) {
1925
20- } , {
21- verbose : true ,
22- ignoreWhitespace : true
26+ } , {
27+ verbose : true ,
28+ ignoreWhitespace : true
2329 } ) ;
2430 new htmlparser . Parser ( handler ) . parseComplete ( html ) ;
2531
@@ -40,7 +46,7 @@ function filterBody(dom) {
4046 } ) ;
4147 }
4248 walk ( dom ) ;
43- return result || dom ;
49+ return result || dom ;
4450}
4551
4652function containsTable ( attr , tables ) {
@@ -62,7 +68,7 @@ function containsTable(attr, tables) {
6268 }
6369 var classes = filterByPrefix ( tables , '.' ) ;
6470 var ids = filterByPrefix ( tables , '#' ) ;
65- return attr && ( _ . include ( classes , attr . class ) || _ . include ( ids , attr . id ) ) ;
71+ return attr && ( _ . include ( classes , attr . class ) || _ . include ( ids , attr . id ) ) ;
6672}
6773
6874function walk ( dom , options ) {
@@ -101,14 +107,16 @@ function walk(dom, options) {
101107 break ;
102108 }
103109 default :
104- result += walk ( elem . children || [ ] , options ) ;
110+ result += walk ( elem . children || [ ] , options ) ;
105111 }
106112 break ;
107113 case 'text' :
108114 if ( elem . raw !== '\r\n' ) result += format . text ( elem , options ) ;
109115 break ;
110116 default :
111- result += walk ( elem . children || [ ] , options ) ;
117+ if ( ! _ . include ( SKIP_TYPES , elem . type ) ) {
118+ result += walk ( elem . children || [ ] , options ) ;
119+ }
112120 }
113121 } ) ;
114122 return result ;
0 commit comments