UNPKG

webpack-angular-translate

Version:

Webpack plugin that extracts the translation-ids with the default texts.

149 lines 6.11 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DocumentContext = exports.HtmlParseContext = void 0; var path = __importStar(require("path")); var HtmlParseContext = /** @class */ (function () { function HtmlParseContext() { /** * The text contents of the element */ this.texts = []; } HtmlParseContext.prototype.enter = function (elementName, attributes, startPosition) { return new ElementContext(this, elementName, attributes, startPosition); }; HtmlParseContext.prototype.addText = function (text) { this.texts.push(text); }; HtmlParseContext.prototype.emitSuppressableError = function (message, position) { if (this.suppressDynamicTranslationErrors) { return; } this.emitError(message, position); }; return HtmlParseContext; }()); exports.HtmlParseContext = HtmlParseContext; var DocumentContext = /** @class */ (function (_super) { __extends(DocumentContext, _super); function DocumentContext(loader, html) { var _this = _super.call(this) || this; _this.loader = loader; _this.html = html; _this.suppressDynamicTranslationErrors = false; return _this; } DocumentContext.prototype.leave = function () { throw new Error("Cannot leave the root context."); }; DocumentContext.prototype.emitError = function (message, position) { var loc = this.loc(position); var relativePath = path.relative(this.loader.context, this.loader.resourcePath); message = "Failed to extract the angular-translate translations from '" + relativePath + "':" + loc.line + ":" + loc.column + ": " + message; this.loader.emitError(new Error(message)); }; DocumentContext.prototype.asHtml = function () { return this.texts.reduce(function (memo, text) { return memo + text.raw; }, ""); }; DocumentContext.prototype.loc = function (position) { var line = 1; var column = 0; for (var i = 0; i < position; ++i) { if (this.html[i] === "\n") { ++line; column = 0; } else { ++column; } } return { line: line, column: column }; }; return DocumentContext; }(HtmlParseContext)); exports.DocumentContext = DocumentContext; /** * Context for an html element. * * The context stores the state about the current (html) element and is used by the parser. * The parser calls `enter` for each new element. This will create a child context of the current context. * The child context inherits some attributes, like if translation-errors should be suppressed. */ var ElementContext = /** @class */ (function (_super) { __extends(ElementContext, _super); function ElementContext(parent, tagName, attributes, startPosition) { var _this = _super.call(this) || this; _this.parent = parent; _this._suppressDynamicTranslationErrorMessage = false; _this.attributes = attributes || []; _this.tagName = tagName; _this.elementStartPosition = startPosition; return _this; } Object.defineProperty(ElementContext.prototype, "suppressDynamicTranslationErrors", { get: function () { return (this._suppressDynamicTranslationErrorMessage || (this.parent && this.parent.suppressDynamicTranslationErrors)); }, set: function (suppress) { this._suppressDynamicTranslationErrorMessage = suppress; }, enumerable: false, configurable: true }); ElementContext.prototype.emitError = function (message, position) { return this.parent.emitError(message, position); }; ElementContext.prototype.asHtml = function () { var result = "<" + this.tagName; result = this.attributes.reduce(function (memo, _a) { var name = _a.name, value = _a.value; return memo + " " + name + "='" + value + "'"; }, result); var text = this.texts.length === 0 ? "..." : this.texts.reduce(function (memo, text) { return memo + text.raw; }, ""); return result + ">" + text + "</" + this.tagName + ">"; }; ElementContext.prototype.leave = function () { return this.parent; }; ElementContext.prototype.loc = function (position) { return this.parent.loc(position); }; return ElementContext; }(HtmlParseContext)); exports.default = ElementContext; //# sourceMappingURL=element-context.js.map