-
Notifications
You must be signed in to change notification settings - Fork 9
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
bug/issue 170 handle support for element properties #184
Merged
thescientist13
merged 2 commits into
master
from
feature/issue-170-adding-element-props
Jan 20, 2025
Merged
Changes from all commits
Commits
Show all changes
2 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 |
---|---|---|
|
@@ -144,7 +144,6 @@ async function getTagName(moduleURL) { | |
} | ||
|
||
async function initializeCustomElement(elementURL, tagName, node = {}, definitions = [], isEntry, props = {}) { | ||
const { attrs = [], childNodes = [] } = node; | ||
|
||
if (!tagName) { | ||
const depth = isEntry ? 1 : 0; | ||
|
@@ -161,11 +160,7 @@ async function initializeCustomElement(elementURL, tagName, node = {}, definitio | |
if (element) { | ||
const elementInstance = new element(data); // eslint-disable-line new-cap | ||
|
||
elementInstance.childNodes = childNodes; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. amazing 😊 |
||
|
||
attrs.forEach((attr) => { | ||
elementInstance.setAttribute(attr.name, attr.value); | ||
}); | ||
Object.assign(elementInstance, node); | ||
|
||
await elementInstance.connectedCallback(); | ||
|
||
|
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,37 @@ | ||
/* | ||
* Use Case | ||
* Run wcc against a component that passes properties to a child component. | ||
* | ||
* User Result | ||
* Should return the expected HTML output based on the content of the passed property. | ||
* | ||
* User Workspace | ||
* src/ | ||
* index.js | ||
* renderer.js | ||
* components/ | ||
* prop-passer.js | ||
* prop-receiver.js | ||
*/ | ||
|
||
import chai from 'chai'; | ||
import { JSDOM } from 'jsdom'; | ||
import { renderToString } from '../../../src/wcc.js'; | ||
|
||
const expect = chai.expect; | ||
|
||
describe('Run WCC For ', function () { | ||
const LABEL = 'Custom Element w/ Element Properties'; | ||
let dom; | ||
|
||
before(async function () { | ||
const { html } = await renderToString(new URL('./src/index.js', import.meta.url)); | ||
dom = new JSDOM(html); | ||
}); | ||
|
||
describe(LABEL, function () { | ||
it('should have a prop-receiver component with a heading tag with text content equal to "bar"', function () { | ||
expect(dom.window.document.querySelector('prop-receiver h2').textContent).to.equal('bar'); | ||
}); | ||
}); | ||
}); |
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,10 @@ | ||
import { html, render } from '../renderer.js'; | ||
|
||
export default class PropPasser extends HTMLElement { | ||
connectedCallback() { | ||
const data = { foo: 'bar' }; | ||
render(html`<prop-receiver .data=${data}></prop-receiver>`, this); | ||
} | ||
} | ||
|
||
customElements.define('prop-passer', PropPasser); |
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,9 @@ | ||
import { html, render } from '../renderer.js'; | ||
|
||
export default class ProperReceiver extends HTMLElement { | ||
connectedCallback() { | ||
render(html`<h2>${this.data.foo}</h2>`, this); | ||
} | ||
} | ||
|
||
customElements.define('prop-receiver', ProperReceiver); |
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,10 @@ | ||
import './components/prop-passer.js'; | ||
import './components/prop-receiver.js'; | ||
|
||
export default class ElementProps extends HTMLElement { | ||
connectedCallback() { | ||
this.innerHTML = '<prop-passer></prop-passer>'; | ||
} | ||
} | ||
|
||
customElements.define('element-props', ElementProps); |
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,90 @@ | ||
import { parseFragment } from 'parse5'; | ||
|
||
function generateUUID() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
const r = Math.floor(Math.random() * 16); | ||
const v = c === 'x' ? r : (r % 4) + 8; | ||
return v.toString(16); | ||
}); | ||
} | ||
|
||
function removeAttribute(element, attribute) { | ||
element.attrs = element.attrs.filter((attr) => attr.name !== attribute); | ||
} | ||
|
||
function handlePropertyAttribute(element, attribute, value, deps) { | ||
const propName = attribute.substring(1); | ||
removeAttribute(element, attribute); | ||
if (!element.props) { element.props = {}; } | ||
element[propName] = deps[value] ?? value; | ||
} | ||
|
||
function buildStringFromTemplate(template) { | ||
const { strings, values } = template; | ||
|
||
if (!strings || !values) { | ||
return { string: '', deps: {} }; | ||
} | ||
|
||
const stringParts = []; | ||
const deps = {}; | ||
let isElement = false; | ||
|
||
strings.reduce((acc, stringAtIndex, index) => { | ||
acc.push(stringAtIndex); | ||
|
||
isElement = | ||
stringAtIndex.includes('<') || stringAtIndex.includes('>') | ||
? stringAtIndex.lastIndexOf('<') > stringAtIndex.lastIndexOf('>') | ||
: isElement; | ||
|
||
const valueAtIndex = values[index]; | ||
|
||
if (valueAtIndex != null) { | ||
const isPrimitive = typeof valueAtIndex === 'string' || typeof valueAtIndex === 'number'; | ||
const valueKey = isPrimitive ? null : generateUUID() + index; | ||
const lastPart = acc[acc.length - 1]; | ||
const needsQuotes = isElement && !lastPart.endsWith('"'); | ||
acc.push(`${needsQuotes ? '"' : ''}${valueKey !== null ? valueKey : valueAtIndex}${needsQuotes ? '"' : ''}`); | ||
|
||
if (valueKey) { | ||
deps[valueKey] = valueAtIndex; | ||
} | ||
} | ||
return acc; | ||
}, stringParts); | ||
|
||
return { string: stringParts.join(''), deps }; | ||
} | ||
|
||
function setAttributes(childNodes, deps) { | ||
childNodes.forEach((element, index)=>{ | ||
const { attrs, nodeName } = element; | ||
if (nodeName === '#comment') { return; } | ||
attrs?.forEach(({ name, value }) => { | ||
if (name.startsWith('.')) { | ||
handlePropertyAttribute(childNodes[index], name, value, deps); | ||
} | ||
}); | ||
if (element.childNodes) { | ||
setAttributes(element.childNodes, deps); | ||
} | ||
}); | ||
} | ||
|
||
export function render(content, container) { | ||
const { string, deps } = buildStringFromTemplate(content); | ||
const parsedContent = parseFragment(string); | ||
|
||
setAttributes(parsedContent.childNodes, deps); | ||
const template = document.createElement('template'); | ||
template.content.childNodes = parsedContent.childNodes; | ||
container.appendChild(template.content.cloneNode(true)); | ||
} | ||
|
||
export const html = (strings, ...values) => { | ||
return { | ||
strings, | ||
values | ||
}; | ||
}; |
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,33 @@ | ||
/* | ||
* Use Case | ||
* Run wcc against a component which sets an attribute of a child heading. | ||
* | ||
* User Result | ||
* Should return a component with a child h2 with the expected attribute and attribute value. | ||
* | ||
* User Workspace | ||
* src/ | ||
* index.js | ||
*/ | ||
|
||
import chai from 'chai'; | ||
import { JSDOM } from 'jsdom'; | ||
import { renderToString } from '../../../src/wcc.js'; | ||
|
||
const expect = chai.expect; | ||
|
||
describe('Run WCC For ', function () { | ||
const LABEL = 'Custom Element using setAttribute'; | ||
let dom; | ||
|
||
before(async function () { | ||
const { html } = await renderToString(new URL('./src/index.js', import.meta.url)); | ||
dom = new JSDOM(html); | ||
}); | ||
|
||
describe(LABEL, function () { | ||
it('should have a heading tag with the "foo" attribute equal to "bar"', function () { | ||
expect(dom.window.document.querySelector('set-attribute-element h2').getAttribute('foo')).to.equal('bar'); | ||
}); | ||
}); | ||
}); |
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,10 @@ | ||
export default class SetAttributeElement extends HTMLElement { | ||
|
||
connectedCallback() { | ||
const heading = document.createElement('h2'); | ||
heading.setAttribute('foo', 'bar'); | ||
this.appendChild(heading); | ||
} | ||
} | ||
|
||
customElements.define('set-attribute-element', SetAttributeElement); |
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.
Just to confirm, are all the changes here just removing comments?
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.
Yes, this is correct!