-
Notifications
You must be signed in to change notification settings - Fork 477
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
Utils.insertStylesheet #280
Conversation
Seems like we may want two functions here. InsertCSS and InsertCSSPath as the way the it worked before this commit is to |
Yeah this breaks the previous function (see the failing test). This could be useful and I'll add it but:
|
Oh crap, I thought the existing Utils.insertCSS was supposed to work this way - do people really want to add single styles? - , hence why I replaced (fixed ha!) it. Obviously I didn't look carefully enough, I'm sorry! Anyway, in this case, I'm not even sure if you should add it, as it could encourage people to use Framer with stylesheets ... which probably is not a good idea. |
Test proposal: describe "insertStyleSheet", ->
it "should work", ->
Utils.insertStyleSheet("https://fonts.googleapis.com/icon?family=Material+Icons")
document.styleSheets[document.styleSheets.length - 1].href.should.notEqual(null) Pros & Cons:
As an alternative, .type could be evaluated, which should return a string of "text/css". This would also work with local stylesheets, however the downside of this method is that it doesn't care if the href is valid or not. |
I believe all that you can accurately do is test that the node was added to
|
Test proposal v2:describe "insertStyleSheet", ->
it "should be added", ->
Utils.delay 0.1, ->
styleSheetCount = document.styleSheets.length
Utils.insertStyleSheet("https://fonts.googleapis.com/icon?family=Material+Icons")
document.styleSheets.length.should.equal(styleSheetCount + 1) This works reliable, but still only for external stylesheets! To be honest, I can't get my head around why local stylesheets seemingly don't increase
Name proposals:
|
Isn't this just an async thing? So to test it you'll have to wait for the style sheet to load (use the Otherwise, I'm happy to merge this. |
@@ -513,6 +513,16 @@ Utils.insertCSS = (css) -> | |||
Utils.domComplete -> | |||
document.body.appendChild(styleElement) | |||
|
|||
Utils.insertStyleSheet = (path) -> | |||
|
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.
Maybe change path
to url
as path implies it's local.
#4 #1 To me it seems like Utils.insertStyleSheet = (url) ->
styleElement = document.createElement("link")
styleElement.rel = "stylesheet"
styleElement.type = "text/css"
styleElement.href = url
Utils.domComplete ->
document.body.appendChild(styleElement)
styleElement.onload = ->
return true ... which should make it testable by doing something like this ... describe "insertStyleSheet", ->
it "should be added", ->
Utils.insertStyleSheet("https://fonts.googleapis.com/icon?family=Material+Icons").should.equal(true)
Utils.insertStyleSheet("bogusUrl").should.notEqual(true)
Utils.insertStyleSheet("mdl/material.css").should.equal(true) ... but since the stylesheet obviously hasn't been loaded at the time of calling the Am I missing something here? |
Yep. Let me show you, I'll make a JSFiddle to explain. |
Here you go: https://jsfiddle.net/LtgLw3pv/1/
If this clicks, you're pretty close to being a JavaScript ninja :-) |
Also:
This is a sometimes confusing part of CoffeeScript: implicit returns:
It's sometimes handy, and sometimes it bites you. More here: http://blog.artillery.com/2014/06/working-with-coffeescript-common-gotchas.html |
Thanks for taking the time, Koen! Really appreciate it. :) And I'm truly sorry for causing so much trouble for a function only maybe a handful of people will ever use. As I said before, using Framer with stylesheets - the way it works right now - is borderline useless anyway but I believe this topic holds some untapped potential.
Believe me or not, I grasped the concept of implicit returns rather quickly some time ago. Actually, I think it's pretty nifty and quite in line with the overall concept and semantics(?) of coffeescript. What confused me in this particular case was the additional component of time, which gave me some head-scratching "inconsistencies". The unexpected implicit returns were just a sign that my code wasn't working properly. |
Don't worry, getting you to be able to contribute is an upside for me too :-) |
I'll take that as a compliment, yet I'd like to dampen your expectations: With 25 I'm pretty late to the party anyway and after failing to come up with a working solution to this problem, it seems like I'll never become a decent coder - let alone a code ninja. Anyway, after putting way too many hours into solving this problem, coming up with four vastly different approaches - with at least one or two promising ones - I'm drained, out of ideas and I finally have to give up. I think one major piece I'm missing is how to successfully pass an argument (url) to a function ( Utils.insertStylesheet = (url, callback) ->
styleElement = document.createElement("link")
styleElement.rel = "stylesheet"
styleElement.type = "text/css"
styleElement.href = url
Utils.domComplete ->
document.body.appendChild(styleElement)
styleElement.addEventListener "load", ->
callback()
confirm = ->
return true
Utils.insertStylesheet("https://fonts.googleapis.com/icon?family=Material+Icons") ->
print confirm() # should equal true
(Also, now that I'm looking at this code again, I think this would also need a callback timeout for testing non-working URLs) |
Tests are hard and take practice. One main thing. Make sure you add the event listener to the Dom object On Wed, Dec 30, 2015 at 11:10 AM marckrenn notifications@github.com wrote:
|
Haha, true. That's (unfortunately) not causing the trouble, though ;) |
Utils.insertStylesheet = (url, callback) ->
styleElement = document.createElement("link")
styleElement.rel = "stylesheet"
styleElement.type = "text/css"
styleElement.href = url
styleElement.addEventListener "load", ->
callback() if callback? # can someone confirm this line?
Utils.domComplete ->
document.body.appendChild(styleElement)
Utils.confirmStylesheet = ->
return true
Utils.insertStylesheet "https://fonts.googleapis.com/icon?family=Material+Icons", ->
print Utils.confirmStylesheet() # returns true WHAT THE HELL. Now onto testing non-working URLs ... |
Utils.insertStylesheetUtils.insertStylesheet = (url, callback) ->
styleElement = document.createElement("link")
styleElement.rel = "stylesheet"
styleElement.type = "text/css"
styleElement.href = url
styleElement.addEventListener "load", ->
callback() if callback?
Utils.domComplete ->
document.body.appendChild(styleElement)
Utils.insertStylesheet.inserted = -> # this is a terrible solution/wording, right?
true # do you prefer return true?
Well, I guess I should ask you about Framer's dom event wrapper. :) Test:describe "insertStylesheet", ->
it "should be inserted", ->
Utils.insertStylesheet "https://fonts.googleapis.com/icon?family=Material+Icons", ->
Utils.insertStylesheet.inserted().should.equal(true)
Utils.insertStylesheet "foo://bar", ->
Utils.insertStylesheet.inserted().should.notEqual(true) # legit?
Utils.insertStylesheet "framer/style.css", ->
Utils.insertStylesheet.inserted().should.equal(true) Do you think this will do @koenbok ? Framer project: |
Test returns the following:
Is there a "save" CSS somewhere in btw love your guys' humbleness 😂 |
Please see http://share.framerjs.com/u8u9oefbf3mi/