Skip to content

Commit

Permalink
Attempt to remove usage of selector
Browse files Browse the repository at this point in the history
  • Loading branch information
maxpoletaev committed Jan 21, 2017
1 parent b3f3f63 commit 03e298d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 35 deletions.
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "jquery.bem",
"version": "1.3.0",
"version": "1.4.0",
"homepage": "https://github.com/zenwalker/jquery-bem",
"authors": [
"Maxim Poletaev <zenwalker2@gmail.com>"
"Maxim Poletaev <max.poletaev@gmail.com>"
],
"description": "Plugin for comfortable work with BEM DOM from jQuery",
"main": "jquery.bem.js",
Expand Down
28 changes: 7 additions & 21 deletions jquery.bem.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
var blockClass = this.getBlockClass($this)
, block = $this.closest('.' + blockClass);

block.selector = blockClass;
return block;
};

Expand Down Expand Up @@ -137,12 +136,10 @@
* @param {Object}
*/
BEM.prototype.setMod = function($this, modKey, modVal) {
var self = this
, selector = $this.selector;
var self = this;

$this.each(function() {
var current = $(this);
current.selector = selector;

var mods = self.extractMods(current)
, baseName = self.getBaseClass(current);
Expand Down Expand Up @@ -174,12 +171,10 @@
* @param {Object}
*/
BEM.prototype.delMod = function($this, modKey, modVal) {
var self = this
, selector = $this.selector;
var self = this;

$this.each(function() {
var current = $(this);
current.selector = selector;

var mods = self.extractMods(current)
, baseName = self.getBaseClass(current);
Expand Down Expand Up @@ -215,12 +210,10 @@
var self = this
, modVal = modVal || null
, inverse = inverse || false
, selector = $this.selector
, result = $();

$this.each(function() {
var current = $(this);
current.selector = selector;

var mods = self.extractMods(current)
, baseName = self.getBaseClass(current);
Expand All @@ -241,7 +234,6 @@
: current.filter('.' + modName));
});

result.selector = selector;
return result;
};

Expand Down Expand Up @@ -334,17 +326,12 @@
var classes, result = [];

if (typeof $this == 'object') {

if ($this.selector.indexOf('.') === 0) {
classes = $this.selector.split('.');
}
else if ($this.attr('class') != undefined) {
if ($this.attr('class') != undefined) {
classes = $this.attr('class').split(' ');
}
else {
return null;
}

}
else {
classes = $this.split('.');
Expand Down Expand Up @@ -496,7 +483,6 @@

$.each(selectors, function(i, sel) {
var classType = self.getClassType(sel);

if (classType && classType != 'mod') {
baseClass = sel;
}
Expand Down Expand Up @@ -595,12 +581,12 @@
toggleMod: function (modKey, modVal1, modVal2) {
if (this.hasMod(modKey, modVal1)) {
return this
.delMod(modKey, modVal1)
.setMod(modKey, modVal2);
.delMod(modKey, modVal1)
.setMod(modKey, modVal2);
} else {
return this
.delMod(modKey, modVal2)
.setMod(modKey, modVal1);
.delMod(modKey, modVal2)
.setMod(modKey, modVal1);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery-bem",
"version": "1.3.0",
"version": "1.4.0",
"description": "Plugin for comfortable work with BEM DOM from jQuery",
"main": "jquery.bem.js",
"repository": {
Expand Down
24 changes: 19 additions & 5 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<head>
<meta charset="utf-8" />
<title>jQuery.BEM Test</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.18.0.css" />
<script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="http://code.jquery.com/qunit/qunit-1.18.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-2.1.1.css" />
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="http://code.jquery.com/qunit/qunit-2.1.1.js"></script>
<script src="./jquery.bem.test.js"></script>
<script src="../jquery.bem.js"></script>
</head>
Expand All @@ -14,9 +14,23 @@
<div id="qunit"></div>

<div id="qunit-fixture">
<div id="block" class="block">
<div id="elem" class="block__elem"></div>
<div id="selecting-elements">
<div class="block">
<div class="block__elem">
<div class="block__subelem"></div>
</div>
</div>

<div id="selecting-with-context">
<div class="block-a block-b">
<div class="block-a__elem">
<div class="block-b__elem"></div>
</div>
</div>
</div>
</div>


</div>

</body>
Expand Down
25 changes: 19 additions & 6 deletions test/jquery.bem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,27 @@ QUnit.test('build selectors', function(assert) {
});

QUnit.test('selecting elements', function(assert) {
var $block = $('#block');
var $elem = $('#elem');
var $fixture = $('#selecting-elements');
var $block = $('.block', $fixture);
var $elem = $('.block__elem', $fixture);
var $subelem = $('.block__subelem', $fixture);

assert.equal($block.ctx('block').elem('elem')[0], $elem[0],
assert.equal($block.elem('elem')[0], $elem[0],
'selecting element'
);

assert.equal($block.ctx('block').elem($elem, 'elem')[0], $elem[0],
'selecting element on specified context'
assert.equal($block.elem('subelem')[0], $subelem[0],
'selecting subelement'
);
});

QUnit.test('selecting elements with context', function(assert) {
var $fixture = $('#selecting-with-context');

var $blockA = $('.block-a', $fixture);
var $elemA = $('.block-a__elem', $fixture);
var $blockB = $('.block-b', $fixture);
var $elemB = $('.block-b__elem', $fixture);

assert.equal($blockA.elem('elem')[0], $elemA[0]);
assert.equal($blockB.elem('elem')[0], $elemB[0]);
});

0 comments on commit 03e298d

Please sign in to comment.