diff --git a/src/Element.php b/src/Element.php index fa363108..32381328 100644 --- a/src/Element.php +++ b/src/Element.php @@ -33,6 +33,8 @@ class Element extends DOMElement { protected $liveProperty_classList; /** @var StringMap */ protected $liveProperty_dataset; + /** @var Array */ + static public $formControlElements = ['button', 'fieldset', 'input', 'object', 'output', 'select', 'textarea']; /** * returns true if the element would be selected by the specified selector @@ -248,6 +250,22 @@ public function prop_set_selected(bool $selected):bool { return $this->selected; } + public function prop_get_form() { + if (\in_array($this->tagName, self::$formControlElements)) { + if ($this->tagName === "input" && $this->getAttribute("type") === "image") + return null; + + if ($this->hasAttribute("form")) { + return $this->getRootDocument()->getElementById($this->getAttribute("form")); + } + else { + return $this->closest('form'); + } + } + + return null; + } + protected function createDataset():StringMap { return new StringMap( $this, diff --git a/test/unit/ElementTest.php b/test/unit/ElementTest.php index 4bbb7627..22b30742 100644 --- a/test/unit/ElementTest.php +++ b/test/unit/ElementTest.php @@ -324,4 +324,65 @@ public function testDatasetIssetUnset() { unset($element->dataset->name); self::assertFalse(isset($element->dataset->name)); } + + public function testFormControlElementsCanHaveFormProperty() { + $document = new HTMLDocument(Helper::HTML_FORM_PROPERTY); + $form = $document->getElementById('form_2'); + + $input = $document->getElementById('f2'); + self::assertEquals($form, $input->form); + + $button = $document->getElementById('f4'); + self::assertEquals($form, $button->form); + + $fieldset = $document->getElementById('f5'); + self::assertEquals($form, $fieldset->form); + + $input = $document->getElementById('f6'); + self::assertEquals($form, $input->form); + + $object = $document->getElementById('f7'); + self::assertEquals($form, $object->form); + + $output = $document->getElementById('f8'); + self::assertEquals($form, $output->form); + + $select = $document->getElementById('f9'); + self::assertEquals($form, $select->form); + } + + public function testFormControlElementReturnsParentFormAsFormPropertyIfItDoesNotHaveFormAttribute() { + $document = new HTMLDocument(Helper::HTML_FORM_PROPERTY); + $form = $document->getElementById('form_1'); + + $input = $document->getElementById('f1'); + self::assertEquals($form, $input->form); + + $button = $document->getElementById('f3'); + self::assertEquals($form, $button->form); + } + + public function testFormControlElementReturnsNullIfItDoesNotHaveFormAttributeAndDoesNotHaveParentForm() { + $document = new HTMLDocument(Helper::HTML_FORM_PROPERTY); + + $input = $document->getElementById('f11'); + self::assertEquals(NULL, $input->form); + } + + public function testNonControlElementRetursNullAsFormProperty() { + $document = new HTMLDocument(Helper::HTML_FORM_PROPERTY); + + $span = $document->getElementById('non_form_control_1'); + self::assertEquals(NULL, $span->form); + + $span = $document->getElementById('non_form_control_2'); + self::assertEquals(NULL, $span->form); + } + + public function testInputElementWithTypeImagetReturnsNullAsFormProperty() { + $document = new HTMLDocument(Helper::HTML_FORM_PROPERTY); + + $input = $document->getElementById('f12'); + self::assertEquals(NULL, $input->form); + } } \ No newline at end of file diff --git a/test/unit/Helper/Helper.php b/test/unit/Helper/Helper.php index 5f6250c6..8cfccd41 100644 --- a/test/unit/Helper/Helper.php +++ b/test/unit/Helper/Helper.php @@ -293,4 +293,39 @@ class Helper { HTML; +const HTML_FORM_PROPERTY = << + + + + Test HTML + + + +
+ + + + +
+ +
+ + +
+ + + + + + + + + + + + + +HTML; + } \ No newline at end of file