-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5061 from WordPress/remove/parser-more-and-noteas…
…er-exception Parser: Remove specific support for <!--more--> tag
- Loading branch information
Showing
16 changed files
with
288 additions
and
494 deletions.
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
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
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(); | ||
|
||
// 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' ); | ||
node.dataset.block = 'core/more'; | ||
if ( customText ) { | ||
node.dataset.customText = customText; | ||
} | ||
if ( noTeaser ) { | ||
// "Boolean" data attribute | ||
node.dataset.noTeaser = ''; | ||
} | ||
return node; | ||
} |
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 |
---|---|---|
@@ -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>' | ||
); | ||
} ); | ||
} ); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
<!-- wp:core/more --> | ||
<!--more--> | ||
<!-- /wp:core/more --> |
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 |
---|---|---|
|
@@ -7,6 +7,6 @@ | |
"noTeaser": false | ||
}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
"originalContent": "<!--more-->" | ||
} | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
<!-- wp:more --> | ||
<!--more--> | ||
<!-- /wp:more --> |
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,2 +1,4 @@ | ||
<!-- wp:core/more {"customText":"Continue Reading","noTeaser":true} --> | ||
<!--more Continue Reading--> | ||
<!--noteaser--> | ||
<!-- /wp:core/more --> |
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
2 changes: 2 additions & 0 deletions
2
blocks/test/fixtures/core__more__custom-text-teaser.serialized.html
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,2 +1,4 @@ | ||
<!-- wp:more {"customText":"Continue Reading","noTeaser":true} --> | ||
<!--more Continue Reading--> | ||
<!--noteaser--> | ||
<!-- /wp:more --> |
Oops, something went wrong.