Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ var getConfig = function(lineString, lineCount) {
return typeof val === 'number' && !isNaN(val);
})
;

if (range[0] > current + padding) {
var collapseEnd = (range[0] - 1 - padding);
if (collapseEnd !== current) {
collapse.push(current + '-' + collapseEnd);
}
}

current = (range[1] || range[0]) + padding + 1;
}

Expand Down Expand Up @@ -106,8 +106,13 @@ module.exports = function() {

var preBlock = findPreviousSibling(highlight.parentElement, 'pre');
var codeBlock = preBlock.childNodes.item(0);

var total = codeBlock.innerHTML.split('\n').length - 1;
//Without trimming, additional characters can create new lines
//this happens when PrismJS is not applied (in testing)
//setting total to length - 1 makes the last not collapsed
if (codeBlock.textContent.slice(-1) === "\n") {
codeBlock.textContent = codeBlock.textContent.slice(0, -1);
}
var total = codeBlock.innerHTML.split('\n').length;
var config = getConfig(highlight.getAttribute('line-highlight'), total);

if (preBlock) {
Expand Down
111 changes: 111 additions & 0 deletions test-collapse-last-line.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
You can also set the `value` of the properties of `pageComponentViewModel` and verify that the `routeData` is updated correctly:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed for the test? Can this be pared down at all?


```html
<div id="mocha"></div>
<link rel="stylesheet" href="//unpkg.com/mocha@5.2.0/mocha.css">
<script src="//unpkg.com/mocha@5.2.0/mocha.js" type="text/javascript"></script>
<script src="//unpkg.com/chai@4.1.2/chai.js" type="text/javascript"></script>
<script type="module">
import { Component, route, value, DefineMap } from "can";

// Mocha / Chai Setup
mocha.setup("bdd")
var assert = chai.assert;

const HomePage = Component.extend({
tag: "home-page",

view: `
<h2>Home Page</h2>
`,

ViewModel: {}
});

const ListPage = Component.extend({
tag: "list-page",

view: `
<h2>List Page</h2>
<p>{{ id }}</p>
`,

ViewModel: {
id: "number"
}
});

const Application = Component.extend({
tag: "app-component",

ViewModel: {
routeData: {
default() {
route.register("{page}", { page: "home" });
route.register("list/{id}", { page: "list" });
route.start();
return route.data;
}
},

get pageComponentViewModel() {
const vmData = {};

if (this.routeData.page === "list") {
vmData.id = value.bind(this.routeData, "id");
}

return vmData;
},

get pageComponent() {
if (this.routeData.page === "home") {
return new HomePage();
} else if (this.routeData.page === "list") {
return new ListPage({
viewModel: this.pageComponentViewModel
});
}
}
},

view: `
{{ pageComponent }}
`
});

describe("Application", () => {
it("pageComponent viewModel", () => {
const routeData = new DefineMap({
page: "home",
id: null
});

const vm = new Application.ViewModel({
routeData: routeData
});

assert.deepEqual(vm.pageComponentViewModel, {}, "viewModelData defaults to empty object");

routeData.update({
page: "list",
id: 10
});

const viewModelId = vm.pageComponentViewModel.id;
assert.equal(viewModelId.value, 10, "routeData.id is passed to pageComponent viewModel");

routeData.id = 20;

assert.equal(viewModelId.value, 20, "setting routeData.id updates the pageComponentViewModel.id");

viewModelId.value = 30;
assert.equal(routeData.id, 30, "setting pageComponentViewModel.id updates routeData.id");
});
});

// start Mocha
mocha.run();
</script>
```
<span line-highlight='97-100,only'/>
35 changes: 34 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,40 @@ describe("bit-docs-html-highlight-line", function() {
close();
done();
}, done);
}, done)
}, done);

});

it("Collapse last line", function(done) {
this.timeout(60000);

var docMap = Promise.resolve({
index: {
name: "index",
demo: "path/to/demo.html",
body: fs.readFileSync(__dirname+"/test-collapse-last-line.md", "utf8")
}
});

generate(docMap, {
html: {
dependencies: {
"bit-docs-html-highlight-line": __dirname
}
},
dest: path.join(__dirname, "temp"),
parent: "index",
forceBuild: true,
debug: true,
minifyBuild: false
}).then(function() {
open("index.html",function(browser, close) {
var doc = browser.window.document;
var collapseCode = doc.querySelectorAll('pre[data-collapse="1-93,104-106"]');
assert.ok(collapseCode);
close();
done();
}, done);
}, done);
});
});