1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- "use strict";
- exports.__esModule = true;
- var tslib_1 = require("tslib");
- var document_head_1 = require("../factories/document-head");
- var get_match_index_1 = require("../utils/get-match-index");
- function transformDocumentHead(document, context) {
- var _a;
- var cstNode = document.cstNode;
- var _b = categorizeNodes(cstNode, context), directives = _b.directives, comments = _b.comments, endComments = _b.endComments;
- var _c = getPosition(cstNode, directives, context), position = _c.position, endMarkerPoint = _c.endMarkerPoint;
- (_a = context.comments).push.apply(_a, tslib_1.__spreadArrays(comments, endComments));
- var createDocumentHeadWithTrailingComment = function (trailingComment) {
- if (trailingComment) {
- context.comments.push(trailingComment);
- }
- return document_head_1.createDocumentHead(position, directives, endComments, trailingComment);
- };
- return {
- createDocumentHeadWithTrailingComment: createDocumentHeadWithTrailingComment,
- documentHeadEndMarkerPoint: endMarkerPoint
- };
- }
- exports.transformDocumentHead = transformDocumentHead;
- function categorizeNodes(document, context) {
- var directives = [];
- var comments = [];
- var endComments = [];
- var hasDirective = false;
- for (var i = document.directives.length - 1; i >= 0; i--) {
- var node = context.transformNode(document.directives[i]);
- if (node.type === "comment") {
- if (hasDirective) {
- comments.unshift(node);
- }
- else {
- endComments.unshift(node);
- }
- }
- else {
- hasDirective = true;
- directives.unshift(node);
- }
- }
- return { directives: directives, comments: comments, endComments: endComments };
- }
- function getPosition(document, directives, context) {
- var endMarkerIndex = get_match_index_1.getMatchIndex(context.text.slice(0, document.valueRange.origStart), /---\s*$/);
- // end marker should start with the first character on the line
- if (endMarkerIndex > 0 && !/[\r\n]/.test(context.text[endMarkerIndex - 1])) {
- endMarkerIndex = -1;
- }
- var range = endMarkerIndex === -1
- ? {
- origStart: document.valueRange.origStart,
- origEnd: document.valueRange.origStart
- }
- : {
- origStart: endMarkerIndex,
- origEnd: endMarkerIndex + 3
- };
- if (directives.length !== 0) {
- range.origStart = directives[0].position.start.offset;
- }
- return {
- position: context.transformRange(range),
- endMarkerPoint: endMarkerIndex === -1 ? null : context.transformOffset(endMarkerIndex)
- };
- }
|