Skip to content

Commit a6e070a

Browse files
committed
Replace spaces with underscores when setting the hash value and inserting the ID into the DOM. Escape the deep link path when querying for the DOM element on hash change.
1 parent 16364fb commit a6e070a

File tree

5 files changed

+14
-10
lines changed

5 files changed

+14
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"dependencies": {
4242
"base64-js": "^1.2.0",
4343
"brace": "0.7.0",
44+
"css.escape": "1.5.1",
4445
"deep-extend": "0.4.1",
4546
"expect": "1.20.2",
4647
"getbase": "^2.8.2",

src/core/components/operations.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react"
22
import PropTypes from "prop-types"
33
import { helpers } from "swagger-client"
4-
import { replaceSpacesWithUnderscores } from "core/utils"
4+
import { createDeepLinkPath } from "core/utils"
55
const { opId } = helpers
66

77
export default class Operations extends React.Component {
@@ -69,7 +69,7 @@ export default class Operations extends React.Component {
6969
let tagExternalDocsDescription = tagObj.getIn(["tagDetails", "externalDocs", "description"])
7070
let tagExternalDocsUrl = tagObj.getIn(["tagDetails", "externalDocs", "url"])
7171

72-
let isShownKey = ["operations-tag", replaceSpacesWithUnderscores(tag)]
72+
let isShownKey = ["operations-tag", createDeepLinkPath(tag)]
7373
let showTag = layoutSelectors.isShown(isShownKey, docExpansion === "full" || docExpansion === "list")
7474

7575
return (
@@ -124,7 +124,7 @@ export default class Operations extends React.Component {
124124

125125
const operationId =
126126
op.getIn(["operation", "operationId"]) || op.getIn(["operation", "__originalOperationId"]) || opId(op.get("operation"), path, method) || op.get("id")
127-
const isShownKey = ["operations", replaceSpacesWithUnderscores(tag), replaceSpacesWithUnderscores(operationId)]
127+
const isShownKey = ["operations", createDeepLinkPath(tag), createDeepLinkPath(operationId)]
128128

129129
const allowTryItOut = specSelectors.allowTryItOutFor(op.get("path"), op.get("method"))
130130
const response = specSelectors.responseFor(op.get("path"), op.get("method"))

src/core/plugins/deep-linking/layout-wrap-actions.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { setHash } from "./helpers"
2+
import { createDeepLinkPath } from "core/utils"
23

34
export const show = (ori, { getConfigs }) => (...args) => {
45
ori(...args)
@@ -19,12 +20,12 @@ export const show = (ori, { getConfigs }) => (...args) => {
1920

2021
if(type === "operations") {
2122
let [, tag, operationId] = thing
22-
setHash(`/${tag}/${operationId}`)
23+
setHash(`/${createDeepLinkPath(tag)}/${createDeepLinkPath(operationId)}`)
2324
}
2425

2526
if(type === "operations-tag") {
2627
let [, tag] = thing
27-
setHash(`/${tag}`)
28+
setHash(`/${createDeepLinkPath(tag)}`)
2829
}
2930
}
3031

src/core/plugins/deep-linking/spec-wrap-actions.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import scrollTo from "scroll-to-element"
2-
import { replaceSpacesWithUnderscores } from "core/utils"
2+
import { escapeDeepLinkPath } from "core/utils"
33

44
const SCROLL_OFFSET = -5
55
let hasHashBeenParsed = false
@@ -28,21 +28,21 @@ export const updateResolved = (ori, { layoutActions, getConfigs }) => (...args)
2828
hash = hash.slice(1)
2929
}
3030

31-
let [tag, operationId] = hash.split("/").map(v => replaceSpacesWithUnderscores(v))
31+
let [tag, operationId] = hash.split("/")
3232

3333
if(tag && operationId) {
3434
// Pre-expand and scroll to the operation
3535
layoutActions.show(["operations-tag", tag], true)
3636
layoutActions.show(["operations", tag, operationId], true)
3737

38-
scrollTo(`#operations-${tag}-${operationId}`, {
38+
scrollTo(`#operations-${escapeDeepLinkPath(tag)}-${escapeDeepLinkPath(operationId)}`, {
3939
offset: SCROLL_OFFSET
4040
})
4141
} else if(tag) {
4242
// Pre-expand and scroll to the tag
4343
layoutActions.show(["operations-tag", tag], true)
4444

45-
scrollTo(`#operations-tag-${tag}`, {
45+
scrollTo(`#operations-tag-${escapeDeepLinkPath(tag)}`, {
4646
offset: SCROLL_OFFSET
4747
})
4848
}

src/core/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import some from "lodash/some"
88
import eq from "lodash/eq"
99
import { memoizedSampleFromSchema, memoizedCreateXMLExample } from "core/plugins/samples/fn"
1010
import win from "./window"
11+
import cssEscape from "css.escape"
1112

1213
const DEFAULT_REPONSE_KEY = "default"
1314

@@ -651,4 +652,5 @@ export const shallowEqualKeys = (a,b, keys) => {
651652
})
652653
}
653654

654-
export const replaceSpacesWithUnderscores = (str) => str.replace(/\s/, "_")
655+
export const createDeepLinkPath = (str) => str.replace(/\s/g, "_")
656+
export const escapeDeepLinkPath = (str) => cssEscape( createDeepLinkPath(str) )

0 commit comments

Comments
 (0)