@@ -245,30 +245,32 @@ export class Accordion extends ConfigurableComponent {
245245 */
246246 createShowHideToggle ( ) {
247247 // Create an inner container to limit the width of the focus state
248- const $showHideToggleFocus = createElement ( 'span' , {
249- class : 'govuk-accordion__section-toggle-focus'
250- } )
251-
252- $showHideToggleFocus . appendChild (
253- createElement ( 'span' , {
254- class : iconClass
255- } )
256- )
257- $showHideToggleFocus . appendChild (
258- createElement ( 'span' , {
259- class : sectionToggleTextClass
260- } )
248+ const $showHideToggleFocus = createElement (
249+ 'span' ,
250+ {
251+ class : 'govuk-accordion__section-toggle-focus'
252+ } ,
253+ [
254+ createElement ( 'span' , {
255+ class : iconClass
256+ } ) ,
257+ createElement ( 'span' , {
258+ class : sectionToggleTextClass
259+ } )
260+ ]
261261 )
262262
263- const $showHideToggle = createElement ( 'span' , {
264- class : 'govuk-accordion__section-toggle' ,
265- // Tell Google not to index the 'show' text as part of the heading. Must be
266- // set on the element before it's added to the DOM.
267- // See https://developers.google.com/search/docs/advanced/robots/robots_meta_tag#data-nosnippet-attr
268- 'data-nosnippet' : ''
269- } )
270-
271- $showHideToggle . appendChild ( $showHideToggleFocus )
263+ const $showHideToggle = createElement (
264+ 'span' ,
265+ {
266+ class : 'govuk-accordion__section-toggle' ,
267+ // Tell Google not to index the 'show' text as part of the heading. Must be
268+ // set on the element before it's added to the DOM.
269+ // See https://developers.google.com/search/docs/advanced/robots/robots_meta_tag#data-nosnippet-attr
270+ 'data-nosnippet' : ''
271+ } ,
272+ [ $showHideToggleFocus ]
273+ )
272274
273275 return $showHideToggle
274276 }
@@ -282,23 +284,25 @@ export class Accordion extends ConfigurableComponent {
282284 createHeadingText ( $span ) {
283285 // Create an inner heading text container to limit the width of the focus
284286 // state
285- const $headingTextFocus = createElement ( 'span' , {
286- class : 'govuk-accordion__section-heading-text-focus'
287- } )
288-
289- // span could contain HTML elements which need moving to the new span
290- // (see https://www.w3.org/TR/2011/WD-html5-20110525/content-models.html#phrasing-content)
291- Array . from ( $span . childNodes ) . forEach ( ( $child ) =>
292- $headingTextFocus . appendChild ( $child )
287+ const $headingTextFocus = createElement (
288+ 'span' ,
289+ {
290+ class : 'govuk-accordion__section-heading-text-focus'
291+ } ,
292+ // span could contain HTML elements which need moving to the new span
293+ // (see https://www.w3.org/TR/2011/WD-html5-20110525/content-models.html#phrasing-content)
294+ Array . from ( $span . childNodes )
293295 )
294296
295297 // Create container for heading text so it can be styled
296- const $headingText = createElement ( 'span' , {
297- class : sectionHeadingTextClass ,
298- id : $span . id
299- } )
300-
301- $headingText . appendChild ( $headingTextFocus )
298+ const $headingText = createElement (
299+ 'span' ,
300+ {
301+ class : sectionHeadingTextClass ,
302+ id : $span . id
303+ } ,
304+ [ $headingTextFocus ]
305+ )
302306
303307 return $headingText
304308 }
@@ -314,25 +318,25 @@ export class Accordion extends ConfigurableComponent {
314318 * @returns {HTMLSpanElement } - The `<span>` element containing the summary
315319 */
316320 createSummarySpan ( $summary ) {
317- // Create an inner summary container to limit the width of the summary
318- // focus state
319- const $summarySpanFocus = createElement ( 'span' , {
320- class : 'govuk-accordion__section-summary-focus'
321- } )
322-
323- const $summarySpan = createElement ( 'span' )
324-
325- $summarySpan . appendChild ( $summarySpanFocus )
321+ const $summarySpan = createElement (
322+ 'span' ,
323+ { } ,
324+ Array . from ( $summary . childNodes )
325+ )
326326
327327 // Get original attributes, and pass them to the replacement
328328 for ( const attr of Array . from ( $summary . attributes ) ) {
329329 $summarySpan . setAttribute ( attr . name , attr . value )
330330 }
331331
332- // Copy original contents of summary to the new summary span
333- Array . from ( $summary . childNodes ) . forEach ( ( $child ) =>
334- $summarySpanFocus . appendChild ( $child )
335- )
332+ // Create an inner summary container to limit the width of the summary
333+ // focus state
334+ const $summarySpanFocus = createElement ( 'span' , {
335+ class : 'govuk-accordion__section-summary-focus'
336+ } )
337+
338+ // As we can't use
339+ $summarySpan . insertAdjacentElement ( 'afterbegin' , $summarySpanFocus )
336340
337341 return $summarySpan
338342 }
0 commit comments