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

Rich Text: Apply attributes properly #15070

Merged
merged 10 commits into from
Apr 24, 2019
36 changes: 36 additions & 0 deletions packages/rich-text/src/test/to-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,42 @@ describe( 'applyValue', () => {
movedCount: 0,
description: 'should not modify',
},
{
current: '<span data-1="">b</span>',
future: '<span>b</span>',
movedCount: 0,
description: 'should remove attribute',
},
{
current: '<span data-1="" data-2="">b</span>',
future: '<span>b</span>',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case anyone was wondering, this test case would fail in master. Added a comment: https://github.com/WordPress/gutenberg/pull/15070/files#diff-829f9f4e942450ee8c33f082c016b173R212.

movedCount: 0,
description: 'should remove attributes',
},
{
current: '<span>a</span>',
future: '<span data-1="">c</span>',
movedCount: 0,
description: 'should add attribute',
},
{
current: '<span>a</span>',
future: '<span data-1="" data-2="">c</span>',
movedCount: 0,
description: 'should add attributes',
},
{
current: '<span data-1="i">a</span>',
future: '<span data-1="ii">a</span>',
movedCount: 0,
description: 'should update attribute',
},
{
current: '<span data-1="i" data-2="ii">a</span>',
future: '<span data-1="ii" data-2="i">a</span>',
movedCount: 0,
description: 'should update attributes',
},
];

cases.forEach( ( { current, future, description, movedCount } ) => {
Expand Down
6 changes: 5 additions & 1 deletion packages/rich-text/src/to-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ export function applyValue( future, current ) {
const futureAttributes = futureChild.attributes;

if ( currentAttributes ) {
for ( let ii = 0; ii < currentAttributes.length; ii++ ) {
let ii = currentAttributes.length;

// Reverse loop because `removeAttribute` on `currentChild`
// changes `currentAttributes`.
while ( ii-- ) {
const { name } = currentAttributes[ ii ];

if ( ! futureChild.getAttribute( name ) ) {
Expand Down