Skip to content
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

Overlay on disabled elements, to catch events and show tooltips #27529

Merged
merged 22 commits into from
Jan 21, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
merge
  • Loading branch information
grzegorz_marzencki committed Jan 13, 2021
commit 6d37251108469577cb524f312bff9be22f764c09
134 changes: 5 additions & 129 deletions packages/blocks/src/api/raw-handling/paste-handler.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
/**
* External dependencies
*/
import { flatMap, filter, compact, identity, difference } from 'lodash';
import { flatMap, compact } from 'lodash';

/**
* WordPress dependencies
*/
import {
getPhrasingContentSchema,
alterSpecialTags,
removeInvalidHTML,
} from '@wordpress/dom';
import { getPhrasingContentSchema, removeInvalidHTML } from '@wordpress/dom';

/**
* Internal dependencies
@@ -44,67 +40,6 @@ import emptyParagraphRemover from './empty-paragraph-remover';
*/
const { console } = window;

const getNonDefaultStyles = ( { defaultStylesEntries, stylesEntries } ) =>
stylesEntries.reduce( ( acc, [ key, values ] ) => {
const defaultValues = ( defaultStylesEntries.find(
( [ defaultKey ] ) => defaultKey === key
) || [] )[ 1 ];
const diff = difference( values, defaultValues );
const nonDefaultStyles = diff.length
? {
[ key ]: diff,
}
: {};
return {
...acc,
...nonDefaultStyles,
};
}, {} );

const getStylesEntries = ( stylesString ) => {
const stylesArr = stylesString
.replace( /\(.*?\)/g, ( match ) => match.replace( / /g, '_' ) )
.split( '"' )
.join( `'` )
.split( ';' )
.filter( ( x ) => x );

console.log( stylesArr );
return stylesArr
.map( ( styles ) => {
const [ key, values = [] ] = styles.trim().split( ':' );
console.log( values );
if ( key === 'color' ) return [ key, [ values ] ];
const valuesArr = values.split( ', ' ).map( ( w ) => w.trim() );
return [ key, valuesArr ];
} )
.replace( /\(.*?\)/g, ( match ) => match.replace( /_/g, '' ) );
};
const defaultStyles =
"color: rgb(40, 48, 61); font-family: -apple-system, system-ui, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif; font-size: 20px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(209, 228, 221); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;";

const fromObjToString = ( obj ) =>
Object.entries( obj )
.map( ( pair ) => pair.join( ':' ) )
.join( '; ' );

const sanitize = ( x ) => x;
const removeDefaultStyles = ( html ) => {
const container = document.createElement( 'div' );
container.innerHTML = html;
const elementStyles = container.firstElementChild.style.cssText;
const defaultStylesEntries = getStylesEntries( defaultStyles );
const stylesEntries = getStylesEntries( elementStyles );
const diff = getNonDefaultStyles( {
defaultStylesEntries,
stylesEntries,
} );
const nonDefaultStyles = fromObjToString( diff );
for ( const child of container.children ) {
child.style = nonDefaultStyles;
}
return container.children.map( ( child ) => child.value );
};
/**
* Filters HTML to only contain phrasing content.
*
@@ -114,17 +49,14 @@ const removeDefaultStyles = ( html ) => {
* @return {string} HTML only containing phrasing content.
*/
function filterInlineHTML( HTML, preserveWhiteSpace ) {
HTML = sanitize( removeDefaultStyles( HTML ) );
HTML = deepFilterHTML( HTML, [
googleDocsUIDRemover,
phrasingContentReducer,
commentRemover,
] );
// HTML = removeInvalidHTML( HTML, getPhrasingContentSchema( 'paste' ), {
// inline: true,
// } );

HTML = alterSpecialTags( HTML );
HTML = removeInvalidHTML( HTML, getPhrasingContentSchema( 'paste' ), {
inline: true,
} );

if ( ! preserveWhiteSpace ) {
HTML = deepFilterHTML( HTML, [ htmlFormattingRemover, brRemover ] );
@@ -136,62 +68,6 @@ function filterInlineHTML( HTML, preserveWhiteSpace ) {
return HTML;
}

function getRawTransformations() {
return filter( getBlockTransforms( 'from' ), { type: 'raw' } ).map(
( transform ) => {
return transform.isMatch
? transform
: {
...transform,
isMatch: ( node ) =>
transform.selector &&
node.matches( transform.selector ),
};
}
);
}

/**
* Converts HTML directly to blocks. Looks for a matching transform for each
* top-level tag. The HTML should be filtered to not have any text between
* top-level tags and formatted in a way that blocks can handle the HTML.
*
* @param {Object} $1 Named parameters.
* @param {string} $1.html HTML to convert.
* @param {Array} $1.rawTransforms Transforms that can be used.
*
* @return {Array} An array of blocks.
*/
function htmlToBlocks( { html, rawTransforms } ) {
const doc = document.implementation.createHTMLDocument( '' );

doc.body.innerHTML = html;

return Array.from( doc.body.children ).map( ( node ) => {
const rawTransform = findTransform( rawTransforms, ( { isMatch } ) =>
isMatch( node )
);

if ( ! rawTransform ) {
return createBlock(
// Should not be hardcoded.
'core/html',
getBlockAttributes( 'core/html', node.outerHTML )
);
}

const { transform, blockName } = rawTransform;

if ( transform ) {
return transform( node );
}
return createBlock(
blockName,
getBlockAttributes( blockName, node.outerHTML )
);
} );
}

/**
* Converts an HTML string to known blocks. Strips everything else.
*
36 changes: 21 additions & 15 deletions packages/shortcode/src/index.js
Original file line number Diff line number Diff line change
@@ -90,23 +90,29 @@ export function next( tag, text, index = 0 ) {
* @return {string} Text with shortcodes replaced.
*/
export function replace( tag, text, callback ) {
return text.replace(
regexp( tag ),
function ( match, left, $3, attrs, slash, content, closing, right ) {
// If both extra brackets exist, the shortcode has been properly
// escaped.
if ( left === '[' && right === ']' ) {
return match;
}
return text.replace( regexp( tag ), function (
match,
left,
$3,
attrs,
slash,
content,
closing,
right
) {
// If both extra brackets exist, the shortcode has been properly
// escaped.
if ( left === '[' && right === ']' ) {
return match;
}

// Create the match object and pass it through the callback.
const result = callback( fromMatch( arguments ) );
// Create the match object and pass it through the callback.
const result = callback( fromMatch( arguments ) );

// Make sure to return any of the extra brackets if they weren't used to
// escape the shortcode.
return result || result === '' ? left + result + right : match;
}
);
// Make sure to return any of the extra brackets if they weren't used to
// escape the shortcode.
return result || result === '' ? left + result + right : match;
} );
}

/**
You are viewing a condensed version of this merge commit. You can view the full changes here.