JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
</head>
<body>
<div id="testArea"></div>
<h1 id="qunit-header">$.fn.getComments example</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
</body>
</html>
JavaScript
// source code for $.fn.getComments()
$(function () {
/**
* $.fn.getComments() is used to extract the html comments from a HTML elements.
*
* @author Larry Battle <http://bateru.com/news/contact-me>
* @license MIT
* @date June 11, 2012
* @version 0.1
* @args {boolean} asArray - If true, returns an array of the comments values.
Otherwise returns jquery objects of the node comments.
* @example <code><pre>
HTML:
<div id="example">I am a div. <!--Duh!--></div>
Javascript:
$("#example").getComments(true) // returns [ "Duh!" ]
</pre></code>
*/
var getCommentsFromEl = function (el, asArray) {
var result,
$el = $(el).contents();
result = $el.filter(function () {
return this.nodeType == 8;
});
if (asArray) {
result = $.makeArray(result.map(function () {
return this.nodeValue;
}));
}
return result;
};
$.fn.getComments = function (asArray) {
return getCommentsFromEl(this, asArray);
};
});
// Hide testArea for testing purposes.
$("#testArea").hide();
// test cases
$(function () {
test("test $.fn.getComments() with elements with no comments", function () {
var func = function (str) {
return $("#testArea").html(str).getComments();
};
var str;
str = "";
equal(func(str).length, 0);
str = "<div>I'm a div</div>";
equal(func(str).length, 0);
str = "No html tags in here.";
equal(func(str).length, 0);
});
test("test $.fn.getComments() with elements with comments", function () {
var func = function (str) {
return $("#testArea").html(str).getComments();
};
var str;
str = "<!--1-->";
equal(func(str).length, 1);
str = "<!--1--><div>This is a div</div><!--2-->";
equal(func(str).length, 2);
str = "<a...