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

Added support for HTML className for Block objects #791

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/ci
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if [ -n "$CI" ]; then
echo "GITHUB_BASE_REF: $GITHUB_BASE_REF"
fi

if [ -n "$CI" ] && [ "$GITHUB_REF" == "refs/heads/master" ] && [ -z "$GITHUB_HEAD_REF" ]; then
if [ -n "$CI" ] && [ "$GITHUB_REF" == "refs/heads/main" ] && [ -z "$GITHUB_HEAD_REF" ]; then
$(base64 --decode <<< ZXhwb3J0IFNBVUNFX1VTRVJOQU1FPWJhc2VjYW1wX3RyaXgK)
$(base64 --decode <<< ZXhwb3J0IFNBVUNFX0FDQ0VTU19LRVk9MjY3OGE4NzMtNzJmNC00NzU2LTkzYjUtZjFhOGUyZTc3ODIxCg==)
else
Expand Down
10 changes: 5 additions & 5 deletions dist/trix-core.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/trix.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@charset "UTF-8";
/*
Trix 1.3.1
Copyright © 2020 Basecamp, LLC
Copyright © 2021 Basecamp, LLC
http://trix-editor.org/*/
trix-editor {
border: 1px solid #bbb;
Expand Down
8 changes: 4 additions & 4 deletions dist/trix.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/trix/models/html_parser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,10 @@ class Trix.HTMLParser extends Trix.BasicObject
while element and element isnt @containerElement
for attribute, config of Trix.config.blockAttributes when config.parse isnt false
if tagName(element) is config.tagName
if config.test?(element) or not config.test
attributes.push(attribute)
attributes.push(config.listAttribute) if config.listAttribute
if element.className is config.className or not config.className
if config.test?(element) or not config.test
attributes.push(attribute)
attributes.push(config.listAttribute) if config.listAttribute
element = element.parentNode
attributes.reverse()

Expand Down
6 changes: 3 additions & 3 deletions src/trix/views/block_view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ class Trix.BlockView extends Trix.ObjectView
if @attributes.length
nodes
else
{tagName} = Trix.config.blockAttributes.default
{tagName, className} = Trix.config.blockAttributes.default
attributes = dir: "rtl" if @block.isRTL()

element = makeElement({tagName, attributes})
element = makeElement({tagName, className, attributes})
element.appendChild(node) for node in nodes
[element]

createContainerElement: (depth) ->
attributeName = @attributes[depth]

{tagName} = getBlockConfig(attributeName)
{tagName, className} = getBlockConfig(attributeName)
attributes = dir: "rtl" if depth is 0 and @block.isRTL()

if attributeName is "attachmentGallery"
Expand Down
29 changes: 29 additions & 0 deletions test/src/unit/html_parser_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ testGroup "Trix.HTMLParser", ->
document = Trix.HTMLParser.parse(html).getDocument()
assert.documentHTMLEqual document, expectedHTML

test "parses block with className", ->
config =
blockWithClassName:
tagName: "div"
className: "class-name-1 class-name-2"

withBlockAttributeConfig config, ->
html = """<div class="class-name-1 class-name-2">a</div>"""
expectedHTML = """<div class="class-name-1 class-name-2"><!--block-->a</div>"""
document = Trix.HTMLParser.parse(html).getDocument()
assert.documentHTMLEqual document, expectedHTML

withTextAttributeConfig = (config = {}, fn) ->
{textAttributes} = Trix.config
originalConfig = {}
Expand All @@ -276,6 +288,23 @@ withTextAttributeConfig = (config = {}, fn) ->
else
delete textAttributes[key]

withBlockAttributeConfig = (config = {}, fn) ->
{blockAttributes} = Trix.config
originalConfig = {}

for key, value of config
originalConfig[key] = blockAttributes[key]
blockAttributes[key] = value

try
fn()
finally
for key, value of originalConfig
if value
blockAttributes[key] = value
else
delete blockAttributes[key]

getOrigin = ->
{protocol, hostname, port} = window.location
"#{protocol}//#{hostname}#{if port then ":#{port}" else ""}"