@@ -55,14 +55,14 @@ firepad.ParseHtml = (function () {
5555 }
5656
5757 ParseOutput . prototype . newlineIfNonEmpty = function ( state ) {
58- this . cleanLine_ ( ) ;
58+ this . cleanLine_ ( true ) ;
5959 if ( this . currentLine . length > 0 ) {
6060 this . newline ( state ) ;
6161 }
6262 } ;
6363
6464 ParseOutput . prototype . newlineIfNonEmptyOrListItem = function ( state ) {
65- this . cleanLine_ ( ) ;
65+ this . cleanLine_ ( true ) ;
6666 if ( this . currentLine . length > 0 || this . currentLineListItemType !== null ) {
6767 this . newline ( state ) ;
6868 }
@@ -84,15 +84,17 @@ firepad.ParseHtml = (function () {
8484 this . currentLineListItemType = type ;
8585 } ;
8686
87- ParseOutput . prototype . cleanLine_ = function ( ) {
87+ ParseOutput . prototype . cleanLine_ = function ( ignoreNbsps ) {
8888 // Kinda' a hack, but we remove leading and trailing spaces (since these aren't significant in html) and
8989 // replaces nbsp's with normal spaces.
9090 if ( this . currentLine . length > 0 ) {
9191 var last = this . currentLine . length - 1 ;
9292 this . currentLine [ 0 ] . text = this . currentLine [ 0 ] . text . replace ( / ^ + / , '' ) ;
9393 this . currentLine [ last ] . text = this . currentLine [ last ] . text . replace ( / + $ / g, '' ) ;
94- for ( var i = 0 ; i < this . currentLine . length ; i ++ ) {
95- this . currentLine [ i ] . text = this . currentLine [ i ] . text . replace ( / \u00a0 / g, ' ' ) ;
94+ if ( ! ignoreNbsps ) {
95+ for ( var i = 0 ; i < this . currentLine . length ; i ++ ) {
96+ this . currentLine [ i ] . text = this . currentLine [ i ] . text . replace ( / \u00a0 / g, ' ' ) ;
97+ }
9698 }
9799 }
98100 // If after stripping trailing whitespace, there's nothing left, clear currentLine out.
@@ -101,14 +103,18 @@ firepad.ParseHtml = (function () {
101103 }
102104 } ;
103105
104- var entityManager_ ;
105- function parseHtml ( html , entityManager ) {
106+ var entityManager_ , codeMirror_ ;
107+ function parseHtml ( html , entityManager , codeMirror ) {
108+ html = html . replace ( / ( \r \n | \n | \r ) ? < h t m l > ( \r \n | \n | \r ) ? < b o d y > ( \r \n | \n | \r ) ? / , '' ) ; // remove <html><body>
109+ html = html . replace ( / ( \r \n | \n | \r ) ? < \/ b o d y > ( \r \n | \n | \r ) ? < \/ h t m l > ( \r \n | \n | \r ) ? / , '' ) ; // remove </body></html>
110+
106111 // Create DIV with HTML (as a convenient way to parse it).
107112 var div = ( firepad . document || document ) . createElement ( 'div' ) ;
108113 div . innerHTML = html ;
109114
110115 // HACK until I refactor this.
111116 entityManager_ = entityManager ;
117+ codeMirror_ = codeMirror ;
112118
113119 var output = new ParseOutput ( ) ;
114120 var state = new ParseState ( ) ;
@@ -138,8 +144,8 @@ firepad.ParseHtml = (function () {
138144
139145 switch ( node . nodeType ) {
140146 case Node . TEXT_NODE :
141- // This probably isn't exactly right, but mostly works...
142- var text = node . nodeValue . replace ( / [ \n \t ] + / g, ' ' ) ;
147+ // replace spaces with so they can withstand cleanLine_
148+ var text = node . nodeValue . replace ( / / g, '\u00a0 ' ) ;
143149 output . currentLine . push ( firepad . Text ( text , state . textFormatting ) ) ;
144150 break ;
145151 case Node . ELEMENT_NODE :
@@ -240,7 +246,29 @@ firepad.ParseHtml = (function () {
240246 }
241247 }
242248
249+ function styleEqual ( s1 , s2 ) {
250+ s1 = s1 . toLowerCase ( ) ; // lower
251+ s1 = s1 . split ( ' ' ) . join ( '' ) ; // remove spaces
252+ s1 = s1 . lastIndexOf ( ";" ) == s1 . length - 1 ? s1 . substring ( 0 , s1 . length - 1 ) : s1 ; // remove trailing ;
253+ s2 = s2 . toLowerCase ( ) ; // lower
254+ s2 = s2 . split ( ' ' ) . join ( '' ) ; // remove spaces
255+ s2 = s2 . lastIndexOf ( ";" ) == s2 . length - 1 ? s2 . substring ( 0 , s2 . length - 1 ) : s2 ; // remove trailing ;
256+ return s1 == s2 ;
257+ }
258+
243259 function parseStyle ( state , styleString ) {
260+ if ( ! this . firepadDefaultStyles ) {
261+ // caching some default styles needed later
262+ var style = window . getComputedStyle ( codeMirror_ . getWrapperElement ( ) ) ;
263+ firepadDefaultStyles = {
264+ fontFamily : style . getPropertyValue ( 'font-family' ) ,
265+ fontSize : style . getPropertyValue ( 'font-size' ) ,
266+ backgroundColor : style . getPropertyValue ( 'background-color' ) ,
267+ color : style . getPropertyValue ( 'color' ) ,
268+ textAlign : style . getPropertyValue ( 'text-align' )
269+ } ;
270+ }
271+
244272 var textFormatting = state . textFormatting ;
245273 var lineFormatting = state . lineFormatting ;
246274 var styles = styleString . split ( ';' ) ;
@@ -265,6 +293,7 @@ firepad.ParseHtml = (function () {
265293 textFormatting = textFormatting . italic ( italic ) ;
266294 break ;
267295 case 'color' :
296+ if ( styleEqual ( val , this . firepadDefaultStyles . color ) ) break ;
268297 textFormatting = textFormatting . color ( val ) ;
269298 break ;
270299 case 'qmid' :
@@ -274,12 +303,15 @@ firepad.ParseHtml = (function () {
274303 textFormatting = textFormatting . qmclass ( ) ;
275304 break ;
276305 case 'background-color' :
306+ if ( styleEqual ( val , this . firepadDefaultStyles . backgroundColor ) ) break ;
277307 textFormatting = textFormatting . backgroundColor ( val ) ;
278308 break ;
279309 case 'text-align' :
310+ if ( styleEqual ( val , this . firepadDefaultStyles . textAlign ) ) break ;
280311 lineFormatting = lineFormatting . align ( val ) ;
281312 break ;
282313 case 'font-size' :
314+ if ( styleEqual ( val , this . firepadDefaultStyles . fontSize ) ) break ;
283315 var size = null ;
284316 var allowedValues = [ 'px' , 'pt' , '%' , 'em' , 'xx-small' , 'x-small' , 'small' , 'medium' , 'large' , 'x-large' , 'xx-large' , 'smaller' , 'larger' ] ;
285317 if ( firepad . utils . stringEndsWith ( val , allowedValues ) ) {
@@ -293,6 +325,7 @@ firepad.ParseHtml = (function () {
293325 }
294326 break ;
295327 case 'font-family' :
328+ if ( styleEqual ( val , this . firepadDefaultStyles . fontFamily ) ) break ;
296329 var font = firepad . utils . trim ( val . split ( ',' ) [ 0 ] ) ; // get first font.
297330 font = font . replace ( / [ ' " ] / g, '' ) ; // remove quotes.
298331 font = font . replace ( / \w \S * / g, function ( txt ) { return txt . charAt ( 0 ) . toUpperCase ( ) + txt . substr ( 1 ) . toLowerCase ( ) } ) ;
0 commit comments