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

Props with dashes in their names are handled as public attributes #202

Open
jsahlen opened this issue Feb 28, 2024 · 0 comments
Open

Props with dashes in their names are handled as public attributes #202

jsahlen opened this issue Feb 28, 2024 · 0 comments

Comments

@jsahlen
Copy link

jsahlen commented Feb 28, 2024

I'm working on a project where I utilize the ability to pass on a lot of attributes to my WebC components, but at the same time using props to control rendering and behavior of those components.

I ran into an issue when trying to use @attributes to automatically render passed attributes, where supposedly private props would be included in the final HTML output. I think that props that have dashes in them (and camelCased when using in the component) aren't marked as private correctly by WebC.

Here's a small test case:

page.webc:

<test-component
  mylowercaseattr="foo"
  my-public-attr="bar"
  @mylowercaseprop="baz"
  @my-dashed-prop="qux"
></test-component>

test-component.webc:

<script webc:type="js">
  console.log(webc.attributes);
  console.log(webc.filterPublicAttributes(webc.attributes));
  ('');
</script>
<div @attributes></div>

Result:

// console.log(webc.attributes)
{
  "myDashedProp": "qux",
  "mylowercaseattr": "foo",
  "mylowercaseprop": "baz",
  "myPublicAttr": "bar",
  "uid": "webc-Ht-95",
  "my-dashed-prop___webc_privacy": "private",
  "my-public-attr___webc_privacy": "public",
  "mylowercaseattr___webc_privacy": "public",
  "mylowercaseprop___webc_privacy": "private",
  "uid___webc_privacy": "private",
}

// console.log(webc.filterPublicAttributes(webc.attributes));
{
  "myDashedProp": "qux",
  "mylowercaseattr": "foo",
  "myPublicAttr": "bar",
}
<div mylowercaseattr="foo" myPublicAttr="bar" myDashedProp="qux"></div>

As you can see, myDashedProp is included in the output, because the property marking it as private within the attributes object is named my-dashed-prop___webc_privacy instead of myDashedProp___webc_privacy.

I'm guessing that these lines in attributeSerializer.js is where it goes wrong, but I don't feel I understand the internals of WebC enough to make a PR at this time.

static async normalizeAttributesForData(attrs) {
let newData = {};
for(let name in attrs) {
let value = attrs[name];
// prop does nothing
// prop-name becomes propName
// @prop-name and :prop-name prefixes should already be removed
newData[AttributeSerializer.camelCaseAttributeName(name)] = value;
// Maintain privacy in new object
let privacy = attrs[`${name}___webc_privacy`];
if(privacy) {
AttributeSerializer.setKeyPrivacy(newData, name, privacy);
}
}
return newData;
}

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

No branches or pull requests

1 participant