0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-18 04:32:28 -05:00

Fix broken references to this.

The clean functions used a `this` reference to call createElement, but are
called as functions not methods so `this` is undefined. Instead, we'll get the
ownerDocument off the nodes passed in, then call the createElement function
directly.
This commit is contained in:
Neil Jenkins 2013-07-17 11:13:49 +10:00
parent fefa32b744
commit d2e9d2214b
4 changed files with 51 additions and 45 deletions

File diff suppressed because one or more lines are too long

View file

@ -2161,8 +2161,8 @@ var fontSizes = {
var spanToSemantic = { var spanToSemantic = {
backgroundColor: { backgroundColor: {
regexp: notWS, regexp: notWS,
replace: function ( colour ) { replace: function ( doc, colour ) {
return this.createElement( 'SPAN', { return createElement( doc, 'SPAN', {
'class': 'highlight', 'class': 'highlight',
style: 'background-color: ' + colour style: 'background-color: ' + colour
}); });
@ -2170,8 +2170,8 @@ var spanToSemantic = {
}, },
color: { color: {
regexp: notWS, regexp: notWS,
replace: function ( colour ) { replace: function ( doc, colour ) {
return this.createElement( 'SPAN', { return createElement( doc, 'SPAN', {
'class': 'colour', 'class': 'colour',
style: 'color:' + colour style: 'color:' + colour
}); });
@ -2179,20 +2179,20 @@ var spanToSemantic = {
}, },
fontWeight: { fontWeight: {
regexp: /^bold/i, regexp: /^bold/i,
replace: function () { replace: function ( doc ) {
return this.createElement( 'B' ); return createElement( doc, 'B' );
} }
}, },
fontStyle: { fontStyle: {
regexp: /^italic/i, regexp: /^italic/i,
replace: function () { replace: function ( doc ) {
return this.createElement( 'I' ); return createElement( doc, 'I' );
} }
}, },
fontFamily: { fontFamily: {
regexp: notWS, regexp: notWS,
replace: function ( family ) { replace: function ( doc, family ) {
return this.createElement( 'SPAN', { return createElement( doc, 'SPAN', {
'class': 'font', 'class': 'font',
style: 'font-family:' + family style: 'font-family:' + family
}); });
@ -2200,8 +2200,8 @@ var spanToSemantic = {
}, },
fontSize: { fontSize: {
regexp: notWS, regexp: notWS,
replace: function ( size ) { replace: function ( doc, size ) {
return this.createElement( 'SPAN', { return createElement( doc, 'SPAN', {
'class': 'size', 'class': 'size',
style: 'font-size:' + size style: 'font-size:' + size
}); });
@ -2212,13 +2212,14 @@ var spanToSemantic = {
var stylesRewriters = { var stylesRewriters = {
SPAN: function ( span, parent ) { SPAN: function ( span, parent ) {
var style = span.style, var style = span.style,
doc = span.ownerDocument,
attr, converter, css, newTreeBottom, newTreeTop, el; attr, converter, css, newTreeBottom, newTreeTop, el;
for ( attr in spanToSemantic ) { for ( attr in spanToSemantic ) {
converter = spanToSemantic[ attr ]; converter = spanToSemantic[ attr ];
css = style[ attr ]; css = style[ attr ];
if ( css && converter.regexp.test( css ) ) { if ( css && converter.regexp.test( css ) ) {
el = converter.replace( css ); el = converter.replace( doc, css );
if ( newTreeBottom ) { if ( newTreeBottom ) {
newTreeBottom.appendChild( el ); newTreeBottom.appendChild( el );
} }
@ -2237,13 +2238,13 @@ var stylesRewriters = {
return newTreeBottom || span; return newTreeBottom || span;
}, },
STRONG: function ( node, parent ) { STRONG: function ( node, parent ) {
var el = this.createElement( 'B' ); var el = createElement( node.ownerDocument, 'B' );
parent.replaceChild( el, node ); parent.replaceChild( el, node );
el.appendChild( empty( node ) ); el.appendChild( empty( node ) );
return el; return el;
}, },
EM: function ( node, parent ) { EM: function ( node, parent ) {
var el = this.createElement( 'I' ); var el = createElement( node.ownerDocument, 'I' );
parent.replaceChild( el, node ); parent.replaceChild( el, node );
el.appendChild( empty( node ) ); el.appendChild( empty( node ) );
return el; return el;
@ -2251,16 +2252,17 @@ var stylesRewriters = {
FONT: function ( node, parent ) { FONT: function ( node, parent ) {
var face = node.face, var face = node.face,
size = node.size, size = node.size,
doc = node.ownerDocument,
fontSpan, sizeSpan, fontSpan, sizeSpan,
newTreeBottom, newTreeTop; newTreeBottom, newTreeTop;
if ( face ) { if ( face ) {
fontSpan = this.createElement( 'SPAN', { fontSpan = createElement( doc, 'SPAN', {
'class': 'font', 'class': 'font',
style: 'font-family:' + face style: 'font-family:' + face
}); });
} }
if ( size ) { if ( size ) {
sizeSpan = this.createElement( 'SPAN', { sizeSpan = createElement( doc, 'SPAN', {
'class': 'size', 'class': 'size',
style: 'font-size:' + fontSizes[ size ] + 'px' style: 'font-size:' + fontSizes[ size ] + 'px'
}); });
@ -2268,14 +2270,14 @@ var stylesRewriters = {
fontSpan.appendChild( sizeSpan ); fontSpan.appendChild( sizeSpan );
} }
} }
newTreeTop = fontSpan || sizeSpan || this.createElement( 'SPAN' ); newTreeTop = fontSpan || sizeSpan || createElement( doc, 'SPAN' );
newTreeBottom = sizeSpan || fontSpan || newTreeTop; newTreeBottom = sizeSpan || fontSpan || newTreeTop;
parent.replaceChild( newTreeTop, node ); parent.replaceChild( newTreeTop, node );
newTreeBottom.appendChild( empty( node ) ); newTreeBottom.appendChild( empty( node ) );
return newTreeBottom; return newTreeBottom;
}, },
TT: function ( node, parent ) { TT: function ( node, parent ) {
var el = this.createElement( 'SPAN', { var el = createElement( node.ownerDocument, 'SPAN', {
'class': 'font', 'class': 'font',
style: 'font-family:menlo,consolas,"courier new",monospace' style: 'font-family:menlo,consolas,"courier new",monospace'
}); });
@ -2346,18 +2348,19 @@ var cleanTree = function ( node, allowStyles ) {
var wrapTopLevelInline = function ( root, tag ) { var wrapTopLevelInline = function ( root, tag ) {
var children = root.childNodes, var children = root.childNodes,
doc = root.ownerDocument,
wrapper = null, wrapper = null,
i, l, child, isBR; i, l, child, isBR;
for ( i = 0, l = children.length; i < l; i += 1 ) { for ( i = 0, l = children.length; i < l; i += 1 ) {
child = children[i]; child = children[i];
isBR = child.nodeName === 'BR'; isBR = child.nodeName === 'BR';
if ( !isBR && isInline( child ) ) { if ( !isBR && isInline( child ) ) {
if ( !wrapper ) { wrapper = this.createElement( tag ); } if ( !wrapper ) { wrapper = createElement( doc, tag ); }
wrapper.appendChild( child ); wrapper.appendChild( child );
i -= 1; i -= 1;
l -= 1; l -= 1;
} else if ( isBR || wrapper ) { } else if ( isBR || wrapper ) {
if ( !wrapper ) { wrapper = this.createElement( tag ); } if ( !wrapper ) { wrapper = createElement( doc, tag ); }
fixCursor( wrapper ); fixCursor( wrapper );
if ( isBR ) { if ( isBR ) {
root.replaceChild( wrapper, child ); root.replaceChild( wrapper, child );

File diff suppressed because one or more lines are too long

View file

@ -1114,8 +1114,8 @@ var fontSizes = {
var spanToSemantic = { var spanToSemantic = {
backgroundColor: { backgroundColor: {
regexp: notWS, regexp: notWS,
replace: function ( colour ) { replace: function ( doc, colour ) {
return this.createElement( 'SPAN', { return createElement( doc, 'SPAN', {
'class': 'highlight', 'class': 'highlight',
style: 'background-color: ' + colour style: 'background-color: ' + colour
}); });
@ -1123,8 +1123,8 @@ var spanToSemantic = {
}, },
color: { color: {
regexp: notWS, regexp: notWS,
replace: function ( colour ) { replace: function ( doc, colour ) {
return this.createElement( 'SPAN', { return createElement( doc, 'SPAN', {
'class': 'colour', 'class': 'colour',
style: 'color:' + colour style: 'color:' + colour
}); });
@ -1132,20 +1132,20 @@ var spanToSemantic = {
}, },
fontWeight: { fontWeight: {
regexp: /^bold/i, regexp: /^bold/i,
replace: function () { replace: function ( doc ) {
return this.createElement( 'B' ); return createElement( doc, 'B' );
} }
}, },
fontStyle: { fontStyle: {
regexp: /^italic/i, regexp: /^italic/i,
replace: function () { replace: function ( doc ) {
return this.createElement( 'I' ); return createElement( doc, 'I' );
} }
}, },
fontFamily: { fontFamily: {
regexp: notWS, regexp: notWS,
replace: function ( family ) { replace: function ( doc, family ) {
return this.createElement( 'SPAN', { return createElement( doc, 'SPAN', {
'class': 'font', 'class': 'font',
style: 'font-family:' + family style: 'font-family:' + family
}); });
@ -1153,8 +1153,8 @@ var spanToSemantic = {
}, },
fontSize: { fontSize: {
regexp: notWS, regexp: notWS,
replace: function ( size ) { replace: function ( doc, size ) {
return this.createElement( 'SPAN', { return createElement( doc, 'SPAN', {
'class': 'size', 'class': 'size',
style: 'font-size:' + size style: 'font-size:' + size
}); });
@ -1165,13 +1165,14 @@ var spanToSemantic = {
var stylesRewriters = { var stylesRewriters = {
SPAN: function ( span, parent ) { SPAN: function ( span, parent ) {
var style = span.style, var style = span.style,
doc = span.ownerDocument,
attr, converter, css, newTreeBottom, newTreeTop, el; attr, converter, css, newTreeBottom, newTreeTop, el;
for ( attr in spanToSemantic ) { for ( attr in spanToSemantic ) {
converter = spanToSemantic[ attr ]; converter = spanToSemantic[ attr ];
css = style[ attr ]; css = style[ attr ];
if ( css && converter.regexp.test( css ) ) { if ( css && converter.regexp.test( css ) ) {
el = converter.replace( css ); el = converter.replace( doc, css );
if ( newTreeBottom ) { if ( newTreeBottom ) {
newTreeBottom.appendChild( el ); newTreeBottom.appendChild( el );
} }
@ -1190,13 +1191,13 @@ var stylesRewriters = {
return newTreeBottom || span; return newTreeBottom || span;
}, },
STRONG: function ( node, parent ) { STRONG: function ( node, parent ) {
var el = this.createElement( 'B' ); var el = createElement( node.ownerDocument, 'B' );
parent.replaceChild( el, node ); parent.replaceChild( el, node );
el.appendChild( empty( node ) ); el.appendChild( empty( node ) );
return el; return el;
}, },
EM: function ( node, parent ) { EM: function ( node, parent ) {
var el = this.createElement( 'I' ); var el = createElement( node.ownerDocument, 'I' );
parent.replaceChild( el, node ); parent.replaceChild( el, node );
el.appendChild( empty( node ) ); el.appendChild( empty( node ) );
return el; return el;
@ -1204,16 +1205,17 @@ var stylesRewriters = {
FONT: function ( node, parent ) { FONT: function ( node, parent ) {
var face = node.face, var face = node.face,
size = node.size, size = node.size,
doc = node.ownerDocument,
fontSpan, sizeSpan, fontSpan, sizeSpan,
newTreeBottom, newTreeTop; newTreeBottom, newTreeTop;
if ( face ) { if ( face ) {
fontSpan = this.createElement( 'SPAN', { fontSpan = createElement( doc, 'SPAN', {
'class': 'font', 'class': 'font',
style: 'font-family:' + face style: 'font-family:' + face
}); });
} }
if ( size ) { if ( size ) {
sizeSpan = this.createElement( 'SPAN', { sizeSpan = createElement( doc, 'SPAN', {
'class': 'size', 'class': 'size',
style: 'font-size:' + fontSizes[ size ] + 'px' style: 'font-size:' + fontSizes[ size ] + 'px'
}); });
@ -1221,14 +1223,14 @@ var stylesRewriters = {
fontSpan.appendChild( sizeSpan ); fontSpan.appendChild( sizeSpan );
} }
} }
newTreeTop = fontSpan || sizeSpan || this.createElement( 'SPAN' ); newTreeTop = fontSpan || sizeSpan || createElement( doc, 'SPAN' );
newTreeBottom = sizeSpan || fontSpan || newTreeTop; newTreeBottom = sizeSpan || fontSpan || newTreeTop;
parent.replaceChild( newTreeTop, node ); parent.replaceChild( newTreeTop, node );
newTreeBottom.appendChild( empty( node ) ); newTreeBottom.appendChild( empty( node ) );
return newTreeBottom; return newTreeBottom;
}, },
TT: function ( node, parent ) { TT: function ( node, parent ) {
var el = this.createElement( 'SPAN', { var el = createElement( node.ownerDocument, 'SPAN', {
'class': 'font', 'class': 'font',
style: 'font-family:menlo,consolas,"courier new",monospace' style: 'font-family:menlo,consolas,"courier new",monospace'
}); });
@ -1299,18 +1301,19 @@ var cleanTree = function ( node, allowStyles ) {
var wrapTopLevelInline = function ( root, tag ) { var wrapTopLevelInline = function ( root, tag ) {
var children = root.childNodes, var children = root.childNodes,
doc = root.ownerDocument,
wrapper = null, wrapper = null,
i, l, child, isBR; i, l, child, isBR;
for ( i = 0, l = children.length; i < l; i += 1 ) { for ( i = 0, l = children.length; i < l; i += 1 ) {
child = children[i]; child = children[i];
isBR = child.nodeName === 'BR'; isBR = child.nodeName === 'BR';
if ( !isBR && isInline( child ) ) { if ( !isBR && isInline( child ) ) {
if ( !wrapper ) { wrapper = this.createElement( tag ); } if ( !wrapper ) { wrapper = createElement( doc, tag ); }
wrapper.appendChild( child ); wrapper.appendChild( child );
i -= 1; i -= 1;
l -= 1; l -= 1;
} else if ( isBR || wrapper ) { } else if ( isBR || wrapper ) {
if ( !wrapper ) { wrapper = this.createElement( tag ); } if ( !wrapper ) { wrapper = createElement( doc, tag ); }
fixCursor( wrapper ); fixCursor( wrapper );
if ( isBR ) { if ( isBR ) {
root.replaceChild( wrapper, child ); root.replaceChild( wrapper, child );