Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first/last/nth-child patch for after/before emulation #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions data/box.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
background: palegoldenrod;
}

.test:before {
.test::before {
content: 'before';
display: block;
box-shadow: -10px -10px 10px red;
}

.test:after {
.test::after {
content: 'after';
/*content: 222;*/
/*content: "("attr(class)")";*/
Expand Down
19 changes: 19 additions & 0 deletions data/complex/child.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.container::before
{
content: 'before';
display: block;
color: blue;
}

.container::after
{
content: 'after';
display: block;
color: red;
}

.container .block:nth-child(odd)
{
color: blueviolet;
font-size: large;
}
6 changes: 6 additions & 0 deletions data/complex/child.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<b:style src="./child.css"/>
<b:isolate/>

<div class="container">
<span class="block">some content</span>
</div>
13 changes: 12 additions & 1 deletion data/complex/panel.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
font-size: 32px;
}

.panel .button {
.panel .button
{
margin: 0 10px;
}

.buttons::before
{
content: 'before buttons';
}

.buttons .button:first-child::before
{
content: 'first button';
}
122 changes: 122 additions & 0 deletions src/app/lib/builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
var walk = require('utils.walker').all;
var sortObject = require('utils.index').sortObject;
var domUtils = require('utils.dom');
var Variant = require('./variant');

/**
* @property {Array} emulators
* @property {Object} generator
*/
module.exports = basis.Class(null, {
className: 'dp.Builder',
extendConstructor_: true,
init: function() {
var emulators = Array.isArray(this.emulators) ? this.emulators : basis.array.from(this.emulators);

this.emulators = {
'*': []
};
this.digest = {};
this.variants = [];
this.ignoredVariantsCount = 0;
this.states = this.generator.getStates();
this.states = this.generator.getStyles().reduce(function(states, style) {
return states.concat(this.handleStyle(style, emulators));
}.bind(this), this.states);

if (this.states.length) {
this.states = this.combine(this.states);
} else {
this.states.push({hasStates: false});
}

this.states.forEach(function(state) {
var newVariant = this.buildVariant(state);

if (!this.addVariant(newVariant)) {
newVariant.destroy();
}
}, this);

console.log('BUILDER', this);
console.log('STATES', this.states);
console.log('EMULATORS', this.emulators);
console.log('VARIANTS', this.variants);
console.log('=======================');

},
handleStyle: function(style, emulators) {
var states = {};

emulators.forEach(function(emulator) {
var newStates = emulator.getStates(style.AST);

if (Array.isArray(newStates)) {
newStates.forEach(function(state) {
var stateString = JSON.stringify(state);

if (!states[stateString]) {
for (var stateName in state) {
if (state.hasOwnProperty(stateName)) {
this.emulators[stateName] = this.emulators[stateName] || [];
basis.array.add(this.emulators[stateName], emulator);
}
}

states[stateString] = state;
}
}, this);
} else {
basis.array.add(this.emulators['*'], emulator);
}
}, this);
style.apply();

return basis.object.values(states);
},
buildVariant: function(states) {
return new Variant({generator: this.generator, emulators: this.emulators, states: states});
},
addVariant: function(candidate) {
var digest;

if (this.variants.indexOf(candidate) > -1) {
return;
}

digest = domUtils.getDigest(candidate.wrapper);

if (!this.digest.hasOwnProperty(digest)) {
this.digest[digest] = true;
this.variants.push(candidate);

return true;
} else {
this.ignoredVariantsCount++;

return false;
}
},
combine: function(states) {
var newStates = {};

states.forEach(function(state) {
Object.keys(newStates).forEach(function(stateName) {
var stateToAdd = basis.object.merge(state, newStates[stateName]);
var objectString = JSON.stringify(sortObject(stateToAdd));

if (!newStates[objectString]) {
newStates[objectString] = stateToAdd;
}
});

var objectString = JSON.stringify(sortObject(state));

if (!newStates[objectString]) {
newStates[objectString] = state;
}
});

return basis.object.values(newStates);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ module.exports = function pseudoContentFactory(pseudoContent) {
}
});
}
}
};
File renamed without changes.
79 changes: 79 additions & 0 deletions src/app/lib/emulators/nthFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
var List = require('csso:utils/list.js');

module.exports = function nthFactory(nth, element, sourceMap) {
var sourceNth = sourceMap.get(nth) || {};
var normalizedArgs;
var classMap = {
'first-child': {
a: '0n',
b: 1
},
'last-child': {
a: '0n',
b: element.parentNode.children.length
}
};
var argMap = {
even: {
a: '2n',
b: 0
},
odd: {
a: '2n',
b: 1
}
};

console.log(nth);

if (['first-child', 'last-child', 'nth-child'].indexOf(nth.name) == -1) {
return;
}

if (nth.type == 'PseudoClass') {
if (!(normalizedArgs = classMap[nth.name])) {
throw new Error('Something awful was happened...');
}
} else if (nth.type == 'FunctionalPseudo') {
var args = nth.arguments.first().sequence.toArray();

if (args.length == 1) {
normalizedArgs = argMap[args[0].value] || {
a: args[0].value,
b: 0
}
} else if (args.length == 3) {
normalizedArgs = {
a: args[0].value,
b: args[2].value
}
} else {
throw new Error('Something awful was happened...');
}
}

return {
change: function(amount) {
console.log(nth.name, amount);
if (amount < 0 || sourceNth.name == 'last-child' && amount > 0) {
return;
}

normalizedArgs.b += amount;
},
apply: function() {
var args = new List([{
type: 'Argument',
sequence: new List([
{type: 'Nth', value: normalizedArgs.a},
{type: 'Operator', value: '+'},
{type: 'Nth', value: normalizedArgs.b}
])
}]);

nth.name = 'nth-child';
nth.type = 'FunctionalPseudo';
nth.arguments = args;
}
}
};
23 changes: 23 additions & 0 deletions src/app/lib/emulators/nthHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var List = require('csso:utils/list.js');

module.exports = {
getStates: function() {
return [];
},
handleToken: function(token, parent, root, sourceMap) {
if (token.type == 'PseudoClass' && token.name == 'only-child') {
var sourceToken = sourceMap.get(token);
var newToken = List.createItem({type: 'PseudoClass', name: 'last-child'});

token.name = 'first-child';

sourceMap.delete(token);
sourceMap.set(newToken.data, sourceToken);

parent.data.sequence.insert(newToken);
}
},
emulate: function(token, parent, root, sourceMap, mapper, value) {
// nothing to do...
}
};
55 changes: 55 additions & 0 deletions src/app/lib/emulators/pseudoClassFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var walk = require('utils.walker').all;

module.exports = function pseudoClassFactory(type) {
var TYPE_NAME = 'pseudo-class-' + type + '__' + basis.genUID();

return {
/**@cut*/__debugName: TYPE_NAME,
getStates: function(AST) {
var allow = false;

//todo сделать оптимально - построить индекс по таким местам или не обходить оставшиеся узлы если стало понятно, что эмуляция нужна
walk(AST, {
PseudoClass: function(token) {
if (token.type == 'PseudoClass' && token.name == type) {
allow = true;
}
}
});

if (allow) {
var states = [];
var state = {};

state[type] = false;
Copy link
Member

Choose a reason for hiding this comment

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

module.exports = function pseudoClassFactory(type) {
  var TYPE_NAME = 'pseudo-class-' + type + '__' + basis.genUID();
  var typeState = basis.fn.wrapper(type);
...
  var states = [typeState(false), typeState(true)];

states.push(state);
state = {};
state[type] = true;
states.push(state);

return states;
}
},
handleToken: function(token, parent, root, sourceMap, mapper) {
if (token.type == 'PseudoClass' && token.name == type) {
token.type = 'Class';
token.name = TYPE_NAME;
}
},
emulate: function(token, parent, root, sourceMap, mapper, value) {
if (value) {
var sourceToken = sourceMap.get(token);

if (sourceToken && sourceToken.type == 'PseudoClass' && sourceToken.name == type) {
var mappedElements = mapper.bySelector(token);

if (mappedElements) {
mappedElements.forEach(function(element) {
element.classList.add(TYPE_NAME);
});
}
}
}
}
};
};
Loading