1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const dom5 = require("dom5/lib/index-next");
|
17 | const util = require("util");
|
18 | const html_document_1 = require("../html/html-document");
|
19 | const jsdoc = require("../javascript/jsdoc");
|
20 | const document_1 = require("./document");
|
21 | const document_backreference_1 = require("./document-backreference");
|
22 | const immutable_1 = require("./immutable");
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | class ScannedInlineDocument {
|
30 | constructor(type, contents, locationOffset, attachedComment, sourceRange, astNode) {
|
31 | this.warnings = [];
|
32 | this.type = type;
|
33 | this.contents = contents;
|
34 | this.locationOffset = locationOffset;
|
35 | this.attachedComment = attachedComment;
|
36 | this.sourceRange = sourceRange;
|
37 | this.astNode = astNode;
|
38 | }
|
39 | resolve(document) {
|
40 | if (!this.scannedDocument) {
|
41 |
|
42 | return;
|
43 | }
|
44 | const inlineDocument = new InlineDocument(this.scannedDocument, document);
|
45 | inlineDocument.resolve();
|
46 | return inlineDocument;
|
47 | }
|
48 | }
|
49 | exports.ScannedInlineDocument = ScannedInlineDocument;
|
50 | class InlineDocument extends document_1.Document {
|
51 | constructor(base, containerDocument) {
|
52 | super(base, containerDocument._analysisContext);
|
53 | immutable_1.unsafeAsMutable(this.kinds).add('inline-document');
|
54 | this._addFeature(new document_backreference_1.DocumentBackreference(containerDocument));
|
55 | }
|
56 | }
|
57 | exports.InlineDocument = InlineDocument;
|
58 | function getAttachedCommentText(node) {
|
59 | const commentNode = getAttachedCommentNode(node);
|
60 | if (!commentNode) {
|
61 | return;
|
62 | }
|
63 | const comment = dom5.getTextContent(commentNode);
|
64 | if (!comment || /@license/.test(comment)) {
|
65 | return;
|
66 | }
|
67 | return jsdoc.unindent(comment).trim();
|
68 | }
|
69 | exports.getAttachedCommentText = getAttachedCommentText;
|
70 | function getAttachedCommentNode(node) {
|
71 | const predecessors = getPreviousSiblings(node);
|
72 | const visiblePredecessors = filterOutInvisibleNodes(predecessors);
|
73 | const [closestVisiblePredecessor] = visiblePredecessors;
|
74 | if (!closestVisiblePredecessor) {
|
75 | return;
|
76 | }
|
77 | if (!dom5.isCommentNode(closestVisiblePredecessor)) {
|
78 | return;
|
79 | }
|
80 | return closestVisiblePredecessor;
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function* filterOutInvisibleNodes(nodeIter) {
|
87 | for (const node of nodeIter) {
|
88 |
|
89 |
|
90 | if (html_document_1.isFakeNode(node)) {
|
91 | continue;
|
92 | }
|
93 |
|
94 | if (dom5.isTextNode(node)) {
|
95 | const text = dom5.getTextContent(node).trim();
|
96 | if (text === '') {
|
97 | continue;
|
98 | }
|
99 | }
|
100 | yield node;
|
101 | }
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | function* getPreviousSiblings(node) {
|
120 | const parent = node.parentNode;
|
121 | if (parent) {
|
122 | const siblings = parent.childNodes;
|
123 | for (let i = siblings.indexOf(node) - 1; i >= 0; i--) {
|
124 | const predecessor = siblings[i];
|
125 | if (html_document_1.isFakeNode(predecessor)) {
|
126 | if (predecessor.childNodes) {
|
127 | yield* iterReverse(predecessor.childNodes);
|
128 | }
|
129 | }
|
130 | else {
|
131 | yield predecessor;
|
132 | }
|
133 | }
|
134 | if (html_document_1.isFakeNode(parent)) {
|
135 | yield* getPreviousSiblings(parent);
|
136 | }
|
137 | }
|
138 | }
|
139 | function* iterReverse(arr) {
|
140 | for (let i = arr.length - 1; i >= 0; i--) {
|
141 | yield arr[i];
|
142 | }
|
143 | }
|
144 | function getLocationOffsetOfStartOfTextContent(node, parsedDocument) {
|
145 | const sourceRange = parsedDocument.sourceRangeForStartTag(node);
|
146 | if (!sourceRange) {
|
147 | throw new Error(`Couldn't extract a location offset from HTML node: ` +
|
148 | `${util.inspect(node)}`);
|
149 | }
|
150 | return { line: sourceRange.end.line, col: sourceRange.end.column };
|
151 | }
|
152 | exports.getLocationOffsetOfStartOfTextContent = getLocationOffsetOfStartOfTextContent;
|
153 |
|
\ | No newline at end of file |