Skip to content

Commit

Permalink
Support root.toString() (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 committed Mar 14, 2018
1 parent a5cd2d7 commit 9678037
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 53 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -1,3 +1,6 @@
# Created by https://www.gitignore.io/api/node

### Node ###
# Logs
logs
*.log
Expand Down Expand Up @@ -57,3 +60,6 @@ typings/
# dotenv environment variables file
.env



# End of https://www.gitignore.io/api/node
55 changes: 55 additions & 0 deletions lib/document.js
@@ -0,0 +1,55 @@
"use strict";
const PostCssRoot = require("postcss/lib/root");
const stringify = require("./stringify");

class Document extends PostCssRoot {
toString (stringifier) {
return super.toString(stringifier || {
stringify: stringify,
});
}

each (callback) {
let wasBreak, lastResult;
this.nodes.forEach(node => {
const result = node.each(callback);
if (result === false) {
wasBreak = true;
} else {
lastResult = result;
}
});
if (wasBreak) {
return false;
} else {
return lastResult;
}
}

append () {
this.last.append.apply(
this.last,
Array.from(arguments)
);
return this;
}

prepend () {
this.first.prepend.apply(
this.first,
Array.from(arguments)
);
return this;
}

insertBefore (exist, add) {
exist.prepend(add);
return this;
}

insertAfter (exist, add) {
exist.append(add);
return this;
}
}
module.exports = Document;
47 changes: 2 additions & 45 deletions lib/parser.js
@@ -1,7 +1,7 @@
"use strict";

const Input = require("postcss/lib/input");
const Root = require("postcss/lib/root");
const Document = require("./document");
const htmlParser = require("./html-parser");
const mdParser = require("./markdown-parser");
const BlockFixer = require("./block-fixer");
Expand All @@ -21,7 +21,7 @@ function parser (source, opts) {
if (!styleTag && !stylesMd && !filenameMatch(/\.(?:md|markdown)$/i)) {
return;
}
const document = new Root();
const document = new Document();
const documentLocalFixer = new BlockFixer(source);
let index = 0;
[].concat(styleTag, stylesMd).filter(Boolean).sort(function (a, b) {
Expand All @@ -42,49 +42,6 @@ function parser (source, opts) {
},
};

document.each = function (callback) {
let wasBreak, lastResult;
this.nodes.forEach(node => {
const result = node.each(callback);
if (result === false) {
wasBreak = true;
} else {
lastResult = result;
}
});
if (wasBreak) {
return false;
} else {
return lastResult;
}
};

document.append = function () {
this.last.append.apply(
this.last,
Array.from(arguments)
);
return this;
};

document.prepend = function () {
this.first.prepend.apply(
this.first,
Array.from(arguments)
);
return this;
};

document.insertBefore = function (exist, add) {
exist.prepend(add);
return this;
};

document.insertAfter = function (exist, add) {
exist.append(add);
return this;
};

return document;
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -17,8 +17,8 @@
"lcov",
"text"
],
"cache": true,
"all": true,
"cache": true,
"check-coverage": true
},
"repository": {
Expand Down Expand Up @@ -68,7 +68,7 @@
"eslint-plugin-promise": "^3.7.0",
"eslint-plugin-standard": "^3.0.1",
"mocha": "^5.0.4",
"nyc": "^11.4.1",
"nyc": "^11.6.0",
"postcss": "^6.0.19",
"postcss-less": "^1.1.3",
"postcss-parser-tests": "^6.2.1",
Expand All @@ -77,7 +77,7 @@
"postcss-scss": "^1.0.4",
"strip-bom": "^3.0.0",
"stylefmt": "^6.0.0",
"stylelint": "^9.1.1",
"stylelint": "^9.1.2",
"sugarss": "^1.0.1"
}
}
7 changes: 2 additions & 5 deletions test/html.js
Expand Up @@ -103,14 +103,11 @@ describe("html tests", () => {
return postcss([
root => {
expect(root.nodes).to.have.lengthOf(2);
expect(root.toString()).equal(less);
},
stylefmt,
]).process(less, {
syntax: syntax({
less: {
parse: require("postcss-less").parse,
},
}),
syntax: syntax(),
from: "less.html",
}).then(result => {
expect(result.content).to.equal(less);
Expand Down

0 comments on commit 9678037

Please sign in to comment.