|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2025, by Samuel Williams. |
| 5 | + |
| 6 | +require 'utopia/content/builder' |
| 7 | +require 'utopia/content/tags' |
| 8 | +require 'xrb/builder' |
| 9 | + |
| 10 | +describe Utopia::Content::Builder do |
| 11 | + it "should inherit from XRB::Builder" do |
| 12 | + expect(Utopia::Content::Builder.superclass).to be == XRB::Builder |
| 13 | + end |
| 14 | + |
| 15 | + it "should accept positional arguments" do |
| 16 | + builder = Utopia::Content::Builder.new(nil, nil, nil, {}) |
| 17 | + |
| 18 | + expect(builder.parent).to be_nil |
| 19 | + expect(builder.tag).to be_nil |
| 20 | + expect(builder.node).to be_nil |
| 21 | + expect(builder.attributes).to be == {} |
| 22 | + end |
| 23 | + |
| 24 | + it "should default attributes to tag.to_hash" do |
| 25 | + tag = XRB::Tag.new('div', false, {'id' => 'test'}) |
| 26 | + |
| 27 | + builder = Utopia::Content::Builder.new(nil, tag, nil) |
| 28 | + |
| 29 | + expect(builder.attributes).to be == {'id' => 'test'} |
| 30 | + end |
| 31 | + |
| 32 | + it "should support fragment rendering via build_markup protocol" do |
| 33 | + builder = Utopia::Content::Builder.new(nil, nil, nil, {}) |
| 34 | + |
| 35 | + fragment = XRB::Builder.fragment do |builder| |
| 36 | + builder.inline('p') do |
| 37 | + builder.text "Hello from fragment" |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + builder.text(fragment) |
| 42 | + |
| 43 | + expect(builder.output).to be =~ /Hello from fragment/ |
| 44 | + expect(builder.output).not.to be =~ /</ |
| 45 | + end |
| 46 | + |
| 47 | + it "should track parent builders" do |
| 48 | + parent = Utopia::Content::Builder.new(nil, nil, nil, {}) |
| 49 | + child = Utopia::Content::Builder.new(parent, nil, nil, {}) |
| 50 | + |
| 51 | + expect(child.parent).to be == parent |
| 52 | + end |
| 53 | + |
| 54 | + it "should track tags" do |
| 55 | + builder = Utopia::Content::Builder.new(nil, nil, nil, {}) |
| 56 | + |
| 57 | + expect(builder.tags).to be == [] |
| 58 | + expect(builder).to be(:empty?) |
| 59 | + |
| 60 | + tag = XRB::Tag.new('div', false, {}) |
| 61 | + builder.tag_begin(tag) |
| 62 | + |
| 63 | + expect(builder.tags).to be == [tag] |
| 64 | + expect(builder).not.to be(:empty?) |
| 65 | + end |
| 66 | + |
| 67 | + it "should support deferred content" do |
| 68 | + builder = Utopia::Content::Builder.new(nil, nil, nil, {}) |
| 69 | + |
| 70 | + expect(builder.deferred).to be == [] |
| 71 | + |
| 72 | + deferred_tag = builder.defer { "deferred content" } |
| 73 | + |
| 74 | + expect(builder.deferred.size).to be == 1 |
| 75 | + expect(deferred_tag.name).to be == "utopia:deferred" |
| 76 | + expect(deferred_tag.attributes[:id]).to be == 0 |
| 77 | + end |
| 78 | + |
| 79 | + it "should write text content" do |
| 80 | + builder = Utopia::Content::Builder.new(nil, nil, nil, {}) |
| 81 | + |
| 82 | + builder.write("Hello ") |
| 83 | + builder.write("World") |
| 84 | + |
| 85 | + expect(builder.output).to be == "Hello World" |
| 86 | + end |
| 87 | + |
| 88 | + it "should escape regular text via text method" do |
| 89 | + builder = Utopia::Content::Builder.new(nil, nil, nil, {}) |
| 90 | + |
| 91 | + builder.text("<script>alert('xss')</script>") |
| 92 | + |
| 93 | + expect(builder.output).to be =~ /<script>/ |
| 94 | + expect(builder.output).not.to be =~ /<script>/ |
| 95 | + end |
| 96 | + |
| 97 | + it "should not escape objects with build_markup protocol" do |
| 98 | + builder = Utopia::Content::Builder.new(nil, nil, nil, {}) |
| 99 | + |
| 100 | + # Use XRB::Builder.fragment which implements build_markup |
| 101 | + fragment = XRB::Builder.fragment do |b| |
| 102 | + b.inline('p') { b.text "Safe HTML" } |
| 103 | + end |
| 104 | + |
| 105 | + builder.text(fragment) |
| 106 | + |
| 107 | + expect(builder.output).to be =~ /<p>Safe HTML<\/p>/ |
| 108 | + end |
| 109 | +end |
0 commit comments