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
fluent-dom: Simplify DOM Overlays #71
Conversation
74279a9
to
0ff6d0b
Compare
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.
I only have a few cosmetic changes that don't require another round of review, so approving.
fluent-dom/src/overlay.js
Outdated
* The goal of overlay is to move the children of `translationElement` | ||
* into `sourceElement` such that `sourceElement`'s own children are not | ||
* replaced, but only have their text nodes and their attributes modified. | ||
* Sanitize the `translationElement` according to the following rules: |
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.
translationFragment
'cause that's what the paramater is.
fluent-dom/src/overlay.js
Outdated
const result = translationElement.ownerDocument.createDocumentFragment(); | ||
|
||
// Take one node from translationElement at a time and check it against | ||
function overlay(translationFragment, sourceElement) { |
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.
sanitzeWithLove
fluent-dom/src/overlay.js
Outdated
} | ||
// If a child of the same type exists in sourceElement, use it as the base | ||
// for the resultChild. | ||
const sourceChild = popChild(sourceElement, childNode.localName); |
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.
I know I said the opposite, but how about shiftElementOfName
?
fluent-dom/src/overlay.js
Outdated
for (const attr of Array.from(translationElement.attributes)) { | ||
if (isAttrNameAllowed(attr.name, sourceElement)) { | ||
sourceElement.setAttribute(attr.name, attr.value); | ||
// Explicitly ignore nested HTML. |
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.
This comment means well, but you still have to know what the code does per spec.
How about // Serialize to a single TextNode
fluent-dom/test/overlay_test.js
Outdated
assert.equal(element.innerHTML, 'FOO <em>BAR</em> BAZ'); | ||
}); | ||
|
||
test('allowed element with nested HTML', function() { |
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.
... with forbidden nested HTML'.
I read this as "allowed ..yadda yadda"
Make DOM overlay logic less complex and more predictable by making the following changes: - Only children of the white-listed types are allowed now. It's not possible anymore to put elements of other types in the source HTML to make exceptions. - The identity of the source element's children is explicitly not kept anymore. This allows us to treat the translation DocumentFragment as the reference for iteration over child nodes. - The overlay function is also no longer recursive. Any nested HTML will be lost and only its textContent will be preserved.
462cb70
to
40be935
Compare
This is a WIP. I'm working on a second commit which changes the underlying logic even further.This PR reduces the complexity of the overlay logic by making the following changes:
Only children of the white-listed types are allowed now. It's not possible anymore to put elements of other types in the source HTML to make exceptions.
The identity of the source element's children is explicitly not kept anymore. This allows us to treat the translation
DocumentFragment
as the reference for iteration over child nodes.The
overlay
function is also no longer recursive. Any nested HTML will be lost and only itstextContent
will be preserved.