-
Notifications
You must be signed in to change notification settings - Fork 850
[HTML5 Article Renderer] Refactor Table Implementation #14002
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
Open
AllanOXDi
wants to merge
7
commits into
learningequality:develop
Choose a base branch
from
AllanOXDi:RefactorSafeHtmlTable
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
753c232
convert SafeHtmlTable to Vue component and remove window size class
AllanOXDi b4946bf
convert SafeHtmlTable to Vue component and remove window size class
AllanOXDi 9861dbd
update SafeHtmlTable tests
AllanOXDi 5c9895d
fix test
AllanOXDi 02ccb56
remove windowSizeClass tests
AllanOXDi 0c3829f
remove dynamic caption ID generation and aria-labelledby
AllanOXDi 3f07da4
remove unnecessary styleOverrides
AllanOXDi 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 hidden or 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 hidden or 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
54 changes: 0 additions & 54 deletions
54
packages/kolibri-common/components/SafeHTML/SafeHtmlTable.js
This file was deleted.
Oops, something went wrong.
135 changes: 135 additions & 0 deletions
135
packages/kolibri-common/components/SafeHTML/SafeHtmlTable.vue
This file contains hidden or 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,135 @@ | ||
| <template> | ||
|
|
||
| <div | ||
| class="table-container" | ||
| data-testid="table-container" | ||
| > | ||
| <table | ||
| :style="tableStyle" | ||
| v-bind="attributes" | ||
| > | ||
| <component :is="renderTableContent" /> | ||
| </table> | ||
| </div> | ||
|
|
||
| </template> | ||
|
|
||
|
|
||
| <script> | ||
|
|
||
| const { ELEMENT_NODE } = | ||
| typeof window !== 'undefined' && window.Node ? window.Node : { ELEMENT_NODE: 1 }; | ||
|
|
||
| export default { | ||
| name: 'SafeHtmlTable', | ||
| props: { | ||
| node: { | ||
| required: true, | ||
| validator: prop => typeof prop === 'object' && prop !== null, | ||
| }, | ||
| attributes: { | ||
| type: Object, | ||
| required: true, | ||
| }, | ||
| mapNode: { | ||
| type: Function, | ||
| required: true, | ||
| }, | ||
| mapChildren: { | ||
| type: Function, | ||
| required: true, | ||
| }, | ||
| }, | ||
|
|
||
| computed: { | ||
| tableStyle() { | ||
| const firstRow = this.node.querySelector && this.node.querySelector('tr'); | ||
| const colCount = firstRow ? firstRow.children.length : 0; | ||
| let tableWidth = '640px'; | ||
| if (colCount > 3) { | ||
| tableWidth = `${colCount * 200}px`; | ||
| } | ||
|
|
||
| return { width: tableWidth }; | ||
| }, | ||
|
|
||
| renderTableContent() { | ||
| const vm = this; | ||
| return { | ||
| functional: true, | ||
| render(h) { | ||
| const tableChildren = []; | ||
|
Member
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. Now that we're just adding |
||
|
|
||
| for (let i = 0; i < vm.node.childNodes.length; i++) { | ||
| const childNode = vm.node.childNodes[i]; | ||
|
|
||
| if (vm.isCaption(childNode)) { | ||
| const captionAttrs = vm.getCaptionAttrs(childNode); | ||
| const captionChildren = vm.mapChildren(childNode.childNodes); | ||
|
|
||
| tableChildren.push( | ||
| h( | ||
| 'caption', | ||
| { | ||
| attrs: captionAttrs, | ||
| class: ['safe-html', captionAttrs.class].filter(Boolean), | ||
| }, | ||
| captionChildren, | ||
| ), | ||
| ); | ||
| } else if (vm.isElementNode(childNode)) { | ||
| tableChildren.push(vm.mapNode(childNode)); | ||
| } | ||
| } | ||
|
|
||
| return tableChildren; | ||
| }, | ||
| }; | ||
| }, | ||
| }, | ||
|
|
||
| methods: { | ||
| isCaption(node) { | ||
| return ( | ||
| node && | ||
| node.nodeType === ELEMENT_NODE && | ||
| typeof node.tagName === 'string' && | ||
| node.tagName.toLowerCase() === 'caption' | ||
| ); | ||
| }, | ||
|
|
||
| isElementNode(node) { | ||
| return node && node.nodeType === ELEMENT_NODE; | ||
| }, | ||
|
|
||
| getCaptionAttrs(node) { | ||
| const captionAttrs = {}; | ||
|
|
||
| for (const attr of node.attributes) { | ||
| const name = attr.name.toLowerCase(); | ||
| if (name === 'id') continue; | ||
| captionAttrs[attr.name] = attr.value; | ||
| } | ||
|
|
||
| return captionAttrs; | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| </script> | ||
|
|
||
|
|
||
| <style scoped> | ||
|
|
||
| .table-container { | ||
| margin: 1em 0; | ||
| overflow-x: auto; | ||
| } | ||
|
|
||
| caption.safe-html { | ||
| padding: 2px 0 4px; | ||
| font-weight: bold; | ||
| text-align: center; | ||
| } | ||
|
|
||
| </style> | ||
This file contains hidden or 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
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.
This implementation looks correct, but architecturally it introduces a potential source of confusion. We’re creating a functional component inside a computed property of a regular component—essentially building a mini component-factory within the component. This adds layers of complexity we’re trying to move away from.
A cleaner alternative would be to generate the table elements dynamically (not programmatically) and then assigning relevant styling defined within the component itself, thus the redundancy of the style override props.
Our goal is to migrate the current functional component (.js) into an Options API or, ideally, a composable-based component structure. This will help keep the codebase more consistent and maintainable going forward.