Skip to content

Escaped characters inside <noscript> on Safari #107

Description

@hacknug

I've encountered a weird issue on Safari where special characters inside a <noscript> tag get escaped when using swup (first load works fine).

The issue seems to be related to how .innerHTML works and I was able to work around it using .innerText instead (.textContent seems to work too). This is most probably the line causing the issue: https://github.com/gmrchk/swup/blob/master/src/modules/getDataFromHtml.js#L6

I was trying to do some custom layout with elements inside this <noscript> when JavaScript is available, which will fallback to a simple flexbox-wrapped grid layout.

Here's the code I wrote that solves the issue. Note the use of a helper function named decodeHTML that helps getting those escaped characters unescaped again.

var getMasonryItems = function (container, selector) {
  var parser = new DOMParser()
  var parsedHtml = parser.parseFromString(decodeHTML(container.innerText), 'text/html')
  // Both .innerText and .textContent seem to work. Don't use .innerHTML !!!

  return parsedHtml.querySelectorAll(selector)
}
var decodeHTML = function (html) {
  var textarea = document.createElement('textarea')
  textarea.innerHTML = html
  return textarea.value
}

var items = getMasonryItems(document.querySelector('noscript'), '[data-item]')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions