Skip to content

Commit 7968db6

Browse files
chan1cyrus2johnjbarton
authored andcommitted
fix(client): Enable loading different file types when running in parent mode without iframe (#3289)
* fix(client): Includes attributes like type in script tags when running in parent mode without iframe Back in #2542, a third option to run tests without iframe is implemented, mostly for lightweight browser. It only allows script element to be loaded dynamically. This fix includes file type like .css to be loaded properly. Fixes #3289
1 parent 6556ab4 commit 7968db6

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

client/karma.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,27 @@ function Karma (socket, iframe, opener, navigator, location) {
6262
childWindow.close()
6363
}
6464
childWindow = opener(url)
65-
// run context on parent element and dynamically loading scripts
65+
// run context on parent element (client_with_context)
66+
// using window.__karma__.scriptUrls to get the html element strings and load them dynamically
6667
} else if (url !== 'about:blank') {
6768
var loadScript = function (idx) {
6869
if (idx < window.__karma__.scriptUrls.length) {
69-
var ele = document.createElement('script')
70-
ele.src = window.__karma__.scriptUrls[idx]
70+
var parser = new DOMParser()
71+
// Revert escaped characters with special roles in HTML before parsing
72+
var string = window.__karma__.scriptUrls[idx]
73+
.replace(/\\x3C/g, '<')
74+
.replace(/\\x3E/g, '>')
75+
var doc = parser.parseFromString(string, 'text/html')
76+
var ele = doc.head.firstChild || doc.body.firstChild
77+
// script elements created by DomParser are marked as unexecutable,
78+
// create a new script element manually and copy necessary properties
79+
// so it is executable
80+
if (ele.tagName && ele.tagName.toLowerCase() === 'script') {
81+
var tmp = ele
82+
ele = document.createElement('script')
83+
ele.src = tmp.src
84+
ele.crossOrigin = tmp.crossOrigin
85+
}
7186
ele.onload = function () {
7287
loadScript(idx + 1)
7388
}

lib/middleware/karma.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ function createKarmaMiddleware (
175175
common.setNoCacheHeaders(response)
176176

177177
const scriptTags = []
178-
const scriptUrls = []
179178
for (const file of files.included) {
180179
let filePath = file.path
181180
const fileType = file.type || path.extname(filePath).substring(1)
@@ -192,8 +191,6 @@ function createKarmaMiddleware (
192191
}
193192
}
194193

195-
scriptUrls.push(filePath)
196-
197194
if (fileType === 'css') {
198195
scriptTags.push(`<link type="text/css" href="${filePath}" rel="stylesheet">`)
199196
} else if (fileType === 'dom') {
@@ -207,6 +204,20 @@ function createKarmaMiddleware (
207204
}
208205
}
209206

207+
const scriptUrls = []
208+
// For client_with_context, html elements are not added directly through an iframe.
209+
// Instead, scriptTags is stored to window.__karma__.scriptUrls first. Later, the
210+
// client will read window.__karma__.scriptUrls and dynamically add them to the DOM
211+
// using DOMParser.
212+
if (requestUrl === '/client_with_context.html') {
213+
for (const script of scriptTags) {
214+
scriptUrls.push(
215+
// Escape characters with special roles (tags) in HTML. Open angle brackets are parsed as tags
216+
// immediately, even if it is within double quotations in browsers
217+
script.replace(/</g, '\\x3C').replace(/>/g, '\\x3E'))
218+
}
219+
}
220+
210221
const mappings = data.includes('%MAPPINGS%') ? files.served.map((file) => {
211222
const filePath = filePathToUrlPath(file.path, basePath, urlRoot, proxyPath)
212223
.replace(/\\/g, '\\\\') // Windows paths contain backslashes and generate bad IDs if not escaped

0 commit comments

Comments
 (0)