-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retain text color when pasting #27399
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b1c9e8f
adding span to schema in order to handle text color. Remove backgorun…
abc73fc
adding possibility to paste coloured text
48001b9
adding custom html tag in order to preserve information about text color
7d2825e
fix in destructuring
bbdfa3b
file refactor
7d37efe
sketch - instead of manualy writing schemas, another solution propose…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 66 additions & 47 deletions
113
packages/blocks/src/api/raw-handling/phrasing-content-reducer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,79 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { includes } from 'lodash'; | ||
import { identity, includes } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { wrap, replaceTag } from '@wordpress/dom'; | ||
|
||
const getSemanticTags = ( { node } ) => { | ||
const { | ||
fontWeight, | ||
fontStyle, | ||
textDecorationLine, | ||
textDecoration, | ||
verticalAlign, | ||
} = node.style; | ||
const tags = []; | ||
if ( fontWeight === 'bold' || fontWeight === '700' ) { | ||
tags.push( 'strong' ); | ||
} | ||
|
||
if ( fontStyle === 'italic' ) { | ||
tags.push( 'em' ); | ||
} | ||
|
||
// Some DOM implementations (Safari, JSDom) don't support | ||
// style.textDecorationLine, so we check style.textDecoration as a | ||
// fallback. | ||
if ( | ||
textDecorationLine === 'line-through' || | ||
includes( textDecoration, 'line-through' ) | ||
) { | ||
tags.push( 's' ); | ||
} | ||
|
||
if ( verticalAlign === 'super' ) { | ||
tags.push( 'sup' ); | ||
} else if ( verticalAlign === 'sub' ) { | ||
tags.push( 'sub' ); | ||
} | ||
return tags; | ||
}; | ||
|
||
const handleSpan = ( { node, doc } ) => { | ||
const semanticTagsToWrap = getSemanticTags( node ); | ||
semanticTagsToWrap.forEach( ( tag ) => | ||
wrap( doc.createElement( tag ), node ) | ||
); | ||
}; | ||
const handleBold = ( { node } ) => replaceTag( node, 'strong' ); | ||
const handleItalic = ( { node } ) => replaceTag( node, 'italic' ); | ||
const handleAnchor = ( { node } ) => { | ||
// In jsdom-jscore, 'node.target' can be null. | ||
// TODO: Explore fixing this by patching jsdom-jscore. | ||
if ( node.target && node.target.toLowerCase() === '_blank' ) { | ||
node.rel = 'noreferrer noopener'; | ||
} else { | ||
node.removeAttribute( 'target' ); | ||
node.removeAttribute( 'rel' ); | ||
} | ||
}; | ||
|
||
const elementNameToHandlerMapper = { | ||
span: handleSpan, | ||
B: handleBold, | ||
I: handleItalic, | ||
A: handleAnchor, | ||
}; | ||
|
||
export default function phrasingContentReducer( node, doc ) { | ||
// In jsdom-jscore, 'node.style' can be null. | ||
// TODO: Explore fixing this by patching jsdom-jscore. | ||
if ( node.nodeName === 'SPAN' && node.style ) { | ||
const { | ||
fontWeight, | ||
fontStyle, | ||
textDecorationLine, | ||
textDecoration, | ||
verticalAlign, | ||
} = node.style; | ||
|
||
if ( fontWeight === 'bold' || fontWeight === '700' ) { | ||
wrap( doc.createElement( 'strong' ), node ); | ||
} | ||
|
||
if ( fontStyle === 'italic' ) { | ||
wrap( doc.createElement( 'em' ), node ); | ||
} | ||
|
||
// Some DOM implementations (Safari, JSDom) don't support | ||
// style.textDecorationLine, so we check style.textDecoration as a | ||
// fallback. | ||
if ( | ||
textDecorationLine === 'line-through' || | ||
includes( textDecoration, 'line-through' ) | ||
) { | ||
wrap( doc.createElement( 's' ), node ); | ||
} | ||
|
||
if ( verticalAlign === 'super' ) { | ||
wrap( doc.createElement( 'sup' ), node ); | ||
} else if ( verticalAlign === 'sub' ) { | ||
wrap( doc.createElement( 'sub' ), node ); | ||
} | ||
} else if ( node.nodeName === 'B' ) { | ||
node = replaceTag( node, 'strong' ); | ||
} else if ( node.nodeName === 'I' ) { | ||
node = replaceTag( node, 'em' ); | ||
} else if ( node.nodeName === 'A' ) { | ||
// In jsdom-jscore, 'node.target' can be null. | ||
// TODO: Explore fixing this by patching jsdom-jscore. | ||
if ( node.target && node.target.toLowerCase() === '_blank' ) { | ||
node.rel = 'noreferrer noopener'; | ||
} else { | ||
node.removeAttribute( 'target' ); | ||
node.removeAttribute( 'rel' ); | ||
} | ||
} | ||
const elementType = node.nodeName; | ||
const elementHandler = | ||
elementNameToHandlerMapper[ elementType ] || identity; | ||
elementHandler( { node, doc } ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would walk the tree instead of relying on regexps - they may have unintended consequences.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All manually placed special chars like < have escape characters so this should not be dangerous, nevertheless, indeed tree traversing is just safe, so it is better to approach, thanks.
I am just not convinced with the entire idea - as I've mentioned in the desc - this is not scalable and there would be needed a huge amount of logic to cover all cases (colors are just a tip of an iceberg). I think it will be better to change the mechanics of unfolding letters from their corresponding spans and the way spans are translated to native html tags.