-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[WIP] Add a basic test for shortcode transformation #7689
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { equal } from 'assert'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import segmentHTMLToShortcodeBlock from '../shortcode-converter'; | ||
import { | ||
getBlockTransforms, | ||
} from '../../factory'; | ||
import { | ||
getPhrasingContentSchema, | ||
} from '../utils'; | ||
|
||
describe( 'segmentHTMLToShortcodeBlock', () => { | ||
const blockTypes = [ | ||
{ | ||
name: 'core/paragraph', | ||
transforms: { | ||
from: [ | ||
{ | ||
type: 'raw', | ||
// Paragraph is a fallback and should be matched last. | ||
priority: 20, | ||
selector: 'p', | ||
schema: { | ||
p: { | ||
children: getPhrasingContentSchema(), | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
{ | ||
name: 'core/shortcode', | ||
transforms: { | ||
from: [ | ||
{ | ||
type: 'shortcode', | ||
// Per "Shortcode names should be all lowercase and use all | ||
// letters, but numbers and underscores should work fine too. | ||
// Be wary of using hyphens (dashes), you'll be better off not | ||
// using them." in https://codex.wordpress.org/Shortcode_API | ||
// Require that the first character be a letter. This notably | ||
// prevents footnote markings ([1]) from being caught as | ||
// shortcodes. | ||
tag: '[a-z][a-z0-9_-]*', | ||
attributes: { | ||
text: { | ||
type: 'string', | ||
shortcode: ( attrs, { content } ) => { | ||
return content; | ||
}, | ||
}, | ||
}, | ||
priority: 20, | ||
}, | ||
], | ||
}, | ||
}, | ||
]; | ||
const transformsFrom = getBlockTransforms( 'from', undefined, blockTypes ); | ||
|
||
it( 'should convert a standalone shortcode between two paragraphs', () => { | ||
const original = `<p>Foo</p> | ||
|
||
[foo bar="apple"] | ||
|
||
<p>Bar</p>`; | ||
const expected = [ | ||
`<p>Foo</p> | ||
|
||
`, | ||
{ | ||
attributes: {}, | ||
innerBlocks: [], | ||
isValid: true, | ||
name: 'core/shortcode', | ||
uuid: 'invalid', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not entirely sure it works in this specific usage, but perhaps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, I suppose I could make the assertions a bit more verbose and inspect each part of the array. Another idea is to create a wrapper for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}, | ||
` | ||
|
||
<p>Bar</p>`, | ||
]; | ||
const transformed = segmentHTMLToShortcodeBlock( original, 0, transformsFrom, blockTypes ); | ||
equal( transformed, expected ); | ||
} ); | ||
} ); |
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.
Noting that
getBlockType
isO(1)
(object lookup by property) and this isO(n)