Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit cf24931

Browse files
committed
Add DocumentType rep.
Fixes #945. Previously it was rendered by the Grip Rep and was causing issue because the packet does not have a ownProperties prop. Adding a dedicated Rep, with similar output as in the markup view fixes the issue. Stubs and tests are added as well. And to be safe, we default ownProperties to an empty object in the Grip rep so we don't have errors anymore.
1 parent 4855ecd commit cf24931

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
// ReactJS
6+
const PropTypes = require("prop-types");
7+
8+
// Reps
9+
const {
10+
getGripType,
11+
isGrip,
12+
wrapRender,
13+
} = require("./rep-utils");
14+
const dom = require("react-dom-factories");
15+
const { span } = dom;
16+
17+
/**
18+
* Renders DOM documentType object.
19+
*/
20+
DocumentType.propTypes = {
21+
object: PropTypes.object.isRequired,
22+
};
23+
24+
function DocumentType(props) {
25+
const { object } = props;
26+
let name = object && object.preview && object.preview.nodeName
27+
? ` ${object.preview.nodeName}`
28+
: "";
29+
return span({
30+
"data-link-actor-id": props.object.actor,
31+
className: "objectBox objectBox-document"
32+
}, `<!DOCTYPE${name}>`);
33+
}
34+
35+
// Registration
36+
function supportsObject(object, noGrip = false) {
37+
if (noGrip === true || !isGrip(object)) {
38+
return false;
39+
}
40+
41+
const type = getGripType(object, noGrip);
42+
return (object.preview && type === "DocumentType");
43+
}
44+
45+
// Exports from this module
46+
module.exports = {
47+
rep: wrapRender(DocumentType),
48+
supportsObject,
49+
};

packages/devtools-reps/src/reps/grip.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ function propIterator(props, object, max) {
147147
});
148148

149149
let properties = object.preview
150-
? object.preview.ownProperties
150+
? object.preview.ownProperties || {}
151151
: {};
152+
152153
let propertiesLength = getPropertiesLength(object);
153154

154155
if (object.preview && object.preview.safeGetterValues) {

packages/devtools-reps/src/reps/rep.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const Accessor = require("./accessor");
2020
const Attribute = require("./attribute");
2121
const DateTime = require("./date-time");
2222
const Document = require("./document");
23+
const DocumentType = require("./document-type");
2324
const Event = require("./event");
2425
const Func = require("./function");
2526
const PromiseRep = require("./promise");
@@ -53,6 +54,7 @@ let reps = [
5354
PromiseRep,
5455
ArrayRep,
5556
Document,
57+
DocumentType,
5658
Window,
5759
ObjectWithText,
5860
ObjectWithURL,
@@ -128,6 +130,7 @@ module.exports = {
128130
CommentNode,
129131
DateTime,
130132
Document,
133+
DocumentType,
131134
ElementNode,
132135
ErrorRep,
133136
Event,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
const stubs = new Map();
6+
stubs.set("html", {
7+
"type": "object",
8+
"actor": "server1.conn7.child1/obj195",
9+
"class": "DocumentType",
10+
"extensible": true,
11+
"frozen": false,
12+
"sealed": false,
13+
"ownPropertyLength": 0,
14+
"preview": {
15+
"kind": "DOMNode",
16+
"nodeType": 10,
17+
"nodeName": "html",
18+
"isConnected": true
19+
}
20+
});
21+
22+
stubs.set("unnamed", {
23+
"type": "object",
24+
"actor": "server1.conn7.child1/obj195",
25+
"class": "DocumentType",
26+
"extensible": true,
27+
"frozen": false,
28+
"sealed": false,
29+
"ownPropertyLength": 0,
30+
"preview": {
31+
"kind": "DOMNode",
32+
"nodeType": 10,
33+
"nodeName": "",
34+
"isConnected": true
35+
}
36+
});
37+
38+
module.exports = stubs;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
const { shallow } = require("enzyme");
6+
const {
7+
REPS,
8+
getRep,
9+
} = require("../rep");
10+
11+
const {
12+
expectActorAttribute
13+
} = require("./test-helpers");
14+
15+
const { DocumentType } = REPS;
16+
const stubs = require("../stubs/document-type");
17+
18+
describe("DocumentType", () => {
19+
const stub = stubs.get("html");
20+
it("correctly selects DocumentType Rep", () => {
21+
expect(getRep(stub)).toBe(DocumentType.rep);
22+
});
23+
24+
it("renders with expected text content on html doctype", () => {
25+
const renderedComponent = shallow(DocumentType.rep({
26+
object: stub
27+
}));
28+
29+
expect(renderedComponent.text()).toEqual("<!DOCTYPE html>");
30+
expectActorAttribute(renderedComponent, stub.actor);
31+
});
32+
33+
it("renders with expected text content on empty doctype", () => {
34+
const unnamedStub = stubs.get("unnamed");
35+
const renderedComponent = shallow(DocumentType.rep({
36+
object: unnamedStub
37+
}));
38+
expect(renderedComponent.text()).toEqual("<!DOCTYPE>");
39+
expectActorAttribute(renderedComponent, unnamedStub.actor);
40+
});
41+
});

0 commit comments

Comments
 (0)