Skip to content

Commit

Permalink
Edit Post: Derive all AdminNotices upgraded notice children
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Nov 16, 2018
1 parent f9b44c5 commit 234b58b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
21 changes: 9 additions & 12 deletions packages/edit-post/src/components/admin-notices/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,17 @@ function getAdminNotices() {
}

/**
* Given an admin notice Element, returns the element from which content should
* be sourced.
* Given an admin notice Element, returns the relevant notice content HTML.
*
* @param {Element} element Admin notice element.
*
* @return {Element} Upgraded notice HTML.
*/
function getNoticeContentSourceFromElement( element ) {
if ( element.firstElementChild && element.firstElementChild.nodeName === 'P' ) {
return element.firstElementChild;
}

return element;
function getNoticeHTMLFromElement( element ) {
return [ ...element.childNodes ].filter( ( child ) => (
child.nodeType !== window.Node.ELEMENT_NODE ||
! child.classList.contains( 'notice-dismiss' )
) ).map( ( child ) => child.outerHTML ).join( '' );
}

/**
Expand Down Expand Up @@ -69,9 +67,8 @@ function getNoticeStatusFromClassList( element ) {
*/
function getNoticeFromElement( element ) {
const status = getNoticeStatusFromClassList( element );
const contentSource = getNoticeContentSourceFromElement( element );
const __unstableHTML = contentSource.innerHTML.trim();
const content = contentSource.textContent.trim();
const content = '';
const __unstableHTML = getNoticeHTMLFromElement( element );
const isDismissible = element.classList.contains( 'is-dismissible' );

return { status, content, __unstableHTML, isDismissible };
Expand All @@ -87,7 +84,7 @@ export class AdminNotices extends Component {
getAdminNotices().forEach( ( element ) => {
// Convert and create.
const notice = getNoticeFromElement( element );
createNotice( notice );
createNotice( notice, { speak: false } );

// Remove (now-redundant) admin notice element.
element.parentNode.removeChild( element );
Expand Down
11 changes: 5 additions & 6 deletions packages/edit-post/src/components/admin-notices/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ describe( 'AdminNotices', () => {
// outputs of (a) non-element first child of the element (whitespace
// text node) and (b) untrimmed content.
document.body.innerHTML = `
<p>
My <strong>notice</strong> text
</p>
<div class="notice updated is-dismissible">
<p>My <strong>notice</strong> text</p>
<p>My second line of text</p>
<button type="button" class="notice-dismiss">
<span class="screen-reader-text">Dismiss this notice.</span>
</button>
Expand All @@ -32,10 +31,10 @@ describe( 'AdminNotices', () => {

expect( createNotice ).toHaveBeenCalledWith( {
status: 'success',
content: '',
__unstableHTML: '<p>My <strong>notice</strong> text</p><p>My second line of text</p>',
isDismissible: true,
content: 'My notice text',
__unstableHTML: 'My <strong>notice</strong> text',
} );
}, { speak: false } );
expect( document.body.childElementCount ).toBe( 0 );
} );
} );

0 comments on commit 234b58b

Please sign in to comment.