0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 15:23:29 -05:00

Cleanup P and Underline tags

node.
This commit is contained in:
Andy Kauffman 2016-05-11 08:16:11 -04:00 committed by Neil Jenkins
parent 8b580ed988
commit 9dcc4fb79f
3 changed files with 78 additions and 48 deletions

View file

@ -1690,7 +1690,7 @@ var fontSizes = {
7: 48 7: 48
}; };
var spanToSemantic = { var styleToSemantic = {
backgroundColor: { backgroundColor: {
regexp: notWS, regexp: notWS,
replace: function ( doc, colour ) { replace: function ( doc, colour ) {
@ -1738,6 +1738,12 @@ var spanToSemantic = {
style: 'font-size:' + size style: 'font-size:' + size
}); });
} }
},
textDecoration: {
regexp: /^underline/i,
replace: function ( doc ) {
return createElement( doc, 'U' );
}
} }
}; };
@ -1750,36 +1756,45 @@ var replaceWithTag = function ( tag ) {
}; };
}; };
var stylesRewriters = { var replaceStyles = function ( node, parent ) {
SPAN: function ( span, parent ) { var style = node.style;
var style = span.style, var doc = node.ownerDocument;
doc = span.ownerDocument, var attr, converter, css, newTreeBottom, newTreeTop, el;
attr, converter, css, newTreeBottom, newTreeTop, el;
for ( attr in spanToSemantic ) { for ( attr in styleToSemantic ) {
converter = spanToSemantic[ attr ]; converter = styleToSemantic[ attr ];
css = style[ attr ]; css = style[ attr ];
if ( css && converter.regexp.test( css ) ) { if ( css && converter.regexp.test( css ) ) {
el = converter.replace( doc, css ); el = converter.replace( doc, css );
if ( !newTreeTop ) {
newTreeTop = el;
}
if ( newTreeBottom ) { if ( newTreeBottom ) {
newTreeBottom.appendChild( el ); newTreeBottom.appendChild( el );
} }
newTreeBottom = el; newTreeBottom = el;
if ( !newTreeTop ) { node.style[ attr ] = '';
newTreeTop = el;
}
} }
} }
if ( newTreeTop ) { if ( newTreeTop ) {
newTreeBottom.appendChild( empty( span ) ); newTreeBottom.appendChild( empty( node ) );
parent.replaceChild( newTreeTop, span ); if ( node.nodeName === 'SPAN' ) {
parent.replaceChild( newTreeTop, node );
} else {
node.appendChild( newTreeTop );
}
} }
return newTreeBottom || span; return newTreeBottom || node;
}, };
var stylesRewriters = {
P: replaceStyles,
SPAN: replaceStyles,
STRONG: replaceWithTag( 'B' ), STRONG: replaceWithTag( 'B' ),
EM: replaceWithTag( 'I' ), EM: replaceWithTag( 'I' ),
INS: replaceWithTag( 'U' ),
STRIKE: replaceWithTag( 'S' ), STRIKE: replaceWithTag( 'S' ),
FONT: function ( node, parent ) { FONT: function ( node, parent ) {
var face = node.face, var face = node.face,

File diff suppressed because one or more lines are too long

View file

@ -10,7 +10,7 @@ var fontSizes = {
7: 48 7: 48
}; };
var spanToSemantic = { var styleToSemantic = {
backgroundColor: { backgroundColor: {
regexp: notWS, regexp: notWS,
replace: function ( doc, colour ) { replace: function ( doc, colour ) {
@ -58,6 +58,12 @@ var spanToSemantic = {
style: 'font-size:' + size style: 'font-size:' + size
}); });
} }
},
textDecoration: {
regexp: /^underline/i,
replace: function ( doc ) {
return createElement( doc, 'U' );
}
} }
}; };
@ -70,36 +76,45 @@ var replaceWithTag = function ( tag ) {
}; };
}; };
var stylesRewriters = { var replaceStyles = function ( node, parent ) {
SPAN: function ( span, parent ) { var style = node.style;
var style = span.style, var doc = node.ownerDocument;
doc = span.ownerDocument, var attr, converter, css, newTreeBottom, newTreeTop, el;
attr, converter, css, newTreeBottom, newTreeTop, el;
for ( attr in spanToSemantic ) { for ( attr in styleToSemantic ) {
converter = spanToSemantic[ attr ]; converter = styleToSemantic[ attr ];
css = style[ attr ]; css = style[ attr ];
if ( css && converter.regexp.test( css ) ) { if ( css && converter.regexp.test( css ) ) {
el = converter.replace( doc, css ); el = converter.replace( doc, css );
if ( !newTreeTop ) {
newTreeTop = el;
}
if ( newTreeBottom ) { if ( newTreeBottom ) {
newTreeBottom.appendChild( el ); newTreeBottom.appendChild( el );
} }
newTreeBottom = el; newTreeBottom = el;
if ( !newTreeTop ) { node.style[ attr ] = '';
newTreeTop = el;
}
} }
} }
if ( newTreeTop ) { if ( newTreeTop ) {
newTreeBottom.appendChild( empty( span ) ); newTreeBottom.appendChild( empty( node ) );
parent.replaceChild( newTreeTop, span ); if ( node.nodeName === 'SPAN' ) {
parent.replaceChild( newTreeTop, node );
} else {
node.appendChild( newTreeTop );
}
} }
return newTreeBottom || span; return newTreeBottom || node;
}, };
var stylesRewriters = {
P: replaceStyles,
SPAN: replaceStyles,
STRONG: replaceWithTag( 'B' ), STRONG: replaceWithTag( 'B' ),
EM: replaceWithTag( 'I' ), EM: replaceWithTag( 'I' ),
INS: replaceWithTag( 'U' ),
STRIKE: replaceWithTag( 'S' ), STRIKE: replaceWithTag( 'S' ),
FONT: function ( node, parent ) { FONT: function ( node, parent ) {
var face = node.face, var face = node.face,