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

Fix component style attribute parser #45

Merged
merged 6 commits into from
Nov 27, 2017

Conversation

ianvieira
Copy link
Contributor

When we have a style attribute on the original HTML, the parsing was not correct, specially with background-image with an external URL, this was not loading because of a split by ':' what breaks the URL. I added an library that generates a more complete style tag using css-to-react-native.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.03%) to 99.206% when pulling f678c3f on ianvieira:master into faad529 on remarkablemark:master.

@remarkablemark
Copy link
Owner

remarkablemark commented Oct 26, 2017

Thanks for opening the pull request and bringing the bug to my attention @ianvieira

My question for you is do you think css-to-react-native is too specific? Will it transform the styles correctly if rendered in the DOM? Or does it make sense to roll out a custom parser that is built on top of css?

Also, since html-react-parser is isomorphic, how should the parsing work in the browser? Let me know what you think.

@remarkablemark remarkablemark added the bug Something isn't working label Oct 26, 2017
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.04%) to 99.2% when pulling 6146c8a on ianvieira:master into faad529 on remarkablemark:master.

@ianvieira
Copy link
Contributor Author

ianvieira commented Nov 23, 2017

@remarkablemark After using the new solution with the css-to-react-native, I've found some issues, so I tried another approach, that is more successful. Now I'm using AST to deconstruct the style string and then creating a new object with the values and property name in camel case. This new approach works faster as before and without the problem of using a isomorphic designed module.

What do you think about the new solution?

@remarkablemark
Copy link
Owner

@ianvieira Nice! I like the AST approach because it'll support the general use cases.

The solution looks great but I found something unfortunate about css-tree. The browser library is 165.7 kB minified which is huge.

The alternative css is 10.81 kB unminified.

I have a library style-to-object which is built on top of css. Could this be used instead? You might want to check out iterator.

I have a few more requests (if you would be so kind):

I want to thank you once more for looking into this and submitting the pull request. I really appreciate your time and effort. If you feel like you're swamped or have no time, I can always take over and lend a helping hand.

@ianvieira
Copy link
Contributor Author

ianvieira commented Nov 26, 2017

Of course, I can address these issues, in fact this will change will help me too, my application bundle is with 900kb and I'm trying to make it smaller. I'll do a new commit with the changes and also check if your library handles my biggest problem, the background with a base64 image, which was not being interpreted rightly.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.08%) to 99.153% when pulling 68cd565 on ianvieira:master into faad529 on remarkablemark:master.

…mel case and assert the string is a string
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.06%) to 99.18% when pulling 4522666 on ianvieira:master into faad529 on remarkablemark:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.06%) to 99.174% when pulling 10ff149 on ianvieira:master into faad529 on remarkablemark:master.

@ianvieira
Copy link
Contributor Author

@remarkablemark I believe now it's how you expected. I tried to address all your requests and also added an base64 image for test, this way we're able to test this edge case and also the url.

@remarkablemark
Copy link
Owner

@ianvieira Thanks for addressing the requests so quickly. I made a few comments and then let's get this thing merged in.

Oh and before I forget, were you able to verify if the style prefix for -ms works as expected?

@@ -5,6 +5,7 @@
*/
var utilities = require('./utilities');
var propertyConfig = require('./property-config');
var parser = require('style-to-object');
Copy link
Owner

Choose a reason for hiding this comment

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

Do you mind renaming parser to styleToObject just for keeping it explicit here?

for (var i = 0, declarationsLen = declarations.length; i < declarationsLen; i++) {
properties = declarations[i].trim().split(':');
parser(style, function(propName, propValue) {
styleObj[utilities.camelCase(propName)] = propValue;
Copy link
Owner

Choose a reason for hiding this comment

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

One thing about the style parser is that css comments is included in the iterator. Could we include an undefined check here?

if (propName && propValue) {
  styleObj[utilities.camelCase(propName)] = propValue;
}

lib/utilities.js Outdated
@@ -6,24 +6,19 @@
* @param {String} string - The string.
* @return {String}
*/

var _hyphenPattern = /-(.)/g;
Copy link
Owner

Choose a reason for hiding this comment

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

Would you mind moving this line above the JSDoc? This is because we can find easier to read the comment when it's next to the function.

package.json Outdated
@@ -30,7 +30,8 @@
],
"dependencies": {
"html-dom-parser": "0.1.2",
"react-dom-core": "0.0.2"
"react-dom-core": "0.0.2",
"style-to-object": "^0.2.0"
Copy link
Owner

Choose a reason for hiding this comment

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

Could you remove the ^? I'm following save-exact since I don't have a package-lock.json or yarn.lock checked in.

@ianvieira
Copy link
Contributor Author

ianvieira commented Nov 26, 2017 via email

@ianvieira
Copy link
Contributor Author

@remarkablemark I finished all the requested changes

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.06%) to 99.18% when pulling 3ca6681 on ianvieira:master into faad529 on remarkablemark:master.

@remarkablemark remarkablemark self-assigned this Nov 27, 2017
@remarkablemark remarkablemark merged commit 8f2bbd2 into remarkablemark:master Nov 27, 2017
@remarkablemark
Copy link
Owner

remarkablemark commented Nov 27, 2017

@ianvieira Awesome work! Also if you're looking to visualize your webpack bundle stats, check out Webpack Visualizer.

@ianvieira
Copy link
Contributor Author

@remarkablemark Thank you! Great tip! I have another question, when this new version will be available at NPM? Currently I'm using my repo as source and I want to change to the package again

@remarkablemark
Copy link
Owner

@ianvieira I'll try to release either tonight or tomorrow morning.

@remarkablemark
Copy link
Owner

@ianvieira I have released 0.4.1:

# npm
npm install html-react-parser@latest --save

# yarn
yarn upgrade html-react-parser --latest

d-lazarev pushed a commit to d-lazarev/html-react-parser that referenced this pull request Apr 5, 2019
Fix component style attribute parser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants