WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 4 years ago

#32300 closed defect (bug) (fixed)

twemoji.js - validate data before passing to twemoji.parse

Reported by: peterwilsoncc Owned by: azaozz
Milestone: 4.3 Priority: normal
Severity: normal Version: 4.2
Component: Formatting Keywords:
Focuses: javascript Cc:
PR Number:

Description

Per #32109 passing an null or undefined node to twemoji for parsing throws a fatal JavaScript error.

I propose validating the data before attempting to emojify it. grabAllTextNodes assumes the presence of node.childNodes, so it's worth checking for that too.

Following a discussion with @azaozz, I'll upload two options:

*.1.diff - checks neither node or node.childNodes are null
*.2.diff - checks neither node or node.textContent are null or undefined

Attachments (4)

32300.1.diff (492 bytes) - added by peterwilsoncc 5 years ago.
validate node && node.childNodes
32300.2.diff (494 bytes) - added by peterwilsoncc 5 years ago.
validate node && node.textContent
32300.3.diff (482 bytes) - added by peterwilsoncc 5 years ago.
32300.4.diff (555 bytes) - added by azaozz 5 years ago.

Download all attachments as: .zip

Change History (9)

@peterwilsoncc
5 years ago

validate node && node.childNodes

@peterwilsoncc
5 years ago

validate node && node.textContent

#1 @peterwilsoncc
5 years ago

After further discussion with azaozz, was considered best to move the validation to wp.emoji.parse.

It's worth unit testing, but I'm not sure how to go about it. I know it works with the following in the console.

wp.emoji.parse( null );
wp.emoji.parse();
wp.emoji.parse( {} );
wp.emoji.parse( [] );
wp.emoji.parse( "" );
wp.emoji.parse( "

#2 @peterwilsoncc
5 years ago

  • Summary changed from twemoji.js - validate nodes passed to parseNode to twemoji.js - validate data before passing to twemoji.parse

@azaozz
5 years ago

#3 @azaozz
5 years ago

  • Milestone changed from Awaiting Review to 4.3

Looking at 32300.3.diff: for an HTML element node.childNodes is an array. It evaluates to true even when empty. We don't need to call twemoji.parse() in these cases, returning early in 32300.4.diff.

#4 @peterwilsoncc
5 years ago

Thanks Andrew, really vauge moment and forgot that within seconds of talking about it :/

Just noticed trac cuts off emoji and beyond in the unit tests, in full from comment:1

wp.emoji.parse( null );
wp.emoji.parse();
wp.emoji.parse( {} );
wp.emoji.parse( [] );
wp.emoji.parse( "" );
wp.emoji.parse( "\uD83D\uDC3C\uD83C\uDDE8\uD83C\uDDF3" ); //panda, china
wp.emoji.parse( document.body );
wp.emoji.parse( function(){} );
wp.emoji.parse( document.createElement( 'p' ).appendChild( document.createElement( 'strong') ) );
wp.emoji.parse( document.createElement( 'svg' ).appendChild( document.createElement( 'path' ) ) );
wp.emoji.parse( window );

The aim is to just chuck everything at it ensure it passes.

#5 @azaozz
4 years ago

  • Owner set to azaozz
  • Resolution set to fixed
  • Status changed from new to closed

In 32679:

Emoji: make sure we parse only strings or DOM nodes that have children.
Props peterwilsoncc. Fixes #32300.

Note: See TracTickets for help on using tickets.