From c0a68005d40b16268b7f7c657f211a05cef74572 Mon Sep 17 00:00:00 2001 From: daboe01 Date: Fri, 16 Sep 2016 23:09:54 +0200 Subject: [PATCH] fixed: native rich pasting was not working robustly enough in chrome --- AppKit/CPTextView/CPTextView.j | 48 +++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/AppKit/CPTextView/CPTextView.j b/AppKit/CPTextView/CPTextView.j index fab161057..867a743ae 100644 --- a/AppKit/CPTextView/CPTextView.j +++ b/AppKit/CPTextView/CPTextView.j @@ -2427,7 +2427,7 @@ var _CPCopyPlaceholder = '-'; { if (node.nodeType === 1 && node.nodeName === 'SPAN') { - var text = node.innerText, + var text = node.innerHTML, style = window.getComputedStyle(node), styleAttributes = @{}; @@ -2441,16 +2441,12 @@ var _CPCopyPlaceholder = '-'; // extract font from the DOM var fontname = style.getPropertyValue('font-family'), - fontsize = parseInt(style.getPropertyValue('font-size'), 10), - isBold = node.firstChild && node.firstChild.nodeName === 'B' || node.firstChild.firstChild && node.firstChild.firstChild.nodeName === 'B', - isItalic = node.firstChild && node.firstChild.nodeName === 'I' || node.firstChild.firstChild && node.firstChild.firstChild.nodeName === 'I'; + fontsize = parseInt(style.getPropertyValue('font-size'), 10); if (fontname && fontsize) - [styleAttributes setObject:isBold? [CPFont boldFontWithName:fontname size:fontsize italic:isItalic] : - [CPFont fontWithName:fontname size:fontsize italic:isItalic] - forKey:CPFontAttributeName]; + [styleAttributes setObject:[CPFont fontWithName:fontname size:fontsize italic:NO] forKey:CPFontAttributeName]; - [rtfdata appendAttributedString:[[CPAttributedString alloc] initWithString:text attributes:styleAttributes]]; + [rtfdata appendAttributedString:[[[CPAttributedString alloc] initWithString:text attributes:styleAttributes] _stringByParsingHTMLEntities]]; } }; @@ -2635,4 +2631,38 @@ var _CPCopyPlaceholder = '-'; } -@end \ No newline at end of file +@end + +@implementation CPAttributedString(_MinimalHTMLParser) + +-(void) _setRegularExpression:(JSObject)re toFontTrait:(CPFontTrait)aTrait +{ + while (match = re.exec(_string)) + { + var attribs = [[self attributesAtIndex:match.index effectiveRange:nil] copy], + font = [attribs objectForKey:CPFontAttributeName]; + [attribs setObject:[[CPFontManager sharedFontManager] convertFont:font toHaveTrait:aTrait] forKey:CPFontAttributeName] + [self setAttributes:attribs range:CPMakeRange(match.index, match[0].length)]; + } +} + +-(void) _replaceEveryOccurenceOfRegularExpression:(JSObject)re withString:(CPString)aString +{ + while (match = re.exec(_string)) + [self replaceCharactersInRange:CPMakeRange(match.index, match[0].length) withString:aString]; +} + + +-(CPAttributedString) _stringByParsingHTMLEntities +{ + [self _setRegularExpression:/(.+?)<\/b>/gi toFontTrait:CPFontBoldTrait]; + [self _setRegularExpression:/(.+?)<\/i>/gi toFontTrait:CPFontItalicTrait]; + [self _replaceEveryOccurenceOfRegularExpression:/<[^>]+>/i withString:'']; + [self _replaceEveryOccurenceOfRegularExpression:/</i withString:'<']; + [self _replaceEveryOccurenceOfRegularExpression:/>/i withString:'>']; + [self _replaceEveryOccurenceOfRegularExpression:/&/i withString:'&']; + + return self; +} + +@end