KHtml

html_formimpl.h
1 /*
2  * This file is part of the DOM implementation for KDE.
3  *
4  * Copyright (C) 1999 Lars Knoll ([email protected])
5  * (C) 1999 Antti Koivisto ([email protected])
6  * (C) 2000 Dirk Mueller ([email protected])
7  * (C) 2004, 2005, 2006 Apple Computer, Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this library; see the file COPYING.LIB. If not, write to
21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  *
24  */
25 #ifndef HTML_FORMIMPL_H
26 #define HTML_FORMIMPL_H
27 
28 #include "html/html_elementimpl.h"
29 #include "html/html_imageimpl.h"
30 #include "dom/html_element.h"
31 #include <wtf/HashSet.h>
32 #include <wtf/Vector.h>
33 
34 class QTextCodec;
35 
36 namespace khtml
37 {
38 class RenderFormElement;
39 class RenderTextArea;
40 class RenderSelect;
41 class RenderLineEdit;
42 class RenderRadioButton;
43 class RenderFileButton;
44 
45 typedef QList<QByteArray> encodingList;
46 }
47 
48 namespace KWallet
49 {
50 class Wallet;
51 }
52 
53 namespace DOM
54 {
55 
56 class HTMLFormElement;
57 class DOMString;
58 class HTMLGenericFormElementImpl;
59 class HTMLOptionElementImpl;
60 class HTMLCollectionImpl;
61 
62 // -------------------------------------------------------------------------
63 
64 class HTMLFormElementImpl : public HTMLElementImpl
65 {
66 public:
67  HTMLFormElementImpl(DocumentImpl *doc, bool implicit);
68  virtual ~HTMLFormElementImpl();
69 
70  Id id() const override;
71 
72  void insertedIntoDocument() override;
73  void removedFromDocument() override;
74  void addId(const DOMString &id) override;
75  void removeId(const DOMString &id) override;
76 
77  // See "past names map" in HTML5, 4.10.3, "The form element"
78  HTMLGenericFormElementImpl *lookupByPastName(const DOMString &id);
79  void bindPastName(HTMLGenericFormElementImpl *element);
80 
81  long length() const;
82 
83  QByteArray formData(bool &ok);
84 
85  DOMString enctype() const
86  {
87  return m_enctype;
88  }
89  void setEnctype(const DOMString &);
90 
91  DOMString target() const;
92  DOMString action() const;
93  HTMLCollectionImpl *elements();
94 
95  bool autoComplete() const
96  {
97  return m_autocomplete;
98  }
99  void doAutoFill();
100  void walletOpened(KWallet::Wallet *w);
101 
102  void parseAttribute(AttributeImpl *attr) override;
103 
104  void uncheckOtherRadioButtonsInGroup(HTMLGenericFormElementImpl *caller, bool setDefaultChecked = false);
105 
106  void registerFormElement(HTMLGenericFormElementImpl *);
107  void removeFormElement(HTMLGenericFormElementImpl *);
108  void registerImgElement(HTMLImageElementImpl *);
109  void removeImgElement(HTMLImageElementImpl *);
110 
111  void submitFromKeyboard();
112  bool prepareSubmit();
113  void submit();
114  void reset();
115 
116  void setMalformed(bool malformed)
117  {
118  m_malformed = malformed;
119  }
120  bool isMalformed() const
121  {
122  return m_malformed;
123  }
124 
125  friend class HTMLFormElement;
126  friend class HTMLFormCollectionImpl;
127 
128 private:
129  // Collects nodes that are inside the toGather set in tree order
130  WTF::Vector<HTMLGenericFormElementImpl *> gatherInTreeOrder(NodeImpl *root,
131  const WTF::HashSet<NodeImpl *> &toGather);
132 
133  void gatherWalletData();
135  QList<HTMLImageElementImpl *> imgElements;
136  DOMString m_target;
137  DOMString m_enctype;
138  QString m_boundary;
139  DOMString m_acceptcharset;
140  bool m_post : 1;
141  bool m_multipart : 1;
142  bool m_autocomplete : 1;
143  bool m_insubmit : 1;
144  bool m_doingsubmit : 1;
145  bool m_inreset : 1;
146  bool m_malformed : 1;
147  bool m_haveTextarea : 1; // for wallet storage
148  bool m_havePassword : 1; // for wallet storage
149  DOMString m_name; // our name
150  QMap<QString, QString> m_walletMap; // for wallet storage
151 
153 };
154 
155 // -------------------------------------------------------------------------
156 
157 class HTMLGenericFormElementImpl : public HTMLElementImpl
158 {
159  friend class HTMLFormElementImpl;
160  friend class khtml::RenderFormElement;
161 
162 public:
163  HTMLGenericFormElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = nullptr);
164  virtual ~HTMLGenericFormElementImpl();
165 
166  HTMLFormElementImpl *form() const
167  {
168  return m_form;
169  }
170 
171  void parseAttribute(AttributeImpl *attr) override;
172  void attach() override;
173  virtual void reset() {}
174 
175  void insertedIntoDocument() override;
176  void removedFromDocument() override;
177 
178  void onSelect();
179  void onChange();
180 
181  bool disabled() const
182  {
183  return m_disabled;
184  }
185  void setDisabled(bool _disabled);
186 
187  bool isFocusableImpl(FocusType ft) const override;
188  virtual bool isEnumerable() const
189  {
190  return false;
191  }
192  virtual bool isDefault() const
193  {
194  return false;
195  }
196 
197  bool readOnly() const
198  {
199  return m_readOnly;
200  }
201  void setReadOnly(bool _readOnly)
202  {
203  m_readOnly = _readOnly;
204  }
205 
206  DOMString name() const;
207  void setName(const DOMString &name);
208 
209  bool isGenericFormElement() const override
210  {
211  return true;
212  }
213  virtual bool isHiddenInput() const
214  {
215  return false;
216  }
217 
218  bool hasPastNames() const
219  {
220  return m_hasPastNames;
221  }
222  void setHasPastNames()
223  {
224  m_hasPastNames = true;
225  }
226 
227  /*
228  * override in derived classes to get the encoded name=value pair
229  * for submitting
230  * return true for a successful control (see HTML4-17.13.2)
231  */
232  virtual bool encoding(const QTextCodec *, khtml::encodingList &, bool)
233  {
234  return false;
235  }
236 
237  void defaultEventHandler(EventImpl *evt) override;
238  virtual bool isEditable();
239 
240  virtual bool unsubmittedFormChanges() const
241  {
242  return false;
243  }
244 
245 protected:
246  HTMLFormElementImpl *getForm() const;
247 
248  DOMStringImpl *m_name;
249  HTMLFormElementImpl *m_form;
250  bool m_disabled, m_readOnly, m_hasPastNames;
251 };
252 
253 // -------------------------------------------------------------------------
254 
255 class HTMLButtonElementImpl : public HTMLGenericFormElementImpl
256 {
257 public:
258  HTMLButtonElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = nullptr);
259 
260  virtual ~HTMLButtonElementImpl();
261 
262  enum typeEnum {
263  SUBMIT,
264  RESET,
265  BUTTON
266  };
267 
268  Id id() const override;
269  bool isEnumerable() const override
270  {
271  return true;
272  }
273 
274  DOMString type() const;
275  typeEnum buttonType() const
276  {
277  return KDE_CAST_BF_ENUM(typeEnum, m_type);
278  }
279  void parseAttribute(AttributeImpl *attr) override;
280  void defaultEventHandler(EventImpl *evt) override;
281  bool encoding(const QTextCodec *, khtml::encodingList &, bool) override;
282  void activate();
283  void attach() override;
284  void click();
285 
286 protected:
287  DOMString m_value;
288  QString m_currValue;
289  KDE_BF_ENUM(typeEnum) m_type : 2;
290  bool m_dirty : 1;
291  bool m_clicked : 1;
292  bool m_activeSubmit : 1;
293 };
294 
295 // -------------------------------------------------------------------------
296 
297 class HTMLFieldSetElementImpl : public HTMLGenericFormElementImpl
298 {
299 public:
300  HTMLFieldSetElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = nullptr);
301 
302  virtual ~HTMLFieldSetElementImpl();
303 
304  Id id() const override;
305  void attach() override;
306  void parseAttribute(AttributeImpl *attr) override;
307 
308 };
309 
310 // -------------------------------------------------------------------------
311 
312 class HTMLInputElementImpl : public HTMLGenericFormElementImpl
313 {
314  friend class khtml::RenderLineEdit;
315  friend class khtml::RenderRadioButton;
316  friend class khtml::RenderFileButton;
317 
318 public:
319  // do not change the order!
320  enum typeEnum {
321  TEXT,
322  PASSWORD,
323  ISINDEX,
324  CHECKBOX,
325  RADIO,
326  SUBMIT,
327  RESET,
328  FILE,
329  HIDDEN,
330  IMAGE,
331  BUTTON
332  };
333 
334  HTMLInputElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = nullptr);
335  virtual ~HTMLInputElementImpl();
336 
337  Id id() const override;
338 
339  bool isEnumerable() const override
340  {
341  return inputType() != IMAGE;
342  }
343  bool isDefault() const override
344  {
345  return m_defaultChecked;
346  }
347  bool isHiddenInput() const override
348  {
349  return inputType() == HIDDEN;
350  }
351 
352  bool autoComplete() const
353  {
354  return m_autocomplete;
355  }
356 
357  bool defaultChecked() const
358  {
359  return m_defaultChecked;
360  }
361  bool checked() const
362  {
363  return m_useDefaultChecked ? m_defaultChecked : m_checked;
364  }
365  void setChecked(bool, bool setDefaultChecked = false);
366  bool indeterminate() const
367  {
368  return m_indeterminate;
369  }
370  void setIndeterminate(bool);
371  long maxLength() const
372  {
373  return m_maxLen;
374  }
375  int size() const
376  {
377  return m_size;
378  }
379  DOMString type() const;
380  void setType(const DOMString &t);
381 
382  DOMString value() const;
383  void setValue(DOMString val);
384 
385  DOMString valueWithDefault() const;
386 
387  bool maintainsState() override
388  {
389  return true;
390  }
391  QString state() override;
392  void restoreState(const QString &state) override;
393 
394  void select();
395  void click();
396 
397  void parseAttribute(AttributeImpl *attr) override;
398 
399  void copyNonAttributeProperties(const ElementImpl *source) override;
400 
401  void attach() override;
402  bool encoding(const QTextCodec *, khtml::encodingList &, bool) override;
403 
404  typeEnum inputType() const
405  {
406  return KDE_CAST_BF_ENUM(typeEnum, m_type);
407  }
408  void reset() override;
409 
410  // used in case input type=image was clicked.
411  int clickX() const
412  {
413  return xPos;
414  }
415  int clickY() const
416  {
417  return yPos;
418  }
419 
420  void defaultEventHandler(EventImpl *evt) override;
421  bool isEditable() override;
422 
423  DOMString altText() const;
424  void activate();
425 
426  void setUnsubmittedFormChange(bool unsubmitted)
427  {
428  m_unsubmittedFormChange = unsubmitted;
429  }
430  bool unsubmittedFormChanges() const override
431  {
432  return m_unsubmittedFormChange;
433  }
434 
435  //Mozilla extensions.
436  long selectionStart();
437  long selectionEnd();
438  void setSelectionStart(long pos);
439  void setSelectionEnd(long pos);
440  void setSelectionRange(long start, long end);
441 
442  // HTML5
443  void setPlaceholder(const DOMString &p);
444  DOMString placeholder() const;
445 
446 protected:
447  void parseType(const DOMString &t);
448 
449  DOMString m_value;
450  int xPos;
451  short m_maxLen;
452  short m_size;
453  short yPos;
454 
455  KDE_BF_ENUM(typeEnum) m_type : 4;
456  bool m_clicked : 1;
457  bool m_checked : 1;
458  bool m_defaultChecked : 1; // could do without by checking ATTR_CHECKED
459  bool m_useDefaultChecked : 1;
460  bool m_indeterminate : 1;
461  bool m_haveType : 1;
462  bool m_activeSubmit : 1;
463  bool m_autocomplete : 1;
464  bool m_inited : 1;
465  bool m_unsubmittedFormChange : 1;
466 };
467 
468 // -------------------------------------------------------------------------
469 
470 class HTMLLabelElementImpl : public HTMLGenericFormElementImpl
471 {
472 public:
473  HTMLLabelElementImpl(DocumentImpl *doc);
474  virtual ~HTMLLabelElementImpl();
475 
476  Id id() const override;
477  void attach() override;
478  void defaultEventHandler(EventImpl *evt) override;
479  bool isFocusableImpl(FocusType ft) const override;
480  NodeImpl *getFormElement();
481 
482 private:
483  DOMString m_formElementID;
484 };
485 
486 // -------------------------------------------------------------------------
487 
488 class HTMLLegendElementImpl : public HTMLGenericFormElementImpl
489 {
490 public:
491  HTMLLegendElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = nullptr);
492  virtual ~HTMLLegendElementImpl();
493 
494  Id id() const override;
495  void attach() override;
496  void parseAttribute(AttributeImpl *attr) override;
497 };
498 
499 // -------------------------------------------------------------------------
500 
501 class HTMLSelectElementImpl : public HTMLGenericFormElementImpl
502 {
503  friend class khtml::RenderSelect;
504 
505 public:
506  HTMLSelectElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = nullptr);
507  ~HTMLSelectElementImpl();
508 
509  Id id() const override;
510 
511  DOMString type() const;
512 
513  HTMLCollectionImpl *options();
514 
515  long selectedIndex() const;
516  void setSelectedIndex(long index);
517 
518  bool isEnumerable() const override
519  {
520  return true;
521  }
522 
523  long length() const;
524 
525  long minWidth() const
526  {
527  return m_minwidth;
528  }
529 
530  long size() const
531  {
532  return m_size;
533  }
534 
535  bool multiple() const
536  {
537  return m_multiple;
538  }
539 
540  void add(HTMLElementImpl *element, HTMLElementImpl *before, int &exceptioncode);
541  using DOM::NodeImpl::remove;
542  void remove(long index);
543 
544  DOMString value() const;
545  void setValue(DOMStringImpl *value);
546 
547  bool maintainsState() override
548  {
549  return true;
550  }
551  QString state() override;
552  void restoreState(const QString &state) override;
553 
554  NodeImpl *insertBefore(NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode) override;
555  void replaceChild(NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode) override;
556  void removeChild(NodeImpl *oldChild, int &exceptioncode) override;
557  void removeChildren() override;
558  NodeImpl *appendChild(NodeImpl *newChild, int &exceptioncode) override;
559  NodeImpl *addChild(NodeImpl *newChild) override;
560 
561  void childrenChanged() override;
562 
563  void parseAttribute(AttributeImpl *attr) override;
564 
565  void attach() override;
566  bool encoding(const QTextCodec *, khtml::encodingList &, bool) override;
567 
568  // get the actual listbox index of the optionIndexth option
569  int optionToListIndex(int optionIndex) const;
570  // reverse of optionToListIndex - get optionIndex from listboxIndex
571  int listToOptionIndex(int listIndex) const;
572 
573  void setRecalcListItems();
574 
575  QVector<HTMLGenericFormElementImpl *> listItems() const
576  {
577  if (m_recalcListItems) {
578  const_cast<HTMLSelectElementImpl *>(this)->recalcListItems();
579  }
580  return m_listItems;
581  }
582  void reset() override;
583  void notifyOptionSelected(HTMLOptionElementImpl *selectedOption, bool selected);
584 
585 private:
586  void recalcListItems() const;
587  HTMLOptionElementImpl *firstSelectedItem() const;
588 
589 protected:
590  mutable QVector<HTMLGenericFormElementImpl *> m_listItems;
591  short m_minwidth;
592  signed short m_size : 15;
593  bool m_multiple : 1;
594  mutable bool m_recalcListItems : 1;
595  mutable unsigned int m_length: 31;
596 };
597 
598 // -------------------------------------------------------------------------
599 
600 class HTMLKeygenElementImpl : public HTMLSelectElementImpl
601 {
602 public:
603  HTMLKeygenElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = nullptr);
604 
605  Id id() const override;
606 
607  DOMString type() const;
608 
609  long selectedIndex() const;
610  void setSelectedIndex(long index);
611 
612  // ### this is just a rough guess
613  bool isEnumerable() const override
614  {
615  return false;
616  }
617 
618  void parseAttribute(AttributeImpl *attr) override;
619  bool encoding(const QTextCodec *, khtml::encodingList &, bool) override;
620 
621 };
622 
623 // -------------------------------------------------------------------------
624 
625 class HTMLOptGroupElementImpl : public HTMLGenericFormElementImpl
626 {
627 public:
628  HTMLOptGroupElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = nullptr)
629  : HTMLGenericFormElementImpl(doc, f) {}
630 
631  Id id() const override;
632 };
633 
634 // ---------------------------------------------------------------------------
635 
636 class HTMLOptionElementImpl : public HTMLGenericFormElementImpl
637 {
638  friend class khtml::RenderSelect;
639  friend class DOM::HTMLSelectElementImpl;
640 
641 public:
642  HTMLOptionElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = nullptr);
643 
644  Id id() const override;
645 
646  DOMString text() const;
647 
648  long index() const;
649  void setIndex(long);
650  void parseAttribute(AttributeImpl *attr) override;
651  DOMString value() const;
652  void setValue(DOMStringImpl *value);
653 
654  bool isDefault() const override
655  {
656  return m_defaultSelected;
657  }
658 
659  // For internal use --- just returns the bit
660  bool selectedBit() const
661  {
662  return m_selected;
663  }
664 
665  // DOM use --- may recompute information of a parent <select>
666  bool selected() const;
667  void setSelected(bool _selected);
668  void setDefaultSelected(bool _defaultSelected);
669 
670  HTMLSelectElementImpl *getSelect() const;
671 
672 protected:
673  DOMString m_value;
674  bool m_selected;
675  bool m_defaultSelected;
676 };
677 
678 // -------------------------------------------------------------------------
679 
680 class HTMLTextAreaElementImpl : public HTMLGenericFormElementImpl
681 {
682  friend class khtml::RenderTextArea;
683 
684 public:
685  enum WrapMethod {
686  ta_NoWrap,
687  ta_Virtual,
688  ta_Physical
689  };
690 
691  HTMLTextAreaElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = nullptr);
692  ~HTMLTextAreaElementImpl();
693 
694  Id id() const override;
695  void childrenChanged() override;
696 
697  long cols() const
698  {
699  return m_cols;
700  }
701 
702  long rows() const
703  {
704  return m_rows;
705  }
706 
707  WrapMethod wrap() const
708  {
709  return m_wrap;
710  }
711 
712  bool isEnumerable() const override
713  {
714  return true;
715  }
716 
717  DOMString type() const;
718 
719  bool maintainsState() override
720  {
721  return true;
722  }
723  QString state() override;
724  void restoreState(const QString &state) override;
725 
726  void select();
727 
728  void parseAttribute(AttributeImpl *attr) override;
729  void attach() override;
730  bool encoding(const QTextCodec *, khtml::encodingList &, bool) override;
731  void reset() override;
732  DOMString value();
733  void setValue(DOMString _value);
734  DOMString defaultValue();
735  void setDefaultValue(DOMString _defaultValue);
736 
737  bool isEditable() override;
738  void setUnsubmittedFormChange(bool unsubmitted)
739  {
740  m_unsubmittedFormChange = unsubmitted;
741  }
742  bool unsubmittedFormChanges() const override
743  {
744  return m_unsubmittedFormChange;
745  }
746 
747  //Mozilla extensions.
748  long selectionStart();
749  long selectionEnd();
750  void setSelectionStart(long pos);
751  void setSelectionEnd(long pos);
752  void setSelectionRange(long start, long end);
753  long textLength();
754 
755  // HTML5
756  void setPlaceholder(const DOMString &p);
757  DOMString placeholder() const;
758 
759 protected:
760  int m_rows;
761  int m_cols;
762  WrapMethod m_wrap;
763  QString m_value;
764  bool m_changed: 1; //States whether the contents has been editted
765  bool m_unsubmittedFormChange: 1;
766  bool m_initialized: 1;
767 };
768 
769 // -------------------------------------------------------------------------
770 
771 class HTMLIsIndexElementImpl : public HTMLInputElementImpl
772 {
773 public:
774  HTMLIsIndexElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = nullptr);
775  ~HTMLIsIndexElementImpl();
776 
777  Id id() const override;
778  void parseAttribute(AttributeImpl *attr) override;
779 
780  DOMString prompt() const;
781  void setPrompt(const DOMString &_value);
782 };
783 
784 } //namespace
785 
786 #endif
QString name(const QVariant &location)
This file is part of the HTML rendering engine for KDE.
KIOFILEWIDGETS_EXPORT void add(const QString &fileClass, const QString &directory)
Type type(const QSqlDatabase &db)
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
int64_t Id
This library provides a full-featured HTML parser and widget.
KGuiItem reset()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.