UNPKG

2.52 kBJavaScriptView Raw
1"use strict";
2exports.__esModule = true;
3var tslib_1 = require("tslib");
4var document_head_1 = require("../factories/document-head");
5var get_match_index_1 = require("../utils/get-match-index");
6function transformDocumentHead(document, context) {
7 var _a;
8 var cstNode = document.cstNode;
9 var _b = categorizeNodes(cstNode, context), directives = _b.directives, comments = _b.comments, endComments = _b.endComments;
10 var _c = getPosition(cstNode, directives, context), position = _c.position, endMarkerPoint = _c.endMarkerPoint;
11 (_a = context.comments).push.apply(_a, tslib_1.__spreadArrays(comments, endComments));
12 var createDocumentHeadWithTrailingComment = function (trailingComment) {
13 if (trailingComment) {
14 context.comments.push(trailingComment);
15 }
16 return document_head_1.createDocumentHead(position, directives, endComments, trailingComment);
17 };
18 return {
19 createDocumentHeadWithTrailingComment: createDocumentHeadWithTrailingComment,
20 documentHeadEndMarkerPoint: endMarkerPoint
21 };
22}
23exports.transformDocumentHead = transformDocumentHead;
24function categorizeNodes(document, context) {
25 var directives = [];
26 var comments = [];
27 var endComments = [];
28 var hasDirective = false;
29 for (var i = document.directives.length - 1; i >= 0; i--) {
30 var node = context.transformNode(document.directives[i]);
31 if (node.type === "comment") {
32 if (hasDirective) {
33 comments.unshift(node);
34 }
35 else {
36 endComments.unshift(node);
37 }
38 }
39 else {
40 hasDirective = true;
41 directives.unshift(node);
42 }
43 }
44 return { directives: directives, comments: comments, endComments: endComments };
45}
46function getPosition(document, directives, context) {
47 var endMarkerIndex = get_match_index_1.getMatchIndex(context.text.slice(0, document.valueRange.origStart), /---\s*$/);
48 var range = endMarkerIndex === -1
49 ? {
50 origStart: document.valueRange.origStart,
51 origEnd: document.valueRange.origStart
52 }
53 : {
54 origStart: endMarkerIndex,
55 origEnd: endMarkerIndex + 3
56 };
57 if (directives.length !== 0) {
58 range.origStart = directives[0].position.start.offset;
59 }
60 return {
61 position: context.transformRange(range),
62 endMarkerPoint: endMarkerIndex === -1 ? null : context.transformOffset(endMarkerIndex)
63 };
64}