Skip to content

Commit 99eec73

Browse files
authored
Fix code copy button (#22)
* Fix code copy button * Adding tests
1 parent d22478f commit 99eec73

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

demo_frame.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,19 @@ module.exports = function(node) {
3131
var js = getJS.call(this, sourceEl);
3232

3333
if (html && html.trim()) {
34-
var dataForHtml = node.querySelector("[data-for=html] > pre code, [data-for=html] > div > pre code");
34+
var htmlPre = node.querySelector('[data-for=html] > pre, [data-for=html] > div > pre');
35+
var dataForHtml = document.createElement('code');
36+
htmlPre.appendChild(dataForHtml);
3537
dataForHtml.innerHTML = escape(html);
3638
prettify(dataForHtml);
3739

3840
show(node.querySelector("[data-tab=html]"));
3941
}
4042

4143
if (js) {
42-
var dataForJS = node.querySelector("[data-for=js] > pre code, [data-for=js] > div > pre code");
44+
var jsPre = node.querySelector('[data-for=js] > pre, [data-for=js] > div > pre');
45+
var dataForJS = document.createElement('code');
46+
jsPre.appendChild(dataForJS);
4347
dataForJS.innerHTML = escape(js);
4448
prettify(dataForJS);
4549

demo_tpl.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// Note that the space in the <code> </code> blocks is significant:
2-
// without it, any demo in a page would break the rest of the code
3-
// examples throughout the page.
1+
// Leaving the content the of the <pre> empty is significant
2+
// <code> element will be appended when processing the code highlighting
3+
// this prevent PrismJS to register "Copy" button for demos at bit-docs-prettify step
4+
45

56
module.exports = `
67
<ul>
@@ -12,9 +13,9 @@ module.exports = `
1213
<iframe></iframe>
1314
</div>
1415
<div class="tab-content" data-for="html">
15-
<pre class="line-numbers language-html"><code> </code></pre>
16+
<pre class="line-numbers language-html"></pre>
1617
</div>
1718
<div class="tab-content" data-for="js">
18-
<pre class="line-numbers language-js"><code> </code></pre>
19+
<pre class="line-numbers language-js"></pre>
1920
</div>
2021
`;

test.js

+40
Original file line numberDiff line numberDiff line change
@@ -507,5 +507,45 @@ describe("bit-docs-tag-demo", function() {
507507
});
508508
});
509509
});
510+
511+
describe("Insert code tag", function() {
512+
insertCodeFor("html");
513+
insertCodeFor("js");
514+
515+
function insertCodeFor(htmlOrJs) {
516+
var tab = htmlOrJs === "html" ? "withoutJs" : "withoutHtml";
517+
describe(htmlOrJs, function(){
518+
519+
before(function() {
520+
return ctx.browser.newPage().then(function(p) {
521+
ctx.page = p;
522+
return ctx.page.goto(
523+
"http://127.0.0.1:8081/test/temp/" + tab + ".html"
524+
);
525+
}).then(function() {
526+
return ctx.page.waitForFunction(function() {
527+
return !!document.querySelector(".tab.active");
528+
});
529+
});
530+
});
531+
532+
after(function() {
533+
return ctx.page.close().then(function() {
534+
ctx.page = null;
535+
});
536+
});
537+
538+
it("inserts " + htmlOrJs + " code", function() {
539+
return ctx.page.evaluate(function(htmlOrJs){
540+
return {
541+
code: document.querySelector("[data-for=" + htmlOrJs + "] > pre"),
542+
}
543+
}, htmlOrJs).then(function(r){
544+
assert.ok(r.code, "Code inserted at the right spot");
545+
})
546+
});
547+
});
548+
}
549+
});
510550
});
511551
});

0 commit comments

Comments
 (0)