-
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
Parser: Remove specific support for <!--more--> tag #5061
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,73 @@ | ||
/** | ||
* Browser dependencies | ||
*/ | ||
const { COMMENT_NODE } = window.Node; | ||
|
||
/** | ||
* Looks for `<!--more-->` comments, as well as the `<!--more Some text-->` | ||
* variant and its `<!--noteaser-->` companion, and replaces them with a custom | ||
* element representing a future block. | ||
* | ||
* The custom element is a way to bypass the rest of the `raw-handling` | ||
* transforms, which would eliminate other kinds of node with which to carry | ||
* `<!--more-->`'s data: nodes with `data` attributes, empty paragraphs, etc. | ||
* | ||
* The custom element is then expected to be recognized by any registered | ||
* block's `raw` transform. | ||
* | ||
* @param {Node} node The node to be processed. | ||
* @return {void} | ||
*/ | ||
export default function( node ) { | ||
if ( | ||
node.nodeType !== COMMENT_NODE || | ||
node.nodeValue.indexOf( 'more' ) !== 0 | ||
) { | ||
// We don't specificially look for `noteaser`, meaning that if one is | ||
// found on its own (and not adjacent to `more`), it will be lost. | ||
return; | ||
} | ||
|
||
// Grab any custom text in the comment | ||
const customText = node.nodeValue.slice( 4 ).trim(); | ||
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. Amusing side note: this fixes a bug in 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. Oh, I missed the previous discussion wherein @dmsnell learned yet another wonderful weird thing about Core. 🙃 |
||
|
||
// When a `<!--more-->` comment is found, we need to look for any | ||
// `<!--noteaser-->` sibling, but it may not be a direct sibling | ||
// (whitespace typically lies in between) | ||
let sibling = node; | ||
let noTeaser = false; | ||
while ( ( sibling = sibling.nextSibling ) ) { | ||
if ( | ||
sibling.nodeType === COMMENT_NODE && | ||
sibling.nodeValue === 'noteaser' | ||
) { | ||
noTeaser = true; | ||
sibling.parentNode.removeChild( sibling ); | ||
break; | ||
} | ||
} | ||
|
||
// Conjure up a custom More element | ||
const more = createMore( customText, noTeaser ); | ||
|
||
// Append it to the top level for later conversion to blocks | ||
let parent = node.parentNode; | ||
while ( parent.nodeName !== 'BODY' ) { | ||
parent = parent.parentNode; | ||
} | ||
parent.appendChild( more ); | ||
node.parentNode.removeChild( node ); | ||
} | ||
|
||
function createMore( customText, noTeaser ) { | ||
const node = document.createElement( 'wp-block' ); | ||
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. Interesting thing :) I wonder if we can also do this for shortcakes... Might enable us to get rid of the "pieces" of HTML and parse it all in one go. |
||
node.dataset.block = 'core/more'; | ||
if ( customText ) { | ||
node.dataset.customText = customText; | ||
} | ||
if ( noTeaser ) { | ||
// "Boolean" data attribute | ||
node.dataset.noTeaser = ''; | ||
} | ||
return node; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { equal } from 'assert'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import specialCommentConverter from '../special-comment-converter'; | ||
import { deepFilterHTML } from '../utils'; | ||
|
||
describe( 'specialCommentConverter', () => { | ||
it( 'should convert a single comment into a basic block', () => { | ||
equal( | ||
deepFilterHTML( '<!--more-->', [ specialCommentConverter ] ), | ||
'<wp-block data-block="core/more"></wp-block>' | ||
); | ||
} ); | ||
it( 'should convert two comments into a block', () => { | ||
equal( | ||
deepFilterHTML( '<!--more--><!--noteaser-->', [ specialCommentConverter ] ), | ||
'<wp-block data-block="core/more" data-no-teaser=""></wp-block>' | ||
); | ||
} ); | ||
it( 'should pass custom text to the block', () => { | ||
equal( | ||
deepFilterHTML( | ||
'<!--more Read all about it!--><!--noteaser-->', | ||
[ specialCommentConverter ] | ||
), | ||
'<wp-block data-block="core/more" data-custom-text="Read all about it!" data-no-teaser=""></wp-block>' | ||
); | ||
} ); | ||
it( 'should handle reformatted content', () => { | ||
const output = deepFilterHTML( | ||
`<p> | ||
<!--more--> | ||
<!--noteaser--> | ||
</p>`, | ||
[ specialCommentConverter ] | ||
); | ||
// Skip the empty paragraph, which other transforms would eliminate | ||
const start = output.indexOf( '</p>' ) + '</p>'.length; | ||
equal( | ||
output.substr( start ), | ||
'<wp-block data-block="core/more" data-no-teaser=""></wp-block>' | ||
); | ||
} ); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
<!-- wp:core/more --> | ||
<!--more--> | ||
<!-- /wp:core/more --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,6 @@ | |
"noTeaser": false | ||
}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
"originalContent": "<!--more-->" | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
<!-- wp:more --> | ||
<!--more--> | ||
<!-- /wp:more --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
<!-- wp:core/more {"customText":"Continue Reading","noTeaser":true} --> | ||
<!--more Continue Reading--> | ||
<!--noteaser--> | ||
<!-- /wp:core/more --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
<!-- wp:more {"customText":"Continue Reading","noTeaser":true} --> | ||
<!--more Continue Reading--> | ||
<!--noteaser--> | ||
<!-- /wp:more --> |
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.
Do you think we should unit test this or is it already covered elsewhere?
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 think some tests are in order 👍