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

Support inline styles when generating HTML #64

Merged
merged 10 commits into from
Jun 14, 2018
Merged

Support inline styles when generating HTML #64

merged 10 commits into from
Jun 14, 2018

Conversation

yuvipanda
Copy link
Contributor

Fixes #14

@yuvipanda yuvipanda changed the title Add back support for inline styles Support inline styles when generating HTML Jun 2, 2018
@yuvipanda
Copy link
Contributor Author

The errors are due to lack of ordering in dictionaries. I think FrozenDict should subclass OrderedDict instead of dict. I'll try to fix that up later today.

Copy link
Member

@MSeal MSeal left a comment

Choose a reason for hiding this comment

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

Looks good, only the pop and test comments need action / answered for 👍

vdom/core.py Outdated

# Validate that all children are VDOMs or strings
if not all([isinstance(c, (VDOM, string_types[:])) for c in self.children]):
raise ValueError('Children must be a list of VDOM objects or strings')

# All style keys & values must be strings
if not all([
Copy link
Member

Choose a reason for hiding this comment

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

[ shouldn't be necessary here (not that it matters much) as all can work against generators

vdom/core.py Outdated
@@ -176,9 +194,15 @@ def from_dict(cls, value):
validate(instance=value, schema=VDOM_SCHEMA, cls=Draft4Validator)
except ValidationError as e:
raise ValidationError(_validate_err_template.format(VDOM_SCHEMA, e))
attributes = value.get('attributes', {})
if 'style' in attributes:
style = attributes.pop('style')
Copy link
Member

Choose a reason for hiding this comment

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

Why pop? Do you want to remove the attribute from the dict?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do! Since style is a dict, while all other attributes are strings.

Copy link
Member

Choose a reason for hiding this comment

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

I meant you're removing elements from an argument dict that a caller may not expect to be mutated. Usually you want to avoid this side-effect for a function and either copy the dict first or make a new dict without the key when removing.

},
title='Test'
).to_html() == '<div style="background-color: pink; color: white" title="Test"><p>Hello world</p></div>'

Copy link
Member

Choose a reason for hiding this comment

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

Can we add a test for the single / double quotes you mention above?


def __init__(self, tag_name, attributes=None, children=None, key=None, schema=None):
def __init__(self, tag_name, attributes=None, style=None, children=None, key=None, schema=None):
Copy link
Member

Choose a reason for hiding this comment

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

When adding a constructor argument to an already established class I tend to add it to the end of the list to avoid positional constructor issues for pre-existing callers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree generally, but in this case:

  1. We have made no releases since introducing the current signature
  2. The current signature is a full break from the previous one

So I think it's ok to put this where it is, since I think those two are more related than children.

If you feel strongly about it I'm happy to change it :)

Copy link
Member

Choose a reason for hiding this comment

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

No strong feelings, just thought I'd point it out if you hadn't thought about it

@yuvipanda
Copy link
Contributor Author

@MSeal ok, I think I got everything!

@yuvipanda
Copy link
Contributor Author

More py2 issues! Will take a look shortly

@yuvipanda
Copy link
Contributor Author

Fixed for real I think!

Copy link
Member

@MSeal MSeal left a comment

Choose a reason for hiding this comment

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

Go merge whenever you want -- thanks for fixing those items!

@rgbkrk rgbkrk merged commit 92a9781 into nteract:master Jun 14, 2018
@rgbkrk
Copy link
Member

rgbkrk commented Jun 14, 2018

Oh heck yes!

'background-color': 'pink',
'color': 'white',
# Quotes should be entity escaped
'font-family': "'something something'"
Copy link
Member

Choose a reason for hiding this comment

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

Oops. I just realized this is wrong, when used within the notebook. On the VDOM spec, these have to match the DOM attribute names which are camelCase.

The to_html is working, the in-notebook version is not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rgbkrk this is a consequence (sortof) of #31 right? so the 'right' thing to do is regardless of input, the output is camelCase for the VDOM JSON and kebab case for html generation. Is that right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or to begin with, let's canonicalize on camelCase, and convert to kebab case for HTML output only?

Copy link
Member

Choose a reason for hiding this comment

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

regardless of input, the output is camelCase for the VDOM JSON and kebab case for html generation. Is that right?

That's correct.

let's canonicalize on camelCase, and convert to kebab case for HTML output only?

Yes please!

@yuvipanda
Copy link
Contributor Author

Thank you for the review, @MSeal!

@yuvipanda yuvipanda deleted the style-dom branch June 14, 2018 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants