# frozen_string_literal: true

require_relative "../test_helper"

module Parser
  class AttributesTest < Minitest::Spec
    include SnapshotUtils

    test "attributes" do
      assert_parsed_snapshot(%(<div id="hello" class="container p-3"></div>))
    end

    test "attribute with dashed name" do
      assert_parsed_snapshot(%(<div data-id="1"></div>))
    end

    test "duplicate attributes" do
      assert_parsed_snapshot(%(<div class="hello" class="container p-3"></div>))
    end

    test "attribute with no quotes value and whitespace and self-closing tag" do
      assert_parsed_snapshot("<img value=hello />")
    end

    test "attribute with no quotes value, no whitespace and self-closing tag" do
      assert_parsed_snapshot("<img value=hello/>")
    end

    test "attribute with no quotes value, no whitespace, and non self-closing tag" do
      assert_parsed_snapshot("<div value=hello>")
    end

    test "attribute value with space after equal sign" do
      assert_parsed_snapshot(%(<input value= "value" />))
    end

    test "attribute value with space before equal sign" do
      assert_parsed_snapshot(%(<input value ="value" />))
    end

    test "attribute value with space before and after equal sign" do
      assert_parsed_snapshot(%(<div class    =    "hello">Content</div>))
    end

    test "attribute value with newline before equal sign" do
      assert_parsed_snapshot(%(<input value
="value" />))
    end

    test "attribute value with newline after equal sign" do
      assert_parsed_snapshot(%(<input value=
"value" />))
    end

    test "attribute value with newline before and after equal sign" do
      assert_parsed_snapshot(%(<input value
=
"value" />))
    end

    test "attribute value with mixed whitespace and newlines around equal sign" do
      assert_parsed_snapshot(%(<div class
  =
  "hello">Content</div>))
    end

    test "attribute value with exclamation point" do
      assert_parsed_snapshot(%(<input value="Hello!" />))
    end

    test "style attribute with url" do
      assert_parsed_snapshot(%(<div style="background-image: url('./images/image.png')"></div>))
    end

    test "double quotes inside single quotes" do
      assert_parsed_snapshot(%(<div data-json='{"key": "value"}'></div>))
    end

    test "multiple nested quotes" do
      assert_parsed_snapshot(%(<div title="She said 'Hello' and 'Goodbye'"></div>))
    end

    test "apostrophe inside single quotes" do
      assert_parsed_snapshot(%(<div data-msg='Don't worry'>Text</div>))
    end

    test "escaped apostrophe inside single quotes" do
      assert_parsed_snapshot(%(<div data-msg='Don\\'t worry'>Text</div>))
    end

    test "escaped double quote inside double quotes" do
      assert_parsed_snapshot(%(<div data-msg="She said \\"Hello\\"">Text</div>))
    end

    test "empty quoted attribute values" do
      assert_parsed_snapshot(%(<input value="" placeholder='' />))
    end

    test "mixed quote types in multiple attributes" do
      assert_parsed_snapshot(%(<input value="double quoted" placeholder='single quoted' />))
    end

    test "erb output with quotes" do
      assert_parsed_snapshot(%(<div title="<%= "quoted string" %>"></div>))
    end

    test "attributes with dots in name" do
      assert_parsed_snapshot(%(<div x-transition.duration.500ms x-show="visible" x-cloak></div>))
    end

    test "complex attribute with dots and values" do
      assert_parsed_snapshot(%(<div x-transition.duration.500ms="fast" data-component.option="value"></div>))
    end

    test "attributes starting with @ symbol" do
      assert_parsed_snapshot(%(<div @click="handleClick" @keyup.enter="submit"></div>))
    end

    test "@ attributes with various patterns" do
      assert_parsed_snapshot(%(<div @submit.prevent @change.debounce.100ms="update" @mouseover></div>))
    end

    test "standalone @ symbol in div tag" do
      assert_parsed_snapshot(%(<div @></div>))
    end

    test "standalone @ symbol followed by whitesapce in div tag" do
      assert_parsed_snapshot(%(<div @ ></div>))
    end

    test "standalone @ symbol followed by whitesapce and identifier in div tag" do
      assert_parsed_snapshot(%(<div @ click></div>))
    end

    test "standalone @ symbol in div tag followed by attribute" do
      assert_parsed_snapshot(%(<div @ data-attribute="test"></div>))
    end

    test "atttribute with @ prefix and now value" do
      assert_parsed_snapshot(%(<div @click></div>))
    end

    test "attribute with backtick quotes (invalid)" do
      assert_parsed_snapshot(%(<div class=`hello`></div>))
    end

    test "attribute with backtick quotes and whitespace (invalid)" do
      assert_parsed_snapshot(%(<div class=`hello world`></div>))
    end

    test "multiple attributes with mixed quotes including backticks (invalid)" do
      assert_parsed_snapshot(%(<div class="valid" id=`invalid` data-test='also-valid'></div>))
    end

    test "self-closing tag with backtick attribute (invalid)" do
      assert_parsed_snapshot(%(<img src=`image.jpg` />))
    end

    test "attribute with backtick containing HTML (invalid)" do
      assert_parsed_snapshot(%(<div data-template=`<span>Hello</span>`></div>))
    end

    test "Vue-style directive attribute with value" do
      assert_parsed_snapshot(%(<div :value="something"></div>))
    end

    test "Vue-style directive attributes multiple" do
      assert_parsed_snapshot(%(<input :model="user" :disabled="isDisabled" :class="className"></input>))
    end

    test "Vue-style directive attribute without value" do
      assert_parsed_snapshot(%(<div :disabled></div>))
    end

    test "Mixed Vue directives and regular attributes" do
      assert_parsed_snapshot(%(<div id="app" :class="dynamicClass" data-test="static"></div>))
    end

    test "Standalone colon with space is invalid" do
      assert_parsed_snapshot(%(<div : class="hello"></div>))
    end

    test "Colon immediately followed by attribute name is valid" do
      assert_parsed_snapshot(%(<div :class="hello"></div>))
    end

    test "Double colon is invalid" do
      assert_parsed_snapshot(%(<div ::value="hello"></div>))
    end

    test "Vue directive with namespace-like syntax" do
      assert_parsed_snapshot(%(<div :v-model="user"></div>))
    end

    test "Empty attribute value with closing bracket immediatly following it" do
      assert_parsed_snapshot(%(<div attribute-name=>div-content</div>))
    end
  end
end
