-
Notifications
You must be signed in to change notification settings - Fork 2
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
smelukov
wants to merge
3
commits into
basisjs:master
Choose a base branch
from
smelukov:nthChild
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,4 @@ module.exports = function pseudoContentFactory(pseudoContent) { | |
} | ||
}); | ||
} | ||
} | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.