Generated with the help of an LLM.
Problem/Motivation
Drupal.behaviors.editorStyleFix, added in #3274635, filters document.styleSheets with sheet.ownerNode.hasAttribute('data-cke'). StyleSheet.ownerNode is nullable in the CSSOM spec, and when a sheet has no owner node the behavior throws:
TypeError: Cannot read properties of null (reading 'hasAttribute')
at core/modules/ckeditor5/js/ckeditor5.js:645:44
at Array.filter (<anonymous>)
at Object.attach (core/modules/ckeditor5/js/ckeditor5.js:645:10)
at core/misc/drupal.js:166:24
at Array.forEach (<anonymous>)
at Drupal.attachBehaviors (core/misc/drupal.js:162:34)
at HTMLDivElement.<anonymous> (core/misc/ajax.js:1409:18)
at Drupal.AjaxCommands.insert (core/misc/ajax.js:1401:19)
The behavior runs on every Drupal.attachBehaviors() call, so any AJAX request on a page that loads ckeditor5.js can hit it. We see it intermittently on 11.4 in end-to-end tests, when a Paragraphs "add" button inserts a paragraph into a node form with CKEditor 5 fields. We have not found out which stylesheet has the null ownerNode.
Steps to reproduce
It is not reliably reproducible by hand. To see the error, run this in the browser console on a page with a CKEditor 5 field, e.g. /node/add/article:
const sheets = [...document.styleSheets, { ownerNode: null, cssRules: [] }];
Object.defineProperty(document, 'styleSheets', { configurable: true, get: () => sheets });
Drupal.behaviors.editorStyleFix.attach(document);
Proposed resolution
Skip sheets without an owner node. Such a sheet cannot be one of CKEditor's <style data-cke> tags.
- sheet.ownerNode.hasAttribute('data-cke') ||
+ sheet.ownerNode?.hasAttribute('data-cke') ||
Remaining tasks
MR, review.
User interface changes
None.
Introduced terminology
None.
API changes
None.
Data model changes
None.
Comments