Skip to content

Commit

Permalink
fix: Trim whitespace from CSS ownerNode innerText (#406)
Browse files Browse the repository at this point in the history
* test: Push failing test for CSSOM white space

* fix: Trim whitespace from CSS ownerNode innerText

Otherwise, we might incorrectly assume this style element has valid CSS
content.

In the future we might want to regex check the content?
  • Loading branch information
Robdel12 authored Oct 29, 2019
1 parent 9b9f441 commit 5a4f830
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/percy-agent-client/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ class DOM {
function isCSSOM() {
const hasHref = styleSheet.href
const ownerNode = styleSheet.ownerNode as HTMLElement
const hasStyleInDom =
ownerNode.innerText && ownerNode.innerText.length > 0
const ownerNodeInnerContent = ownerNode.innerText && ownerNode.innerText.trim()
const hasStyleInDom = ownerNodeInnerContent.length > 0

return !hasHref && !hasStyleInDom && styleSheet.cssRules
}
Expand Down
25 changes: 24 additions & 1 deletion test/percy-agent-client/dom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ function createExample(dom: string) {
}

// create a stylesheet in the DOM and add rules using the CSSOM
function createCSSOM() {
function createCSSOM(options = {} as any) {
const { whiteSpace } = options
const style = document.createElement('style')
const testingContainer = document.querySelector('.test-container') as HTMLElement

style.type = 'text/css'

if (whiteSpace) {
style.innerText = ' '
}

testingContainer.appendChild(style)

const cssomStyleSheet = document.styleSheets[0] as CSSStyleSheet
Expand Down Expand Up @@ -138,6 +144,23 @@ describe('DOM -', () => {
.equal('.box { height: 200px; width: 200px; background-color: blue; }')
})
})

describe('with white space in the style ownerNode', () => {
beforeEach(() => {
const exampleDOM = createExample('<div class="box"></div>')

createCSSOM({ whiteSpace: true })
dom = new DOM(exampleDOM)
})

it('does not break the CSSOM', () => {
const serializedCSSOM = dom.clonedDOM.querySelectorAll('[data-percy-cssom-serialized]') as HTMLElement[]

expect(serializedCSSOM.length).to.equal(1)
expect(serializedCSSOM[0].innerText)
.to.equal('.box { height: 500px; width: 500px; background-color: green; }')
})
})
})

describe('CSSOM with JS enabled', () => {
Expand Down

0 comments on commit 5a4f830

Please sign in to comment.