all files / lib/services/ selectorPrinting.js

84.98% Statements 232/273
71.14% Branches 106/149
90.7% Functions 39/43
86.14% Lines 230/267
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402                               97× 69× 69× 69× 66×       31×   74× 74×   74× 61×   74×                 63× 63×     63×           99× 76×   99× 19× 19×         99×                             35×     14× 14× 14×                   12× 16× 16× 16×       16× 16×   16× 16×   16×     13×   13× 12×         13× 13× 13×         13× 13×             68× 68× 84×                                       54× 54× 54×   11× 11×                                                         68×           35× 35×   46× 46× 46× 11×       46× 64× 58× 12× 12× 12×   46×   58×   58× 58× 58× 58×   64×       64×         61×     35×   26×   35×     35× 35× 35× 35× 35× 26× 11×     11×   26×     35× 35× 11× 11× 11×     35× 35×      
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
(function (factory) {
    Eif (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        Iif (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "../parser/cssNodes"], factory);
    }
})(function (require, exports) {
    /*---------------------------------------------------------------------------------------------
     *  Copyright (c) Microsoft Corporation. All rights reserved.
     *  Licensed under the MIT License. See License.txt in the project root for license information.
     *--------------------------------------------------------------------------------------------*/
    'use strict';
    Object.defineProperty(exports, "__esModule", { value: true });
    var nodes = require("../parser/cssNodes");
    var Element = (function () {
        function Element() {
        }
        Element.prototype.findAttribute = function (name) {
            if (this.attributes) {
                for (var _i = 0, _a = this.attributes; _i < _a.length; _i++) {
                    var attribute = _a[_i];
                    if (attribute.name === name) {
                        return attribute.value;
                    }
                }
            }
            return null;
        };
        Element.prototype.addChild = function (child) {
            Eif (child instanceof Element) {
                child.parent = this;
            }
            if (!this.children) {
                this.children = [];
            }
            this.children.push(child);
        };
        Element.prototype.append = function (text) {
            Eif (this.attributes) {
                var last = this.attributes[this.attributes.length - 1];
                last.value = last.value + text;
            }
        };
        Element.prototype.prepend = function (text) {
            if (this.attributes) {
                var first = this.attributes[0];
                first.value = text + first.value;
            }
        };
        Element.prototype.findRoot = function () {
            var curr = this;
            while (curr.parent && !(curr.parent instanceof RootElement)) {
                curr = curr.parent;
            }
            return curr;
        };
        Element.prototype.removeChild = function (child) {
            Eif (this.children) {
                var index = this.children.indexOf(child);
                Eif (index !== -1) {
                    this.children.splice(index, 1);
                    return true;
                }
            }
            return false;
        };
        Element.prototype.addAttr = function (name, value) {
            if (!this.attributes) {
                this.attributes = [];
            }
            for (var _i = 0, _a = this.attributes; _i < _a.length; _i++) {
                var attribute = _a[_i];
                Iif (attribute.name === name) {
                    attribute.value += ' ' + value;
                    return;
                }
            }
            this.attributes.push({ name: name, value: value });
        };
        Element.prototype.clone = function (cloneChildren) {
            Iif (cloneChildren === void 0) { cloneChildren = true; }
            var elem = new Element();
            Eif (this.attributes) {
                elem.attributes = [];
                for (var _i = 0, _a = this.attributes; _i < _a.length; _i++) {
                    var attribute = _a[_i];
                    elem.addAttr(attribute.name, attribute.value);
                }
            }
            Iif (cloneChildren && this.children) {
                elem.children = [];
                for (var index = 0; index < this.children.length; index++) {
                    elem.addChild(this.children[index].clone());
                }
            }
            return elem;
        };
        Element.prototype.cloneWithParent = function () {
            var clone = this.clone(false);
            Iif (this.parent && !(this.parent instanceof RootElement)) {
                var parentClone = this.parent.cloneWithParent();
                parentClone.addChild(clone);
            }
            return clone;
        };
        return Element;
    }());
    exports.Element = Element;
    var RootElement = (function (_super) {
        __extends(RootElement, _super);
        function RootElement() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        return RootElement;
    }(Element));
    exports.RootElement = RootElement;
    var LabelElement = (function (_super) {
        __extends(LabelElement, _super);
        function LabelElement(label) {
            var _this = _super.call(this) || this;
            _this.addAttr('name', label);
            return _this;
        }
        return LabelElement;
    }(Element));
    exports.LabelElement = LabelElement;
    var MarkedStringPrinter = (function () {
        function MarkedStringPrinter(quote) {
            this.quote = quote;
            // empty
        }
        MarkedStringPrinter.prototype.print = function (element) {
            this.result = [];
            Eif (element instanceof RootElement) {
                this.doPrint(element.children, 0);
            }
            else {
                this.doPrint([element], 0);
            }
            var value = this.result.join('\n');
            return [{ language: 'html', value: value }];
        };
        MarkedStringPrinter.prototype.doPrint = function (elements, indent) {
            for (var _i = 0, elements_1 = elements; _i < elements_1.length; _i++) {
                var element = elements_1[_i];
                this.doPrintElement(element, indent);
                if (element.children) {
                    this.doPrint(element.children, indent + 1);
                }
            }
        };
        MarkedStringPrinter.prototype.writeLine = function (level, content) {
            var indent = new Array(level + 1).join('  ');
            this.result.push(indent + content);
        };
        MarkedStringPrinter.prototype.doPrintElement = function (element, indent) {
            var _this = this;
            var name = element.findAttribute('name');
            // special case: a simple label
            if (element instanceof LabelElement) {
                this.writeLine(indent, name);
                return;
            }
            // the real deal
            var content = ['<'];
            // element name
            if (name) {
                content.push(name);
            }
            else {
                content.push('element');
            }
            // attributes
            Eif (element.attributes) {
                element.attributes.forEach(function (attr) {
                    if (attr.name !== 'name') {
                        content.push(' ');
                        content.push(attr.name);
                        var value = attr.value;
                        Eif (value) {
                            content.push('=');
                            content.push(quotes.ensure(value, _this.quote));
                        }
                    }
                });
            }
            content.push('>');
            this.writeLine(indent, content.join(''));
        };
        return MarkedStringPrinter;
    }());
    var quotes;
    (function (quotes) {
        function ensure(value, which) {
            return which + remove(value) + which;
        }
        quotes.ensure = ensure;
        function remove(value) {
            var match = value.match(/^['"](.*)["']$/);
            if (match) {
                return match[1];
            }
            return value;
        }
        quotes.remove = remove;
    })(quotes || (quotes = {}));
    function toElement(node, parentElement) {
        var result = new Element();
        node.getChildren().forEach(function (child) {
            switch (child.type) {
                case nodes.NodeType.SelectorCombinator:
                    Eif (parentElement) {
                        var segments = child.getText().split('&');
                        Iif (segments.length === 1) {
                            // should not happen
                            result.addAttr('name', segments[0]);
                            break;
                        }
                        result = parentElement.cloneWithParent();
                        Iif (segments[0]) {
                            var root = result.findRoot();
                            root.prepend(segments[0]);
                        }
                        for (var i = 1; i < segments.length; i++) {
                            Iif (i > 1) {
                                var clone = parentElement.cloneWithParent();
                                result.addChild(clone.findRoot());
                                result = clone;
                            }
                            result.append(segments[i]);
                        }
                    }
                    break;
                case nodes.NodeType.SelectorPlaceholder:
                    Iif (child.getText() === '@at-root') {
                        return result;
                    }
                // fall through
                case nodes.NodeType.ElementNameSelector:
                    var text = child.getText();
                    result.addAttr('name', text === '*' ? 'element' : text);
                    break;
                case nodes.NodeType.ClassSelector:
                    result.addAttr('class', child.getText().substring(1));
                    break;
                case nodes.NodeType.IdentifierSelector:
                    result.addAttr('id', child.getText().substring(1));
                    break;
                case nodes.NodeType.MixinDeclaration:
                    result.addAttr('class', child.getName());
                    break;
                case nodes.NodeType.PseudoSelector:
                    result.addAttr(child.getText(), '');
                    break;
                case nodes.NodeType.AttributeSelector:
                    var expr = child.getChildren()[0];
                    Eif (expr) {
                        var value = void 0;
                        if (expr.getRight()) {
                            switch (expr.getOperator().getText()) {
                                case '|=':
                                    // excatly or followed by -words
                                    value = quotes.remove(expr.getRight().getText()) + "-\u2026";
                                    break;
                                case '^=':
                                    // prefix
                                    value = quotes.remove(expr.getRight().getText()) + "\u2026";
                                    break;
                                case '$=':
                                    // suffix
                                    value = "\u2026" + quotes.remove(expr.getRight().getText());
                                    break;
                                case '~=':
                                    // one of a list of words
                                    value = " \u2026 " + quotes.remove(expr.getRight().getText()) + " \u2026 ";
                                    break;
                                case '*=':
                                    // substring
                                    value = "\u2026" + quotes.remove(expr.getRight().getText()) + "\u2026";
                                    break;
                                default:
                                    value = quotes.remove(expr.getRight().getText());
                                    break;
                            }
                        }
                        result.addAttr(expr.getLeft().getText(), value);
                    }
                    break;
            }
        });
        return result;
    }
    exports.toElement = toElement;
    function selectorToMarkedString(node) {
        var root = selectorToElement(node);
        return new MarkedStringPrinter('"').print(root);
    }
    exports.selectorToMarkedString = selectorToMarkedString;
    function simpleSelectorToMarkedString(node) {
        var element = toElement(node);
        return new MarkedStringPrinter('"').print(element);
    }
    exports.simpleSelectorToMarkedString = simpleSelectorToMarkedString;
    var SelectorElementBuilder = (function () {
        function SelectorElementBuilder(element) {
            this.prev = null;
            this.element = element;
        }
        SelectorElementBuilder.prototype.processSelector = function (selector) {
            var _this = this;
            var parentElement = null;
            if (!(this.element instanceof RootElement)) {
                if (selector.getChildren().some(function (c) { return c.hasChildren() && c.getChild(0).type === nodes.NodeType.SelectorCombinator; })) {
                    var curr = this.element.findRoot();
                    Eif (curr.parent instanceof RootElement) {
                        parentElement = this.element;
                        this.element = curr.parent;
                        this.element.removeChild(curr);
                        this.prev = null;
                    }
                }
            }
            selector.getChildren().forEach(function (selectorChild) {
                if (selectorChild instanceof nodes.SimpleSelector) {
                    if (_this.prev instanceof nodes.SimpleSelector) {
                        var labelElement = new LabelElement('\u2026');
                        _this.element.addChild(labelElement);
                        _this.element = labelElement;
                    }
                    else if (_this.prev && (_this.prev.matches('+') || _this.prev.matches('~'))) {
                        _this.element = _this.element.parent;
                    }
                    if (_this.prev && _this.prev.matches('~')) {
                        _this.element.addChild(toElement(selectorChild));
                        _this.element.addChild(new LabelElement('\u22EE'));
                    }
                    var thisElement = toElement(selectorChild, parentElement);
                    var root = thisElement.findRoot();
                    _this.element.addChild(root);
                    _this.element = thisElement;
                }
                Eif (selectorChild instanceof nodes.SimpleSelector ||
                    selectorChild.type === nodes.NodeType.SelectorCombinatorParent ||
                    selectorChild.type === nodes.NodeType.SelectorCombinatorSibling ||
                    selectorChild.type === nodes.NodeType.SelectorCombinatorAllSiblings) {
                    _this.prev = selectorChild;
                }
            });
        };
        return SelectorElementBuilder;
    }());
    function isNewSelectorContext(node) {
        switch (node.type) {
            case nodes.NodeType.MixinDeclaration:
            case nodes.NodeType.Stylesheet:
                return true;
        }
        return false;
    }
    function selectorToElement(node) {
        Iif (node.matches('@at-root')) {
            return null;
        }
        var root = new RootElement();
        var parentRuleSets = [];
        Eif (node.getParent() instanceof nodes.RuleSet) {
            var parent = node.getParent().getParent(); // parent of the selector's ruleset
            while (parent && !isNewSelectorContext(parent)) {
                if (parent instanceof nodes.RuleSet) {
                    Iif (parent.getSelectors().matches('@at-root')) {
                        break;
                    }
                    parentRuleSets.push(parent);
                }
                parent = parent.getParent();
            }
        }
        var builder = new SelectorElementBuilder(root);
        for (var i = parentRuleSets.length - 1; i >= 0; i--) {
            var selector = parentRuleSets[i].getSelectors().getChild(0);
            Eif (selector) {
                builder.processSelector(selector);
            }
        }
        builder.processSelector(node);
        return root;
    }
    exports.selectorToElement = selectorToElement;
});
//# sourceMappingURL=selectorPrinting.js.map