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

Framework: Optimize decodeEntities to skip unnecessary decoding #9725

Merged
merged 2 commits into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 2 additions & 3 deletions client/lib/formatting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import stripTags from 'striptags';
/**
* Internal Dependencies
*/
import warn from 'lib/warn';
import decode from './decode-entities';

function decodeEntities( text ) {
if ( text === undefined || text === false || text === null ) {
warn( 'Don\'t call `decodeEntities` with an `undefined`, `false`, or `null` value.' );
// Bypass decode if text doesn't include entities
if ( 'string' !== typeof text || -1 === text.indexOf( '&' ) ) {
return text;
}

Expand Down
34 changes: 19 additions & 15 deletions client/state/document-head/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { compact } from 'lodash';
/**
* Internal dependencies
*/
import createSelector from 'lib/create-selector';
import { decodeEntities } from 'lib/formatting';
import { getSelectedSiteId, isSiteSection } from 'state/ui/selectors';
import { getSiteTitle } from 'state/sites/selectors';
Expand Down Expand Up @@ -59,25 +60,28 @@ export function getDocumentHeadCappedUnreadCount( state ) {
* @param {Object} state Global state tree
* @return {String} Formatted title
*/
export function getDocumentHeadFormattedTitle( state ) {
let title = '';
export const getDocumentHeadFormattedTitle = createSelector(
( state ) => {
let title = '';

const unreadCount = getDocumentHeadCappedUnreadCount( state );
if ( unreadCount ) {
title += `(${ unreadCount }) `;
}
const unreadCount = getDocumentHeadCappedUnreadCount( state );
if ( unreadCount ) {
title += `(${ unreadCount }) `;
}

title += compact( [
getDocumentHeadTitle( state ),
isSiteSection( state ) && getSiteTitle( state, getSelectedSiteId( state ) )
] ).join( ' ‹ ' );
title += compact( [
getDocumentHeadTitle( state ),
isSiteSection( state ) && getSiteTitle( state, getSelectedSiteId( state ) )
] ).join( ' ‹ ' );

if ( title ) {
title = decodeEntities( title ) + ' — ';
}
if ( title ) {
title = decodeEntities( title ) + ' — ';
}

return title + 'WordPress.com';
}
return title + 'WordPress.com';
},
( state ) => state.documentHead
);

/**
* Returns an array of document meta objects as set by the DocumentHead
Expand Down
4 changes: 4 additions & 0 deletions client/state/document-head/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import {
} from '../selectors';

describe( 'selectors', () => {
beforeEach( () => {
getDocumentHeadFormattedTitle.memoizedSelector.cache.clear();
} );

describe( '#getDocumentHeadTitle()', () => {
it( 'should return the currently set title', () => {
const title = getDocumentHeadTitle( {
Expand Down