Skip to content

Commit

Permalink
Added little extension to do exact searching of text by default.
Browse files Browse the repository at this point in the history
Should probably add rough search functionality later.
  • Loading branch information
blakewest committed Oct 1, 2014
1 parent 95c53cb commit def6e81
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ you can specify a "type" of DOM element to narrow the search by doing...
Behave.find('email', 'field') // Finds els of type 'input', 'select', 'option', 'label', 'textarea', or 'form'
Behave.find('Sign Up', 'clickable') // Finds els of type 'button', or 'a'
Behave.find('danger', 'icon') // Finds els of type 'icon', 'div', or 'span'
Behave.find('Birthday', 'display') // Searches text of all elements.
Behave.find('Birthday', 'display') // Searches EXACT text of all elements.
```

Behave also finds and makes available each element on your page instantly! Let's say your page is...
Expand Down
9 changes: 7 additions & 2 deletions dist/behave.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/behave.min.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions src/behave.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
window.Behave = {};
// Little jQuery extension to have exact equals;
$.expr[':'].textEquals = function(a, i, m) {
var match = $(a).text().match("^" + m[3] + "$")
return match && match.length > 0;
}

window.Behave = {};
Behave.view = $(document.body);
Behave.domTypes = {
field: {
Expand Down Expand Up @@ -34,7 +39,7 @@ Behave.find = function(identifier, type) {
_.each(searchParams.attrOptions, function(attrOption) {
switch (attrOption) {
case 'contains':
var filter = ":contains(" + identifier + ")"
var filter = ":textEquals("+ identifier + ")"
element = Behave.view.find(elType + filter);
break;
case 'class':
Expand Down
9 changes: 6 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@
bResult.is('button').should.eql(true);
bResult.text().should.eql(jqResult.text());
});
it("should default to doing a rough search", function() {
var bResult = Behave.find('Subdo', 'clickable');
bResult.text().should.eql(jqResult.text());
it("should default to doing an exact search", function() {
var bRoughResult = Behave.find('Subdo', 'clickable');
var bExactResult = Behave.find('Subdomain', 'clickable');
bRoughResult.is('button').should.eql(false);
bExactResult.is('button').should.eql(true);
bExactResult.text().should.eql(jqResult.text());
});
it("should find things based on href", function() {
bResult = Behave.find("www.test.com", 'clickable');
Expand Down
2 changes: 1 addition & 1 deletion test/testRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div id="mocha"></div>
<script src="../node_modules/should/should.min.js"></script>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../bower_components/jQuery/dist/jquery.min.js"></script>
<script src="../bower_components/jQuery/jquery.min.js"></script>
<script src="../bower_components/lodash/dist/lodash.min.js"></script>
<script src="templates.js"></script>
<script src="../src/behave.js"></script>
Expand Down

0 comments on commit def6e81

Please sign in to comment.