GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(client): Enable loading different file types when running in pare… · karma-runner/karma@7968db6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • client
    • karma.js
  • lib/middleware
    • karma.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎client/karma.js‎

Copy file name to clipboardExpand all lines: client/karma.js
+18-3Lines 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
}
Collapse file

‎lib/middleware/karma.js‎

Copy file name to clipboardExpand all lines: lib/middleware/karma.js
+14-3Lines 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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.