Unverified Commit c480c37c authored by Lachlan Campbell's avatar Lachlan Campbell Committed by GitHub

Skip undefined attribute in Head (#9856)

* Fix #8655, skip rendering meta tags with undefined props

* Filter all tags, not just meta

* Only render defined props

* Remove filtering of undefined strings
Co-Authored-By: 's avatarTim Neutkens <tim@timneutkens.nl>

* Replace Object.entries

* Remove filtering code

* Simplify code

* Add test

* Add tests for undefined head prop value and tweak check

* Update to strip undefined prop values to match react

* Update head.js
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
Co-authored-by: 's avatarJoe Haddad <timer150@gmail.com>
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 9a8c3100
......@@ -95,6 +95,9 @@ function reactElementToDOM({ type, props }) {
if (!props.hasOwnProperty(p)) continue
if (p === 'children' || p === 'dangerouslySetInnerHTML') continue
// we don't render undefined props to the DOM
if (props[p] === undefined) continue
const attr = DOMAttributeNames[p] || p.toLowerCase()
el.setAttribute(attr, props[p])
}
......
......@@ -16,6 +16,9 @@ export default () => (
<meta content="my meta" />
{/* this will not render the content prop */}
<meta name="empty-content" content={undefined} />
{/* allow duplicates for specific tags */}
<meta property="article:tag" content="tag1" key="tag1key" />
<meta property="article:tag" content="tag2" key="tag2key" />
......
......@@ -1097,6 +1097,15 @@ describe('Client Navigation', () => {
await browser.close()
})
it('should handle undefined prop in head client-side', async () => {
const browser = await webdriver(context.appPort, '/head')
const value = await browser.eval(
`document.querySelector('meta[name="empty-content"]').hasAttribute('content')`
)
expect(value).toBe(false)
})
renderingSuite(
(p, q) => renderViaHTTP(context.appPort, p, q),
(p, q) => fetchViaHTTP(context.appPort, p, q)
......
......@@ -17,6 +17,14 @@ export default function(render, fetch) {
expect(html.includes('My component!')).toBeTruthy()
})
it('should handle undefined prop in head server-side', async () => {
const html = await render('/head')
const $ = cheerio.load(html)
const value = 'content' in $('meta[name="empty-content"]').attr()
expect(value).toBe(false)
})
test('renders with fragment syntax', async () => {
const html = await render('/fragment-syntax')
expect(html.includes('My component!')).toBeTruthy()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment