-
Notifications
You must be signed in to change notification settings - Fork 68
rx-html: Resolve naming conflicts between HTML and SVG attributes #3986
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
xerial
wants to merge
2
commits into
main
Choose a base branch
from
i3953
base: main
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 1 commit
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 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,124 @@ | ||
| # airframe-rx-html | ||
|
|
||
| A reactive HTML/SVG rendering library for Scala. | ||
|
|
||
| ## Import Options | ||
|
|
||
| ### Separate Imports (Traditional approach) | ||
|
|
||
| When using HTML and SVG separately, you can import them individually: | ||
|
|
||
| ```scala | ||
| import wvlet.airframe.rx.html.all.* // HTML tags and attributes | ||
| import wvlet.airframe.rx.html.svgTags.* // SVG tags | ||
| import wvlet.airframe.rx.html.svgAttrs.* // SVG attributes | ||
| ``` | ||
|
|
||
| ### Unified Import (New approach) | ||
|
|
||
| To use both HTML and SVG together without naming conflicts: | ||
|
|
||
| ```scala | ||
| import wvlet.airframe.rx.html.unifiedAll.* | ||
| ``` | ||
|
|
||
| ## Handling Naming Conflicts | ||
|
|
||
| The unified import resolves naming conflicts between HTML and SVG by prefixing SVG versions with `svg`: | ||
|
|
||
| ### Simple Attribute Conflicts | ||
| These attributes exist in both HTML and SVG namespaces: | ||
|
|
||
| | Attribute | HTML Version | SVG Version | | ||
| |-----------|--------------|-------------| | ||
| | class | `class` or `cls` | `svgClass` | | ||
| | style | `style` | `svgStyle` | | ||
| | type | `type` | `svgType` | | ||
| | xmlns | `xmlns` | `svgXmlns` | | ||
| | max | `max` | `svgMax` | | ||
| | min | `min` | `svgMin` | | ||
| | height | `height` | `svgHeight` | | ||
| | width | `width` | `svgWidth` | | ||
| | id | `id` | `svgId` | | ||
|
|
||
| ### Tag/Attribute Conflicts | ||
| These names exist as both tags and attributes: | ||
|
|
||
| | Name | HTML Attribute | SVG Tag | SVG Attribute | | ||
| |------|----------------|---------|---------------| | ||
| | title | `title` | `svgTitle` | N/A | | ||
| | pattern | `pattern` | `svgPattern` | N/A | | ||
| | filter | N/A | `svgFilter` | `svgFilterAttr` | | ||
| | mask | N/A | `svgMask` | `svgMaskAttr` | | ||
| | clipPath | N/A | `svgClipPath` | `svgClipPathAttr` | | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Using Unified Import | ||
|
|
||
| ```scala | ||
| import wvlet.airframe.rx.html.unifiedAll.* | ||
|
|
||
| val mixedContent = div( | ||
| cls -> "container", // HTML class attribute | ||
| style -> "padding: 20px;", // HTML style attribute | ||
| h1("Data Visualization"), | ||
| svg( | ||
| svgWidth -> "400", // SVG width attribute | ||
| svgHeight -> "300", // SVG height attribute | ||
| svgClass -> "chart", // SVG class attribute | ||
| svgStyle -> "border: 1px solid #ccc;", // SVG style attribute | ||
| g( | ||
| transform -> "translate(50, 50)", | ||
| rect( | ||
| x -> "0", | ||
| y -> "0", | ||
| svgWidth -> "300", | ||
| svgHeight -> "200", | ||
| fill -> "steelblue" | ||
| ), | ||
| text( | ||
| x -> "150", | ||
| y -> "100", | ||
| textAnchor -> "middle", | ||
| svgStyle -> "fill: white; font-size: 24px;", | ||
| "Chart Title" | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| ``` | ||
|
|
||
| ### Traditional Separate Imports | ||
|
|
||
| If you prefer to keep HTML and SVG separate: | ||
|
|
||
| ```scala | ||
| import wvlet.airframe.rx.html.all.* | ||
|
|
||
| val htmlContent = div( | ||
| cls -> "container", | ||
| style -> "padding: 20px;", | ||
| h1("Hello World") | ||
| ) | ||
| ``` | ||
|
|
||
| ```scala | ||
| import wvlet.airframe.rx.html.svgTags.* | ||
| import wvlet.airframe.rx.html.svgAttrs.* | ||
|
|
||
| val svgContent = svg( | ||
| width -> "100", | ||
| height -> "100", | ||
| circle( | ||
| cx -> "50", | ||
| cy -> "50", | ||
| r -> "40", | ||
| fill -> "red" | ||
| ) | ||
| ) | ||
| ``` | ||
|
|
||
| ## Namespace Handling | ||
|
|
||
| All SVG attributes and tags are automatically created with the correct SVG namespace (`http://www.w3.org/2000/svg`), ensuring proper rendering in browsers. |
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
61 changes: 61 additions & 0 deletions
61
airframe-rx-html/src/main/scala/wvlet/airframe/rx/html/UnifiedTags.scala
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,61 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package wvlet.airframe.rx.html | ||
|
|
||
| /** | ||
| * Unified tags and attributes that resolves naming conflicts between HTML and SVG. | ||
| * For conflicting names, SVG versions are prefixed with 'svg'. | ||
| * | ||
| * Due to complex inheritance conflicts where some names exist as both tags and attributes, | ||
| * we selectively import and re-export members rather than extending all traits. | ||
| */ | ||
| trait UnifiedTags extends HtmlTags with HtmlAttrs with RxEmbedding { | ||
| // Import SVG tags except the conflicting ones | ||
| export svgTags.{ | ||
| title => _, pattern => _, filter => _, mask => _, clipPath => _, | ||
| * | ||
| } | ||
|
|
||
| // Import SVG attributes except the conflicting ones | ||
| export svgAttrs.{ | ||
| `class` => _, style => _, `type` => _, xmlns => _, max => _, min => _, | ||
| height => _, width => _, id => _, filter => _, mask => _, clipPath => _, | ||
| * | ||
| } | ||
|
|
||
| // SVG attributes with prefixed names for conflicts | ||
| lazy val svgClass = new HtmlAttributeOf("class", Namespace.svg) | ||
| lazy val svgStyle = new HtmlAttributeOf("style", Namespace.svg) | ||
| lazy val svgType = new HtmlAttributeOf("type", Namespace.svg) | ||
| lazy val svgXmlns = new HtmlAttributeOf("xmlns", Namespace.svg) | ||
| lazy val svgMax = new HtmlAttributeOf("max", Namespace.svg) | ||
| lazy val svgMin = new HtmlAttributeOf("min", Namespace.svg) | ||
| lazy val svgHeight = new HtmlAttributeOf("height", Namespace.svg) | ||
| lazy val svgWidth = new HtmlAttributeOf("width", Namespace.svg) | ||
| lazy val svgId = new HtmlAttributeOf("id", Namespace.svg) | ||
|
|
||
| // SVG tags/attributes that conflict with HTML attributes | ||
| lazy val svgTitle = svgTags.title // SVG title tag | ||
| lazy val svgPattern = svgTags.pattern // SVG pattern tag | ||
| lazy val svgFilter = svgTags.filter // SVG filter tag | ||
| lazy val svgMask = svgTags.mask // SVG mask tag | ||
| lazy val svgClipPath = svgTags.clipPath // SVG clipPath tag | ||
|
|
||
| // SVG attributes for the conflicting tag names | ||
| lazy val svgFilterAttr = new HtmlAttributeOf("filter", Namespace.svg) | ||
| lazy val svgMaskAttr = new HtmlAttributeOf("mask", Namespace.svg) | ||
| lazy val svgClipPathAttr = new HtmlAttributeOf("clip-path", Namespace.svg) | ||
| } | ||
|
|
||
| // Object moved to syntaxes.scala | ||
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
Oops, something went wrong.
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 comment
// Object moved to syntaxes.scalaappears to be a leftover from development. It can be removed to improve the clarity of this new file.