UNPKG

2.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * @license
5 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
6 * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
8 * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
9 * Code distributed by Google as part of the polymer project is also
10 * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11 */
12const dom5 = require("dom5/lib/index-next");
13const parse5 = require("parse5");
14const shadyCSS = require("shady-css-parser");
15const stream = require("stream");
16class NoCommentStringifier extends shadyCSS.Stringifier {
17 comment(node) {
18 const value = node.value;
19 if (value.indexOf('@license') >= 0) {
20 return value;
21 }
22 return '';
23 }
24}
25const parser = new shadyCSS.Parser();
26const stringifier = new NoCommentStringifier();
27const pred = dom5.predicates;
28const isInlineStyle = pred.AND(pred.hasTagName('style'), pred.OR(pred.NOT(pred.hasAttr('type')), pred.hasAttrValue('type', 'text/css')));
29/**
30 * Transforms all inline styles in `html` with `filter`
31 */
32function html(text) {
33 const ast = parse5.parse(text);
34 const styleNodes = dom5.queryAll(ast, isInlineStyle, dom5.childNodesIncludeTemplate);
35 for (const styleNode of styleNodes) {
36 const text = dom5.getTextContent(styleNode);
37 dom5.setTextContent(styleNode, css(text));
38 }
39 return parse5.serialize(ast);
40}
41exports.html = html;
42const APPLY_NO_SEMI = /@apply\s*\(?--[\w-]+\)?[^;]/g;
43function addSemi(match) {
44 return match + ';';
45}
46function css(text) {
47 text = text.replace(APPLY_NO_SEMI, addSemi);
48 return stringifier.stringify(parser.parse(text));
49}
50exports.css = css;
51class GulpTransform extends stream.Transform {
52 constructor() {
53 super({ objectMode: true });
54 }
55 _transform(file, _encoding, callback) {
56 if (file.isStream()) {
57 return callback(new Error('css-slam does not support streams'));
58 }
59 if (file.contents) {
60 let contents;
61 if (file.path.slice(-5) === '.html') {
62 contents = file.contents.toString();
63 file.contents = new Buffer(html(contents));
64 }
65 else if (file.path.slice(-4) === '.css') {
66 contents = file.contents.toString();
67 file.contents = new Buffer(css(contents));
68 }
69 }
70 callback(undefined, file);
71 }
72}
73exports.GulpTransform = GulpTransform;
74function gulp() {
75 return new GulpTransform();
76}
77exports.gulp = gulp;
78//# sourceMappingURL=index.js.map
\No newline at end of file